declare_schema 0.10.0 → 0.12.0.pre.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +31 -1
  3. data/Gemfile.lock +1 -1
  4. data/README.md +16 -6
  5. data/lib/declare_schema.rb +12 -1
  6. data/lib/declare_schema/extensions/active_record/fields_declaration.rb +4 -2
  7. data/lib/declare_schema/model.rb +59 -15
  8. data/lib/declare_schema/model/column.rb +2 -2
  9. data/lib/declare_schema/model/field_spec.rb +4 -4
  10. data/lib/declare_schema/model/foreign_key_definition.rb +6 -11
  11. data/lib/declare_schema/model/habtm_model_shim.rb +2 -2
  12. data/lib/declare_schema/model/index_definition.rb +8 -25
  13. data/lib/declare_schema/schema_change/all.rb +22 -0
  14. data/lib/declare_schema/schema_change/base.rb +45 -0
  15. data/lib/declare_schema/schema_change/column_add.rb +27 -0
  16. data/lib/declare_schema/schema_change/column_change.rb +32 -0
  17. data/lib/declare_schema/schema_change/column_remove.rb +20 -0
  18. data/lib/declare_schema/schema_change/column_rename.rb +23 -0
  19. data/lib/declare_schema/schema_change/foreign_key_add.rb +25 -0
  20. data/lib/declare_schema/schema_change/foreign_key_remove.rb +20 -0
  21. data/lib/declare_schema/schema_change/index_add.rb +33 -0
  22. data/lib/declare_schema/schema_change/index_remove.rb +20 -0
  23. data/lib/declare_schema/schema_change/primary_key_change.rb +33 -0
  24. data/lib/declare_schema/schema_change/table_add.rb +37 -0
  25. data/lib/declare_schema/schema_change/table_change.rb +36 -0
  26. data/lib/declare_schema/schema_change/table_remove.rb +22 -0
  27. data/lib/declare_schema/schema_change/table_rename.rb +22 -0
  28. data/lib/declare_schema/version.rb +1 -1
  29. data/lib/generators/declare_schema/migration/USAGE +14 -24
  30. data/lib/generators/declare_schema/migration/migration_generator.rb +40 -38
  31. data/lib/generators/declare_schema/migration/migrator.rb +190 -187
  32. data/lib/generators/declare_schema/migration/templates/migration.rb.erb +3 -3
  33. data/spec/lib/declare_schema/api_spec.rb +3 -1
  34. data/spec/lib/declare_schema/field_spec_spec.rb +3 -3
  35. data/spec/lib/declare_schema/generator_spec.rb +2 -2
  36. data/spec/lib/declare_schema/interactive_primary_key_spec.rb +60 -25
  37. data/spec/lib/declare_schema/migration_generator_spec.rb +471 -377
  38. data/spec/lib/declare_schema/model/column_spec.rb +2 -6
  39. data/spec/lib/declare_schema/model/foreign_key_definition_spec.rb +28 -16
  40. data/spec/lib/declare_schema/model/habtm_model_shim_spec.rb +4 -6
  41. data/spec/lib/declare_schema/model/index_definition_spec.rb +4 -4
  42. data/spec/lib/declare_schema/schema_change/base_spec.rb +75 -0
  43. data/spec/lib/declare_schema/schema_change/column_add_spec.rb +30 -0
  44. data/spec/lib/declare_schema/schema_change/column_change_spec.rb +33 -0
  45. data/spec/lib/declare_schema/schema_change/column_remove_spec.rb +30 -0
  46. data/spec/lib/declare_schema/schema_change/column_rename_spec.rb +28 -0
  47. data/spec/lib/declare_schema/schema_change/foreign_key_add_spec.rb +29 -0
  48. data/spec/lib/declare_schema/schema_change/foreign_key_remove_spec.rb +29 -0
  49. data/spec/lib/declare_schema/schema_change/index_add_spec.rb +56 -0
  50. data/spec/lib/declare_schema/schema_change/index_remove_spec.rb +29 -0
  51. data/spec/lib/declare_schema/schema_change/primary_key_change_spec.rb +69 -0
  52. data/spec/lib/declare_schema/schema_change/table_add_spec.rb +50 -0
  53. data/spec/lib/declare_schema/schema_change/table_change_spec.rb +30 -0
  54. data/spec/lib/declare_schema/schema_change/table_remove_spec.rb +27 -0
  55. data/spec/lib/declare_schema/schema_change/table_rename_spec.rb +27 -0
  56. data/spec/lib/generators/declare_schema/migration/migrator_spec.rb +59 -11
  57. data/spec/spec_helper.rb +1 -1
  58. data/spec/support/acceptance_spec_helpers.rb +2 -2
  59. metadata +34 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e993cfd0cf72aa7610f32743ce838d6c7e6ec07370cd3427649ab80cce7e9ac2
4
- data.tar.gz: 273de3135fa09049b57a598342a36c283dc956498016dbc98297910233b1b5bb
3
+ metadata.gz: e55426213e7d15730b0531d3dba4036a4b18f3967b73a7619e60f27d15fc5b55
4
+ data.tar.gz: 294eef4ce49c912df24403ab59998e61641c681ea1e98fa6c9fa00d66a4c4502
5
5
  SHA512:
6
- metadata.gz: 1f28b92b3614dc61323b386eeb67c681873c983acd66869b705e6c920c906c9429f26030135e916726fa0b068c6b68e5cce57902f64c230c0a5cf289fdec9645
7
- data.tar.gz: 2d23d69ffedf63881a3d33bbde3f2bcc62ed9e12938e981bb7388400a3f4dec28aa3ade8714a3c352c93b743496e70777f6a54e710d91d7e6adb2ca66fc23954
6
+ metadata.gz: 3a3ebaa029176ff322a396baf65aaca878f631ae3ac0acad2a2290fc73275c56c312a00e1423219a30055f39ca17f1873a55f0d8be8a4dfeb38f821718c57128
7
+ data.tar.gz: 25d48b29e96b5508b52c07ca869221d506f05370a38af76994a33bbfcb67153f379b9605a60e9169876bebb63e6db5bf9acbf1f09aab900d03fc05be404cd643
data/CHANGELOG.md CHANGED
@@ -4,9 +4,35 @@ 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.12.0] - Unreleased
8
+ ### Added
9
+ - `belongs_to` now always infers the `limit:` of the foreign key to match that of the primary key it points to.
10
+ Note: this isn't possible for polymorphic foreign keys, so it assumes `limit: 8` there...unless the schema
11
+ was migrated in the past with `limit: 4`.
12
+
13
+ ## [0.11.1] - 2021-03-26
14
+ ### Fixed
15
+ - Fixed a bug where up and down in generated migration would be empty in Rails 4.
16
+
17
+ ## [0.11.0] - 2021-03-22
18
+ ### Removed
19
+ - Removed `g|m|c` prompt entirely, since it was confusing. Instead, the migration is
20
+ always generated; the user may press ^C at the filename prompt to cancel.
21
+ The migration will be run if `--migrate` is passed; otherwise, the migrate command will be displayed to be run later.
22
+ ### Added
23
+ - Added the new configuration option `DeclareSchema.@db_migrate_command =`.
24
+ ### Fixed
25
+ - Fixed bug where foreign key constraint names are not globally unique
26
+
27
+ ## [0.10.1] - 2021-03-18
28
+ ### Fixed
29
+ - Migration steps are now generated in a defined dependency order, so that--for example--indexes that depend
30
+ on columns are deleted first, before the columns themselves are deleted (since the latter implicitly does the former, which would break the migration when run).
31
+ - Related to the above, down migration steps are now always generated in exactly the reverse order of the up migration steps.
32
+
7
33
  ## [0.10.0] - 2021-03-17
8
34
  ### Deprecated
9
- - The `fields` dsl method is being deprecated.
35
+ - Deprecated the `fields` DSL method in favor of `declare_schema`.
10
36
 
11
37
  ### Added
12
38
  - Added the `declare_schema` method to replace `fields`. We now expect a column's type to come before the name
@@ -148,6 +174,10 @@ using the appropriate Rails configuration attributes.
148
174
  ### Added
149
175
  - Initial version from https://github.com/Invoca/hobo_fields v4.1.0.
150
176
 
177
+ [0.12.0]: https://github.com/Invoca/declare_schema/compare/v0.11.1...v0.12.0
178
+ [0.11.1]: https://github.com/Invoca/declare_schema/compare/v0.11.0...v0.11.1
179
+ [0.11.0]: https://github.com/Invoca/declare_schema/compare/v0.10.1...v0.11.0
180
+ [0.10.1]: https://github.com/Invoca/declare_schema/compare/v0.10.0...v0.10.1
151
181
  [0.10.0]: https://github.com/Invoca/declare_schema/compare/v0.9.0...v0.10.0
152
182
  [0.9.0]: https://github.com/Invoca/declare_schema/compare/v0.8.0...v0.9.0
153
183
  [0.8.0]: https://github.com/Invoca/declare_schema/compare/v0.7.1...v0.8.0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- declare_schema (0.10.0)
4
+ declare_schema (0.12.0.pre.2)
5
5
  rails (>= 4.2)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -4,10 +4,10 @@ Declare your Rails/active_record model schemas and have database migrations gene
4
4
 
5
5
  ## Example
6
6
 
7
- Make a model and declare your schema within a `fields do ... end` block:
7
+ Make a model and declare your schema within a `declare_schema do ... end` block:
8
8
  ```ruby
9
9
  class Company < ActiveRecord::Base
10
- fields do
10
+ declare_schema do
11
11
  company_name :string, limit: 100
12
12
  ticker_symbol :string, limit: 4, null: true, index: true, unique: true
13
13
  employee_count :integer
@@ -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._
@@ -177,7 +187,7 @@ at three separate levels
177
187
 
178
188
  ### Table Configuration
179
189
  In order to configure a table's default character set and collation, the `charset` and
180
- `collation` arguments can be added to the `fields` block.
190
+ `collation` arguments can be added to the `declare_schema` block.
181
191
 
182
192
  For example, if you have a comments model that needs `utf8mb4` support, it would look
183
193
  like the following:
@@ -187,7 +197,7 @@ like the following:
187
197
  # frozen_string_literal: true
188
198
 
189
199
  class Comment < ActiveRecord::Base
190
- fields charset: "utf8mb4", collation: "utf8mb4_bin" do
200
+ declare_schema charset: "utf8mb4", collation: "utf8mb4_bin" do
191
201
  subject :string, limit: 255
192
202
  content :text, limit: 0xffff_ffff
193
203
  end
@@ -207,7 +217,7 @@ look like the following:
207
217
  # frozen_string_literal: true
208
218
 
209
219
  class Comment < ActiveRecord::Base
210
- fields do
220
+ declare_schema do
211
221
  subject :string, limit: 255
212
222
  context :text, limit: 0xffff_ffff, charset: "utf8mb4", collation: "utf8mb4_bin"
213
223
  end
@@ -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
 
@@ -7,12 +7,14 @@ require 'declare_schema/field_declaration_dsl'
7
7
 
8
8
  module DeclareSchema
9
9
  module Macros
10
+ attr_reader :_table_options
11
+
10
12
  def fields(table_options = {}, &block)
11
13
  # Any model that calls 'fields' gets DeclareSchema::Model behavior
12
14
  DeclareSchema::Model.mix_in(self)
13
15
 
14
16
  @include_in_migration = true
15
- @table_options = table_options
17
+ @_table_options = table_options
16
18
 
17
19
  if block
18
20
  dsl = DeclareSchema::FieldDeclarationDsl.new(self)
@@ -31,7 +33,7 @@ module DeclareSchema
31
33
 
32
34
  # @include_in_migration = false #||= options.fetch(:include_in_migration, true); options.delete(:include_in_migration)
33
35
  @include_in_migration = true # TODO: Add back or delete the include_in_migration feature
34
- @table_options = table_options
36
+ @_table_options = table_options
35
37
 
36
38
  if block
37
39
  dsl = DeclareSchema::Dsl.new(self, null: false)
@@ -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
@@ -101,7 +99,10 @@ module DeclareSchema
101
99
  end
102
100
  end
103
101
 
104
- # Extend belongs_to so that it creates a FieldSpec for the foreign key
102
+ # Extend belongs_to so that it
103
+ # 1. creates a FieldSpec for the foreign key
104
+ # 2. declares an index on the foreign key
105
+ # 3. declares a foreign_key constraint
105
106
  def belongs_to(name, scope = nil, **options)
106
107
  column_options = {}
107
108
 
@@ -111,7 +112,10 @@ module DeclareSchema
111
112
  options[:optional] # infer :null from :optional
112
113
  end || false
113
114
  column_options[:default] = options.delete(:default) if options.has_key?(:default)
114
- column_options[:limit] = options.delete(:limit) if options.has_key?(:limit)
115
+ if options.has_key?(:limit)
116
+ options.delete(:limit)
117
+ ActiveSupport::Deprecation.warn("belongs_to limit: is deprecated since it is now inferred")
118
+ end
115
119
 
116
120
  index_options = {}
117
121
  index_options[:name] = options.delete(:index) if options.has_key?(:index)
@@ -130,7 +134,7 @@ module DeclareSchema
130
134
 
131
135
  fk_options[:dependent] = options.delete(:far_end_dependent) if options.has_key?(:far_end_dependent)
132
136
 
133
- if Rails::VERSION::MAJOR >= 5
137
+ if ActiveSupport::VERSION::MAJOR >= 5
134
138
  super
135
139
  else
136
140
  super(name, scope, options.except(:optional))
@@ -138,7 +142,25 @@ module DeclareSchema
138
142
 
139
143
  refl = reflections[name.to_s] or raise "Couldn't find reflection #{name} in #{reflections.keys}"
140
144
  fkey = refl.foreign_key or raise "Couldn't find foreign_key for #{name} in #{refl.inspect}"
141
- declare_field(fkey.to_sym, :integer, column_options)
145
+ fkey_id_column_options = column_options.dup
146
+
147
+ # Note: the foreign key limit: should match the primary key limit:. (If there is a foreign key constraint,
148
+ # those limits _must_ match.) We'd like to call _infer_fk_limit and get the limit right from the PK.
149
+ # But we can't here, because that will mess up the autoloader to follow every belongs_to association right
150
+ # when it is declared. So instead we assume :bigint (integer limit: 8) below, while also registering this
151
+ # pre_migration: callback to double-check that assumption Just In Time--right before we generate a migration.
152
+ #
153
+ # The one downside of this approach is that application code that asks the field_spec for the declared
154
+ # foreign key limit: will always get 8 back even if this is a grandfathered foreign key that points to
155
+ # a limit: 4 primary key. It seems unlikely that any application code would do this.
156
+ fkey_id_column_options[:pre_migration] = ->(field_spec) do
157
+ if (inferred_limit = _infer_fk_limit(fkey, refl))
158
+ field_spec.sql_options[:limit] = inferred_limit
159
+ end
160
+ end
161
+
162
+ declare_field(fkey.to_sym, :bigint, fkey_id_column_options)
163
+
142
164
  if refl.options[:polymorphic]
143
165
  foreign_type = options[:foreign_type] || "#{name}_type"
144
166
  _declare_polymorphic_type_field(foreign_type, column_options)
@@ -149,7 +171,29 @@ module DeclareSchema
149
171
  end
150
172
  end
151
173
 
152
- if ::Rails::VERSION::MAJOR < 5
174
+ def _infer_fk_limit(fkey, refl)
175
+ if refl.options[:polymorphic]
176
+ if (fkey_column = columns_hash[fkey.to_s]) && fkey_column.type == :integer
177
+ fkey_column.limit
178
+ end
179
+ else
180
+ klass = refl.klass or raise "Couldn't find belongs_to klass for #{name} in #{refl.inspect}"
181
+ if (pk_id_type = klass._table_options&.[](:id))
182
+ if pk_id_type == :integer
183
+ 4
184
+ end
185
+ else
186
+ if klass.table_exists? && (pk_column = klass.columns_hash[klass._declared_primary_key])
187
+ pk_id_type = pk_column.type
188
+ if pk_id_type == :integer
189
+ pk_column.limit
190
+ end
191
+ end
192
+ end
193
+ end
194
+ end
195
+
196
+ if ::ActiveSupport::VERSION::MAJOR < 5
153
197
  def primary_key
154
198
  super || 'id'
155
199
  end
@@ -157,27 +201,27 @@ module DeclareSchema
157
201
 
158
202
  # returns the primary key (String) as declared with primary_key =
159
203
  # unlike the `primary_key` method, DOES NOT query the database to find the actual primary key in use right now
160
- # if no explicit primary key set, returns the default_defined_primary_key
161
- def _defined_primary_key
204
+ # if no explicit primary key set, returns the _default_declared_primary_key
205
+ def _declared_primary_key
162
206
  if defined?(@primary_key)
163
207
  @primary_key&.to_s
164
- end || _default_defined_primary_key
208
+ end || _default_declared_primary_key
165
209
  end
166
210
 
167
211
  private
168
212
 
169
- # if this is a derived class, returns the base class's _defined_primary_key
213
+ # if this is a derived class, returns the base class's _declared_primary_key
170
214
  # otherwise, returns 'id'
171
- def _default_defined_primary_key
215
+ def _default_declared_primary_key
172
216
  if self == base_class
173
217
  'id'
174
218
  else
175
- base_class._defined_primary_key
219
+ base_class._declared_primary_key
176
220
  end
177
221
  end
178
222
 
179
223
  def _rails_default_primary_key
180
- ::DeclareSchema::Model::IndexDefinition.new(self, [_defined_primary_key.to_sym], unique: true, name: DeclareSchema::Model::IndexDefinition::PRIMARY_KEY_NAME)
224
+ ::DeclareSchema::Model::IndexDefinition.new(self, [_declared_primary_key.to_sym], unique: true, name: DeclareSchema::Model::IndexDefinition::PRIMARY_KEY_NAME)
181
225
  end
182
226
 
183
227
  # Declares the "foo_type" field that accompanies the "foo_id"
@@ -227,7 +271,7 @@ module DeclareSchema
227
271
  ActiveRecord::Coders::JSON
228
272
  elsif [:load, :dump].all? { |x| class_name_or_coder.respond_to?(x) }
229
273
  class_name_or_coder
230
- elsif Rails::VERSION::MAJOR >= 5
274
+ elsif ActiveSupport::VERSION::MAJOR >= 5
231
275
  ActiveRecord::Coders::YAMLColumn.new(attr_name, class_name_or_coder)
232
276
  else
233
277
  ActiveRecord::Coders::YAMLColumn.new(class_name_or_coder)
@@ -53,13 +53,13 @@ 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
60
60
  column.type_cast_from_database(default_value)
61
61
  else
62
- cast_type = ActiveRecord::Base.connection.send(:lookup_cast_type, type) or
62
+ cast_type = ActiveRecord::Base.connection.send(:lookup_cast_type, type.to_s) or
63
63
  raise "cast_type not found for #{type}"
64
64
  cast_type.deserialize(default_value)
65
65
  end
@@ -47,9 +47,9 @@ module DeclareSchema
47
47
  end
48
48
 
49
49
  def initialize(model, name, type, position: 0, **options)
50
- _defined_primary_key = model._defined_primary_key
50
+ _declared_primary_key = model._declared_primary_key
51
51
 
52
- name.to_s == _defined_primary_key and raise ArgumentError, "you may not provide a field spec for the primary key #{name.inspect}"
52
+ name.to_s == _declared_primary_key and raise ArgumentError, "you may not provide a field spec for the primary key #{name.inspect}"
53
53
 
54
54
  @model = model
55
55
  @name = name.to_sym
@@ -99,8 +99,8 @@ module DeclareSchema
99
99
 
100
100
  if @type.in?([:text, :string])
101
101
  if ActiveRecord::Base.connection.class.name.match?(/mysql/i)
102
- @options[:charset] ||= model.table_options[:charset] || ::DeclareSchema.default_charset
103
- @options[:collation] ||= model.table_options[:collation] || ::DeclareSchema.default_collation
102
+ @options[:charset] ||= model._table_options&.[](:charset) || ::DeclareSchema.default_charset
103
+ @options[:collation] ||= model._table_options&.[](:collation) || ::DeclareSchema.default_collation
104
104
  else
105
105
  @options.delete(:charset)
106
106
  @options.delete(:collation)
@@ -7,20 +7,20 @@ module DeclareSchema
7
7
  class ForeignKeyDefinition
8
8
  include Comparable
9
9
 
10
- attr_reader :constraint_name, :model, :foreign_key, :foreign_key_name, :options, :on_delete_cascade
10
+ attr_reader :constraint_name, :model, :foreign_key, :foreign_key_name, :parent_table_name, :child_table_name, :options, :on_delete_cascade
11
+
11
12
 
12
13
  def initialize(model, foreign_key, options = {})
13
14
  @model = model
14
15
  @foreign_key = foreign_key.to_s.presence
15
16
  @options = options
16
17
 
17
- @child_table = model.table_name # unless a table rename, which would happen when a class is renamed??
18
+ @child_table_name = model.table_name # unless a table rename, which would happen when a class is renamed??
18
19
  @parent_table_name = options[:parent_table]&.to_s
19
20
  @foreign_key_name = options[:foreign_key]&.to_s || @foreign_key
20
21
 
21
- @constraint_name = options[:constraint_name]&.to_s ||
22
- options[:index_name]&.to_s ||
23
- 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)
24
24
  @on_delete_cascade = options[:dependent] == :delete
25
25
  end
26
26
 
@@ -61,11 +61,6 @@ module DeclareSchema
61
61
  foreign_key.sub(/_id\z/, '').camelize.constantize.table_name
62
62
  end
63
63
 
64
- def to_add_statement
65
- "add_foreign_key(#{@child_table.inspect}, #{parent_table_name.inspect}, " +
66
- "column: #{@foreign_key_name.inspect}, name: #{@constraint_name.inspect})"
67
- end
68
-
69
64
  def <=>(rhs)
70
65
  key <=> rhs.send(:key)
71
66
  end
@@ -75,7 +70,7 @@ module DeclareSchema
75
70
  private
76
71
 
77
72
  def key
78
- @key ||= [@child_table, parent_table_name, @foreign_key_name, @on_delete_cascade].map(&:to_s)
73
+ @key ||= [@child_table_name, parent_table_name, @foreign_key_name, @on_delete_cascade].map(&:to_s)
79
74
  end
80
75
 
81
76
  def hash
@@ -29,7 +29,7 @@ module DeclareSchema
29
29
  @connection = connection
30
30
  end
31
31
 
32
- def table_options
32
+ def _table_options
33
33
  {}
34
34
  end
35
35
 
@@ -47,7 +47,7 @@ module DeclareSchema
47
47
  false # no single-column primary key in database
48
48
  end
49
49
 
50
- def _defined_primary_key
50
+ def _declared_primary_key
51
51
  false # no single-column primary key declared
52
52
  end
53
53
 
@@ -37,7 +37,7 @@ module DeclareSchema
37
37
  def for_model(model, old_table_name = nil)
38
38
  t = old_table_name || model.table_name
39
39
 
40
- primary_key_columns = Array(model.connection.primary_key(t)).presence || sqlite_compound_primary_key(model, t) or
40
+ primary_key_columns = Array(model.connection.primary_key(t)).presence || fallback_find_primary_key(model, t) or
41
41
  raise "could not find primary key for table #{t} in #{model.connection.columns(t).inspect}"
42
42
 
43
43
  primary_key_found = false
@@ -67,8 +67,8 @@ module DeclareSchema
67
67
  private
68
68
 
69
69
  # This is the old approach which is still needed for MySQL in Rails 4 and SQLite
70
- def sqlite_compound_primary_key(model, table)
71
- ActiveRecord::Base.connection.class.name.match?(/SQLite3Adapter/) || Rails::VERSION::MAJOR < 5 or return nil
70
+ def fallback_find_primary_key(model, table)
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
 
@@ -83,9 +83,11 @@ module DeclareSchema
83
83
  end
84
84
  end
85
85
 
86
- pk_index = connection.indexes(table).find { |index| index.name.to_s == PRIMARY_KEY_NAME } or return nil
87
-
88
- Array(pk_index.columns)
86
+ if (pk_index = connection.indexes(table).find { |index| index.name.to_s == PRIMARY_KEY_NAME })
87
+ Array(pk_index.columns)
88
+ elsif model.connection.columns(table).any? { |col| col.name == 'id' }
89
+ ['id']
90
+ end
89
91
  end
90
92
  end
91
93
 
@@ -93,25 +95,6 @@ module DeclareSchema
93
95
  name == PRIMARY_KEY_NAME
94
96
  end
95
97
 
96
- def to_add_statement(new_table_name, existing_primary_key = nil)
97
- if primary_key? && !ActiveRecord::Base.connection.class.name.match?(/SQLite3Adapter/)
98
- to_add_primary_key_statement(new_table_name, existing_primary_key)
99
- else
100
- # Note: + below keeps that interpolated string from being frozen, so we can << into it.
101
- r = +"add_index #{new_table_name.to_sym.inspect}, #{fields.map(&:to_sym).inspect}"
102
- r << ", unique: true" if unique
103
- r << ", where: '#{where}'" if where.present?
104
- r << ", name: '#{name}'"
105
- r
106
- end
107
- end
108
-
109
- def to_add_primary_key_statement(new_table_name, existing_primary_key)
110
- drop = "DROP PRIMARY KEY, " if existing_primary_key
111
- statement = "ALTER TABLE #{new_table_name} #{drop}ADD PRIMARY KEY (#{fields.join(', ')})"
112
- "execute #{statement.inspect}"
113
- end
114
-
115
98
  def to_key
116
99
  @key ||= [table, fields, name, unique, where].map(&:to_s)
117
100
  end