This repository has been archived by the owner on Jun 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy path0001-db-migration-add-script-to-squash-DB-migrations.patch
1237 lines (1225 loc) · 52.1 KB
/
0001-db-migration-add-script-to-squash-DB-migrations.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
324
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
371
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
401
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
432
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
474
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
514
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
584
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
611
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
644
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
695
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
748
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
790
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
830
831
832
833
834
835
836
837
838
839
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
894
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
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
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
994
995
996
997
998
999
1000
From 138b178fc7bb1a789f1cf48e367cb0ed9c60cd9c Mon Sep 17 00:00:00 2001
From: Mark Yen <[email protected]>
Date: Wed, 15 May 2019 12:03:32 -0700
Subject: [PATCH] db migration: add script to squash DB migrations
This adds a rake task to squash DB migrations, outputting a SCF-style
patch that will blank out the squashed migration scripts, and instead
create a new migration that configures the database schema from scratch.
This should lead to faster migrations (as we don't need to repeatedly
do schema changes), and let things fail safer (as most migrations now do
nothing, they can be replayed with no worries).
The migration script has a hard-coded date that is the limit for
squashing, so that we can still upgrade from existing installations that
may have some of the migrations applied.
It appears that there are still some differences with a database
generated via the squashed migration, compared to a normal one:
- Missing / added charset=utf8, collate=utf8_bin
(This shouldn't be an issue, because of table defaults)
- Some indexes &c. are in a different column order.
This is not expected to cause any issues.
To use the script:
- Get a working cloud_controller_ng container (e.g. in api-group), where
the previous migrations have _not_ been patched.
- Apply this patch.
- Update the constants at the top of `db/helpers/squasher.rb`:
- MAXIMUM_MIGRATION_TIMESTAMP
- PREVIOUS_SQUASHED_MIGRATIONS
- Run as the `vcap` user (i.e. `su vcap -`)
- Source `/var/vcap/jobs/cloud_controller_ng/bin/ruby_version.sh`
(To pick the the correct ruby)
- Run `bin/rake db:squash NAME=/tmp/patch.sh`
- Copy `/tmp/patch.sh` where it would get used.
- Update this patch file so we have a record of the new timestamps.
---
db/helpers/squasher.rb | 1128 +++++++++++++++++++++++++++++++++++
lib/cloud_controller/db_migrator.rb | 7 +-
lib/tasks/db.rake | 18 +-
3 files changed, 1149 insertions(+), 4 deletions(-)
create mode 100644 db/helpers/squasher.rb
diff --git a/db/helpers/squasher.rb b/db/helpers/squasher.rb
new file mode 100644
index 000000000..ca2509afd
--- /dev/null
+++ b/db/helpers/squasher.rb
@@ -0,0 +1,1128 @@
+require 'set'
+
+def status(msg)
+ puts " #{msg}" if DBSquasher::Sequel.instance.verbose
+end
+
+module DBSquasher
+
+ # Any migrations after this time still not be squashed
+ MAXIMUM_MIGRATION_TIMESTAMP = '20190327'
+
+ # List of previous squashed migration timestamps; this list is used to
+ # create additional empty migrations (to ensure we can upgrade from
+ # previously-squashed migrations).
+ PREVIOUS_SQUASHED_MIGRATIONS = %w(
+ )
+
+ class Sequel
+
+ class Table
+ def initialize(name)
+ @name = name
+ @primary_key = nil
+ @members = []
+ @receiver = nil
+ @charset = nil
+ @dependent_tables = Set.new
+ end
+ attr_writer :name
+ attr_accessor :charset
+ attr_reader :dependent_tables
+
+ # receiver munging for context changes
+ def using_context(receiver)
+ raise "Nested use for using_context" if @receiver
+ @receiver = receiver
+ begin
+ yield
+ ensure
+ @receiver = nil
+ end
+ end
+
+ def method_missing(method, *args, &block)
+ raise "invalid receiver" if @receiver.eql? self
+ @receiver.send method, *args, &block
+ end
+
+ def primary_key(key, opts={})
+ raise "Adding primary key again to #{key}" unless @primary_key.nil?
+ @primary_key = [key, opts]
+ end
+ alias_method :add_primary_key, :primary_key
+
+ def column(type, name, opts={})
+ status "Adding column #{@name}::#{name} (type #{type}) (#{opts.inspect})"
+ column = { kind: :column, name: name, type: type, opts: opts }
+ @members << column
+ if opts[:unique]
+ i = index([name], name: name)
+ i[:auto] = [column, :unique]
+ end
+ end
+
+ def column_names
+ _members_of_kind(:column).map { |c| c[:opts][:name] || c[:name] }
+ end
+
+ def _show_full_columns
+ # We want to return an array of hashes, with the keys:
+ # :Field -> column name
+ # :Null -> opts[:null], as `YES` / `NO`
+ # :Default -> opts[:default]
+ # :Type
+ _members_of_kind(:column).map do |column|
+ default = column[:opts][:default]
+ default = 'CURRENT_TIMESTAMP' if default == ::Sequel::CURRENT_TIMESTAMP
+ type = column[:method] || column[:type]
+ null = case column[:opts][:null]
+ when true then 'YES'
+ when false then 'NO'
+ else [:Timestamp].include?(type) ? 'NO' : 'YES'
+ end
+ {
+ Field: column[:opts][:name] || column[:name],
+ Null: null,
+ Default: default,
+ Type: column[:method] || column[:type],
+ }
+ end
+ end
+
+ COLUMN_TYPES = %w(
+ Integer String File Fixnum Bignum Float BigDecimal Date
+ DateTime Time Timestamp Numeric TrueClass FalseClass Boolean
+ )
+
+ # We don't use method_missing? because that's a good way to figure
+ # out what functionality we need to implement
+ COLUMN_TYPES.each do |type_name|
+ type_name = type_name.to_sym
+ define_method type_name do |name, opts={}|
+ status "Adding column #{@name}::#{name} (method #{type_name}) (#{opts.inspect})"
+ column = { kind: :column, name: name, method: type_name, opts: opts }
+ @members << column
+ if opts[:unique]
+ index_name = opts[:unique_constraint_name] || opts[:name]
+ index_name ||= name
+ i = index([name], name: index_name.to_sym, unique: true)
+ i[:auto] = [column, :unique]
+ end
+ if opts[:index]
+ idx_opts = { name: "#{@name}_#{name}".to_sym }
+ idx_opts.merge! opts[:index] if opts[:index].is_a? Hash
+ i = index([name], idx_opts)
+ i[:auto] = [column, :index]
+ end
+ end
+ end
+
+ def add_column(name, type, opts={})
+ raise "Adding duplicate column #{name} in table #{@name}" if _members_of_kind(:column).any? { |c| c[:name] == name }
+ if type.is_a? Class
+ self.send type.name, name, opts
+ else
+ column type, name, opts # yes the argument order is different
+ end
+ end
+
+ def rename_column(old_name, new_name, opts={})
+ opts = parse_column_definition(opts) if opts.is_a? String
+ status "Renaming column #{@name}::#{old_name} -> #{new_name} (#{opts.inspect})"
+ _find_column(old_name) do |c|
+ c[:name] = new_name
+ if [:type, :method].any? { |k| opts.has_key? k }
+ c.delete_if { |k| [:type, :method].include? k }
+ c[:type] = opts[:type] if opts.has_key? :type
+ c[:method] = opts[:method] if opts.has_key? :method
+ case (c[:method] || c[:type]).to_s.downcase.to_sym
+ when :string, :text, :mediumtext
+ # Delete existing text size when changing text type
+ # Fixes:
+ # 20151231224207_increase_security_group_rules_length.rb
+ c[:opts].delete :size
+ c[:opts].delete :text
+ end
+ end
+ c[:opts].merge! opts.reject { |k| [:type, :method].include? k }
+ status " (Result: #{c.inspect})"
+ _members_of_kind(:index).each do |index|
+ index[:columns].map! { |k| k.to_sym == old_name ? new_name : k }
+ end
+ Sequel.instance.tables.each do |table|
+ Sequel.instance._rename_foreign_column table, @name.to_sym, old_name, new_name
+ end
+ end
+ # If there was a foreign key on this column, that was then dropped,
+ # delete the automatically-created index
+ dropped_indexes = []
+ @members.each_with_index do |member, i|
+ next unless member[:kind] == :index
+ next unless member[:auto]
+ columns, reason = member[:auto]
+ next unless reason = :foreign_key
+ dropped_indexes << i if columns.include? new_name
+ end
+ dropped_indexes.reverse_each do |i|
+ status " Dropping index created due to foreign key: #{@members[i].inspect}"
+ @members.delete i
+ end
+ end
+
+ # When a column in a table is renamed, we need to go through all the
+ # other tables and rename the foreign keys to match
+ def _rename_foreign_column(table, old_name, new_name)
+ old_name = old_name.to_sym
+ new_name = new_name.to_sym
+ _members_of_kind(:constraint).each do |constraint|
+ next unless constraint[:type] == :foreign_key
+ next unless constraint[:table] == table.to_sym
+ key = constraint[:opts][:key] || constraint[:columns]
+ key = [key] unless key.is_a? Array
+ next unless key.include? :old_name
+ if constraint[:opts][:key]
+ if constraint[:opts][:key].is_a? Array
+ constraint[:opts][:key].map! { |k| k.to_sym == old_name ? new_name : k }
+ else
+ constraint[:opts][:key] = new_name
+ end
+ else
+ if constraint[:columns].is_a? Array
+ constraint[:columns].map! { |k| k.to_sym == old_name ? new_name : k }
+ else
+ constraint[:columns] = new_name
+ end
+ end
+ end
+ end
+
+ def drop_column(name, opts={})
+ raise "drop_column with cascade not supported" if opts[:cascade]
+ status "Dropping column #{@name}::#{name} (#{opts.inspect})"
+ @members.reject! { |m| m[:kind] == :column && m[:name] == name }
+ dropped_indexes = []
+ @members.each_with_index do |member, i|
+ next unless member[:kind] == :index
+ member[:columns].delete name
+ dropped_indexes << i if member[:columns].empty?
+ end
+ dropped_indexes.reverse.each do |i|
+ @members.delete_at(i)
+ end
+ end
+
+ def set_column_allow_null(name, allow_null=true)
+ status "Setting #{@name}::#{name} to allow null #{allow_null}"
+ _find_column(name) { |c| c[:opts].merge! null: allow_null }
+ end
+
+ def set_column_not_null(name)
+ set_column_allow_null name, false
+ end
+
+ def set_column_type(name, type, opts={})
+ status "Setting #{@name}::#{name} to type #{type} (#{opts.inspect})"
+ if type.is_a? Class
+ opts = opts.merge(method: type.name.to_sym)
+ else
+ opts = opts.merge(type: type.to_sym)
+ end
+ rename_column name, name, opts
+ end
+
+ def set_column_default(name, default)
+ status "Setting #{@name}::#{name} to default #{default.inspect}"
+ _find_column(name) { |c| c[:opts].merge! default: default }
+ end
+
+ def index(columns, opts={})
+ columns = [columns] unless columns.is_a? Array
+ opts[:name] = opts[:name].to_sym if opts[:name]
+ # We need to explicitly give the index a name based on the
+ # current table name, in case the table gets renamed later.
+ opts[:name] ||= "#{@name}_#{columns.join('_')}_index".to_sym
+ existing = _members_of_kind(:index).find do |c|
+ c[:opts][:name] == opts[:name]
+ end
+ if existing
+ status "Updating existing index on #{@name}: #{columns} (#{opts.inspect})"
+ existing[:columns] = columns
+ existing[:opts].merge! opts
+ existing
+ else
+ @members << { kind: :index, columns: columns, opts: opts }
+ status "Adding index on #{@name}: #{@members.last.inspect}"
+ @members.last
+ end
+ end
+ alias_method :add_index, :index
+
+ def indexes
+ Hash.new.tap do |h|
+ _members_of_kind(:index).map do |index|
+ h[index[:opts][:name] || index[:columns]] = index
+ end
+ end
+ end
+
+ def drop_index(columns, opts={})
+ raise "drop_index with cascade not supported" if opts[:cascade]
+ status "Dropping index on #{@name}: #{columns} (#{opts.inspect})"
+ index = nil
+ if opts[:name]
+ index = @members.each_with_index.find_index do |member, i|
+ next false unless member[:kind] == :index
+ member[:opts][:name] == opts[:name]
+ end
+ end
+ if index.nil?
+ columns = [columns] unless columns.is_a? Array
+ index = @members.each_with_index.reverse_each.find do |member, i|
+ next false unless member[:kind] == :index
+ member[:columns] == columns
+ end.last
+ end
+ unless index.nil?
+ message = " Dropping #{@members[index].inspect}"
+ if @members[index][:auto]
+ column, key = @members[index][:auto]
+ message += " (with auto-index #{key})"
+ column[:opts].delete key
+ end
+ status message
+ return @members.delete_at index
+ end
+ return if opts[:if_exists]
+ _members_of_kind(:index).each { |i| status " unmatched index: #{i.inspect}" }
+ status "Could not find index #{columns} to drop (#{opts.inspect}), ignored"
+ end
+
+ def foreign_key(columns, table=nil, opts={})
+ opts = {table: table}.merge(opts)
+ @members << {
+ kind: :constraint,
+ type: :foreign_key,
+ columns: columns,
+ opts: opts
+ }
+ @dependent_tables << table unless table.nil?
+ status "Adding foreign key on #{@name}: #{@members.last.inspect} (#{columns.inspect})"
+ end
+ alias_method :add_foreign_key, :foreign_key
+
+ def drop_foreign_key(name, opts={})
+ # There are four ways to find the constraint to drop:
+ # 1. `opts[:name]` is given
+ # 2. `name` is the name of the foreign key
+ # 3. `name` is an array (of the column names)
+ # 4. `name` is the name of the column (i.e. as above, but array of 1)
+ status "Dropping foreign key on #{@name}: #{name} (#{opts.inspect})"
+ name = opts[:name] || name
+ opts = opts.merge(type: :foreign_key)
+ index = _find_constraint(name, opts)
+ if index.nil?
+ _members_of_kind(:constraint).each { |c| status " Remaining: #{c.inspect}" }
+ raise "Could not drop foreign key #{name} / #{opts.inspect} on table #{@name}"
+ end
+ # When MySQL creates a foreign key, it also automatically creates
+ # an index; however, dropping that foreign key does _not_ then
+ # drop the index. We need to compensate for that by converting
+ # the to-be-dropped foreign key into an index instead.
+ name = "#{@name}_#{name.join('_')}_index".to_sym if name.is_a? Array
+ existing_index = _members_of_kind(:index).find { |c| c[:opts][:name] == name }
+ if existing_index
+ # We have the index already, somehow. Just drop the FK.
+ status " Found existing index in #{@name}: #{existing_index.inspect}"
+ @members.delete_at index
+ return
+ end
+ # We do _not_ have an index for the FK; convert it.
+ columns = @members[index][:columns]
+ columns = [columns] unless columns.is_a? Array
+ @members[index] = {
+ kind: :index,
+ columns: columns,
+ opts: { name: name },
+ auto: [columns, :foreign_key]
+ }
+ status " Converting foreign key #{@name}::#{name} into index #{@members[index].inspect}"
+ end
+
+ # This is a helper to select for foreign keys; not to be used by the
+ # migration scripts.
+ def foreign_keys
+ _members_of_kind(:constraint).select { |c| c[:type] == :foreign_key }
+ end
+
+ def foreign_key_list(opts={})
+ raise "foreign_key_list does not support opts" unless opts.empty?
+ # This is used in 20130530173342_fix_permission_fk_problems.rb
+ # It appears to expect a list of hashes, which contain the
+ # `:columns` key; the value is an array of the generated FK name?
+ # As well as a `:name` column with the name…
+ # We need a `:table` key with the referenced table, too, for FKs
+ status " foreign_key_list(#{@name})"
+ foreign_keys.map do |fk|
+ status " foreign key: #{fk.inspect}"
+ columns = fk[:columns].is_a?(Array) ? fk[:columns] : [fk[:opts][:name]]
+ fk[:opts].merge columns: columns
+ end
+ end
+
+ def unique(columns, opts={})
+ if opts[:name].is_a?(Array) && opts[:name].length == 1
+ opts[:name] = opts[:name].first
+ end
+ @members << { kind: :constraint, type: :unique, columns: columns, opts: opts}
+ status "Adding uniqe constraint on #{@name}: #{@members.last.inspect}"
+ end
+ alias_method :add_unique_constraint, :unique
+
+ def drop_constraint(name, opts={})
+ status "Dropping constraint on #{@name}: #{name} (#{opts.inspect})"
+ index = _find_constraint(name, opts)
+ unless index.nil?
+ status " Dropping #{index}: #{@members[index]}"
+ @members.delete_at index
+ return
+ end
+ _members_of_kind(:constraint).each { |c| status " Remaining: #{c.inspect}" }
+ raise "Could not drop constraint #{name} / #{opts} on table #{@name}"
+ end
+
+ def print(writer=STDOUT)
+ charset = @charset.nil? ? '' : ", charset: :#{@charset}"
+ writer.puts " create_table! :#{@name}#{charset} do"
+
+ @members.each do |member|
+ send "_print_#{member[:kind]}".to_sym, writer, member
+ end
+
+ unless @primary_key.nil?
+ key = @primary_key.first.inspect
+ args = _opts_to_arg_list @primary_key.last
+ writer.puts " primary_key #{key}#{args}"
+ end
+
+ writer.puts ' end'
+ end
+
+ # We print the foreign keys separately because we can actually end
+ # up with loops...
+ def print_foreign_keys(writer)
+ return if foreign_keys.empty?
+ writer.puts " alter_table :#{@name} do"
+ foreign_keys.each do |foreign_key|
+ #status " Foreign key: #{@name} -> #{foreign_key.inspect}"
+ columns = foreign_key[:columns].inspect
+ opts = foreign_key[:opts]
+ table = ''
+ if opts[:table]
+ table = ", #{opts[:table].inspect}"
+ opts.delete :table
+ end
+ writer.puts " add_foreign_key #{columns}#{table}#{_opts_to_arg_list opts}"
+ end
+ writer.puts ' end'
+ end
+
+ # When a table is renamed, we need to fix up all the foreign keys to
+ # point to the new table name. This is called in that case.
+ def _on_rename_table(old_name, new_name)
+ foreign_keys.each do |fk|
+ fk[:opts][:table] = new_name if fk[:opts][:table] == old_name
+ end
+ end
+
+ private
+
+ # We keep columns, keys, etc. all in the same list to preserve
+ # ordering where possible; this method returns all members of the
+ # given kind (:column, :index, :constraint)
+ def _members_of_kind(kind)
+ @members.select { |m| m[:kind] == kind }
+ end
+
+ # This makes the default name for a foreign key
+ def _make_foreign_key_name(name)
+ name = [name] unless name.is_a? Array
+ "fk_#{name.join('_')}_fkey".to_sym
+ end
+
+ # Find a column (with a given name) and yield it to a block.
+ # Raises an error if the column is not found.
+ def _find_column(name, &block)
+ column = _members_of_kind(:column).find { |c| c[:name] == name }
+ raise "Could not find column #{name} in table #{@name}" unless column
+ yield column
+ end
+
+ # Get the name of a constraint (for e.g. printing).
+ def _get_constraint_name(constraint)
+ return constraint[:opts][:name] if constraint[:opts][:name]
+ case constraint[:type]
+ when :foreign_key
+ _make_foreign_key_name(constraint[:columns])
+ when :unique
+ "#{@name}_#{constraint[:columns].map(&:to_s).join('_')}_key".to_sym
+ else
+ raise "Don't know how to fake a name for constraint #{constraint.inspect}"
+ end
+ end
+
+ # Look for a constraint that matches the given name/options (the
+ # options are used as another way to pass in the name). Confusingly
+ # the name may be the column(s) to match instead, due to how Sequel
+ # / MySQL actually does the matching...
+ def _find_constraint(name, opts={})
+ filters = case name
+ when String, Symbol
+ [
+ Proc.new do |constraint|
+ _get_constraint_name(constraint) == name.to_sym
+ end,
+ Proc.new do |constraint|
+ columns = constraint[:columns]
+ (columns.is_a?(Array) ? columns : [columns]) == [name]
+ end
+ ]
+ when Array
+ [
+ Proc.new do |constraint|
+ columns = constraint[:columns]
+ (columns.is_a?(Array) ? columns : [columns]) == name
+ end
+ ]
+ else
+ raise "Don't know how to drop constraints based on name #{name.inspect}"
+ end
+
+ index = nil
+ filters.each do |filter|
+ index = @members.index do |member|
+ next false unless member[:kind] == :constraint
+ next false unless [nil, member[:type]].include? opts[:type]
+ filter.call member
+ end
+ break if index
+ end
+ index
+ end
+
+ # Pretty-prints the given options list (using Ruby 1.9 hash syntax),
+ # and fix up things like how CURRENT_TIMESTAMP is referred to.
+ def _opts_to_arg_list(opts)
+ args = ''
+ opts.each do |k, v|
+ v = case v
+ when ::Sequel::CURRENT_TIMESTAMP, 'current_timestamp' then 'Sequel::CURRENT_TIMESTAMP'
+ when Symbol, String, NilClass then v.inspect
+ else v
+ end
+ args += ", #{k}: #{v}"
+ end
+ args
+ end
+
+ # Print out the statement required to create a column (via
+ # Sequel::Schema::CreateTableGenerator)
+ def _print_column(writer, column)
+ if column[:method]
+ args = "#{column[:method]} #{column[:name].inspect}"
+ else
+ args = "column #{column[:name].inspect}, #{column[:type].inspect}"
+ end
+ # For unknown reasons, the database we get from step-by-step
+ # migration (without squashing) does not contain indexes created
+ # with custom names in the column statement. Drop those here
+ # too, to better match the correct output.
+ opts = column[:opts].dup
+ opts.delete :index if opts[:index].is_a? Hash
+ writer.puts " #{args}#{_opts_to_arg_list opts}"
+ end
+
+ # Print out the statement required to create an index (via
+ # Sequel::Schema::CreateTableGenerator)
+ def _print_index(writer, index)
+ _, auto_reason = index[:auto] || [nil, nil]
+ # Do not actually add indexes that are implied
+ return if [:unqiue, :index].include? auto_reason
+ if auto_reason
+ # Check for duplicate indexes; skip any auto-generated indexes
+ # if they overlap (which would cause an error)
+ return if _members_of_kind(:index).any? do |other_index|
+ next false if other_index.equal? index
+ next false unless index[:columns] == other_index[:columns]
+ next false unless index[:unique] == other_index[:unique]
+ true
+ end
+ end
+
+ name = index[:opts][:name]
+ if name.nil?
+ index[:opts].delete :name
+ else
+ # Remove indexes that are implied from other columns
+ return if _members_of_kind(:column).any? do |column|
+ column[:opts][:unique] && column[:opts][:unique_constraint_name] == name
+ end
+ end
+ columns = index[:columns].inspect
+ if index[:columns].length == 1
+ col = index[:columns].first
+ return if _find_column(col) { |c| c[:opts][:index] }
+ columns = col.inspect
+ end
+ writer.puts " index #{columns}#{_opts_to_arg_list index[:opts]}"
+ end
+
+ # Print out the statement required to create a constraint (via
+ # Sequel::Schema::CreateTableGenerator)
+ def _print_constraint(writer, constraint)
+ case constraint[:type]
+ when :unique
+ writer.puts " unique #{constraint[:columns].inspect}#{_opts_to_arg_list constraint[:opts]}"
+ end
+ end
+
+ # this is used for `run()`, for ALTER TABLE (MODIFY / CHANGE)
+ # takes a table column definition and return the matching opts
+ def parse_column_definition(column_def)
+ opts = {}
+ column_def = column_def.strip.split.join(' ')
+ loop do
+ case column_def
+ when /default null$/i
+ opts[:default] = nil
+ when /default (?<default_value>[\w'"`]+)$/i
+ opts[:default] = $~[:default_value]
+ when /not null$/i
+ opts[:null] = false
+ when /null$/i
+ opts[:null] = true
+ when /collate (?<quote>[`"']?)(?<collation>\w+)\k<quote>$/i
+ opts[:collate] = $~[:collation]
+ else break
+ end
+ column_def = $~.pre_match.strip
+ end
+
+ type_mapping = {
+ longtext: :LongText,
+ mediumtext: :MediumText,
+ text: String,
+ }
+
+ method = COLUMN_TYPES.find { |t| t.downcase == column_def.downcase }
+ if method
+ opts.delete :type
+ opts[:method] = method.to_sym
+ elsif type_mapping.has_key? column_def.downcase.to_sym
+ opts.delete :method
+ opts[:type] = type_mapping[column_def.downcase.to_sym]
+ else
+ raise "Don't know how do handle ALTER TABLE column definition `#{column_def}`"
+ end
+
+ if opts.has_key? :default
+ type = opts[:method] || opts[:type]
+ case type
+ when :TrueClass, :FalseClass, :Boolean
+ opts[:default] = case opts[:default]
+ when %q<true>, %q<'true'> then true
+ when %q<false>, %q<'false'> then false
+ else opts[:default]
+ end
+ when :DateTime, :Time, :Timestamp
+ opts[:default] = case opts[:default]
+ when %q<CURRENT_TIMESTAMP>, %q<'CURRENT_TIMESTAMP'> then ::Sequel::CURRENT_TIMESTAMP
+ else opts[:default]
+ end
+ when :String
+ if opts[:default] =~ /^(?<quote>['"`])(?<value>.*)\k<quote>$/
+ opts[:default] = $~[:value]
+ end
+ when :Integer, :Fixnum, :Bignum, :BigDecimal, :Numeric
+ if opts[:default] =~ /^(?<quote>['"`])(?<value>.*)\k<quote>$/
+ opts[:default] = $~[:value].to_i
+ end
+ when :Float
+ if opts[:default] =~ /^(?<quote>['"`])(?<value>.*)\k<quote>$/
+ opts[:default] = $~[:value].to_f
+ end
+ else
+ raise "Don't know how to munge default value #{opts[:default]} for type #{type.inspect}"
+ end
+ end
+ opts
+ end
+
+ end
+
+ # Fake DataSet class; most of this only works because we rely on having
+ # no data to deal with (just schema modification).
+ class DataSet < Array
+ def initialize(db, data=[])
+ @db = db
+ concat data
+ end
+ attr_reader :db
+
+ def update(*args)
+ raise 'updating non-empty dataset' unless empty?
+ end
+
+ def truncate
+ raise 'truncate should not be called with non-empty data' unless empty?
+ end
+
+ def to_a
+ # confusingly, this gets used as a dataset instead of an array
+ self
+ end
+
+ def filter(opts={})
+ raise 'filtering non-empty dataset' unless empty?
+ self # filtering an empty dataset is still empty
+ end
+ alias_method :all, :filter
+ alias_method :where, :filter
+ alias_method :exclude, :filter
+
+ def select(*columns)
+ raise 'selecting from non-empty dataset' unless empty?
+ self # We assume no data to modify
+ end
+ alias_method :group_by, :select
+ alias_method :select_all, :select
+
+ def having(&block)
+ raise 'having from non-empty dataset' unless empty?
+ self
+ end
+
+ def join(dataset, expression={})
+ raise 'Cannot join on non-empty datasets' unless empty? && dataset.empty?
+ self
+ end
+
+ def with_sql(stmt)
+ case stmt
+ when /
+ ^\s*
+ SHOW \s+ FULL \s+ COLUMNS \s+ FROM \s+
+ (?<table_quote>[`'"])
+ (?<table_name>\w+)
+ \k<table_quote>
+ /ix
+ # 20150514190458_fix_mysql_collations.rb
+ @db.run stmt
+ else
+ raise "Don't know how to run SQL statement #{stmt}"
+ end
+ end
+ end
+
+ CURRENT_TIMESTAMP = ::Sequel::CURRENT_TIMESTAMP
+
+ class FakeMySQLDB
+ def initialize
+ @tables = Hash.new
+ @receiver = nil
+ @default_charset = nil
+ @verbose = false
+ @migrations_files = Set.new
+ end
+ attr_writer :receiver
+ attr_accessor :verbose
+
+ def binding
+ Kernel.binding
+ end
+
+ def adapter_scheme
+ :mysql
+ end
+ alias :database_type :adapter_scheme
+
+ def migration(receiver, &block)
+ @receiver = receiver
+ begin
+ instance_exec &block
+ ensure
+ @receiver = nil
+ end
+ end
+
+ def add_migration_file filename
+ @migrations_files << filename
+ end
+
+ def up
+ yield
+ end
+ alias_method :change, :up
+ alias_method :transaction, :up # we don't need transactions
+
+ def down
+ # Do nothing for down migrations
+ end
+
+ def tables
+ @tables.keys
+ end
+
+ def create_table(table_name, &block)
+ raise "Table #{table_name} already exists" if @tables.has_key? table_name
+ table = Table.new(table_name)
+ @tables[table_name] = table
+ status "Creating table #{table_name}"
+ table.using_context(@receiver) do
+ table.instance_eval &block
+ end
+ end
+
+ def alter_table(table_name, &block)
+ table = @tables[table_name]
+ raise "Tried to alter missing table #{table}" unless table
+ status "Altering table #{table_name}"
+ table.using_context(@receiver) do
+ table.instance_eval &block
+ end
+ end
+
+ def drop_table(*names)
+ names.each { |name| @tables.delete(name) }
+ end
+ alias_method :drop_table?, :drop_table
+
+ def rename_table(old_name, new_name)
+ raise "Tried to renamed non-existent table #{old_name}" unless @tables.has_key? old_name
+ raise "Conflict renaming table #{old_name} to #{new_name}" if @tables.has_key? new_name
+ @tables[new_name] = @tables[old_name]
+ @tables.delete old_name
+ @tables[new_name].name = new_name
+ @tables.values.each do |table|
+ table._on_rename_table(old_name, new_name)
+ end
+ end
+
+ def run(stmt)
+ # Uh-oh, we need to run arbitrary SQL
+ # Luckily, we can handle most things because we have no data
+
+ case stmt
+
+ # UPDATE …
+ when /^\s*update\b/i
+ return
+
+ # INSERT INTO table SELECT …
+ # INSERT INTO table (column, column) SELECT …
+ # insert… select is okay, since select is always empty
+ when /
+ ^\s*
+ insert \s+ into \s+
+ \w+\b # table name
+ (?: \s* \( (?: \s* \w+ \s* , )* \s* \w+ \s* \) )? # optional columns
+ \s* select
+ /ix
+ return
+
+ # DELETE FROM table …
+ # DELETE thing FROM table …
+ # deleting from empty tables is okay
+ when /
+ ^\s*
+ delete \s+
+ (?: \w+ \s+ ) ?
+ from \b
+ /ix
+ return
+
+ # 20150113201824_fix_created_at_column.rb
+ # 20150514190458_fix_mysql_collations.rb
+ # ALTER TABLE … MODIFY …
+ when /
+ ^\s*
+ ALTER \s+ TABLE \s+
+ (?<table_quote> [`'"]? )
+ (?<table_name> \w+)
+ \k<table_quote> \s+
+ (?<modifications>
+ (?:
+ MODIFY \s+
+ (?<column_quote> [`'"]? )
+ (?<column_name> \w+ )
+ \k<column_quote>
+ (?<column_def> (?: \s+ [`'"\w]+)* )
+ (?: \s* , \s* | \s* ; \s* $)
+ )+
+ )
+ /ix
+ table_name = $~[:table_name].to_sym
+ table = @tables[table_name]
+ modifications = $~[:modifications]
+ raise "Failed to find table #{table_name} to alter" unless table
+ modifications.scan(/
+ MODIFY \s+
+ (?<column_quote> [`'"]? )
+ (?<column_name> \w+ )
+ \k<column_quote>
+ (?<column_def> [^,;]* )
+ (?: , \s* | ; \s* $)
+ /ix) do
+ column_name =$~[:column_name].to_sym
+ column_def = $~[:column_def]
+ status " Modifying table #{table_name}: #{column_name} -> #{column_def}"
+ table.rename_column column_name, column_name, column_def
+ end
+
+ # 20130318175647_convert_mysql_text_types_for_services.rb
+ # ALTER TABLE … CHANGE …
+ when /
+ ^\s*
+ ALTER \s+ TABLE \s+
+ `?(?<table_name> \w+ )`? \s+
+ CHANGE \s+ (?: COLUMN \s+ )?
+ `?(?<old_column> \w+ )`? \s+
+ `?(?<new_column> \w+ )`?
+ (?<column_def> (?: \s+ \w+ )* )
+ \s* ;?
+ \s* $
+ /ix
+
+ table = @tables[$~[:table_name].to_sym]
+ raise "Failed to find table #{$~[:table_name]} to alter" unless table
+ old_column = $~[:old_column].to_sym
+ new_column = $~[:new_column].to_sym
+ table.rename_column old_column, new_column, $~[:column_def]
+ return
+
+ # ALTER DATABASE DEFAULT CHARACTER SET…
+ when /
+ ^\s*
+ ALTER \s+ DATABASE \s+
+ DEFAULT \s+ CHARACTER \s+ SET \s+
+ (?<default_charset> \w+ )
+ \s* ;?
+ \s*$
+ /ix
+
+ @default_charset = $~[:default_charset]
+ return
+
+ # ALTER TABLE … CONVERT TO CHARACTER SET…
+ when /
+ ^\s*
+ ALTER \s+ TABLE \s*
+ `(?<table_name> \w+ )` \s*
+ CONVERT \s+ TO \s+ CHARACTER \s+ SET \s+
+ (?<charset> \w+ )
+ \s* ;?
+ \s* $
+ /ix
+ table = @tables[$~[:table_name].to_sym]
+ raise "Failed to find table #{$~[:table_name]} to convert charset" unless table
+ table.charset = $~[:charset].to_sym
+ return
+
+
+ # 20150514190458_fix_mysql_collations.rb
+ # SHOW FULL COLUMNS FROM…
+ when /
+ ^\s*
+ SHOW \s+ FULL \s+ COLUMNS \s+ FROM \s+
+ (?<table_quote>[`'"])
+ (?<table_name>\w+)
+ \k<table_quote>
+ /ix
+ table_name = $~[:table_name].to_sym
+ table = @tables[table_name]
+ raise "Failed to find table #{table_name} to list columns" unless table
+ table._show_full_columns
+
+ else
+ raise "Could not run arbitrary SQL statement #{stmt}"
+ end
+ end
+