declare_schema 0.10.0 → 0.10.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -1
  3. data/Gemfile.lock +1 -1
  4. data/lib/declare_schema/model/foreign_key_definition.rb +4 -8
  5. data/lib/declare_schema/model/index_definition.rb +0 -19
  6. data/lib/declare_schema/schema_change/all.rb +22 -0
  7. data/lib/declare_schema/schema_change/base.rb +45 -0
  8. data/lib/declare_schema/schema_change/column_add.rb +27 -0
  9. data/lib/declare_schema/schema_change/column_change.rb +32 -0
  10. data/lib/declare_schema/schema_change/column_remove.rb +20 -0
  11. data/lib/declare_schema/schema_change/column_rename.rb +23 -0
  12. data/lib/declare_schema/schema_change/foreign_key_add.rb +25 -0
  13. data/lib/declare_schema/schema_change/foreign_key_remove.rb +20 -0
  14. data/lib/declare_schema/schema_change/index_add.rb +33 -0
  15. data/lib/declare_schema/schema_change/index_remove.rb +20 -0
  16. data/lib/declare_schema/schema_change/primary_key_change.rb +33 -0
  17. data/lib/declare_schema/schema_change/table_add.rb +37 -0
  18. data/lib/declare_schema/schema_change/table_change.rb +36 -0
  19. data/lib/declare_schema/schema_change/table_remove.rb +22 -0
  20. data/lib/declare_schema/schema_change/table_rename.rb +22 -0
  21. data/lib/declare_schema/version.rb +1 -1
  22. data/lib/generators/declare_schema/migration/migrator.rb +172 -174
  23. data/spec/lib/declare_schema/interactive_primary_key_spec.rb +52 -14
  24. data/spec/lib/declare_schema/migration_generator_spec.rb +175 -303
  25. data/spec/lib/declare_schema/model/foreign_key_definition_spec.rb +0 -12
  26. data/spec/lib/declare_schema/schema_change/base_spec.rb +75 -0
  27. data/spec/lib/declare_schema/schema_change/column_add_spec.rb +30 -0
  28. data/spec/lib/declare_schema/schema_change/column_change_spec.rb +33 -0
  29. data/spec/lib/declare_schema/schema_change/column_remove_spec.rb +30 -0
  30. data/spec/lib/declare_schema/schema_change/column_rename_spec.rb +28 -0
  31. data/spec/lib/declare_schema/schema_change/foreign_key_add_spec.rb +29 -0
  32. data/spec/lib/declare_schema/schema_change/foreign_key_remove_spec.rb +29 -0
  33. data/spec/lib/declare_schema/schema_change/index_add_spec.rb +56 -0
  34. data/spec/lib/declare_schema/schema_change/index_remove_spec.rb +29 -0
  35. data/spec/lib/declare_schema/schema_change/primary_key_change_spec.rb +69 -0
  36. data/spec/lib/declare_schema/schema_change/table_add_spec.rb +50 -0
  37. data/spec/lib/declare_schema/schema_change/table_change_spec.rb +30 -0
  38. data/spec/lib/declare_schema/schema_change/table_remove_spec.rb +27 -0
  39. data/spec/lib/declare_schema/schema_change/table_rename_spec.rb +27 -0
  40. data/spec/lib/generators/declare_schema/migration/migrator_spec.rb +59 -11
  41. data/spec/support/acceptance_spec_helpers.rb +2 -2
  42. metadata +34 -5
@@ -36,7 +36,32 @@ RSpec.describe 'DeclareSchema Migration Generator interactive primary key' do
36
36
 
37
37
  nuke_model_class(Foo)
38
38
 
39
- ### migrate to
39
+ # The ActiveRecord sqlite3 driver has a bug where rename_column recreates the entire table, but forgets to set the primary key:
40
+ #
41
+ # [7] pry(#<RSpec::ExampleGroups::DeclareSchemaMigrationGeneratorInteractivePrimaryKey>)> u = 'rename_column :foos, :foo_id, :id'
42
+ # => "rename_column :foos, :foo_id, :id"
43
+ # [8] pry(#<RSpec::ExampleGroups::DeclareSchemaMigrationGeneratorInteractivePrimaryKey>)> ActiveRecord::Migration.class_eval(u)
44
+ # (0.0ms) begin transaction
45
+ # (pry):17
46
+ # (0.2ms) CREATE TEMPORARY TABLE "afoos" ("id" integer NOT NULL)
47
+ # (pry):17
48
+ # (0.1ms) INSERT INTO "afoos" ("id")
49
+ #
50
+ # (pry):17
51
+ # (0.4ms) DROP TABLE "foos"
52
+ # (pry):17
53
+ # (0.1ms) CREATE TABLE "foos" ("id" integer NOT NULL)
54
+ # (pry):17
55
+ # (0.1ms) INSERT INTO "foos" ("id")
56
+ #
57
+ # (pry):17
58
+ # (0.1ms) DROP TABLE "afoos"
59
+ # (pry):17
60
+ # (0.9ms) commit transaction
61
+ if defined?(SQLite3)
62
+ ActiveRecord::Base.connection.execute("drop table foos")
63
+ ActiveRecord::Base.connection.execute("CREATE TABLE foos (id integer PRIMARY KEY AUTOINCREMENT NOT NULL)")
64
+ end
40
65
 
41
66
  if Rails::VERSION::MAJOR >= 5 && !defined?(Mysql2) # TODO TECH-4814 Put this test back for Mysql2
42
67
  # replace custom primary_key
@@ -49,12 +74,6 @@ RSpec.describe 'DeclareSchema Migration Generator interactive primary key' do
49
74
  allow_any_instance_of(DeclareSchema::Support::ThorShell).to receive(:ask).with(/one of the rename choices or press enter to keep/) { 'drop id' }
50
75
  generate_migrations '-n', '-m'
51
76
  expect(Foo._defined_primary_key).to eq('foo_id')
52
-
53
- ### ensure it doesn't cause further migrations
54
-
55
- # check no further migrations
56
- up = Generators::DeclareSchema::Migration::Migrator.run.first
57
- expect(up).to eq("")
58
77
  end
59
78
  end
60
79
  end
@@ -85,7 +104,32 @@ RSpec.describe 'DeclareSchema Migration Generator interactive primary key' do
85
104
 
86
105
  nuke_model_class(Foo)
87
106
 
88
- ### migrate to
107
+ # The ActiveRecord sqlite3 driver has a bug where rename_column recreates the entire table, but forgets to set the primary key:
108
+ #
109
+ # [7] pry(#<RSpec::ExampleGroups::DeclareSchemaMigrationGeneratorInteractivePrimaryKey>)> u = 'rename_column :foos, :foo_id, :id'
110
+ # => "rename_column :foos, :foo_id, :id"
111
+ # [8] pry(#<RSpec::ExampleGroups::DeclareSchemaMigrationGeneratorInteractivePrimaryKey>)> ActiveRecord::Migration.class_eval(u)
112
+ # (0.0ms) begin transaction
113
+ # (pry):17
114
+ # (0.2ms) CREATE TEMPORARY TABLE "afoos" ("id" integer NOT NULL)
115
+ # (pry):17
116
+ # (0.1ms) INSERT INTO "afoos" ("id")
117
+ #
118
+ # (pry):17
119
+ # (0.4ms) DROP TABLE "foos"
120
+ # (pry):17
121
+ # (0.1ms) CREATE TABLE "foos" ("id" integer NOT NULL)
122
+ # (pry):17
123
+ # (0.1ms) INSERT INTO "foos" ("id")
124
+ #
125
+ # (pry):17
126
+ # (0.1ms) DROP TABLE "afoos"
127
+ # (pry):17
128
+ # (0.9ms) commit transaction
129
+ if defined?(SQLite3)
130
+ ActiveRecord::Base.connection.execute("drop table foos")
131
+ ActiveRecord::Base.connection.execute("CREATE TABLE foos (id integer PRIMARY KEY AUTOINCREMENT NOT NULL)")
132
+ end
89
133
 
90
134
  if Rails::VERSION::MAJOR >= 5 && !defined?(Mysql2) # TODO TECH-4814 Put this test back for Mysql2
91
135
  # replace custom primary_key
@@ -99,12 +143,6 @@ RSpec.describe 'DeclareSchema Migration Generator interactive primary key' do
99
143
  allow_any_instance_of(DeclareSchema::Support::ThorShell).to receive(:ask).with(/one of the rename choices or press enter to keep/) { 'drop id' }
100
144
  generate_migrations '-n', '-m'
101
145
  expect(Foo.primary_key).to eq('foo_id')
102
-
103
- ### ensure it doesn't cause further migrations
104
-
105
- # check no further migrations
106
- up = Generators::DeclareSchema::Migration::Migrator.run.first
107
- expect(up).to eq("")
108
146
  end
109
147
  end
110
148
  end
@@ -10,16 +10,6 @@ RSpec.describe 'DeclareSchema Migration Generator' do
10
10
  before do
11
11
  load File.expand_path('prepare_testapp.rb', __dir__)
12
12
  end
13
-
14
- let(:charset_alter_table) do
15
- if defined?(Mysql2)
16
- <<~EOS
17
-
18
-
19
- execute "ALTER TABLE `adverts` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin"
20
- EOS
21
- end
22
- end
23
13
  let(:text_limit) do
24
14
  if defined?(Mysql2)
25
15
  ", limit: 4294967295"
@@ -30,6 +20,11 @@ RSpec.describe 'DeclareSchema Migration Generator' do
30
20
  ', charset: "utf8mb4", collation: "utf8mb4_bin"'
31
21
  end
32
22
  end
23
+ let(:create_table_charset_and_collation) do
24
+ if defined?(Mysql2)
25
+ ", options: \"CHARACTER SET utf8mb4 COLLATE utf8mb4_bin\""
26
+ end
27
+ end
33
28
  let(:datetime_precision) do
34
29
  if defined?(Mysql2) && Rails::VERSION::MAJOR >= 5
35
30
  ', precision: 0'
@@ -81,9 +76,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
81
76
  up, _ = Generators::DeclareSchema::Migration::Migrator.run.tap do |migrations|
82
77
  expect(migrations).to(
83
78
  migrate_up(<<~EOS.strip)
84
- create_table :adverts, id: :bigint do |t|
79
+ create_table :adverts, id: :bigint#{create_table_charset_and_collation} do |t|
85
80
  t.string :name, limit: 250, null: true#{charset_and_collation}
86
- end#{charset_alter_table}
81
+ end
87
82
  EOS
88
83
  .and migrate_down("drop_table :adverts")
89
84
  )
@@ -119,8 +114,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
119
114
  add_column :adverts, :published_at, :datetime, null: true
120
115
  EOS
121
116
  .and migrate_down(<<~EOS.strip)
122
- remove_column :adverts, :body
123
117
  remove_column :adverts, :published_at
118
+ remove_column :adverts, :body
124
119
  EOS
125
120
  )
126
121
 
@@ -152,8 +147,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
152
147
  remove_column :adverts, :name
153
148
  EOS
154
149
  .and migrate_down(<<~EOS.strip)
155
- remove_column :adverts, :title
156
150
  add_column :adverts, :name, :string, limit: 250, null: true#{charset_and_collation}
151
+ remove_column :adverts, :title
157
152
  EOS
158
153
  )
159
154
 
@@ -370,17 +365,13 @@ RSpec.describe 'DeclareSchema Migration Generator' do
370
365
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
371
366
  migrate_up(<<~EOS.strip)
372
367
  add_column :adverts, :category_id, :integer, limit: 8, null: false
373
-
374
- add_index :adverts, [:category_id], name: 'on_category_id'
375
-
376
- #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"on_category_id\")\n" if defined?(Mysql2)}
368
+ add_index :adverts, [:category_id], name: :on_category_id
369
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :on_category_id" if defined?(Mysql2)}
377
370
  EOS
378
371
  .and migrate_down(<<~EOS.strip)
372
+ #{"remove_foreign_key :adverts, name: :on_category_id" if defined?(Mysql2)}
373
+ remove_index :adverts, name: :on_category_id
379
374
  remove_column :adverts, :category_id
380
-
381
- remove_index :adverts, name: :on_category_id rescue ActiveRecord::StatementInvalid
382
-
383
- #{"remove_foreign_key(\"adverts\", name: \"on_category_id\")\n" if defined?(Mysql2)}
384
375
  EOS
385
376
  )
386
377
 
@@ -398,11 +389,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
398
389
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
399
390
  migrate_up(<<~EOS.strip)
400
391
  add_column :adverts, :c_id, :integer, limit: 8, null: false
401
-
402
- add_index :adverts, [:c_id], name: 'on_c_id'
403
-
404
- #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"on_category_id\")\n" +
405
- "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"on_c_id\")" if defined?(Mysql2)}
392
+ add_index :adverts, [:c_id], name: :on_c_id
393
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :on_category_id\n" +
394
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :on_c_id" if defined?(Mysql2)}
406
395
  EOS
407
396
  )
408
397
 
@@ -420,9 +409,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
420
409
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
421
410
  migrate_up(<<~EOS.strip)
422
411
  add_column :adverts, :category_id, :integer, limit: 8, null: false
423
-
424
- #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"on_category_id\")\n" +
425
- "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"on_c_id\")" if defined?(Mysql2)}
412
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :on_category_id\n" +
413
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :on_c_id" if defined?(Mysql2)}
426
414
  EOS
427
415
  )
428
416
 
@@ -440,11 +428,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
440
428
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
441
429
  migrate_up(<<~EOS.strip)
442
430
  add_column :adverts, :category_id, :integer, limit: 8, null: false
443
-
444
- add_index :adverts, [:category_id], name: 'my_index'
445
-
446
- #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"on_category_id\")\n" +
447
- "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"on_c_id\")" if defined?(Mysql2)}
431
+ add_index :adverts, [:category_id], name: :my_index
432
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :on_category_id\n" +
433
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :on_c_id" if defined?(Mysql2)}
448
434
  EOS
449
435
  )
450
436
 
@@ -468,17 +454,15 @@ RSpec.describe 'DeclareSchema Migration Generator' do
468
454
  add_column :adverts, :created_at, :datetime, null: true
469
455
  add_column :adverts, :updated_at, :datetime, null: true
470
456
  add_column :adverts, :lock_version, :integer#{lock_version_limit}, null: false, default: 1
471
-
472
- #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"on_category_id\")\n" +
473
- "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"on_c_id\")" if defined?(Mysql2)}
457
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :on_category_id\n" +
458
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :on_c_id" if defined?(Mysql2)}
474
459
  EOS
475
460
  .and migrate_down(<<~EOS.strip)
476
- remove_column :adverts, :created_at
477
- remove_column :adverts, :updated_at
461
+ #{"remove_foreign_key :adverts, name: :on_c_id\n" +
462
+ "remove_foreign_key :adverts, name: :on_category_id" if defined?(Mysql2)}
478
463
  remove_column :adverts, :lock_version
479
-
480
- #{"remove_foreign_key(\"adverts\", name: \"on_category_id\")\n" +
481
- "remove_foreign_key(\"adverts\", name: \"on_c_id\")" if defined?(Mysql2)}
464
+ remove_column :adverts, :updated_at
465
+ remove_column :adverts, :created_at
482
466
  EOS
483
467
  )
484
468
 
@@ -499,11 +483,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
499
483
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
500
484
  migrate_up(<<~EOS.strip)
501
485
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
502
-
503
- add_index :adverts, [:title], name: 'on_title'
504
-
505
- #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"on_category_id\")\n" +
506
- "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"on_c_id\")" if defined?(Mysql2)}
486
+ add_index :adverts, [:title], name: :on_title
487
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :on_category_id\n" +
488
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :on_c_id" if defined?(Mysql2)}
507
489
  EOS
508
490
  )
509
491
 
@@ -520,11 +502,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
520
502
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
521
503
  migrate_up(<<~EOS.strip)
522
504
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
523
-
524
- add_index :adverts, [:title], unique: true, name: 'on_title'
525
-
526
- #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"on_category_id\")\n" +
527
- "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"on_c_id\")" if defined?(Mysql2)}
505
+ add_index :adverts, [:title], name: :on_title, unique: true
506
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :on_category_id\n" +
507
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :on_c_id" if defined?(Mysql2)}
528
508
  EOS
529
509
  )
530
510
 
@@ -541,11 +521,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
541
521
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
542
522
  migrate_up(<<~EOS.strip)
543
523
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
544
-
545
- add_index :adverts, [:title], name: 'my_index'
546
-
547
- #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"on_category_id\")\n" +
548
- "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"on_c_id\")" if defined?(Mysql2)}
524
+ add_index :adverts, [:title], name: :my_index
525
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :on_category_id\n" +
526
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :on_c_id" if defined?(Mysql2)}
549
527
  EOS
550
528
  )
551
529
 
@@ -560,11 +538,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
560
538
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
561
539
  migrate_up(<<~EOS.strip)
562
540
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
563
-
564
- add_index :adverts, [:title], name: 'on_title'
565
-
566
- #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"on_category_id\")\n" +
567
- "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"on_c_id\")" if defined?(Mysql2)}
541
+ add_index :adverts, [:title], name: :on_title
542
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :on_category_id\n" +
543
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :on_c_id" if defined?(Mysql2)}
568
544
  EOS
569
545
  )
570
546
 
@@ -579,11 +555,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
579
555
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
580
556
  migrate_up(<<~EOS.strip)
581
557
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
582
-
583
- add_index :adverts, [:title], unique: true, name: 'my_index'
584
-
585
- #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"on_category_id\")\n" +
586
- "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"on_c_id\")" if defined?(Mysql2)}
558
+ add_index :adverts, [:title], name: :my_index, unique: true
559
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :on_category_id\n" +
560
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :on_c_id" if defined?(Mysql2)}
587
561
  EOS
588
562
  )
589
563
 
@@ -598,11 +572,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
598
572
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
599
573
  migrate_up(<<~EOS.strip)
600
574
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
601
-
602
- add_index :adverts, [:title, :category_id], name: 'on_title_and_category_id'
603
-
604
- #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"on_category_id\")\n" +
605
- "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"on_c_id\")" if defined?(Mysql2)}
575
+ add_index :adverts, [:title, :category_id], name: :on_title_and_category_id
576
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :on_category_id\n" +
577
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :on_c_id" if defined?(Mysql2)}
606
578
  EOS
607
579
  )
608
580
 
@@ -614,7 +586,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
614
586
 
615
587
  ### Rename a table
616
588
 
617
- # The migration generator respects the `set_table_name` declaration, although as before, we need to explicitly tell the generator that we want a rename rather than a create and a drop.
589
+ # The migration generator respects the `set_table_name` declaration, although as before,
590
+ # we need to explicitly tell the generator that we want a rename rather than a create and a drop.
618
591
 
619
592
  class Advert < ActiveRecord::Base
620
593
  self.table_name = "ads"
@@ -630,31 +603,21 @@ RSpec.describe 'DeclareSchema Migration Generator' do
630
603
  expect(Generators::DeclareSchema::Migration::Migrator.run("adverts" => "ads")).to(
631
604
  migrate_up(<<~EOS.strip)
632
605
  rename_table :adverts, :ads
633
-
634
606
  add_column :ads, :title, :string, limit: 250, null: true#{charset_and_collation}
635
607
  add_column :ads, :body, :text#{', limit: 4294967295' if defined?(Mysql2)}, null: true#{charset_and_collation}
636
-
637
- #{if defined?(SQLite3)
638
- "add_index :ads, [:id], unique: true, name: 'PRIMARY'\n"
639
- elsif defined?(Mysql2)
640
- "execute \"ALTER TABLE ads DROP PRIMARY KEY, ADD PRIMARY KEY (id)\"\n\n" +
641
- "add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"on_category_id\")\n" +
642
- "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"on_c_id\")"
608
+ #{if defined?(Mysql2)
609
+ "add_foreign_key :adverts, :categories, column: :category_id, name: :on_category_id\n" +
610
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :on_c_id"
643
611
  end}
644
612
  EOS
645
613
  .and migrate_down(<<~EOS.strip)
646
- remove_column :ads, :title
614
+ #{if defined?(Mysql2)
615
+ "remove_foreign_key :adverts, name: :on_c_id\n" +
616
+ "remove_foreign_key :adverts, name: :on_category_id"
617
+ end}
647
618
  remove_column :ads, :body
648
-
619
+ remove_column :ads, :title
649
620
  rename_table :ads, :adverts
650
-
651
- #{if defined?(SQLite3)
652
- "add_index :adverts, [:id], unique: true, name: 'PRIMARY'\n"
653
- elsif defined?(Mysql2)
654
- "execute \"ALTER TABLE adverts DROP PRIMARY KEY, ADD PRIMARY KEY (id)\"\n\n" +
655
- "remove_foreign_key(\"adverts\", name: \"on_category_id\")\n" +
656
- "remove_foreign_key(\"adverts\", name: \"on_c_id\")"
657
- end}
658
621
  EOS
659
622
  )
660
623
 
@@ -684,29 +647,15 @@ RSpec.describe 'DeclareSchema Migration Generator' do
684
647
  expect(Generators::DeclareSchema::Migration::Migrator.run("adverts" => "advertisements")).to(
685
648
  migrate_up(<<~EOS.strip)
686
649
  rename_table :adverts, :advertisements
687
-
688
650
  add_column :advertisements, :title, :string, limit: 250, null: true#{charset_and_collation}
689
651
  add_column :advertisements, :body, :text#{', limit: 4294967295' if defined?(Mysql2)}, null: true#{charset_and_collation}
690
652
  remove_column :advertisements, :name
691
-
692
- #{if defined?(SQLite3)
693
- "add_index :advertisements, [:id], unique: true, name: 'PRIMARY'"
694
- elsif defined?(Mysql2)
695
- "execute \"ALTER TABLE advertisements DROP PRIMARY KEY, ADD PRIMARY KEY (id)\""
696
- end}
697
653
  EOS
698
654
  .and migrate_down(<<~EOS.strip)
699
- remove_column :advertisements, :title
655
+ add_column :advertisements, :name, :string, limit: 250, null: true#{charset_and_collation}
700
656
  remove_column :advertisements, :body
701
- add_column :adverts, :name, :string, limit: 250, null: true#{charset_and_collation}
702
-
657
+ remove_column :advertisements, :title
703
658
  rename_table :advertisements, :adverts
704
-
705
- #{if defined?(SQLite3)
706
- "add_index :adverts, [:id], unique: true, name: 'PRIMARY'"
707
- elsif defined?(Mysql2)
708
- "execute \"ALTER TABLE adverts DROP PRIMARY KEY, ADD PRIMARY KEY (id)\""
709
- end}
710
659
  EOS
711
660
  )
712
661
 
@@ -753,13 +702,11 @@ RSpec.describe 'DeclareSchema Migration Generator' do
753
702
  expect(migrations).to(
754
703
  migrate_up(<<~EOS.strip)
755
704
  add_column :adverts, :type, :string, limit: 250, null: true#{charset_and_collation}
756
-
757
- add_index :adverts, [:type], name: 'on_type'
705
+ add_index :adverts, [:type], name: :on_type
758
706
  EOS
759
707
  .and migrate_down(<<~EOS.strip)
760
708
  remove_column :adverts, :type
761
-
762
- remove_index :adverts, name: :on_type rescue ActiveRecord::StatementInvalid
709
+ remove_index :adverts, name: :on_type
763
710
  EOS
764
711
  )
765
712
  end
@@ -802,8 +749,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
802
749
  change_column :adverts, :name, :string, limit: 250, null: true, default: "No Name"#{charset_and_collation}
803
750
  EOS
804
751
  .and migrate_down(<<~EOS.strip)
752
+ change_column :adverts, :name, :string, limit: 250, null: true, default: "Untitled"#{charset_and_collation}
805
753
  rename_column :adverts, :name, :title
806
- change_column :adverts, :title, :string, limit: 250, null: true, default: "Untitled"#{charset_and_collation}
807
754
  EOS
808
755
  )
809
756
 
@@ -821,15 +768,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
821
768
  expect(Generators::DeclareSchema::Migration::Migrator.run(adverts: :ads)).to(
822
769
  migrate_up(<<~EOS.strip)
823
770
  rename_table :adverts, :ads
824
-
825
771
  add_column :ads, :created_at, :datetime, null: false
826
772
  change_column :ads, :title, :string, limit: 250, null: false, default: \"Untitled\"#{charset_and_collation}
827
-
828
- #{if defined?(SQLite3)
829
- "add_index :ads, [:id], unique: true, name: 'PRIMARY'"
830
- elsif defined?(Mysql2)
831
- 'execute "ALTER TABLE ads DROP PRIMARY KEY, ADD PRIMARY KEY (id)"'
832
- end}
833
773
  EOS
834
774
  )
835
775
 
@@ -856,12 +796,6 @@ RSpec.describe 'DeclareSchema Migration Generator' do
856
796
  expect(Generators::DeclareSchema::Migration::Migrator.run(adverts: { id: :advert_id })).to(
857
797
  migrate_up(<<~EOS.strip)
858
798
  rename_column :adverts, :id, :advert_id
859
-
860
- #{if defined?(SQLite3)
861
- "add_index :adverts, [:advert_id], unique: true, name: 'PRIMARY'"
862
- elsif defined?(Mysql2)
863
- 'execute "ALTER TABLE adverts DROP PRIMARY KEY, ADD PRIMARY KEY (advert_id)"'
864
- end}
865
799
  EOS
866
800
  )
867
801
 
@@ -1252,12 +1186,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1252
1186
 
1253
1187
  up, _ = Generators::DeclareSchema::Migration::Migrator.run.tap do |migrations|
1254
1188
  expect(migrations).to(
1255
- migrate_up(<<~EOS.strip)
1256
- create_table :adverts, id: :bigint do |t|
1189
+ migrate_up(<<~EOS.strip)
1190
+ create_table :adverts, id: :bigint#{create_table_charset_and_collation} do |t|
1257
1191
  t.string :name, limit: 250, null: true#{charset_and_collation}
1258
- end#{charset_alter_table}
1259
- EOS
1260
- .and migrate_down("drop_table :adverts")
1192
+ end
1193
+ EOS
1194
+ .and migrate_down("drop_table :adverts")
1261
1195
  )
1262
1196
  end
1263
1197
 
@@ -1286,14 +1220,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1286
1220
  Advert.reset_column_information
1287
1221
 
1288
1222
  expect(migrate).to(
1289
- migrate_up(<<~EOS.strip)
1223
+ migrate_up(<<~EOS.strip)
1290
1224
  add_column :adverts, :body, :text#{text_limit}, null: true#{charset_and_collation}
1291
1225
  add_column :adverts, :published_at, :datetime, null: true
1292
- EOS
1293
- .and migrate_down(<<~EOS.strip)
1294
- remove_column :adverts, :body
1226
+ EOS
1227
+ .and migrate_down(<<~EOS.strip)
1295
1228
  remove_column :adverts, :published_at
1296
- EOS
1229
+ remove_column :adverts, :body
1230
+ EOS
1297
1231
  )
1298
1232
 
1299
1233
  Advert.field_specs.clear # not normally needed
@@ -1319,14 +1253,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1319
1253
  end
1320
1254
 
1321
1255
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
1322
- migrate_up(<<~EOS.strip)
1256
+ migrate_up(<<~EOS.strip)
1323
1257
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
1324
1258
  remove_column :adverts, :name
1325
- EOS
1326
- .and migrate_down(<<~EOS.strip)
1327
- remove_column :adverts, :title
1259
+ EOS
1260
+ .and migrate_down(<<~EOS.strip)
1328
1261
  add_column :adverts, :name, :string, limit: 250, null: true#{charset_and_collation}
1329
- EOS
1262
+ remove_column :adverts, :title
1263
+ EOS
1330
1264
  )
1331
1265
 
1332
1266
  expect(Generators::DeclareSchema::Migration::Migrator.run(adverts: { name: :title })).to(
@@ -1540,20 +1474,16 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1540
1474
  end
1541
1475
 
1542
1476
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
1543
- migrate_up(<<~EOS.strip)
1477
+ migrate_up(<<~EOS.strip)
1544
1478
  add_column :adverts, :category_id, :integer, limit: 8, null: false
1545
-
1546
- add_index :adverts, [:category_id], name: 'on_category_id'
1547
-
1548
- #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"on_category_id\")\n" if defined?(Mysql2)}
1549
- EOS
1550
- .and migrate_down(<<~EOS.strip)
1479
+ add_index :adverts, [:category_id], name: :on_category_id
1480
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :on_category_id\n" if defined?(Mysql2)}
1481
+ EOS
1482
+ .and migrate_down(<<~EOS.strip)
1483
+ #{"remove_foreign_key :adverts, name: :on_category_id" if defined?(Mysql2)}
1484
+ remove_index :adverts, name: :on_category_id
1551
1485
  remove_column :adverts, :category_id
1552
-
1553
- remove_index :adverts, name: :on_category_id rescue ActiveRecord::StatementInvalid
1554
-
1555
- #{"remove_foreign_key(\"adverts\", name: \"on_category_id\")\n" if defined?(Mysql2)}
1556
- EOS
1486
+ EOS
1557
1487
  )
1558
1488
 
1559
1489
  Advert.field_specs.delete(:category_id)
@@ -1568,14 +1498,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1568
1498
  end
1569
1499
 
1570
1500
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
1571
- migrate_up(<<~EOS.strip)
1501
+ migrate_up(<<~EOS.strip)
1572
1502
  add_column :adverts, :c_id, :integer, limit: 8, null: false
1573
-
1574
- add_index :adverts, [:c_id], name: 'on_c_id'
1575
-
1576
- #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"on_category_id\")\n" +
1577
- "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"on_c_id\")" if defined?(Mysql2)}
1578
- EOS
1503
+ add_index :adverts, [:c_id], name: :on_c_id
1504
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :on_category_id\n" +
1505
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :on_c_id" if defined?(Mysql2)}
1506
+ EOS
1579
1507
  )
1580
1508
 
1581
1509
  Advert.field_specs.delete(:c_id)
@@ -1590,12 +1518,11 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1590
1518
  end
1591
1519
 
1592
1520
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
1593
- migrate_up(<<~EOS.strip)
1521
+ migrate_up(<<~EOS.strip)
1594
1522
  add_column :adverts, :category_id, :integer, limit: 8, null: false
1595
-
1596
- #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"on_category_id\")\n" +
1597
- "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"on_c_id\")" if defined?(Mysql2)}
1598
- EOS
1523
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :on_category_id\n" +
1524
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :on_c_id" if defined?(Mysql2)}
1525
+ EOS
1599
1526
  )
1600
1527
 
1601
1528
  Advert.field_specs.delete(:category_id)
@@ -1610,14 +1537,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1610
1537
  end
1611
1538
 
1612
1539
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
1613
- migrate_up(<<~EOS.strip)
1540
+ migrate_up(<<~EOS.strip)
1614
1541
  add_column :adverts, :category_id, :integer, limit: 8, null: false
1615
-
1616
- add_index :adverts, [:category_id], name: 'my_index'
1617
-
1618
- #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"on_category_id\")\n" +
1619
- "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"on_c_id\")" if defined?(Mysql2)}
1620
- EOS
1542
+ add_index :adverts, [:category_id], name: :my_index
1543
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :on_category_id\n" +
1544
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :on_c_id" if defined?(Mysql2)}
1545
+ EOS
1621
1546
  )
1622
1547
 
1623
1548
  Advert.field_specs.delete(:category_id)
@@ -1636,22 +1561,20 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1636
1561
  end
1637
1562
 
1638
1563
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
1639
- migrate_up(<<~EOS.strip)
1564
+ migrate_up(<<~EOS.strip)
1640
1565
  add_column :adverts, :created_at, :datetime, null: true
1641
1566
  add_column :adverts, :updated_at, :datetime, null: true
1642
1567
  add_column :adverts, :lock_version, :integer#{lock_version_limit}, null: false, default: 1
1643
-
1644
- #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"on_category_id\")\n" +
1645
- "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"on_c_id\")" if defined?(Mysql2)}
1646
- EOS
1647
- .and migrate_down(<<~EOS.strip)
1648
- remove_column :adverts, :created_at
1649
- remove_column :adverts, :updated_at
1568
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :on_category_id\n" +
1569
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :on_c_id" if defined?(Mysql2)}
1570
+ EOS
1571
+ .and migrate_down(<<~EOS.strip)
1572
+ #{"remove_foreign_key :adverts, name: :on_c_id\n" +
1573
+ "remove_foreign_key :adverts, name: :on_category_id" if defined?(Mysql2)}
1650
1574
  remove_column :adverts, :lock_version
1651
-
1652
- #{"remove_foreign_key(\"adverts\", name: \"on_category_id\")\n" +
1653
- "remove_foreign_key(\"adverts\", name: \"on_c_id\")" if defined?(Mysql2)}
1654
- EOS
1575
+ remove_column :adverts, :updated_at
1576
+ remove_column :adverts, :created_at
1577
+ EOS
1655
1578
  )
1656
1579
 
1657
1580
  Advert.field_specs.delete(:updated_at)
@@ -1669,14 +1592,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1669
1592
  end
1670
1593
 
1671
1594
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
1672
- migrate_up(<<~EOS.strip)
1595
+ migrate_up(<<~EOS.strip)
1673
1596
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
1674
-
1675
- add_index :adverts, [:title], name: 'on_title'
1676
-
1677
- #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"on_category_id\")\n" +
1678
- "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"on_c_id\")" if defined?(Mysql2)}
1679
- EOS
1597
+ add_index :adverts, [:title], name: :on_title
1598
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :on_category_id\n" +
1599
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :on_c_id" if defined?(Mysql2)}
1600
+ EOS
1680
1601
  )
1681
1602
 
1682
1603
  Advert.index_definitions.delete_if { |spec| spec.fields==["title"] }
@@ -1690,14 +1611,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1690
1611
  end
1691
1612
 
1692
1613
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
1693
- migrate_up(<<~EOS.strip)
1614
+ migrate_up(<<~EOS.strip)
1694
1615
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
1695
-
1696
- add_index :adverts, [:title], unique: true, name: 'on_title'
1697
-
1698
- #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"on_category_id\")\n" +
1699
- "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"on_c_id\")" if defined?(Mysql2)}
1700
- EOS
1616
+ add_index :adverts, [:title], name: :on_title, unique: true
1617
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :on_category_id\n" +
1618
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :on_c_id" if defined?(Mysql2)}
1619
+ EOS
1701
1620
  )
1702
1621
 
1703
1622
  Advert.index_definitions.delete_if { |spec| spec.fields == ["title"] }
@@ -1711,14 +1630,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1711
1630
  end
1712
1631
 
1713
1632
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
1714
- migrate_up(<<~EOS.strip)
1633
+ migrate_up(<<~EOS.strip)
1715
1634
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
1716
-
1717
- add_index :adverts, [:title], name: 'my_index'
1718
-
1719
- #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"on_category_id\")\n" +
1720
- "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"on_c_id\")" if defined?(Mysql2)}
1721
- EOS
1635
+ add_index :adverts, [:title], name: :my_index
1636
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :on_category_id\n" +
1637
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :on_c_id" if defined?(Mysql2)}
1638
+ EOS
1722
1639
  )
1723
1640
 
1724
1641
  Advert.index_definitions.delete_if { |spec| spec.fields==["title"] }
@@ -1730,14 +1647,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1730
1647
  end
1731
1648
 
1732
1649
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
1733
- migrate_up(<<~EOS.strip)
1650
+ migrate_up(<<~EOS.strip)
1734
1651
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
1735
-
1736
- add_index :adverts, [:title], name: 'on_title'
1737
-
1738
- #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"on_category_id\")\n" +
1739
- "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"on_c_id\")" if defined?(Mysql2)}
1740
- EOS
1652
+ add_index :adverts, [:title], name: :on_title
1653
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :on_category_id\n" +
1654
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :on_c_id" if defined?(Mysql2)}
1655
+ EOS
1741
1656
  )
1742
1657
 
1743
1658
  Advert.index_definitions.delete_if { |spec| spec.fields == ["title"] }
@@ -1749,14 +1664,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1749
1664
  end
1750
1665
 
1751
1666
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
1752
- migrate_up(<<~EOS.strip)
1667
+ migrate_up(<<~EOS.strip)
1753
1668
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
1754
-
1755
- add_index :adverts, [:title], unique: true, name: 'my_index'
1756
-
1757
- #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"on_category_id\")\n" +
1758
- "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"on_c_id\")" if defined?(Mysql2)}
1759
- EOS
1669
+ add_index :adverts, [:title], name: :my_index, unique: true
1670
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :on_category_id\n" +
1671
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :on_c_id" if defined?(Mysql2)}
1672
+ EOS
1760
1673
  )
1761
1674
 
1762
1675
  Advert.index_definitions.delete_if { |spec| spec.fields == ["title"] }
@@ -1768,14 +1681,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1768
1681
  end
1769
1682
 
1770
1683
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
1771
- migrate_up(<<~EOS.strip)
1684
+ migrate_up(<<~EOS.strip)
1772
1685
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
1773
-
1774
- add_index :adverts, [:title, :category_id], name: 'on_title_and_category_id'
1775
-
1776
- #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"on_category_id\")\n" +
1777
- "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"on_c_id\")" if defined?(Mysql2)}
1778
- EOS
1686
+ add_index :adverts, [:title, :category_id], name: :on_title_and_category_id
1687
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :on_category_id\n" +
1688
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :on_c_id" if defined?(Mysql2)}
1689
+ EOS
1779
1690
  )
1780
1691
 
1781
1692
  Advert.index_definitions.delete_if { |spec| spec.fields==["title", "category_id"] }
@@ -1800,34 +1711,24 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1800
1711
  Advert.reset_column_information
1801
1712
 
1802
1713
  expect(Generators::DeclareSchema::Migration::Migrator.run("adverts" => "ads")).to(
1803
- migrate_up(<<~EOS.strip)
1714
+ migrate_up(<<~EOS.strip)
1804
1715
  rename_table :adverts, :ads
1805
-
1806
1716
  add_column :ads, :title, :string, limit: 250, null: true#{charset_and_collation}
1807
1717
  add_column :ads, :body, :text#{', limit: 4294967295' if defined?(Mysql2)}, null: true#{charset_and_collation}
1808
-
1809
- #{if defined?(SQLite3)
1810
- "add_index :ads, [:id], unique: true, name: 'PRIMARY'\n"
1811
- elsif defined?(Mysql2)
1812
- "execute \"ALTER TABLE ads DROP PRIMARY KEY, ADD PRIMARY KEY (id)\"\n\n" +
1813
- "add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"on_category_id\")\n" +
1814
- "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"on_c_id\")"
1718
+ #{if defined?(Mysql2)
1719
+ "add_foreign_key :adverts, :categories, column: :category_id, name: :on_category_id\n" +
1720
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :on_c_id"
1815
1721
  end}
1816
1722
  EOS
1817
- .and migrate_down(<<~EOS.strip)
1818
- remove_column :ads, :title
1819
- remove_column :ads, :body
1820
-
1821
- rename_table :ads, :adverts
1822
-
1823
- #{if defined?(SQLite3)
1824
- "add_index :adverts, [:id], unique: true, name: 'PRIMARY'\n"
1825
- elsif defined?(Mysql2)
1826
- "execute \"ALTER TABLE adverts DROP PRIMARY KEY, ADD PRIMARY KEY (id)\"\n\n" +
1827
- "remove_foreign_key(\"adverts\", name: \"on_category_id\")\n" +
1828
- "remove_foreign_key(\"adverts\", name: \"on_c_id\")"
1829
- end}
1830
- EOS
1723
+ .and migrate_down(<<~EOS.strip)
1724
+ #{if defined?(Mysql2)
1725
+ "remove_foreign_key :adverts, name: :on_c_id\n" +
1726
+ "remove_foreign_key :adverts, name: :on_category_id"
1727
+ end}
1728
+ remove_column :ads, :body
1729
+ remove_column :ads, :title
1730
+ rename_table :ads, :adverts
1731
+ EOS
1831
1732
  )
1832
1733
 
1833
1734
  # Set the table name back to what it should be and confirm we're in sync:
@@ -1854,32 +1755,18 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1854
1755
  end
1855
1756
 
1856
1757
  expect(Generators::DeclareSchema::Migration::Migrator.run("adverts" => "advertisements")).to(
1857
- migrate_up(<<~EOS.strip)
1758
+ migrate_up(<<~EOS.strip)
1858
1759
  rename_table :adverts, :advertisements
1859
-
1860
1760
  add_column :advertisements, :title, :string, limit: 250, null: true#{charset_and_collation}
1861
1761
  add_column :advertisements, :body, :text#{', limit: 4294967295' if defined?(Mysql2)}, null: true#{charset_and_collation}
1862
1762
  remove_column :advertisements, :name
1863
-
1864
- #{if defined?(SQLite3)
1865
- "add_index :advertisements, [:id], unique: true, name: 'PRIMARY'"
1866
- elsif defined?(Mysql2)
1867
- "execute \"ALTER TABLE advertisements DROP PRIMARY KEY, ADD PRIMARY KEY (id)\""
1868
- end}
1869
- EOS
1870
- .and migrate_down(<<~EOS.strip)
1871
- remove_column :advertisements, :title
1763
+ EOS
1764
+ .and migrate_down(<<~EOS.strip)
1765
+ add_column :advertisements, :name, :string, limit: 250, null: true#{charset_and_collation}
1872
1766
  remove_column :advertisements, :body
1873
- add_column :adverts, :name, :string, limit: 250, null: true#{charset_and_collation}
1874
-
1767
+ remove_column :advertisements, :title
1875
1768
  rename_table :advertisements, :adverts
1876
-
1877
- #{if defined?(SQLite3)
1878
- "add_index :adverts, [:id], unique: true, name: 'PRIMARY'"
1879
- elsif defined?(Mysql2)
1880
- "execute \"ALTER TABLE adverts DROP PRIMARY KEY, ADD PRIMARY KEY (id)\""
1881
- end}
1882
- EOS
1769
+ EOS
1883
1770
  )
1884
1771
 
1885
1772
  ### Drop a table
@@ -1891,14 +1778,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1891
1778
  # Dropping tables is where the automatic down-migration really comes in handy:
1892
1779
 
1893
1780
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
1894
- migrate_up(<<~EOS.strip)
1781
+ migrate_up(<<~EOS.strip)
1895
1782
  drop_table :adverts
1896
- EOS
1897
- .and migrate_down(<<~EOS.strip)
1783
+ EOS
1784
+ .and migrate_down(<<~EOS.strip)
1898
1785
  create_table "adverts"#{table_options}, force: :cascade do |t|
1899
1786
  t.string "name", limit: 250#{charset_and_collation}
1900
1787
  end
1901
- EOS
1788
+ EOS
1902
1789
  )
1903
1790
 
1904
1791
  ## STI
@@ -1923,16 +1810,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1923
1810
 
1924
1811
  up, _ = Generators::DeclareSchema::Migration::Migrator.run do |migrations|
1925
1812
  expect(migrations).to(
1926
- migrate_up(<<~EOS.strip)
1813
+ migrate_up(<<~EOS.strip)
1927
1814
  add_column :adverts, :type, :string, limit: 250, null: true#{charset_and_collation}
1928
-
1929
- add_index :adverts, [:type], name: 'on_type'
1930
- EOS
1931
- .and migrate_down(<<~EOS.strip)
1815
+ add_index :adverts, [:type], name: :on_type
1816
+ EOS
1817
+ .and migrate_down(<<~EOS.strip)
1818
+ remove_index :adverts, name: :on_type
1932
1819
  remove_column :adverts, :type
1933
-
1934
- remove_index :adverts, name: :on_type rescue ActiveRecord::StatementInvalid
1935
- EOS
1820
+ EOS
1936
1821
  )
1937
1822
  end
1938
1823
 
@@ -1969,14 +1854,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1969
1854
  end
1970
1855
 
1971
1856
  expect(Generators::DeclareSchema::Migration::Migrator.run(adverts: { title: :name })).to(
1972
- migrate_up(<<~EOS.strip)
1857
+ migrate_up(<<~EOS.strip)
1973
1858
  rename_column :adverts, :title, :name
1974
1859
  change_column :adverts, :name, :string, limit: 250, null: true, default: "No Name"#{charset_and_collation}
1975
- EOS
1976
- .and migrate_down(<<~EOS.strip)
1860
+ EOS
1861
+ .and migrate_down(<<~EOS.strip)
1862
+ change_column :adverts, :name, :string, limit: 250, null: true, default: "Untitled"#{charset_and_collation}
1977
1863
  rename_column :adverts, :name, :title
1978
- change_column :adverts, :title, :string, limit: 250, null: true, default: "Untitled"#{charset_and_collation}
1979
- EOS
1864
+ EOS
1980
1865
  )
1981
1866
 
1982
1867
  ### Rename a table and add a column
@@ -1991,18 +1876,11 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1991
1876
  end
1992
1877
 
1993
1878
  expect(Generators::DeclareSchema::Migration::Migrator.run(adverts: :ads)).to(
1994
- migrate_up(<<~EOS.strip)
1879
+ migrate_up(<<~EOS.strip)
1995
1880
  rename_table :adverts, :ads
1996
-
1997
1881
  add_column :ads, :created_at, :datetime, null: false
1998
1882
  change_column :ads, :title, :string, limit: 250, null: false, default: \"Untitled\"#{charset_and_collation}
1999
-
2000
- #{if defined?(SQLite3)
2001
- "add_index :ads, [:id], unique: true, name: 'PRIMARY'"
2002
- elsif defined?(Mysql2)
2003
- 'execute "ALTER TABLE ads DROP PRIMARY KEY, ADD PRIMARY KEY (id)"'
2004
- end}
2005
- EOS
1883
+ EOS
2006
1884
  )
2007
1885
 
2008
1886
  class Advert < ActiveRecord::Base
@@ -2026,15 +1904,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
2026
1904
  end
2027
1905
 
2028
1906
  expect(Generators::DeclareSchema::Migration::Migrator.run(adverts: { id: :advert_id })).to(
2029
- migrate_up(<<~EOS.strip)
1907
+ migrate_up(<<~EOS.strip)
2030
1908
  rename_column :adverts, :id, :advert_id
2031
-
2032
- #{if defined?(SQLite3)
2033
- "add_index :adverts, [:advert_id], unique: true, name: 'PRIMARY'"
2034
- elsif defined?(Mysql2)
2035
- 'execute "ALTER TABLE adverts DROP PRIMARY KEY, ADD PRIMARY KEY (advert_id)"'
2036
- end}
2037
- EOS
1909
+ EOS
2038
1910
  )
2039
1911
 
2040
1912
  nuke_model_class(Advert)