declare_schema 0.10.1 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4a529991bad5ab16728f11b3f6c165b62853c3b68713e0ec69d6370d7d3c0e5b
4
- data.tar.gz: 6a131f8af82b018b843f639e501371cf0db9a65b91a4acb99aea44d3eb1674d9
3
+ metadata.gz: 516d119d979224ddbe841b2dd5117fc5891b5c79e09261443c7df6f3c2335910
4
+ data.tar.gz: 32621c93b56814f9a8dd30b2d622d2c6a4c536cdc40b2145d078134252e14401
5
5
  SHA512:
6
- metadata.gz: 655ab291337bf8f11fb249de1b97608e7ff73b14ff543f9248e3f92d9bf1770553b8bc2f962c72e8763d27ae559c35bdff1f12f4b6b460a1d46abfab00e931c0
7
- data.tar.gz: b67d3340bb42b91bddd0940e0a9e5a895bd57f2017ef8d4712e1e4331ba46e78b17d9f7e9b0e60de9f921232897c0edeffe37621060632ecb18267db1ce15dc0
6
+ metadata.gz: 912c885f9fea463bae49937b6cb85ba6f7ab8cd4739327e6c662eb7b3cece701e1d42be9f251b97ba4a7b544eddd2a854a99ad8992d4a1fa5563acfd81cff989
7
+ data.tar.gz: 7429b27a56a0cfc27f8af16216528ee7137e5f9d0f20301f5c6177decc816c09b663bf78e5ea22d955b03ed58a7a103663980ec08ab14bb2d27f6d6ccb581787
data/CHANGELOG.md CHANGED
@@ -4,6 +4,16 @@ Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
4
4
 
5
5
  Note: this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [0.11.0] - 2021-03-22
8
+ ### Removed
9
+ - Removed `g|m|c` prompt entirely, since it was confusing. Instead, the migration is
10
+ always generated; the user may press ^C at the filename prompt to cancel.
11
+ The migration will be run if `--migrate` is passed; otherwise, the migrate command will be displayed to be run later.
12
+ ### Added
13
+ - Added the new configuration option `DeclareSchema.@db_migrate_command =`.
14
+ ### Fixed
15
+ - Fixed bug where foreign key constraint names are not globally unique
16
+
7
17
  ## [0.10.1] - 2021-03-18
8
18
  ### Fixed
9
19
  - Migration steps are now generated in a defined dependency order, so that--for example--indexes that depend
@@ -154,6 +164,7 @@ using the appropriate Rails configuration attributes.
154
164
  ### Added
155
165
  - Initial version from https://github.com/Invoca/hobo_fields v4.1.0.
156
166
 
167
+ [0.11.0]: https://github.com/Invoca/declare_schema/compare/v0.10.1...v0.11.0
157
168
  [0.10.1]: https://github.com/Invoca/declare_schema/compare/v0.10.0...v0.10.1
158
169
  [0.10.0]: https://github.com/Invoca/declare_schema/compare/v0.9.0...v0.10.0
159
170
  [0.9.0]: https://github.com/Invoca/declare_schema/compare/v0.8.0...v0.9.0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- declare_schema (0.10.1)
4
+ declare_schema (0.11.0)
5
5
  rails (>= 4.2)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -57,7 +57,7 @@ during the initialization of your Rails application.
57
57
 
58
58
  ### before_generating_migration callback
59
59
 
60
- During the initializtion process for generating migrations, `DeclareSchema` will
60
+ During the initialization process for generating migrations, `DeclareSchema` will
61
61
  trigger the `eager_load!` on the `Rails` application and all `Rails::Engine`s loaded
62
62
  into scope. If you need to generate migrations for models that aren't automatically loaded by `eager_load!`,
63
63
  load them in the `before_generating_migration` block.
@@ -163,6 +163,16 @@ turn all tables into `utf8mb4` supporting tables:
163
163
  DeclareSchema.default_charset = "utf8mb4"
164
164
  DeclareSchema.default_collation = "utf8mb4_bin"
165
165
  ```
166
+ #### db:migrate Command
167
+ `declare_schema` can run the migration once it is generated, if the `--migrate` option is passed.
168
+ If not, it will display the command to run later. By default this command is
169
+ ```
170
+ bundle exec rails db:migrate
171
+ ```
172
+ If your repo has a different command to run for migrations, you can configure it like this:
173
+ ```ruby
174
+ `DeclareSchema.db_migrate_command = "bundle exec rails db:migrate_immediate"`
175
+ ```
166
176
 
167
177
  ## Declaring Character Set and Collation
168
178
  _Note: This feature currently only works for MySQL database configurations._
@@ -28,10 +28,16 @@ module DeclareSchema
28
28
  @default_null = false
29
29
  @default_generate_foreign_keys = true
30
30
  @default_generate_indexing = true
31
+ @db_migrate_command =
32
+ if ActiveSupport::VERSION::MAJOR < 5
33
+ "bundle exec rake db:migrate"
34
+ else
35
+ "bundle exec rails db:migrate"
36
+ end
31
37
 
32
38
  class << self
33
39
  attr_reader :default_charset, :default_collation, :default_text_limit, :default_string_limit, :default_null,
34
- :default_generate_foreign_keys, :default_generate_indexing
40
+ :default_generate_foreign_keys, :default_generate_indexing, :db_migrate_command
35
41
 
36
42
  def to_class(type)
37
43
  case type
@@ -78,6 +84,11 @@ module DeclareSchema
78
84
  generate_indexing.in?([true, false]) or raise ArgumentError, "generate_indexing must be either true or false (got #{generate_indexing.inspect})"
79
85
  @default_generate_indexing = generate_indexing
80
86
  end
87
+
88
+ def db_migrate_command=(db_migrate_command)
89
+ db_migrate_command.is_a?(String) or raise ArgumentError, "db_migrate_command must be a string (got #{db_migrate_command.inspect})"
90
+ @db_migrate_command = db_migrate_command
91
+ end
81
92
  end
82
93
  end
83
94
 
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rails'
4
-
5
3
  require 'declare_schema/extensions/module'
6
4
 
7
5
  module DeclareSchema
@@ -130,7 +128,7 @@ module DeclareSchema
130
128
 
131
129
  fk_options[:dependent] = options.delete(:far_end_dependent) if options.has_key?(:far_end_dependent)
132
130
 
133
- if Rails::VERSION::MAJOR >= 5
131
+ if ActiveSupport::VERSION::MAJOR >= 5
134
132
  super
135
133
  else
136
134
  super(name, scope, options.except(:optional))
@@ -149,7 +147,7 @@ module DeclareSchema
149
147
  end
150
148
  end
151
149
 
152
- if ::Rails::VERSION::MAJOR < 5
150
+ if ::ActiveSupport::VERSION::MAJOR < 5
153
151
  def primary_key
154
152
  super || 'id'
155
153
  end
@@ -227,7 +225,7 @@ module DeclareSchema
227
225
  ActiveRecord::Coders::JSON
228
226
  elsif [:load, :dump].all? { |x| class_name_or_coder.respond_to?(x) }
229
227
  class_name_or_coder
230
- elsif Rails::VERSION::MAJOR >= 5
228
+ elsif ActiveSupport::VERSION::MAJOR >= 5
231
229
  ActiveRecord::Coders::YAMLColumn.new(attr_name, class_name_or_coder)
232
230
  else
233
231
  ActiveRecord::Coders::YAMLColumn.new(class_name_or_coder)
@@ -53,7 +53,7 @@ module DeclareSchema
53
53
  def deserialize_default_value(column, type, default_value)
54
54
  type or raise ArgumentError, "must pass type; got #{type.inspect}"
55
55
 
56
- case Rails::VERSION::MAJOR
56
+ case ActiveSupport::VERSION::MAJOR
57
57
  when 4
58
58
  # TODO: Delete this Rails 4 support ASAP! This could be wrong, since it's using the type of the old column...which
59
59
  # might be getting migrated to a new type. We should be using just type as below. -Colin
@@ -19,9 +19,8 @@ module DeclareSchema
19
19
  @parent_table_name = options[:parent_table]&.to_s
20
20
  @foreign_key_name = options[:foreign_key]&.to_s || @foreign_key
21
21
 
22
- @constraint_name = options[:constraint_name]&.to_s ||
23
- options[:index_name]&.to_s ||
24
- IndexDefinition.index_name(@foreign_key_name)
22
+ @constraint_name = options[:constraint_name]&.to_s.presence ||
23
+ model.connection.index_name(model.table_name, column: @foreign_key_name)
25
24
  @on_delete_cascade = options[:dependent] == :delete
26
25
  end
27
26
 
@@ -68,7 +68,7 @@ module DeclareSchema
68
68
 
69
69
  # This is the old approach which is still needed for MySQL in Rails 4 and SQLite
70
70
  def sqlite_compound_primary_key(model, table)
71
- ActiveRecord::Base.connection.class.name.match?(/SQLite3Adapter/) || Rails::VERSION::MAJOR < 5 or return nil
71
+ ActiveRecord::Base.connection.class.name.match?(/SQLite3Adapter/) || ActiveSupport::VERSION::MAJOR < 5 or return nil
72
72
 
73
73
  connection = model.connection.dup
74
74
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeclareSchema
4
- VERSION = "0.10.1"
4
+ VERSION = "0.11.0"
5
5
  end
@@ -1,47 +1,37 @@
1
1
  Description:
2
2
 
3
3
  This generator compares your existing schema against the
4
- schema declared inside your fields declarations in your
5
- models.
4
+ schema declared inside your declare_schema do ... end
5
+ declarations in your models.
6
6
 
7
7
  If the generator finds differences, it will display the
8
- migration it has created, and ask you if you wish to
9
- [g]enerate migration, generate and [m]igrate now or [c]ancel?
10
- Enter "g" to just generate the migration but do not run it.
11
- Enter "m" to generate the migration and run it, or press "c"
12
- to do nothing.
13
-
14
- The generator will then prompt you for the migration name,
8
+ migration it has created, and prompt you for the migration name,
15
9
  supplying a numbered default name.
16
10
 
17
11
  The generator is conservative and will prompt you to resolve
18
12
  any ambiguities.
19
13
 
20
- Examples:
14
+ Example:
21
15
 
22
- $ rails generate declare_schema:migration
16
+ $ bundle exec rails generate declare_schema:migration
23
17
 
24
18
  ---------- Up Migration ----------
25
- create_table :foos do |t|
19
+ create_table :users do |t|
20
+ t.string :first_name, limit: 50
21
+ t.string :last_name, limit: 50
26
22
  t.datetime :created_at
27
23
  t.datetime :updated_at
28
24
  end
29
25
  ----------------------------------
30
26
 
31
27
  ---------- Down Migration --------
32
- drop_table :foos
28
+ drop_table :users
33
29
  ----------------------------------
34
- What now: [g]enerate migration, generate and [m]igrate now or [c]ancel? m
35
-
36
- Migration filename:
37
- (you can type spaces instead of '_' -- every little helps)
38
- Filename [declare_schema_migration_2]: create_foo
30
+ Migration filename: (spaces will be converted to _) [declare_schema_migration_2]: create users
39
31
  exists db/migrate
40
- create db/migrate/20091023183838_create_foo.rb
41
- (in /work/foo)
42
- == CreateFoo: migrating ======================================================
43
- -- create_table(:yos)
44
- -> 0.0856s
45
- == CreateFoo: migrated (0.0858s) =============================================
32
+ create db/migrate/20091023183838_create_users.rb
33
+
34
+ Not running migration since --migrate not given. When you are ready, run:
46
35
 
36
+ bundle exec rails db:migrate
47
37
 
@@ -38,20 +38,18 @@ module DeclareSchema
38
38
  type: :boolean,
39
39
  desc: "Don't prompt for a migration name - just pick one"
40
40
 
41
- class_option :generate,
42
- aliases: '-g',
43
- type: :boolean,
44
- desc: "Don't prompt for action - generate the migration"
45
-
46
41
  class_option :migrate,
47
42
  aliases: '-m',
48
43
  type: :boolean,
49
- desc: "Don't prompt for action - generate and migrate"
44
+ desc: "After generating migration, run it"
50
45
 
51
46
  def migrate
52
47
  return if migrations_pending?
53
48
 
54
- generator = Generators::DeclareSchema::Migration::Migrator.new(->(c, d, k, p) { extract_renames!(c, d, k, p) })
49
+ generator = Generators::DeclareSchema::Migration::Migrator.new do |to_create, to_drop, kind_str, name_prefix|
50
+ extract_renames!(to_create, to_drop, kind_str, name_prefix)
51
+ end
52
+
55
53
  up, down = generator.generate
56
54
 
57
55
  if up.blank?
@@ -67,34 +65,31 @@ module DeclareSchema
67
65
  say down
68
66
  say "----------------------------------"
69
67
 
70
- action = options[:generate] && 'g' ||
71
- options[:migrate] && 'm' ||
72
- choose("\nWhat now: [g]enerate migration, generate and [m]igrate now or [c]ancel?", /^(g|m|c)$/)
73
-
74
- if action != 'c'
75
- if name.blank? && !options[:default_name]
76
- final_migration_name = choose("\nMigration filename: [<enter>=#{migration_name}|<custom_name>]:", /^[a-z0-9_ ]*$/, migration_name).strip.gsub(' ', '_')
77
- end
78
- final_migration_name = migration_name if final_migration_name.blank?
79
-
80
- up.gsub!("\n", "\n ")
81
- up.gsub!(/ +\n/, "\n")
82
- down.gsub!("\n", "\n ")
83
- down.gsub!(/ +\n/, "\n")
84
-
85
- @up = up
86
- @down = down
87
- @migration_class_name = final_migration_name.camelize
88
-
89
- migration_template('migration.rb.erb', "db/migrate/#{final_migration_name.underscore}.rb")
90
- if action == 'm'
91
- case Rails::VERSION::MAJOR
92
- when 4
93
- rake('db:migrate')
94
- else
95
- rails_command('db:migrate')
96
- end
68
+ final_migration_name =
69
+ name.presence ||
70
+ if !options[:default_name]
71
+ choose("\nMigration filename (spaces will be converted to _) [#{default_migration_name}]:", /^[a-z0-9_ ]*$/,
72
+ default_migration_name).strip.gsub(' ', '_').presence
73
+ end ||
74
+ default_migration_name
75
+
76
+ @up = indent(up, 4)
77
+ @down = indent(down, 4)
78
+ @migration_class_name = final_migration_name.camelize
79
+
80
+ migration_template('migration.rb.erb', "db/migrate/#{final_migration_name.underscore}.rb")
81
+
82
+ db_migrate_command = ::DeclareSchema.db_migrate_command
83
+ if options[:migrate]
84
+ say db_migrate_command
85
+ bare_rails_command = db_migrate_command.sub(/\Abundle exec +/, '').sub(/\Arake +|rails +/, '')
86
+ if ActiveSupport::VERSION::MAJOR < 5
87
+ rake(bare_rails_command)
88
+ else
89
+ rails_command(bare_rails_command)
97
90
  end
91
+ else
92
+ say "\nNot running migration since --migrate not given. When you are ready, run:\n\n #{db_migrate_command}\n\n"
98
93
  end
99
94
  rescue ::DeclareSchema::UnknownTypeError => ex
100
95
  say "Invalid field type: #{ex}"
@@ -102,8 +97,15 @@ module DeclareSchema
102
97
 
103
98
  private
104
99
 
100
+ if ActiveSupport::VERSION::MAJOR < 5
101
+ def indent(string, columns)
102
+ whitespace = ' ' * columns
103
+ string.gsub("\n", "\n#{whitespace}").gsub!(/ +\n/, "\n")
104
+ end
105
+ end
106
+
105
107
  def migrations_pending?
106
- migrations = case Rails::VERSION::MAJOR
108
+ migrations = case ActiveSupport::VERSION::MAJOR
107
109
  when 4
108
110
  ActiveRecord::Migrator.migrations('db/migrate')
109
111
  when 5
@@ -111,7 +113,7 @@ module DeclareSchema
111
113
  else
112
114
  ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths, ActiveRecord::SchemaMigration).migrations
113
115
  end
114
- pending_migrations = case Rails::VERSION::MAJOR
116
+ pending_migrations = case ActiveSupport::VERSION::MAJOR
115
117
  when 4, 5
116
118
  ActiveRecord::Migrator.new(:up, migrations).pending_migrations
117
119
  else
@@ -176,8 +178,8 @@ module DeclareSchema
176
178
  to_rename
177
179
  end
178
180
 
179
- def migration_name
180
- name || Generators::DeclareSchema::Migration::Migrator.default_migration_name
181
+ def default_migration_name
182
+ Generators::DeclareSchema::Migration::Migrator.default_migration_name
181
183
  end
182
184
  end
183
185
  end
@@ -47,8 +47,8 @@ module Generators
47
47
  deprecate :default_charset=, :default_collation=, :default_charset, :default_collation, deprecator: ActiveSupport::Deprecation.new('1.0', 'declare_schema')
48
48
  end
49
49
 
50
- def initialize(ambiguity_resolver = {}, renames: nil)
51
- @ambiguity_resolver = ambiguity_resolver
50
+ def initialize(renames: nil, &block)
51
+ @ambiguity_resolver = block
52
52
  @drops = []
53
53
  @renames = renames
54
54
  end
@@ -483,7 +483,7 @@ module Generators
483
483
  parent_columns = connection.columns(parent_table) rescue []
484
484
  pk_limit =
485
485
  if (pk_column = parent_columns.find { |column| column.name.to_s == "id" }) # right now foreign keys assume id is the target
486
- if Rails::VERSION::MAJOR < 5
486
+ if ActiveSupport::VERSION::MAJOR < 5
487
487
  pk_column.cast_type.limit
488
488
  else
489
489
  pk_column.limit
@@ -549,7 +549,7 @@ module Generators
549
549
  end
550
550
  end
551
551
 
552
- SchemaDumper = case Rails::VERSION::MAJOR
552
+ SchemaDumper = case ActiveSupport::VERSION::MAJOR
553
553
  when 4
554
554
  ActiveRecord::SchemaDumper
555
555
  else
@@ -1,4 +1,4 @@
1
- class <%= @migration_class_name %> < (Rails::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)
1
+ class <%= @migration_class_name %> < (ActiveSupport::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)
2
2
  def self.up
3
3
  <%= @up %>
4
4
  end
@@ -39,7 +39,7 @@ RSpec.describe 'DeclareSchema API' do
39
39
 
40
40
  load_models
41
41
 
42
- if Rails::VERSION::MAJOR == 5
42
+ if ActiveSupport::VERSION::MAJOR == 5
43
43
  # TODO: get this to work on Travis for Rails 6
44
44
  generate_migrations '-n', '-m'
45
45
  end
@@ -12,7 +12,7 @@ RSpec.describe DeclareSchema::Model::FieldSpec do
12
12
  before do
13
13
  load File.expand_path('prepare_testapp.rb', __dir__)
14
14
 
15
- if Rails::VERSION::MAJOR < 5
15
+ if ActiveSupport::VERSION::MAJOR < 5
16
16
  allow(col_spec).to receive(:type_cast_from_database, &:itself)
17
17
  end
18
18
  end
@@ -184,7 +184,7 @@ RSpec.describe DeclareSchema::Model::FieldSpec do
184
184
 
185
185
  describe '#schema_attributes' do
186
186
  let(:col_spec) do
187
- case Rails::VERSION::MAJOR
187
+ case ActiveSupport::VERSION::MAJOR
188
188
  when 4
189
189
  cast_type = ActiveRecord::Type::Integer.new(limit: 8)
190
190
  ActiveRecord::ConnectionAdapters::Column.new("price", nil, cast_type, "integer(8)", false)
@@ -27,7 +27,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
27
27
  end
28
28
  EOS
29
29
 
30
- case Rails::VERSION::MAJOR
30
+ case ActiveSupport::VERSION::MAJOR
31
31
  when 4, 5
32
32
  expect_test_definition_to_eq('alpha/beta', <<~EOS)
33
33
  require "test_helper"
@@ -50,7 +50,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
50
50
  EOS
51
51
  end
52
52
 
53
- case Rails::VERSION::MAJOR
53
+ case ActiveSupport::VERSION::MAJOR
54
54
  when 4
55
55
  expect_test_fixture_to_eq('alpha/beta', <<~EOS)
56
56
  # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rails'
4
3
  begin
5
4
  require 'mysql2'
6
5
  rescue LoadError
@@ -63,7 +62,7 @@ RSpec.describe 'DeclareSchema Migration Generator interactive primary key' do
63
62
  ActiveRecord::Base.connection.execute("CREATE TABLE foos (id integer PRIMARY KEY AUTOINCREMENT NOT NULL)")
64
63
  end
65
64
 
66
- if Rails::VERSION::MAJOR >= 5 && !defined?(Mysql2) # TODO TECH-4814 Put this test back for Mysql2
65
+ if ActiveSupport::VERSION::MAJOR >= 5 && !defined?(Mysql2) # TODO TECH-4814 Put this test back for Mysql2
67
66
  # replace custom primary_key
68
67
  class Foo < ActiveRecord::Base
69
68
  fields do
@@ -131,7 +130,7 @@ RSpec.describe 'DeclareSchema Migration Generator interactive primary key' do
131
130
  ActiveRecord::Base.connection.execute("CREATE TABLE foos (id integer PRIMARY KEY AUTOINCREMENT NOT NULL)")
132
131
  end
133
132
 
134
- if Rails::VERSION::MAJOR >= 5 && !defined?(Mysql2) # TODO TECH-4814 Put this test back for Mysql2
133
+ if ActiveSupport::VERSION::MAJOR >= 5 && !defined?(Mysql2) # TODO TECH-4814 Put this test back for Mysql2
135
134
  # replace custom primary_key
136
135
  class Foo < ActiveRecord::Base
137
136
  declare_schema do
@@ -1,11 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rails'
4
3
  begin
5
4
  require 'mysql2'
6
5
  rescue LoadError
7
6
  end
8
7
 
8
+ begin
9
+ require 'sqlite3'
10
+ rescue LoadError
11
+ end
12
+
9
13
  RSpec.describe 'DeclareSchema Migration Generator' do
10
14
  before do
11
15
  load File.expand_path('prepare_testapp.rb', __dir__)
@@ -26,20 +30,20 @@ RSpec.describe 'DeclareSchema Migration Generator' do
26
30
  end
27
31
  end
28
32
  let(:datetime_precision) do
29
- if defined?(Mysql2) && Rails::VERSION::MAJOR >= 5
33
+ if defined?(Mysql2) && ActiveSupport::VERSION::MAJOR >= 5
30
34
  ', precision: 0'
31
35
  end
32
36
  end
33
37
  let(:table_options) do
34
38
  if defined?(Mysql2)
35
- ", options: \"#{'ENGINE=InnoDB ' if Rails::VERSION::MAJOR == 5}DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\"" +
36
- if Rails::VERSION::MAJOR >= 6
39
+ ", options: \"#{'ENGINE=InnoDB ' if ActiveSupport::VERSION::MAJOR == 5}DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\"" +
40
+ if ActiveSupport::VERSION::MAJOR >= 6
37
41
  ', charset: "utf8mb4", collation: "utf8mb4_bin"'
38
42
  else
39
43
  ''
40
44
  end
41
45
  else
42
- ", id: :integer" unless Rails::VERSION::MAJOR < 5
46
+ ", id: :integer" unless ActiveSupport::VERSION::MAJOR < 5
43
47
  end
44
48
  end
45
49
  let(:lock_version_limit) do
@@ -87,7 +91,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
87
91
  ActiveRecord::Migration.class_eval(up)
88
92
  expect(Advert.columns.map(&:name)).to eq(["id", "name"])
89
93
 
90
- if Rails::VERSION::MAJOR < 5
94
+ if ActiveSupport::VERSION::MAJOR < 5
91
95
  # Rails 4 drivers don't always create PK properly. Fix that by dropping and recreating.
92
96
  ActiveRecord::Base.connection.execute("drop table adverts")
93
97
  if defined?(Mysql2)
@@ -366,10 +370,10 @@ RSpec.describe 'DeclareSchema Migration Generator' do
366
370
  migrate_up(<<~EOS.strip)
367
371
  add_column :adverts, :category_id, :integer, limit: 8, null: false
368
372
  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)}
373
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id" if defined?(Mysql2)}
370
374
  EOS
371
375
  .and migrate_down(<<~EOS.strip)
372
- #{"remove_foreign_key :adverts, name: :on_category_id" if defined?(Mysql2)}
376
+ #{"remove_foreign_key :adverts, name: :index_adverts_on_category_id" if defined?(Mysql2)}
373
377
  remove_index :adverts, name: :on_category_id
374
378
  remove_column :adverts, :category_id
375
379
  EOS
@@ -390,8 +394,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
390
394
  migrate_up(<<~EOS.strip)
391
395
  add_column :adverts, :c_id, :integer, limit: 8, null: false
392
396
  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)}
397
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" +
398
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :index_adverts_on_c_id" if defined?(Mysql2)}
395
399
  EOS
396
400
  )
397
401
 
@@ -409,8 +413,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
409
413
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
410
414
  migrate_up(<<~EOS.strip)
411
415
  add_column :adverts, :category_id, :integer, limit: 8, null: false
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)}
416
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" +
417
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :index_adverts_on_c_id" if defined?(Mysql2)}
414
418
  EOS
415
419
  )
416
420
 
@@ -429,8 +433,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
429
433
  migrate_up(<<~EOS.strip)
430
434
  add_column :adverts, :category_id, :integer, limit: 8, null: false
431
435
  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)}
436
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" +
437
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :index_adverts_on_c_id" if defined?(Mysql2)}
434
438
  EOS
435
439
  )
436
440
 
@@ -454,12 +458,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
454
458
  add_column :adverts, :created_at, :datetime, null: true
455
459
  add_column :adverts, :updated_at, :datetime, null: true
456
460
  add_column :adverts, :lock_version, :integer#{lock_version_limit}, null: false, default: 1
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)}
461
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" +
462
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :index_adverts_on_c_id" if defined?(Mysql2)}
459
463
  EOS
460
464
  .and migrate_down(<<~EOS.strip)
461
- #{"remove_foreign_key :adverts, name: :on_c_id\n" +
462
- "remove_foreign_key :adverts, name: :on_category_id" if defined?(Mysql2)}
465
+ #{"remove_foreign_key :adverts, name: :index_adverts_on_c_id\n" +
466
+ "remove_foreign_key :adverts, name: :index_adverts_on_category_id" if defined?(Mysql2)}
463
467
  remove_column :adverts, :lock_version
464
468
  remove_column :adverts, :updated_at
465
469
  remove_column :adverts, :created_at
@@ -484,8 +488,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
484
488
  migrate_up(<<~EOS.strip)
485
489
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
486
490
  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)}
491
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" +
492
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :index_adverts_on_c_id" if defined?(Mysql2)}
489
493
  EOS
490
494
  )
491
495
 
@@ -503,8 +507,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
503
507
  migrate_up(<<~EOS.strip)
504
508
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
505
509
  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)}
510
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" +
511
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :index_adverts_on_c_id" if defined?(Mysql2)}
508
512
  EOS
509
513
  )
510
514
 
@@ -522,8 +526,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
522
526
  migrate_up(<<~EOS.strip)
523
527
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
524
528
  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)}
529
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" +
530
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :index_adverts_on_c_id" if defined?(Mysql2)}
527
531
  EOS
528
532
  )
529
533
 
@@ -539,8 +543,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
539
543
  migrate_up(<<~EOS.strip)
540
544
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
541
545
  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)}
546
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" +
547
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :index_adverts_on_c_id" if defined?(Mysql2)}
544
548
  EOS
545
549
  )
546
550
 
@@ -556,8 +560,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
556
560
  migrate_up(<<~EOS.strip)
557
561
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
558
562
  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)}
563
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" +
564
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :index_adverts_on_c_id" if defined?(Mysql2)}
561
565
  EOS
562
566
  )
563
567
 
@@ -573,8 +577,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
573
577
  migrate_up(<<~EOS.strip)
574
578
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
575
579
  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)}
580
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" +
581
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :index_adverts_on_c_id" if defined?(Mysql2)}
578
582
  EOS
579
583
  )
580
584
 
@@ -606,14 +610,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
606
610
  add_column :ads, :title, :string, limit: 250, null: true#{charset_and_collation}
607
611
  add_column :ads, :body, :text#{', limit: 4294967295' if defined?(Mysql2)}, null: true#{charset_and_collation}
608
612
  #{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"
613
+ "add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" +
614
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :index_adverts_on_c_id"
611
615
  end}
612
616
  EOS
613
617
  .and migrate_down(<<~EOS.strip)
614
618
  #{if defined?(Mysql2)
615
- "remove_foreign_key :adverts, name: :on_c_id\n" +
616
- "remove_foreign_key :adverts, name: :on_category_id"
619
+ "remove_foreign_key :adverts, name: :index_adverts_on_c_id\n" +
620
+ "remove_foreign_key :adverts, name: :index_adverts_on_category_id"
617
621
  end}
618
622
  remove_column :ads, :body
619
623
  remove_column :ads, :title
@@ -836,6 +840,54 @@ RSpec.describe 'DeclareSchema Migration Generator' do
836
840
  expect(Ad.field_specs['company'].options[:validates].inspect).to eq("{:presence=>true, :uniqueness=>{:case_sensitive=>false}}")
837
841
  end
838
842
 
843
+ context 'models with the same parent foreign key relation' do
844
+ before do
845
+ class Category < ActiveRecord::Base
846
+ fields do
847
+ name :string, limit: 250, null: true
848
+ end
849
+ end
850
+ class Advertiser < ActiveRecord::Base
851
+ fields do
852
+ name :string, limit: 250, null: true
853
+ end
854
+ belongs_to :category, limit: 8
855
+ end
856
+ class Affiliate < ActiveRecord::Base
857
+ fields do
858
+ name :string, limit: 250, null: true
859
+ end
860
+ belongs_to :category, limit: 8
861
+ end
862
+ end
863
+
864
+ it 'will genereate unique constraint names' do
865
+ expect(Generators::DeclareSchema::Migration::Migrator.run).to(
866
+ migrate_up(<<~EOS.strip)
867
+ create_table :categories, id: :bigint, options: "CHARACTER SET utf8mb4 COLLATE utf8mb4_bin" do |t|
868
+ t.string :name, limit: 250, null: true, charset: "utf8mb4", collation: "utf8mb4_bin"
869
+ end
870
+ create_table :advertisers, id: :bigint, options: "CHARACTER SET utf8mb4 COLLATE utf8mb4_bin" do |t|
871
+ t.string :name, limit: 250, null: true, charset: "utf8mb4", collation: "utf8mb4_bin"
872
+ t.integer :category_id, limit: 8, null: false
873
+ end
874
+ create_table :affiliates, id: :bigint, options: "CHARACTER SET utf8mb4 COLLATE utf8mb4_bin" do |t|
875
+ t.string :name, limit: 250, null: true, charset: "utf8mb4", collation: "utf8mb4_bin"
876
+ t.integer :category_id, limit: 8, null: false
877
+ end
878
+ add_index :advertisers, [:category_id], name: :on_category_id
879
+ add_index :affiliates, [:category_id], name: :on_category_id
880
+ add_foreign_key :advertisers, :categories, column: :category_id, name: :index_advertisers_on_category_id
881
+ add_foreign_key :affiliates, :categories, column: :category_id, name: :index_affiliates_on_category_id
882
+ EOS
883
+ )
884
+ migrate
885
+
886
+ nuke_model_class(Advertiser)
887
+ nuke_model_class(Affiliate)
888
+ end
889
+ end if !defined?(SQLite3) && ActiveRecord::VERSION::MAJOR >= 5
890
+
839
891
  describe 'serialize' do
840
892
  before do
841
893
  class Ad < ActiveRecord::Base
@@ -1020,8 +1072,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1020
1072
  end
1021
1073
  end
1022
1074
 
1023
- context "for Rails #{Rails::VERSION::MAJOR}" do
1024
- if Rails::VERSION::MAJOR >= 5
1075
+ context "for Rails #{ActiveSupport::VERSION::MAJOR}" do
1076
+ if ActiveSupport::VERSION::MAJOR >= 5
1025
1077
  let(:optional_true) { { optional: true } }
1026
1078
  let(:optional_false) { { optional: false } }
1027
1079
  else
@@ -1127,13 +1179,13 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1127
1179
  migration_content = File.read(migrations.first)
1128
1180
  first_line = migration_content.split("\n").first
1129
1181
  base_class = first_line.split(' < ').last
1130
- expect(base_class).to eq("(Rails::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)")
1182
+ expect(base_class).to eq("(ActiveSupport::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)")
1131
1183
  end
1132
1184
  end
1133
1185
 
1134
1186
  context 'Does not generate migrations' do
1135
1187
  it 'for aliased fields bigint -> integer limit 8' do
1136
- if Rails::VERSION::MAJOR >= 5 || !ActiveRecord::Base.connection.class.name.match?(/SQLite3Adapter/)
1188
+ if ActiveSupport::VERSION::MAJOR >= 5 || !ActiveRecord::Base.connection.class.name.match?(/SQLite3Adapter/)
1137
1189
  class Advert < active_record_base_class.constantize
1138
1190
  fields do
1139
1191
  price :bigint
@@ -1145,7 +1197,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1145
1197
  migrations = Dir.glob('db/migrate/*declare_schema_migration*.rb')
1146
1198
  expect(migrations.size).to eq(1), migrations.inspect
1147
1199
 
1148
- if defined?(Mysql2) && Rails::VERSION::MAJOR < 5
1200
+ if defined?(Mysql2) && ActiveSupport::VERSION::MAJOR < 5
1149
1201
  ActiveRecord::Base.connection.execute("ALTER TABLE adverts ADD PRIMARY KEY (id)")
1150
1202
  end
1151
1203
 
@@ -1198,7 +1250,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1198
1250
  ActiveRecord::Migration.class_eval(up)
1199
1251
  expect(Advert.columns.map(&:name)).to eq(["id", "name"])
1200
1252
 
1201
- if Rails::VERSION::MAJOR < 5
1253
+ if ActiveSupport::VERSION::MAJOR < 5
1202
1254
  # Rails 4 drivers don't always create PK properly. Fix that by dropping and recreating.
1203
1255
  ActiveRecord::Base.connection.execute("drop table adverts")
1204
1256
  if defined?(Mysql2)
@@ -1477,10 +1529,10 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1477
1529
  migrate_up(<<~EOS.strip)
1478
1530
  add_column :adverts, :category_id, :integer, limit: 8, null: false
1479
1531
  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)}
1532
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" if defined?(Mysql2)}
1481
1533
  EOS
1482
1534
  .and migrate_down(<<~EOS.strip)
1483
- #{"remove_foreign_key :adverts, name: :on_category_id" if defined?(Mysql2)}
1535
+ #{"remove_foreign_key :adverts, name: :index_adverts_on_category_id" if defined?(Mysql2)}
1484
1536
  remove_index :adverts, name: :on_category_id
1485
1537
  remove_column :adverts, :category_id
1486
1538
  EOS
@@ -1501,8 +1553,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1501
1553
  migrate_up(<<~EOS.strip)
1502
1554
  add_column :adverts, :c_id, :integer, limit: 8, null: false
1503
1555
  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)}
1556
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" +
1557
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :index_adverts_on_c_id" if defined?(Mysql2)}
1506
1558
  EOS
1507
1559
  )
1508
1560
 
@@ -1520,8 +1572,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1520
1572
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
1521
1573
  migrate_up(<<~EOS.strip)
1522
1574
  add_column :adverts, :category_id, :integer, limit: 8, null: false
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)}
1575
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" +
1576
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :index_adverts_on_c_id" if defined?(Mysql2)}
1525
1577
  EOS
1526
1578
  )
1527
1579
 
@@ -1540,8 +1592,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1540
1592
  migrate_up(<<~EOS.strip)
1541
1593
  add_column :adverts, :category_id, :integer, limit: 8, null: false
1542
1594
  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)}
1595
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" +
1596
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :index_adverts_on_c_id" if defined?(Mysql2)}
1545
1597
  EOS
1546
1598
  )
1547
1599
 
@@ -1565,12 +1617,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1565
1617
  add_column :adverts, :created_at, :datetime, null: true
1566
1618
  add_column :adverts, :updated_at, :datetime, null: true
1567
1619
  add_column :adverts, :lock_version, :integer#{lock_version_limit}, null: false, default: 1
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)}
1620
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" +
1621
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :index_adverts_on_c_id" if defined?(Mysql2)}
1570
1622
  EOS
1571
1623
  .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)}
1624
+ #{"remove_foreign_key :adverts, name: :index_adverts_on_c_id\n" +
1625
+ "remove_foreign_key :adverts, name: :index_adverts_on_category_id" if defined?(Mysql2)}
1574
1626
  remove_column :adverts, :lock_version
1575
1627
  remove_column :adverts, :updated_at
1576
1628
  remove_column :adverts, :created_at
@@ -1595,8 +1647,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1595
1647
  migrate_up(<<~EOS.strip)
1596
1648
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
1597
1649
  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)}
1650
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" +
1651
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :index_adverts_on_c_id" if defined?(Mysql2)}
1600
1652
  EOS
1601
1653
  )
1602
1654
 
@@ -1614,8 +1666,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1614
1666
  migrate_up(<<~EOS.strip)
1615
1667
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
1616
1668
  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)}
1669
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" +
1670
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :index_adverts_on_c_id" if defined?(Mysql2)}
1619
1671
  EOS
1620
1672
  )
1621
1673
 
@@ -1633,8 +1685,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1633
1685
  migrate_up(<<~EOS.strip)
1634
1686
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
1635
1687
  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)}
1688
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" +
1689
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :index_adverts_on_c_id" if defined?(Mysql2)}
1638
1690
  EOS
1639
1691
  )
1640
1692
 
@@ -1650,8 +1702,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1650
1702
  migrate_up(<<~EOS.strip)
1651
1703
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
1652
1704
  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)}
1705
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" +
1706
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :index_adverts_on_c_id" if defined?(Mysql2)}
1655
1707
  EOS
1656
1708
  )
1657
1709
 
@@ -1667,8 +1719,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1667
1719
  migrate_up(<<~EOS.strip)
1668
1720
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
1669
1721
  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)}
1722
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" +
1723
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :index_adverts_on_c_id" if defined?(Mysql2)}
1672
1724
  EOS
1673
1725
  )
1674
1726
 
@@ -1684,8 +1736,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1684
1736
  migrate_up(<<~EOS.strip)
1685
1737
  add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
1686
1738
  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)}
1739
+ #{"add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" +
1740
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :index_adverts_on_c_id" if defined?(Mysql2)}
1689
1741
  EOS
1690
1742
  )
1691
1743
 
@@ -1716,14 +1768,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1716
1768
  add_column :ads, :title, :string, limit: 250, null: true#{charset_and_collation}
1717
1769
  add_column :ads, :body, :text#{', limit: 4294967295' if defined?(Mysql2)}, null: true#{charset_and_collation}
1718
1770
  #{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"
1771
+ "add_foreign_key :adverts, :categories, column: :category_id, name: :index_adverts_on_category_id\n" +
1772
+ "add_foreign_key :adverts, :categories, column: :c_id, name: :index_adverts_on_c_id"
1721
1773
  end}
1722
1774
  EOS
1723
1775
  .and migrate_down(<<~EOS.strip)
1724
1776
  #{if defined?(Mysql2)
1725
- "remove_foreign_key :adverts, name: :on_c_id\n" +
1726
- "remove_foreign_key :adverts, name: :on_category_id"
1777
+ "remove_foreign_key :adverts, name: :index_adverts_on_c_id\n" +
1778
+ "remove_foreign_key :adverts, name: :index_adverts_on_category_id"
1727
1779
  end}
1728
1780
  remove_column :ads, :body
1729
1781
  remove_column :ads, :title
@@ -1946,6 +1998,55 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1946
1998
  expect(Ad.field_specs['company'].options[:validates].inspect).to eq("{:presence=>true, :uniqueness=>{:case_sensitive=>false}}")
1947
1999
  end
1948
2000
 
2001
+ context 'models with the same parent foreign key relation' do
2002
+ before do
2003
+ class Category < ActiveRecord::Base
2004
+ fields do
2005
+ name :string, limit: 250, null: true
2006
+ end
2007
+ end
2008
+ class Advertiser < ActiveRecord::Base
2009
+ fields do
2010
+ name :string, limit: 250, null: true
2011
+ end
2012
+ belongs_to :category, limit: 8
2013
+ end
2014
+ class Affiliate < ActiveRecord::Base
2015
+ fields do
2016
+ name :string, limit: 250, null: true
2017
+ end
2018
+ belongs_to :category, limit: 8
2019
+ end
2020
+ end
2021
+
2022
+ it 'will genereate unique constraint names' do
2023
+ expect(Generators::DeclareSchema::Migration::Migrator.run).to(
2024
+ migrate_up(<<~EOS.strip)
2025
+ create_table :categories, id: :bigint, options: "CHARACTER SET utf8mb4 COLLATE utf8mb4_bin" do |t|
2026
+ t.string :name, limit: 250, null: true, charset: "utf8mb4", collation: "utf8mb4_bin"
2027
+ end
2028
+ create_table :advertisers, id: :bigint, options: "CHARACTER SET utf8mb4 COLLATE utf8mb4_bin" do |t|
2029
+ t.string :name, limit: 250, null: true, charset: "utf8mb4", collation: "utf8mb4_bin"
2030
+ t.integer :category_id, limit: 8, null: false
2031
+ end
2032
+ create_table :affiliates, id: :bigint, options: "CHARACTER SET utf8mb4 COLLATE utf8mb4_bin" do |t|
2033
+ t.string :name, limit: 250, null: true, charset: "utf8mb4", collation: "utf8mb4_bin"
2034
+ t.integer :category_id, limit: 8, null: false
2035
+ end
2036
+ add_index :advertisers, [:category_id], name: :on_category_id
2037
+ add_index :affiliates, [:category_id], name: :on_category_id
2038
+ add_foreign_key :advertisers, :categories, column: :category_id, name: :index_advertisers_on_category_id
2039
+ add_foreign_key :affiliates, :categories, column: :category_id, name: :index_affiliates_on_category_id
2040
+ EOS
2041
+ )
2042
+ binding.pry
2043
+ migrate
2044
+
2045
+ nuke_model_class(Advertiser)
2046
+ nuke_model_class(Affiliate)
2047
+ end
2048
+ end if !defined?(SQLite3) && ActiveRecord::VERSION::MAJOR >= 5
2049
+
1949
2050
  describe 'serialize' do
1950
2051
  before do
1951
2052
  class Ad < ActiveRecord::Base
@@ -2130,8 +2231,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
2130
2231
  end
2131
2232
  end
2132
2233
 
2133
- context "for Rails #{Rails::VERSION::MAJOR}" do
2134
- if Rails::VERSION::MAJOR >= 5
2234
+ context "for Rails #{ActiveSupport::VERSION::MAJOR}" do
2235
+ if ActiveSupport::VERSION::MAJOR >= 5
2135
2236
  let(:optional_true) { { optional: true } }
2136
2237
  let(:optional_false) { { optional: false } }
2137
2238
  else
@@ -2237,13 +2338,13 @@ RSpec.describe 'DeclareSchema Migration Generator' do
2237
2338
  migration_content = File.read(migrations.first)
2238
2339
  first_line = migration_content.split("\n").first
2239
2340
  base_class = first_line.split(' < ').last
2240
- expect(base_class).to eq("(Rails::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)")
2341
+ expect(base_class).to eq("(ActiveSupport::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)")
2241
2342
  end
2242
2343
  end
2243
2344
 
2244
2345
  context 'Does not generate migrations' do
2245
2346
  it 'for aliased fields bigint -> integer limit 8' do
2246
- if Rails::VERSION::MAJOR >= 5 || !ActiveRecord::Base.connection.class.name.match?(/SQLite3Adapter/)
2347
+ if ActiveSupport::VERSION::MAJOR >= 5 || !ActiveRecord::Base.connection.class.name.match?(/SQLite3Adapter/)
2247
2348
  class Advert < active_record_base_class.constantize
2248
2349
  declare_schema do
2249
2350
  bigint :price
@@ -2255,7 +2356,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
2255
2356
  migrations = Dir.glob('db/migrate/*declare_schema_migration*.rb')
2256
2357
  expect(migrations.size).to eq(1), migrations.inspect
2257
2358
 
2258
- if defined?(Mysql2) && Rails::VERSION::MAJOR < 5
2359
+ if defined?(Mysql2) && ActiveSupport::VERSION::MAJOR < 5
2259
2360
  ActiveRecord::Base.connection.execute("ALTER TABLE adverts ADD PRIMARY KEY (id)")
2260
2361
  end
2261
2362
 
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rails'
4
-
5
3
  begin
6
4
  require 'mysql2'
7
5
  rescue LoadError
@@ -16,7 +14,7 @@ RSpec.describe DeclareSchema::Model::Column do
16
14
 
17
15
  describe 'class methods' do
18
16
  describe '.native_type?' do
19
- if Rails::VERSION::MAJOR >= 5
17
+ if ActiveSupport::VERSION::MAJOR >= 5
20
18
  let(:native_types) { [:string, :text, :integer, :float, :decimal, :datetime, :time, :date, :binary, :boolean, :json] }
21
19
  else
22
20
  let(:native_types) { [:string, :text, :integer, :float, :decimal, :datetime, :time, :date, :binary, :boolean] }
@@ -65,9 +63,7 @@ RSpec.describe DeclareSchema::Model::Column do
65
63
  end
66
64
 
67
65
  describe '.deserialize_default_value' do
68
- require 'rails'
69
-
70
- if ::Rails::VERSION::MAJOR >= 5
66
+ if ::ActiveSupport::VERSION::MAJOR >= 5
71
67
  it 'deserializes :boolean' do
72
68
  expect(described_class.deserialize_default_value(nil, :boolean, 'true')).to eq(true)
73
69
  expect(described_class.deserialize_default_value(nil, :boolean, 'false')).to eq(false)
@@ -26,7 +26,7 @@ RSpec.describe DeclareSchema::Model::ForeignKeyDefinition do
26
26
  subject { described_class.new(model, foreign_key, options)}
27
27
 
28
28
  before do
29
- allow(connection).to receive(:index_name).with('models', column: 'network_id') { 'on_network_id' }
29
+ allow(model.connection).to receive(:index_name).with(any_args) { 'index_on_network_id' }
30
30
  end
31
31
 
32
32
  describe '#initialize' do
@@ -36,7 +36,7 @@ RSpec.describe DeclareSchema::Model::ForeignKeyDefinition do
36
36
  end
37
37
 
38
38
  context 'when most options passed' do
39
- let(:options) { { parent_table: :networks, foreign_key: :the_network_id, index_name: :index_on_network_id } }
39
+ let(:options) { { parent_table: :networks, foreign_key: :the_network_id } }
40
40
 
41
41
  it 'normalizes symbols to strings' do
42
42
  expect(subject.foreign_key).to eq('network_id')
@@ -50,8 +50,7 @@ RSpec.describe DeclareSchema::Model::ForeignKeyDefinition do
50
50
 
51
51
  context 'when all options passed' do
52
52
  let(:foreign_key) { nil }
53
- let(:options) { { parent_table: :networks, foreign_key: :the_network_id, index_name: :index_on_network_id,
54
- constraint_name: :constraint_1, dependent: :delete } }
53
+ let(:options) { { parent_table: :networks, foreign_key: :the_network_id, constraint_name: :constraint_1, dependent: :delete } }
55
54
 
56
55
  it 'normalizes symbols to strings' do
57
56
  expect(subject.foreign_key).to be_nil
@@ -61,6 +60,19 @@ RSpec.describe DeclareSchema::Model::ForeignKeyDefinition do
61
60
  expect(subject.on_delete_cascade).to be_truthy
62
61
  end
63
62
  end
63
+
64
+ context 'when constraint name passed as empty string' do
65
+ let(:options) { { constraint_name: "" } }
66
+ it 'defaults to rails constraint name' do
67
+ expect(subject.constraint_name).to eq("index_on_network_id")
68
+ end
69
+ end
70
+
71
+ context 'when no constraint name passed' do
72
+ it 'defaults to rails constraint name' do
73
+ expect(subject.constraint_name).to eq("index_on_network_id")
74
+ end
75
+ end
64
76
  end
65
77
  end
66
78
 
@@ -107,7 +119,7 @@ RSpec.describe DeclareSchema::Model::ForeignKeyDefinition do
107
119
  subject { described_class.new(model, foreign_key, options)}
108
120
 
109
121
  before do
110
- allow(connection).to receive(:index_name).with('models', column: 'network_id') { 'on_network_id' }
122
+ allow(model.connection).to receive(:index_name).with(any_args) { 'index_on_network_id' }
111
123
  end
112
124
 
113
125
  describe '#initialize' do
@@ -117,7 +129,7 @@ RSpec.describe DeclareSchema::Model::ForeignKeyDefinition do
117
129
  end
118
130
 
119
131
  context 'when most options passed' do
120
- let(:options) { { parent_table: :networks, foreign_key: :the_network_id, index_name: :index_on_network_id } }
132
+ let(:options) { { parent_table: :networks, foreign_key: :the_network_id } }
121
133
 
122
134
  it 'normalizes symbols to strings' do
123
135
  expect(subject.foreign_key).to eq('network_id')
@@ -131,8 +143,7 @@ RSpec.describe DeclareSchema::Model::ForeignKeyDefinition do
131
143
 
132
144
  context 'when all options passed' do
133
145
  let(:foreign_key) { nil }
134
- let(:options) { { parent_table: :networks, foreign_key: :the_network_id, index_name: :index_on_network_id,
135
- constraint_name: :constraint_1, dependent: :delete } }
146
+ let(:options) { { parent_table: :networks, foreign_key: :the_network_id, constraint_name: :constraint_1, dependent: :delete } }
136
147
 
137
148
  it 'normalizes symbols to strings' do
138
149
  expect(subject.foreign_key).to be_nil
@@ -142,6 +153,19 @@ RSpec.describe DeclareSchema::Model::ForeignKeyDefinition do
142
153
  expect(subject.on_delete_cascade).to be_truthy
143
154
  end
144
155
  end
156
+
157
+ context 'when constraint name passed as empty string' do
158
+ let(:options) { { constraint_name: "" } }
159
+ it 'defaults to rails constraint name' do
160
+ expect(subject.constraint_name).to eq("index_on_network_id")
161
+ end
162
+ end
163
+
164
+ context 'when no constraint name passed' do
165
+ it 'defaults to rails constraint name' do
166
+ expect(subject.constraint_name).to eq("index_on_network_id")
167
+ end
168
+ end
145
169
  end
146
170
  end
147
171
 
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rails'
4
-
5
3
  begin
6
4
  require 'mysql2'
7
5
  rescue LoadError
@@ -75,7 +75,7 @@ RSpec.describe DeclareSchema::Model::IndexDefinition do
75
75
  ActiveRecord::Base.connection.execute <<~EOS
76
76
  CREATE TABLE index_definition_test_models (
77
77
  id INTEGER NOT NULL PRIMARY KEY,
78
- name #{if defined?(Sqlite3) then 'TEXT' else 'VARCHAR(255)' end} NOT NULL
78
+ name #{if defined?(SQLite3) then 'TEXT' else 'VARCHAR(255)' end} NOT NULL
79
79
  )
80
80
  EOS
81
81
  ActiveRecord::Base.connection.execute <<~EOS
@@ -110,7 +110,7 @@ RSpec.describe DeclareSchema::Model::IndexDefinition do
110
110
  let(:model_class) { IndexDefinitionCompoundIndexModel }
111
111
 
112
112
  it 'returns the indexes for the model' do
113
- if Rails::VERSION::MAJOR < 5
113
+ if ActiveSupport::VERSION::MAJOR < 5
114
114
  expect(model_class.connection).to receive(:primary_key).with('index_definition_compound_index_models').and_return(nil)
115
115
  connection_stub = instance_double(ActiveRecord::Base.connection.class, "connection")
116
116
  expect(connection_stub).to receive(:indexes).
@@ -184,7 +184,7 @@ RSpec.describe DeclareSchema::Model::IndexDefinition do
184
184
  ActiveRecord::Base.connection.execute <<~EOS
185
185
  CREATE TABLE index_definition_test_models (
186
186
  id INTEGER NOT NULL PRIMARY KEY,
187
- name #{if defined?(Sqlite3) then 'TEXT' else 'VARCHAR(255)' end} NOT NULL
187
+ name #{if defined?(SQLite3) then 'TEXT' else 'VARCHAR(255)' end} NOT NULL
188
188
  )
189
189
  EOS
190
190
  ActiveRecord::Base.connection.execute <<~EOS
@@ -219,7 +219,7 @@ RSpec.describe DeclareSchema::Model::IndexDefinition do
219
219
  let(:model_class) { IndexDefinitionCompoundIndexModel }
220
220
 
221
221
  it 'returns the indexes for the model' do
222
- if Rails::VERSION::MAJOR < 5
222
+ if ActiveSupport::VERSION::MAJOR < 5
223
223
  expect(model_class.connection).to receive(:primary_key).with('index_definition_compound_index_models').and_return(nil)
224
224
  connection_stub = instance_double(ActiveRecord::Base.connection.class, "connection")
225
225
  expect(connection_stub).to receive(:indexes).
data/spec/spec_helper.rb CHANGED
@@ -27,7 +27,7 @@ RSpec.configure do |config|
27
27
  RSpec::Support::ObjectFormatter.default_instance.max_formatted_output_length = 2_000
28
28
 
29
29
  def active_record_base_class
30
- if Rails::VERSION::MAJOR == 4
30
+ if ActiveSupport::VERSION::MAJOR == 4
31
31
  'ActiveRecord::Base'
32
32
  else
33
33
  'ApplicationRecord'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: declare_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.1
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Invoca Development adapted from hobo_fields by Tom Locke
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-18 00:00:00.000000000 Z
11
+ date: 2021-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -128,7 +128,7 @@ homepage: https://github.com/Invoca/declare_schema
128
128
  licenses: []
129
129
  metadata:
130
130
  allowed_push_host: https://rubygems.org
131
- post_install_message:
131
+ post_install_message:
132
132
  rdoc_options: []
133
133
  require_paths:
134
134
  - lib
@@ -144,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
144
  version: 1.3.6
145
145
  requirements: []
146
146
  rubygems_version: 3.0.3
147
- signing_key:
147
+ signing_key:
148
148
  specification_version: 4
149
149
  summary: Database schema declaration and migration generator for Rails
150
150
  test_files: []