Module: XMigra::MSSQLSpecifics

Defined in:
lib/xmigra/db_support/mssql.rb

Defined Under Namespace

Classes: StatisticsObject

Constant Summary collapse

SYSTEM_NAME =
'Microsoft SQL Server'
IDENTIFIER_SUBPATTERN =
'[a-z_@#][a-z0-9@$#_]*|"[^\[\]"]+"|\[[^\[\]]+\]'
DBNAME_PATTERN =
/^
  (?:(#{IDENTIFIER_SUBPATTERN})\.)?
  (#{IDENTIFIER_SUBPATTERN})
$/ix
STATISTICS_FILE =
'statistics-objects.yaml'
ID_COLLATION =
'Latin1_General_CS_AS'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.init_schema(schema_config) ⇒ Object



1221
1222
1223
1224
1225
1226
1227
1228
# File 'lib/xmigra/db_support/mssql.rb', line 1221

def init_schema(schema_config)
  Console.output_section "Microsoft SQL Server Specifics" do
    if Console.yes_no("Use more verbose syntax compatible with SQL Server 2005", :no)
      schema_config.dbinfo["MSSQL 2005 compatible"] = true
      puts "Configured for SQL Server 2005 compatibility mode."
    end
  end
end

.object_type_codes(type) ⇒ Object



1209
1210
1211
1212
1213
1214
1215
# File 'lib/xmigra/db_support/mssql.rb', line 1209

def object_type_codes(type)
  case type
  when StoredProcedure then %w{P PC}
  when View then ['V']
  when Function then %w{AF FN FS FT IF TF}
  end
end

.string_literal(s) ⇒ Object



1217
1218
1219
# File 'lib/xmigra/db_support/mssql.rb', line 1217

def string_literal(s)
  "N'#{s.gsub("'","''")}'"
end

.strip_identifier_quoting(s) ⇒ Object



1200
1201
1202
1203
1204
1205
1206
1207
# File 'lib/xmigra/db_support/mssql.rb', line 1200

def strip_identifier_quoting(s)
  case
  when s.empty? then return s
  when s[0,1] == "[" && s[-1,1] == "]" then return s[1..-2]
  when s[0,1] == '"' && s[-1,1] == '"' then return s[1..-2]
  else return s
  end
end

Instance Method Details

#alter_table_columns_sql_statements(col_pairs) ⇒ Object



1192
1193
1194
1195
1196
1197
# File 'lib/xmigra/db_support/mssql.rb', line 1192

def alter_table_columns_sql_statements(col_pairs)
  col_pairs.map do |_, col|
    nullability = (col.nullable? ? "" : "NOT ") + "NULL"
    "ALTER TABLE #{name} ALTER COLUMN #{col.name} #{col.type} #{nullability};"
  end
end

#amend_script_parts(parts) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/xmigra/db_support/mssql.rb', line 123

def amend_script_parts(parts)
  parts.insert_after(
    :create_and_fill_indexes_table_sql,
    :create_and_fill_statistics_table_sql
  )
  parts.insert_after(
    :remove_undesired_indexes_sql,
    :remove_undesired_statistics_sql
  )
  parts.insert_after(:create_new_indexes_sql, :create_new_statistics_sql)
end

#batch_separatorObject



1016
1017
1018
# File 'lib/xmigra/db_support/mssql.rb', line 1016

def batch_separator
  "GO\n"
end

#branch_id_literalObject



1066
1067
1068
# File 'lib/xmigra/db_support/mssql.rb', line 1066

def branch_id_literal
  @mssql_branch_id_literal ||= MSSQLSpecifics.string_literal(XMigra.secure_digest(branch_identifier))
end

#branch_upgrade_sqlObject



1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
# File 'lib/xmigra/db_support/mssql.rb', line 1081

def branch_upgrade_sql
  return unless respond_to? :branch_identifier
  
  parts = [<<-"END_OF_SQL"]
IF #{upgrading_to_new_branch_test_sql}
BEGIN
  PRINT N'Migrating from previous schema branch:';
  
  DECLARE @sqlcmd NVARCHAR(MAX);
  
  DECLARE CmdCursor CURSOR LOCAL FOR
  SELECT bu.[UpgradeSql]
  FROM [xmigra].[branch_upgrade] bu
  WHERE bu.[Next] = #{branch_id_literal}
  ORDER BY bu.[ApplicationOrder] ASC;
  
  OPEN CmdCursor;
  
  FETCH NEXT FROM CmdCursor INTO @sqlcmd;
  
  WHILE @@FETCH_STATUS = 0 BEGIN
EXECUTE sp_executesql @sqlcmd;

FETCH NEXT FROM CmdCursor INTO @sqlcmd;
  END;
  
  CLOSE CmdCursor;
  DEALLOCATE CmdCursor;
  
  DECLARE @applied NVARCHAR(80);
  DECLARE @old_branch NVARCHAR(80);
  
  SELECT TOP(1) @applied = [CompletesMigration], @old_branch = [Current]
  FROM [xmigra].[branch_upgrade]
  WHERE [Next] = #{branch_id_literal};
  
  -- Delete the "applied" record for the migration if there was one, so that
  -- a new record with this ID can be inserted.
  DELETE FROM [xmigra].[applied] WHERE [MigrationID] = @applied;
  
  -- Create a "version bridge" record in the "applied" table for the branch upgrade
  INSERT INTO [xmigra].[applied] ([MigrationID], [VersionBridgeMark], [Description])
  VALUES (@applied, 1, N'Branch upgrade from branch ' + @old_branch);
END;

DELETE FROM [xmigra].[branch_upgrade];

  END_OF_SQL
  
  if branch_upgrade.applicable? migrations
    batch_template = <<-"END_OF_SQL"
INSERT INTO [xmigra].[branch_upgrade]
([Current], [Next], [CompletesMigration], [UpgradeSql])
VALUES (
  #{branch_id_literal},
  #{MSSQLSpecifics.string_literal(branch_upgrade.target_branch)},
  #{MSSQLSpecifics.string_literal(branch_upgrade.migration_completed_id)},
  %s
);
    END_OF_SQL
    
    each_batch(branch_upgrade.sql) do |batch|
      # Insert the batch into the [xmigra].[branch_upgrade] table
      parts << (batch_template % MSSQLSpecifics.string_literal(batch))
    end
  else
    # Insert a placeholder that only declares the current branch of the schema
    parts << <<-"END_OF_SQL"
INSERT INTO [xmigra].[branch_upgrade] ([Current]) VALUES (#{branch_id_literal});
    END_OF_SQL
  end
  
  return parts.join("\n")
end

#check_chain_continuity_sqlObject



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/xmigra/db_support/mssql.rb', line 475

def check_chain_continuity_sql
  <<-"END_OF_SQL"
IF NOT #{upgrading_to_new_branch_test_sql}
BEGIN
  PRINT N'Checking migration chain continuity:';
  -- Get the [xmigra].[migrations] ApplicationOrder of the most recent version bridge migration
  DECLARE @BridgePoint INT;
  SET @BridgePoint = (
SELECT COALESCE(MAX(m.[ApplicationOrder]), 0)
FROM [xmigra].[applied] a
INNER JOIN [xmigra].[migrations] m
  ON a.[MigrationID] = m.[MigrationID]
WHERE a.[VersionBridgeMark] <> 0
  );
  
  -- Test for previously applied migrations that break the continuity of the
  -- migration chain in this script:
  IF EXISTS (
SELECT *
FROM [xmigra].[applied] a
INNER JOIN [xmigra].[migrations] m
  ON a.[MigrationID] = m.[MigrationID]
INNER JOIN [xmigra].[migrations] p
  ON m.[ApplicationOrder] - 1 = p.[ApplicationOrder]
WHERE p.[ApplicationOrder] > @BridgePoint
AND p.[MigrationID] NOT IN (
  SELECT a2.[MigrationID] FROM [xmigra].[applied] a2
)
  )
  BEGIN
RAISERROR(
  N'Previously applied migrations interrupt the continuity of the migration chain',
  11,
  1
);
  END;
END;
  END_OF_SQL
end

#check_execution_environment_sqlObject



135
136
137
138
139
140
141
142
143
# File 'lib/xmigra/db_support/mssql.rb', line 135

def check_execution_environment_sql
  <<-"END_OF_SQL"
PRINT N'Checking execution environment:';
IF DB_NAME() IN ('master', 'tempdb', 'model', 'msdb')
BEGIN
  RAISERROR(N'Please select an appropriate target database for the update script.', 11, 1);
END;
  END_OF_SQL
end

#check_existence_sql(for_existence, error_message) ⇒ Object



1020
1021
1022
1023
1024
1025
1026
1027
1028
# File 'lib/xmigra/db_support/mssql.rb', line 1020

def check_existence_sql(for_existence, error_message)
  error_message = sprintf(error_message, quoted_name)
  
  return <<-"END_OF_SQL"

IF #{"NOT" if for_existence} #{existence_test_sql}
RAISERROR(N'#{error_message}', 11, 1);
  END_OF_SQL
end

#check_preceding_migrations_sqlObject



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/xmigra/db_support/mssql.rb', line 433

def check_preceding_migrations_sql
  parts = []
  
  parts << (<<-"END_OF_SQL") if production 
IF EXISTS (
  SELECT TOP(1) * FROM [xmigra].[branch_upgrade]
) AND NOT EXISTS (
  SELECT TOP(1) * FROM [xmigra].[branch_upgrade]
  WHERE #{branch_id_literal} IN ([Current], [Next])
)
RAISERROR (N'Existing database is from a different (and non-upgradable) branch.', 11, 1);

  END_OF_SQL
  
  parts << (<<-"END_OF_SQL")
IF NOT #{upgrading_to_new_branch_test_sql}
BEGIN
  PRINT N'Checking preceding migrations:';
  -- Get the ApplicationOrder of the most recent version bridge migration
  DECLARE @VersionBridge INT;
  SET @VersionBridge = (
SELECT COALESCE(MAX([ApplicationOrder]), 0)
FROM [xmigra].[applied]
WHERE [VersionBridgeMark] <> 0
  );
  
  -- Check for existence of applied migrations after the latest version
  -- bridge that are not in [xmigra].[migrations]
  IF EXISTS (
SELECT * FROM [xmigra].[applied] a
WHERE a.[ApplicationOrder] > @VersionBridge
AND a.[MigrationID] NOT IN (
  SELECT m.[MigrationID] FROM [xmigra].[migrations] m
)
  )
  RAISERROR (N'Unknown in-branch migrations have been applied.', 11, 1);
END;
  END_OF_SQL
  
  return parts.join('')
end

#create_and_fill_indexes_table_sqlObject



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/xmigra/db_support/mssql.rb', line 372

def create_and_fill_indexes_table_sql
  intro = <<-"END_OF_SQL"
PRINT N'Creating and filling index manipulation table:';
IF EXISTS (
  SELECT * FROM sys.objects
  WHERE object_id = OBJECT_ID(N'[xmigra].[updated_indexes]')
  AND type IN (N'U')
)
BEGIN
  DROP TABLE [xmigra].[updated_indexes];
END;
GO

CREATE TABLE [xmigra].[updated_indexes] (
  [IndexID]                  NVARCHAR(80) NOT NULL PRIMARY KEY
);
GO

  END_OF_SQL
  
  insertion = <<-"END_OF_SQL"
INSERT INTO [xmigra].[updated_indexes] ([IndexID]) VALUES
  END_OF_SQL
  
  strlit = MSSQLSpecifics.method :string_literal
  return intro + (insertion + indexes.collect do |index|
    "(#{strlit[index.id]})"
  end.join(",\n") + ";\n" unless indexes.empty?).to_s
end

#create_and_fill_migration_table_sqlObject



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/xmigra/db_support/mssql.rb', line 325

def create_and_fill_migration_table_sql
  intro = <<-"END_OF_SQL"
IF EXISTS (
  SELECT * FROM sys.objects
  WHERE object_id = OBJECT_ID(N'[xmigra].[migrations]')
  AND type IN (N'U')
)
BEGIN
  DROP TABLE [xmigra].[migrations];
END;
GO

CREATE TABLE [xmigra].[migrations] (
  [MigrationID]            nvarchar(80) COLLATE #{ID_COLLATION} NOT NULL,
  [ApplicationOrder]       int NOT NULL,
  [Description]            ntext NOT NULL,
  [Install]                bit NOT NULL DEFAULT(0)
);
GO

  END_OF_SQL
  
  mig_insert = <<-"END_OF_SQL"
INSERT INTO [xmigra].[migrations] (
  [MigrationID],
  [ApplicationOrder],
  [Description]
) VALUES
  END_OF_SQL
  
  if (@db_info || {}).fetch('MSSQL 2005 compatible', false).eql?(true)
    parts = [intro]
    (0...migrations.length).each do |i|
      m = migrations[i]
      description_literal = MSSQLSpecifics.string_literal(m.description.strip)
      parts << mig_insert + "(N'#{m.id}', #{i + 1}, #{description_literal});\n"
    end
    return parts.join('')
  else
    return intro + mig_insert + (0...migrations.length).collect do |i|
      m = migrations[i]
      description_literal = MSSQLSpecifics.string_literal(m.description.strip)
      "(N'#{m.id}', #{i + 1}, #{description_literal})"
    end.join(",\n") + ";\n"
  end
end

#create_and_fill_statistics_table_sqlObject



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/xmigra/db_support/mssql.rb', line 402

def create_and_fill_statistics_table_sql
  intro = <<-"END_OF_SQL"
PRINT N'Creating and filling statistics object manipulation table:';
IF EXISTS (
  SELECT * FROM sys.objects
  WHERE object_id = OBJECT_ID(N'[xmigra].[updated_statistics]')
  AND type in (N'U')
)
BEGIN
  DROP TABLE [xmigra].[updated_statistics];
END;
GO

CREATE TABLE [xmigra].[updated_statistics] (
  [Name] nvarchar(100) NOT NULL PRIMARY KEY,
  [Columns] nvarchar(256) NOT NULL
);
GO

  END_OF_SQL
  
  insertion = <<-"END_OF_SQL"
INSERT INTO [xmigra].[updated_statistics] ([Name], [Columns]) VALUES
  END_OF_SQL
  
  strlit = MSSQLSpecifics.method :string_literal
  return intro + (insertion + stats_objs.collect do |stats_obj|
    "(#{strlit[stats_obj.name]}, #{strlit[stats_obj.columns]})"
  end.join(",\n") + ";\n" unless stats_objs.empty?).to_s
end

#create_new_indexes_sqlObject



749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
# File 'lib/xmigra/db_support/mssql.rb', line 749

def create_new_indexes_sql
  indexes.collect do |index|
    index_id_literal = MSSQLSpecifics.string_literal(index.id)
    index_name_literal = MSSQLSpecifics.string_literal(index.name)
    <<-"END_OF_SQL"
PRINT N'Index ' + #{index_id_literal} + ':';
IF EXISTS(
  SELECT * FROM [xmigra].[updated_indexes] ui
  WHERE ui.[IndexID] = #{index_id_literal}
  AND ui.[IndexID] NOT IN (
SELECT i.[IndexID] FROM [xmigra].[indexes] i
  )
)
BEGIN
  IF EXISTS (
SELECT * FROM sys.indexes
WHERE [name] = #{index_name_literal}
  )
  BEGIN
RAISERROR(N'An index already exists named %s', 11, 1, #{index_name_literal});
  END;
  
  PRINT N'    Creating...';
  #{index.definition_sql};
  
  IF (SELECT COUNT(*) FROM sys.indexes WHERE [name] = #{index_name_literal}) <> 1
  BEGIN
RAISERROR(N'Index %s was not created by its definition.', 11, 1,
  #{index_name_literal});
  END;

  INSERT INTO [xmigra].[indexes] ([IndexID], [name])
  VALUES (#{index_id_literal}, #{index_name_literal});
END
ELSE
BEGIN
  PRINT N'    Already exists.';
END;
    END_OF_SQL
  end.join(ddl_block_separator)
end

#create_new_statistics_sqlObject



791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
# File 'lib/xmigra/db_support/mssql.rb', line 791

def create_new_statistics_sql
  stats_objs.collect do |stats_obj|
    stats_name = MSSQLSpecifics.string_literal(stats_obj.name)
    strlit = lambda {|s| MSSQLSpecifics.string_literal(s)}
    
    stats_obj.creation_sql
    <<-"END_OF_SQL"
PRINT N'Statistics object #{stats_obj.name}:';
IF EXISTS (
  SELECT * FROM [xmigra].[updated_statistics] us
  WHERE us.[Name] = #{stats_name}
  AND us.[Columns] NOT IN (
SELECT s.[Columns]
FROM [xmigra].[statistics] s
WHERE s.[Name] = us.[Name]
  )
)
BEGIN
  IF EXISTS (
SELECT * FROM sys.stats
WHERE [name] = #{stats_name}
  )
  BEGIN
RAISERROR(N'A statistics object named %s already exists.', 11, 1, #{stats_name})
  END;
  
  PRINT N'    Creating...';
  #{stats_obj.creation_sql}
  
  INSERT INTO [xmigra].[statistics] ([Name], [Columns])
  VALUES (#{stats_name}, #{strlit[stats_obj.columns]})
END
ELSE
BEGIN
  PRINT N'    Already exists.';
END;
    END_OF_SQL
  end.join(ddl_block_separator)
end

#creation_noticeObject



1030
1031
1032
# File 'lib/xmigra/db_support/mssql.rb', line 1030

def creation_notice
  return "PRINT " + MSSQLSpecifics.string_literal("Creating #{printable_type} #{quoted_name}:") + ";"
end

#ddl_block_separatorObject



41
# File 'lib/xmigra/db_support/mssql.rb', line 41

def ddl_block_separator; "\nGO\n"; end

#each_batch(sql) ⇒ Object



999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
# File 'lib/xmigra/db_support/mssql.rb', line 999

def each_batch(sql)
  current_batch_lines = []
  sql.each_line do |line|
    if line.strip.upcase == 'GO'
      batch = current_batch_lines.join('')
      yield batch unless batch.strip.empty?
      current_batch_lines.clear
    else
      current_batch_lines << line
    end
  end
  unless current_batch_lines.empty?
    batch = current_batch_lines.join('')
    yield batch unless batch.strip.empty?
  end
end

#ensure_permissions_table_sqlObject



840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
# File 'lib/xmigra/db_support/mssql.rb', line 840

def ensure_permissions_table_sql
  strlit = MSSQLSpecifics.method(:string_literal)
  <<-"END_OF_SQL"
-- ------------ SET UP XMIGRA PERMISSION TRACKING OBJECTS ------------ --

PRINT N'Setting up XMigra permission tracking:';
IF NOT EXISTS (
  SELECT * FROM sys.schemas
  WHERE name = N'xmigra'
)
BEGIN
  EXEC sp_executesql N'
CREATE SCHEMA [xmigra] AUTHORIZATION [dbo];
  ';
END;
GO

IF NOT EXISTS(
  SELECT * FROM sys.objects
  WHERE object_id = OBJECT_ID(N'[xmigra].[revokable_permissions]')
  AND type IN (N'U')
)
BEGIN
  CREATE TABLE [xmigra].[revokable_permissions] (
[permissions] nvarchar(200) NOT NULL,
[object] nvarchar(260) NOT NULL,
[principal_id] int NOT NULL
  ) ON [PRIMARY];
END;
GO

IF EXISTS(
  SELECT * FROM sys.objects
  WHERE object_id = OBJECT_ID(N'[xmigra].[ip_prepare_revoke]')
  AND type IN (N'P', N'PC')
)
BEGIN
  DROP PROCEDURE [xmigra].[ip_prepare_revoke];
END;
GO

CREATE PROCEDURE [xmigra].[ip_prepare_revoke]
(
  @permissions nvarchar(200),
  @object nvarchar(260),
  @principal sysname
)
AS
BEGIN
  INSERT INTO [xmigra].[revokable_permissions] ([permissions], [object], [principal_id])
  VALUES (@permissions, @object, DATABASE_PRINCIPAL_ID(@principal));
END;
  END_OF_SQL
end

#ensure_version_tables_sqlObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/xmigra/db_support/mssql.rb', line 145

def ensure_version_tables_sql
  <<-"END_OF_SQL"
PRINT N'Ensuring version tables:';
IF NOT EXISTS (
  SELECT * FROM sys.schemas
  WHERE name = N'xmigra'
)
BEGIN
  EXEC sp_executesql N'
CREATE SCHEMA [xmigra] AUTHORIZATION [dbo];
  ';
END;
GO

IF NOT EXISTS (
  SELECT * FROM sys.objects
  WHERE object_id = OBJECT_ID(N'[xmigra].[applied]')
  AND type IN (N'U')
)
BEGIN
  CREATE TABLE [xmigra].[applied] (
[MigrationID]            nvarchar(80) COLLATE #{ID_COLLATION} NOT NULL,
[ApplicationOrder]       int IDENTITY(1,1) NOT NULL,
[VersionBridgeMark]      bit NOT NULL,
[Description]            nvarchar(max) NOT NULL,

CONSTRAINT [PK_version] PRIMARY KEY CLUSTERED (
  [MigrationID] ASC
) WITH (
  PAD_INDEX = OFF,
  STATISTICS_NORECOMPUTE  = OFF,
  IGNORE_DUP_KEY = OFF,
  ALLOW_ROW_LOCKS = ON,
  ALLOW_PAGE_LOCKS = ON
) ON [PRIMARY]
  ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY];
END;
GO

IF NOT EXISTS (
  SELECT * FROM sys.columns
  WHERE object_id = OBJECT_ID(N'[xmigra].[applied]')
  AND name = N'MigrationID'
  AND collation_name = N'#{ID_COLLATION}'
)
BEGIN
  ALTER TABLE xmigra.applied DROP CONSTRAINT PK_version;
  ALTER TABLE xmigra.applied ALTER COLUMN [MigrationID] nvarchar(80) COLLATE Latin1_General_CS_AS NOT NULL;
  ALTER TABLE xmigra.applied ADD CONSTRAINT PK_version PRIMARY KEY ([MigrationID] ASC) WITH (
PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE  = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON
  ) ON [PRIMARY];
END;

IF NOT EXISTS (
  SELECT * FROM sys.objects
  WHERE object_id = OBJECT_ID(N'[xmigra].[previous_states]')
  AND type IN (N'U')
)
BEGIN
  CREATE TABLE [xmigra].[previous_states] (
[Changed]                   datetime NOT NULL,
[MigrationApplicationOrder] int NOT NULL,
[FromMigrationID]           nvarchar(80) COLLATE #{ID_COLLATION},
[ToRangeStartMigrationID]   nvarchar(80) COLLATE #{ID_COLLATION} NOT NULL,
[ToRangeEndMigrationID]     nvarchar(80) COLLATE #{ID_COLLATION} NOT NULL,

CONSTRAINT [PK_previous_states] PRIMARY KEY CLUSTERED (
  [Changed] ASC,
  [MigrationApplicationOrder] ASC
)
  );
END;
GO

IF NOT EXISTS (
  SELECT * FROM sys.objects
  WHERE object_id = OBJECT_ID(N'[xmigra].[DF_version_VersionBridgeMark]')
  AND type IN (N'D')
)
BEGIN
  ALTER TABLE [xmigra].[applied] ADD CONSTRAINT [DF_version_VersionBridgeMark]
DEFAULT (0) FOR [VersionBridgeMark];
END;
GO

IF NOT EXISTS (
  SELECT * FROM sys.objects
  WHERE object_id = OBJECT_ID(N'[xmigra].[access_objects]')
  AND type IN (N'U')
)
BEGIN
  CREATE TABLE [xmigra].[access_objects] (
[type] nvarchar(40) NOT NULL,
[name] nvarchar(256) NOT NULL,
[order] int identity(1,1) NOT NULL,

CONSTRAINT [PK_access_objects] PRIMARY KEY CLUSTERED (
  [name] ASC
) WITH (
  PAD_INDEX = OFF,
  STATISTICS_NORECOMPUTE  = OFF,
  IGNORE_DUP_KEY = OFF,
  ALLOW_ROW_LOCKS = ON,
  ALLOW_PAGE_LOCKS = ON
) ON [PRIMARY]
  ) ON [PRIMARY];
END;
GO

IF NOT EXISTS (
  SELECT * FROM sys.objects
  WHERE object_id = OBJECT_ID(N'[xmigra].[indexes]')
  AND type in (N'U')
)
BEGIN
  CREATE TABLE [xmigra].[indexes] (
[IndexID] nvarchar(80) NOT NULL PRIMARY KEY,
[name] nvarchar(256) NOT NULL
  ) ON [PRIMARY];
END;

IF NOT EXISTS (
  SELECT * FROM sys.objects
  WHERE object_id = OBJECT_ID(N'[xmigra].[statistics]')
  AND type in (N'U')
)
BEGIN
  CREATE TABLE [xmigra].[statistics] (
[Name] nvarchar(100) NOT NULL PRIMARY KEY,
[Columns] nvarchar(256) NOT NULL
  ) ON [PRIMARY];
END;

IF NOT EXISTS (
  SELECT * FROM sys.objects
  WHERE object_id = OBJECT_ID(N'[xmigra].[branch_upgrade]')
  AND type in (N'U')
)
BEGIN
  CREATE TABLE [xmigra].[branch_upgrade] (
[ApplicationOrder] int identity(1,1) NOT NULL,
[Current] nvarchar(80) COLLATE #{ID_COLLATION} NOT NULL PRIMARY KEY,
[Next] nvarchar(80) COLLATE #{ID_COLLATION} NULL,
[UpgradeSql] nvarchar(max) NULL,
[CompletesMigration] nvarchar(80) COLLATE #{ID_COLLATION} NULL
  ) ON [PRIMARY];
END;
GO
ALTER TABLE [xmigra].[branch_upgrade] ALTER COLUMN [CompletesMigration] nvarchar(80) COLLATE #{ID_COLLATION} NULL;

IF EXISTS (
  SELECT * FROM sys.objects
  WHERE object_id = OBJECT_ID(N'[xmigra].[last_applied_migrations]')
  AND type IN (N'V')
)
BEGIN
  DROP VIEW [xmigra].[last_applied_migrations];
END;
GO

CREATE VIEW [xmigra].[last_applied_migrations] AS
SELECT
  ROW_NUMBER() OVER (ORDER BY a.[ApplicationOrder] DESC) AS [RevertOrder],
  a.[Description]
FROM
  [xmigra].[applied] a
WHERE
  a.[ApplicationOrder] > COALESCE((
SELECT TOP (1) ps.[MigrationApplicationOrder]
FROM [xmigra].[previous_states] ps
JOIN [xmigra].[applied] a2 ON ps.[ToRangeStartMigrationID] = a2.[MigrationID]
ORDER BY ps.[Changed] DESC
  ), 0);
  END_OF_SQL
end

#existence_test_sqlObject



1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
# File 'lib/xmigra/db_support/mssql.rb', line 1054

def existence_test_sql
  object_type_list = object_type_codes.collect {|t| "N'#{t}'"}.join(', ')
  
  return <<-"END_OF_SQL"
EXISTS (
  SELECT * FROM sys.objects
  WHERE object_id = OBJECT_ID(N'#{quoted_name}')
  AND type IN (#{object_type_list})
)
  END_OF_SQL
end

#filename_metavariableObject



42
# File 'lib/xmigra/db_support/mssql.rb', line 42

def filename_metavariable; "[{filename}]"; end

#function_definition_template_sqlObject



1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
# File 'lib/xmigra/db_support/mssql.rb', line 1180

def function_definition_template_sql
  <<-"END_OF_SQL"
CREATE FUNCTION [{filename}] (
  <<<parameters>>>
) RETURNS <<<return-type>>>
AS BEGIN
  <<<statements>>>
  RETURN <<<return-value-expression>>>;
END
END_OF_SQL
end

#grant_permissions_sql(permissions, object, principal) ⇒ Object



945
946
947
948
949
950
951
952
953
954
# File 'lib/xmigra/db_support/mssql.rb', line 945

def grant_permissions_sql(permissions, object, principal)
  strlit = MSSQLSpecifics.method(:string_literal)
  permissions_string = permissions.to_a.join(', ')
  
  <<-"END_OF_SQL"
PRINT N'Granting #{permissions_string} on #{object} to #{principal}:';
GRANT #{permissions_string} ON #{object} TO #{principal};
EXEC [xmigra].[ip_prepare_revoke] #{strlit[permissions_string]}, #{strlit[object]}, #{strlit[principal]};
  END_OF_SQL
end

#granting_permissions_comment_sqlObject



937
938
939
940
941
942
943
# File 'lib/xmigra/db_support/mssql.rb', line 937

def granting_permissions_comment_sql
  <<-"END_OF_SQL"
  
-- ---------------------- GRANTING PERMISSIONS ----------------------- --

  END_OF_SQL
end

#in_ddl_transaction(options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/xmigra/db_support/mssql.rb', line 60

def in_ddl_transaction(options={})
  parts = []
  parts << <<-"END_OF_SQL"
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

SET NOCOUNT ON
GO
-------------------- ERROR REFERENCE LINE --------------------
DECLARE @BATCH_START_OFFSET INTEGER; SET @BATCH_START_OFFSET = 0;
BEGIN TRY
  BEGIN TRAN;
  END_OF_SQL
  
  offset_lines = 5
  each_batch(yield) do |batch|
    batch_literal = MSSQLSpecifics.string_literal("\n" + batch)
    parts << "SET @BATCH_START_OFFSET = #{offset_lines}; EXEC sp_executesql @statement = #{batch_literal}; SET @BATCH_START_OFFSET = 0;"
    offset_lines += parts[-1].count("\n") + 1
  end
  
  if options[:dry_run]
    parts << "  PRINT N'Dry-run successful.  Rolling back changes.'; ROLLBACK TRAN;"
  else
    parts << "  COMMIT TRAN;"
  end
  parts << <<-"END_OF_SQL"
END TRY
BEGIN CATCH
  ROLLBACK TRAN;
  
  DECLARE @ErrorMessage NVARCHAR(4000);
  DECLARE @ErrorSeverity INT;
  DECLARE @ErrorState INT;
  
  PRINT N'Update failed: ' + ERROR_MESSAGE();
  PRINT N'    State: ' + CAST(ERROR_STATE() AS NVARCHAR);
  PRINT N'    Line: ' + CAST(@BATCH_START_OFFSET + ERROR_LINE() - 1 AS NVARCHAR) + N' after ERROR REFERENCE LINE'

  SELECT 
  @ErrorMessage = N'Update failed: ' + ERROR_MESSAGE(),
  @ErrorSeverity = ERROR_SEVERITY(),
  @ErrorState = ERROR_STATE();

  -- Use RAISERROR inside the CATCH block to return error
  -- information about the original error that caused
  -- execution to jump to the CATCH block.
  RAISERROR (@ErrorMessage, -- Message text.
         @ErrorSeverity, -- Severity.
         @ErrorState -- State.
         );
END CATCH;
  END_OF_SQL
  
  return parts.join("\n")
end

#index_template_sqlObject



1156
1157
1158
1159
1160
1161
# File 'lib/xmigra/db_support/mssql.rb', line 1156

def index_template_sql
  <<-"END_OF_SQL"
CREATE INDEX [{filename}]
ON <<<table>>> (<<<columns>>>);
END_OF_SQL
end

#insert_access_creation_record_sqlObject



956
957
958
959
960
961
962
963
# File 'lib/xmigra/db_support/mssql.rb', line 956

def insert_access_creation_record_sql
  name_literal = MSSQLSpecifics.string_literal(quoted_name)
  
  <<-"END_OF_SQL"
INSERT INTO [xmigra].[access_objects] ([type], [name])
VALUES (N'#{self.class::OBJECT_TYPE}', #{name_literal});
  END_OF_SQL
end

#migration_application_sqlObject

Call on an extended Migration object to get the SQL to execute.



966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
# File 'lib/xmigra/db_support/mssql.rb', line 966

def migration_application_sql
  id_literal = MSSQLSpecifics.string_literal(id)
  template = <<-"END_OF_SQL"
IF EXISTS (
  SELECT * FROM [xmigra].[migrations]
  WHERE [MigrationID] = #{id_literal}
  AND [Install] <> 0
)
BEGIN
  PRINT #{MSSQLSpecifics.string_literal('Applying "' + File.basename(file_path) + '":')};
  
%s

  INSERT INTO [xmigra].[applied] ([MigrationID], [Description])
  VALUES (#{id_literal}, #{MSSQLSpecifics.string_literal(description)});
END;
  END_OF_SQL
  
  parts = []
  
  each_batch(sql) do |batch|
    parts << batch
  end
  
  return (template % parts.collect do |batch|
    "EXEC sp_executesql @statement = " + MSSQLSpecifics.string_literal(batch) + ";"
  end.join("\n"))
end

#name_partsObject



1034
1035
1036
1037
1038
1039
1040
1041
1042
# File 'lib/xmigra/db_support/mssql.rb', line 1034

def name_parts
  if m = DBNAME_PATTERN.match(name)
    [m[1], m[2]].compact.collect do |p|
      MSSQLSpecifics.strip_identifier_quoting(p)
    end
  else
    raise XMigra::Error, "Invalid database object name"
  end
end

#object_type_codesObject



1050
1051
1052
# File 'lib/xmigra/db_support/mssql.rb', line 1050

def object_type_codes
  MSSQLSpecifics.object_type_codes(self)
end

#procedure_definition_template_sqlObject



1170
1171
1172
1173
1174
1175
1176
1177
1178
# File 'lib/xmigra/db_support/mssql.rb', line 1170

def procedure_definition_template_sql
  <<-"END_OF_SQL"
CREATE PROCEDURE [{filename}]
  <<<parameters>>>
AS BEGIN
  <<<statements>>>
END
END_OF_SQL
end

#production_config_check_sqlObject



585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# File 'lib/xmigra/db_support/mssql.rb', line 585

def production_config_check_sql
  unless production
    id_literal = MSSQLSpecifics.string_literal(@migrations[0].id)
    <<-"END_OF_SQL"
PRINT N'Checking for production status:';
IF EXISTS (
  SELECT * FROM [xmigra].[migrations]
  WHERE [MigrationID] = #{id_literal}
  AND [Install] <> 0
)
BEGIN
  CREATE TABLE [xmigra].[development] (
[info] nvarchar(200) NOT NULL PRIMARY KEY
  );
END;
GO

IF NOT EXISTS (
  SELECT * FROM [sys].[objects]
  WHERE object_id = OBJECT_ID(N'[xmigra].[development]')
  AND type = N'U'
)
RAISERROR(N'Development script cannot be applied to a production database.', 11, 1);
    END_OF_SQL
  end
end

#quoted_nameObject



1044
1045
1046
1047
1048
# File 'lib/xmigra/db_support/mssql.rb', line 1044

def quoted_name
  name_parts.collect do |p|
    "[]".insert(1, p)
  end.join('.')
end

#remove_access_artifacts_sqlObject



612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
# File 'lib/xmigra/db_support/mssql.rb', line 612

def remove_access_artifacts_sql
  # Iterate the [xmigra].[access_objects] table and drop all access
  # objects previously created by xmigra
  return <<-"END_OF_SQL"
PRINT N'Removing data access artifacts:';
DECLARE @sqlcmd NVARCHAR(1000); -- Built SQL command
DECLARE @obj_name NVARCHAR(256); -- Name of object to drop
DECLARE @obj_type NVARCHAR(40); -- Type of object to drop

DECLARE AccObjs_cursor CURSOR LOCAL FOR
SELECT [name], [type]
FROM [xmigra].[access_objects]
ORDER BY [order] DESC;

OPEN AccObjs_cursor;

FETCH NEXT FROM AccObjs_cursor INTO @obj_name, @obj_type;

WHILE @@FETCH_STATUS = 0 BEGIN
  SET @sqlcmd = N'DROP ' + @obj_type + N' ' + @obj_name + N';';
  EXEC sp_executesql @sqlcmd;
  
  FETCH NEXT FROM AccObjs_cursor INTO @obj_name, @obj_type;
END;

CLOSE AccObjs_cursor;
DEALLOCATE AccObjs_cursor;

DELETE FROM [xmigra].[access_objects];

END_OF_SQL
end

#remove_undesired_indexes_sqlObject



645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
# File 'lib/xmigra/db_support/mssql.rb', line 645

def remove_undesired_indexes_sql
  <<-"END_OF_SQL"
PRINT N'Removing undesired indexes:';
-- Iterate over indexes in [xmigra].[indexes] that don't have an entry in
-- [xmigra].[updated_indexes].
DECLARE @sqlcmd NVARCHAR(1000); -- Built SQL command
DECLARE @index_name NVARCHAR(256); -- Name of index to drop
DECLARE @table_name SYSNAME; -- Name of table owning index
DECLARE @match_count INT; -- Number of matching index names

DECLARE Index_cursor CURSOR LOCAL FOR
SELECT 
  xi.[name], 
  MAX(QUOTENAME(OBJECT_SCHEMA_NAME(si.object_id)) + N'.' + QUOTENAME(OBJECT_NAME(si.object_id))), 
  COUNT(*)
FROM [xmigra].[indexes] xi
INNER JOIN sys.indexes si ON si.[name] = xi.[name]
WHERE xi.[IndexID] NOT IN (
  SELECT [IndexID]
  FROM [xmigra].[updated_indexes]
)
GROUP BY xi.[name];

OPEN Index_cursor;

FETCH NEXT FROM Index_cursor INTO @index_name, @table_name, @match_count;

WHILE @@FETCH_STATUS = 0 BEGIN
  IF @match_count > 1
  BEGIN
RAISERROR(N'Multiple indexes are named %s', 11, 1, @index_name);
  END;
  
  SET @sqlcmd = N'DROP INDEX ' + @index_name + N' ON ' + @table_name + N';';
  EXEC sp_executesql @sqlcmd;
  PRINT N'    Removed ' + @index_name + N'.';
  
  FETCH NEXT FROM Index_cursor INTO @index_name, @table_name, @match_count;
END;

CLOSE Index_cursor;
DEALLOCATE Index_cursor;

DELETE FROM [xmigra].[indexes]
WHERE [IndexID] NOT IN (
  SELECT ui.[IndexID]
  FROM [xmigra].[updated_indexes] ui
);
  END_OF_SQL
end

#remove_undesired_statistics_sqlObject



696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
# File 'lib/xmigra/db_support/mssql.rb', line 696

def remove_undesired_statistics_sql
  <<-"END_OF_SQL"
PRINT N'Removing undesired statistics objects:';
-- Iterate over statistics in [xmigra].[statistics] that don't have an entry in
-- [xmigra].[updated_statistics].
DECLARE @sqlcmd NVARCHAR(1000); -- Built SQL command
DECLARE @statsobj_name NVARCHAR(256); -- Name of statistics object to drop
DECLARE @table_name SYSNAME; -- Name of table owning the statistics object
DECLARE @match_count INT; -- Number of matching statistics object names

DECLARE Stats_cursor CURSOR LOCAL FOR
SELECT 
  QUOTENAME(xs.[Name]), 
  MAX(QUOTENAME(OBJECT_SCHEMA_NAME(ss.object_id)) + N'.' + QUOTENAME(OBJECT_NAME(ss.object_id))), 
  COUNT(ss.object_id)
FROM [xmigra].[statistics] xs
INNER JOIN sys.stats ss ON ss.[name] = xs.[Name]
WHERE xs.[Columns] NOT IN (
  SELECT us.[Columns]
  FROM [xmigra].[updated_statistics] us
  WHERE us.[Name] = xs.[Name]
)
GROUP BY xs.[Name];

OPEN Stats_cursor;

FETCH NEXT FROM Stats_cursor INTO @statsobj_name, @table_name, @match_count;

WHILE @@FETCH_STATUS = 0 BEGIN
  IF @match_count > 1
  BEGIN
RAISERROR(N'Multiple indexes are named %s', 11, 1, @statsobj_name);
  END;
  
  SET @sqlcmd = N'DROP STATISTICS ' + @table_name + N'.' + @statsobj_name + N';';
  EXEC sp_executesql @sqlcmd;
  PRINT N'    Removed statistics object ' + @statsobj_name + N'.'
  
  FETCH NEXT FROM Stats_cursor INTO @statsobj_name, @table_name, @match_count;
END;

CLOSE Stats_cursor;
DEALLOCATE Stats_cursor;

DELETE FROM [xmigra].[statistics]
WHERE [Columns] NOT IN (
  SELECT us.[Columns]
  FROM [xmigra].[updated_statistics] us
  WHERE us.[Name] = [Name]
);
  END_OF_SQL
end

#reversion_tracking_sqlObject



995
996
997
# File 'lib/xmigra/db_support/mssql.rb', line 995

def reversion_tracking_sql
  "DELETE FROM [xmigra].[applied] WHERE [MigrationID] = '#{id}';\n"
end

#revoke_previous_permissions_sqlObject



895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
# File 'lib/xmigra/db_support/mssql.rb', line 895

def revoke_previous_permissions_sql
  <<-"END_OF_SQL"

-- ------------- REVOKING PREVIOUSLY GRANTED PERMISSIONS ------------- --

PRINT N'Revoking previously granted permissions:';
-- Iterate over permissions listed in [xmigra].[revokable_permissions]
DECLARE @sqlcmd NVARCHAR(1000); -- Built SQL command
DECLARE @permissions NVARCHAR(200); 
DECLARE @object NVARCHAR(260); 
DECLARE @principal NVARCHAR(150);

DECLARE Permission_cursor CURSOR LOCAL FOR
SELECT
  xp.[permissions],
  xp.[object],
  QUOTENAME(sdp.name)
FROM [xmigra].[revokable_permissions] xp
INNER JOIN sys.database_principals sdp ON xp.principal_id = sdp.principal_id;

OPEN Permission_cursor;

FETCH NEXT FROM Permission_cursor INTO @permissions, @object, @principal;

WHILE @@FETCH_STATUS = 0 BEGIN
  SET @sqlcmd = N'REVOKE ' + @permissions + N' ON ' + @object + ' FROM ' + @principal + N';';
  BEGIN TRY
EXEC sp_executesql @sqlcmd;
  END TRY
  BEGIN CATCH
  END CATCH
  
  FETCH NEXT FROM Permission_cursor INTO @permissions, @object, @principal;
END;

CLOSE Permission_cursor;
DEALLOCATE Permission_cursor;

DELETE FROM [xmigra].[revokable_permissions];
  END_OF_SQL
end

#select_for_install_sqlObject



515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# File 'lib/xmigra/db_support/mssql.rb', line 515

def select_for_install_sql
  <<-"END_OF_SQL"
PRINT N'Selecting migrations to apply:';
DECLARE @BridgePoint INT;
IF #{upgrading_to_new_branch_test_sql}
BEGIN
  -- Get the [xmigra].[migrations] ApplicationOrder of the record corresponding to the branch transition
  SET @BridgePoint = (
SELECT MAX(m.[ApplicationOrder])
FROM [xmigra].[migrations] m
INNER JOIN [xmigra].[branch_upgrade] bu
  ON m.[MigrationID] = bu.[CompletesMigration]
  );
  
  UPDATE [xmigra].[migrations]
  SET [Install] = 1
  WHERE [ApplicationOrder] > @BridgePoint;
END
ELSE BEGIN
  -- Get the [xmigra].[migrations] ApplicationOrder of the most recent version bridge migration
  SET @BridgePoint = (
SELECT COALESCE(MAX(m.[ApplicationOrder]), 0)
FROM [xmigra].[applied] a
INNER JOIN [xmigra].[migrations] m
  ON a.[MigrationID] = m.[MigrationID]
WHERE a.[VersionBridgeMark] <> 0
  );
  
  UPDATE [xmigra].[migrations]
  SET [Install] = 1
  WHERE [MigrationID] NOT IN (
SELECT a.[MigrationID] FROM [xmigra].[applied] a
  )
  AND [ApplicationOrder] > @BridgePoint;
END;

INSERT INTO [xmigra].[previous_states] (
  [Changed],
  [MigrationApplicationOrder],
  [FromMigrationID],
  [ToRangeStartMigrationID],
  [ToRangeEndMigrationID]
)
SELECT TOP (1)
  CURRENT_TIMESTAMP,
  -- Application order of last installed migration --
  COALESCE(
( 
  SELECT TOP(1) [ApplicationOrder] FROM [xmigra].[applied]
  ORDER BY [ApplicationOrder] DESC
),
0
  ),
  ( -- Last installed migration --
SELECT TOP (1) [MigrationID]
FROM [xmigra].[applied]
ORDER BY [ApplicationOrder] DESC
  ),
  m.[MigrationID],
  ( -- Last migration to install --
SELECT TOP(1) [MigrationID] FROM [xmigra].[migrations]
WHERE [Install] <> 0
ORDER BY [ApplicationOrder] DESC
  )
FROM [xmigra].[migrations] m
WHERE m.[Install] <> 0
ORDER BY m.[ApplicationOrder] ASC;
  END_OF_SQL
end

#stats_objsObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/xmigra/db_support/mssql.rb', line 44

def stats_objs
  return @stats_objs if @stats_objs
  
  begin
    stats_data = YAML::load_file(path.join(MSSQLSpecifics::STATISTICS_FILE))
  rescue Errno::ENOENT
    return @stats_objs = [].freeze
  end
  
  @stats_objs = stats_data.collect {|item| StatisticsObject.new(*item)}
  @stats_objs.each {|o| o.freeze}
  @stats_objs.freeze
  
  return @stats_objs
end

#upgrade_cleanup_sqlObject



831
832
833
834
835
836
837
838
# File 'lib/xmigra/db_support/mssql.rb', line 831

def upgrade_cleanup_sql
  <<-"END_OF_SQL"
PRINT N'Cleaning up from the upgrade:';
DROP TABLE [xmigra].[migrations];
DROP TABLE [xmigra].[updated_indexes];
DROP TABLE [xmigra].[updated_statistics];
  END_OF_SQL
end

#upgrading_to_new_branch_test_sqlObject



1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
# File 'lib/xmigra/db_support/mssql.rb', line 1070

def upgrading_to_new_branch_test_sql
  return "(0 = 1)" unless respond_to? :branch_identifier
  
  (<<-"END_OF_SQL").chomp
(EXISTS (
  SELECT TOP(1) * FROM [xmigra].[branch_upgrade]
  WHERE [Next] = #{branch_id_literal}
))
  END_OF_SQL
end

#view_definition_template_sqlObject



1163
1164
1165
1166
1167
1168
# File 'lib/xmigra/db_support/mssql.rb', line 1163

def view_definition_template_sql
  <<-"END_OF_SQL"
CREATE VIEW [{filename}]
AS SELECT <<<query>>>;
END_OF_SQL
end