declare_schema 0.3.0.pre.1 → 0.3.0.pre.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2a82a5e54f5dadf8a733520efe057d8b1a0319e1725ef82d41d97004ee68cf0c
4
- data.tar.gz: 899e9f135706d4883b448bcffc09b5f962cb84892c1bf1be27e6f2e36209454f
3
+ metadata.gz: 3364c39b6579bb69804417a10b5b287208e6fdfe5a23bf57b6b3f928f69fe539
4
+ data.tar.gz: 2fbc6abab32d750ef1af34d6888b5954ee27da2563acd8595728d3d23d184244
5
5
  SHA512:
6
- metadata.gz: 0465ffd5522605f9e0c61beffc2571ab76d077020596d82fe616c60cd631c6663b399c9c5d02c8983d9060223f3bb100932baf411efa69a3a7f236097784ae9e
7
- data.tar.gz: 4c6b7b49067650cd0791257614dc84e1f6f6a9191346578249fa196dc0d9f621c491d597aaa32f82c64bde8f6e7fe8e18f1974f89c4677ade6c8203aede20ec4
6
+ metadata.gz: 7e884c085c0d72f1146d7b7f52abbf1f44b19389e2f96e2858b855b11041c94406b0be159a300366d0abeec2f36cf9056d3eddd61352600c780a992ca3971817
7
+ data.tar.gz: 1d45f652bd8d0de74b71fec913c4005be8f3212de7fa7942ad587d67059f67115ce8d797be9d71121e792ab66aee84c2adaaa013766b6d96cb21eda1d35522b4
@@ -6,10 +6,8 @@ Note: this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0
6
6
 
7
7
  ## [0.3.0] - Unreleased
8
8
  ### Added
9
- - Added support for `belongs_to optional:`.
10
- If given, it is passed through to `ActiveRecord`'s `belong_to`.
11
- If not given in Rails 5+, the `optional:` value is set equal to the `null:` value (default: `false`) and that
12
- is is passed to `ActiveRecord`'s `belong_to`.
9
+ - Added a new callback `before_generating_migration` to the `Migrator` that can be
10
+ defined in order to custom load more models that might be missed by `eager_load!`
13
11
 
14
12
  ## [0.2.0] - 2020-10-26
15
13
  ### Added
@@ -20,7 +18,7 @@ is is passed to `ActiveRecord`'s `belong_to`.
20
18
 
21
19
  ### Fixed
22
20
  - Fixed a bug where `:text limit: 0xffff_ffff` (max size) was omitted from migrations.
23
- - Fixed a bug where `:bigint` foreign keys were omitted from the migration.
21
+ - Fixed a bug where `:bigint` foreign keys were omitted from the migration.
24
22
 
25
23
  ## [0.1.3] - 2020-10-08
26
24
  ### Changed
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- declare_schema (0.3.0.pre.1)
4
+ declare_schema (0.3.0.pre.2)
5
5
  rails (>= 4.2)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -50,6 +50,26 @@ Migration filename: [<enter>=declare_schema_migration_1|<custom_name>]: add_comp
50
50
  ```
51
51
  Note that the migration generator is interactive -- it can't tell the difference between renaming something vs. adding one thing and removing another, so sometimes it will ask you to clarify.
52
52
 
53
+ ## Migrator Configuration
54
+
55
+ The following configuration options are available for the gem and can be used
56
+ during the initialization of your Rails application.
57
+
58
+ ### before_generating_migration callback
59
+
60
+ During the initializtion process for generating migrations, `DeclareSchema` will
61
+ trigger the `eager_load!` on the `Rails` application and all `Rails::Engine`s loaded
62
+ into scope. If you need to generate migrations for models that aren't automatically loaded by `eager_load!`,
63
+ load them in the `before_generating_migration` block.
64
+
65
+ **Example Configuration**
66
+
67
+ ```ruby
68
+ DeclareSchema::Migration::Migrator.before_generating_migration do
69
+ require 'lib/some/hidden/models.rb'
70
+ end
71
+ ```
72
+
53
73
  ## Installing
54
74
 
55
75
  Install the `DeclareSchema` gem directly:
@@ -106,7 +106,15 @@ module DeclareSchema
106
106
  end
107
107
 
108
108
  # Extend belongs_to so that it creates a FieldSpec for the foreign key
109
- def belongs_to(name, scope = nil, **options, &block)
109
+ def belongs_to(name, *args, &block)
110
+ if args.size == 0 || (args.size == 1 && args[0].is_a?(Proc))
111
+ options = {}
112
+ args.push(options)
113
+ elsif args.size == 1
114
+ options = args[0]
115
+ else
116
+ options = args[1]
117
+ end
110
118
  column_options = {}
111
119
  column_options[:null] = options.delete(:null) || false
112
120
  column_options[:default] = options.delete(:default) if options.has_key?(:default)
@@ -121,27 +129,20 @@ module DeclareSchema
121
129
  fk_options[:constraint_name] = options.delete(:constraint) if options.has_key?(:constraint)
122
130
  fk_options[:index_name] = index_options[:name]
123
131
 
124
- fk = options[:foreign_key]&.to_s || "#{name}_id"
125
-
126
- if !options.has_key?(:optional) && Rails::VERSION::MAJOR >= 5
127
- options[:optional] = column_options[:null]
128
- end
129
-
130
132
  fk_options[:dependent] = options.delete(:far_end_dependent) if options.has_key?(:far_end_dependent)
131
-
132
- super(name, scope, options)
133
-
134
- refl = reflections[name.to_s] or raise "Couldn't find reflection #{name} in #{reflections.keys}"
135
- fkey = refl.foreign_key or raise "Couldn't find foreign_key for #{name} in #{refl.inspect}"
136
- declare_field(fkey.to_sym, :integer, column_options)
137
- if refl.options[:polymorphic]
138
- foreign_type = options[:foreign_type] || "#{name}_type"
139
- declare_polymorphic_type_field(foreign_type, column_options)
140
- index([foreign_type, fkey], index_options) if index_options[:name] != false
141
- else
142
- index(fkey, index_options) if index_options[:name] != false
143
- options[:constraint_name] = options
144
- constraint(fkey, fk_options) if fk_options[:constraint_name] != false
133
+ super(name, *args, &block).tap do |_bt|
134
+ refl = reflections[name.to_s] or raise "Couldn't find reflection #{name} in #{reflections.keys}"
135
+ fkey = refl.foreign_key
136
+ declare_field(fkey.to_sym, :integer, column_options)
137
+ if refl.options[:polymorphic]
138
+ foreign_type = options[:foreign_type] || "#{name}_type"
139
+ declare_polymorphic_type_field(foreign_type, column_options)
140
+ index([foreign_type, fkey], index_options) if index_options[:name] != false
141
+ else
142
+ index(fkey, index_options) if index_options[:name] != false
143
+ options[:constraint_name] = options
144
+ constraint(fkey, fk_options) if fk_options[:constraint_name] != false
145
+ end
145
146
  end
146
147
  end
147
148
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeclareSchema
4
- VERSION = "0.3.0.pre.1"
4
+ VERSION = "0.3.0.pre.2"
5
5
  end
@@ -69,12 +69,14 @@ module Generators
69
69
  class Migrator
70
70
  class Error < RuntimeError; end
71
71
 
72
- @ignore_models = []
73
- @ignore_tables = []
74
- @active_record_class = ActiveRecord::Base
72
+ @ignore_models = []
73
+ @ignore_tables = []
74
+ @before_generating_migration_callback = nil
75
+ @active_record_class = ActiveRecord::Base
75
76
 
76
77
  class << self
77
78
  attr_accessor :ignore_models, :ignore_tables, :disable_indexing, :disable_constraints, :active_record_class
79
+ attr_reader :before_generating_migration_callback
78
80
 
79
81
  def active_record_class
80
82
  @active_record_class.is_a?(Class) or @active_record_class = @active_record_class.to_s.constantize
@@ -110,6 +112,11 @@ module Generators
110
112
  def native_types
111
113
  @native_types ||= fix_native_types(connection.native_database_types)
112
114
  end
115
+
116
+ def before_generating_migration(&block)
117
+ block or raise ArgumentError, 'A block is required when setting the before_generating_migration callback'
118
+ @before_generating_migration_callback = block
119
+ end
113
120
  end
114
121
 
115
122
  def initialize(ambiguity_resolver = {})
@@ -120,14 +127,12 @@ module Generators
120
127
 
121
128
  attr_accessor :renames
122
129
 
123
- # TODO: Add an application callback (maybe an initializer in a special group?) that
124
- # the application can use to load other models that live in the database, to support DeclareSchema migrations
125
- # for them.
126
130
  def load_rails_models
127
131
  ActiveRecord::Migration.verbose = false
128
132
 
129
133
  Rails.application.eager_load!
130
134
  Rails::Engine.subclasses.each(&:eager_load!)
135
+ self.class.before_generating_migration_callback&.call
131
136
  end
132
137
 
133
138
  # Returns an array of model classes that *directly* extend
@@ -129,7 +129,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
129
129
  end
130
130
  end
131
131
 
132
- up = Generators::DeclareSchema::Migration::Migrator.run.first
132
+ up, down = Generators::DeclareSchema::Migration::Migrator.run
133
133
  expect(up).to eq("add_column :adverts, :price, :integer, limit: 2")
134
134
 
135
135
  # Now run the migration, then change the limit:
@@ -154,7 +154,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
154
154
  end
155
155
  end
156
156
 
157
- up = Generators::DeclareSchema::Migration::Migrator.run.first
157
+ up, down = Generators::DeclareSchema::Migration::Migrator.run
158
158
  expect(up).to eq("add_column :adverts, :price, :decimal")
159
159
 
160
160
  # Limits are generally not needed for `text` fields, because by default, `text` fields will use the maximum size
@@ -170,7 +170,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
170
170
  end
171
171
  end
172
172
 
173
- up = Generators::DeclareSchema::Migration::Migrator.run.first
173
+ up, down = Generators::DeclareSchema::Migration::Migrator.run
174
174
  expect(up).to eq(<<~EOS.strip)
175
175
  add_column :adverts, :price, :decimal
176
176
  add_column :adverts, :notes, :text, null: false
@@ -194,7 +194,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
194
194
  end
195
195
  end
196
196
 
197
- up = Generators::DeclareSchema::Migration::Migrator.run.first
197
+ up, down = Generators::DeclareSchema::Migration::Migrator.run
198
198
  expect(up).to eq(<<~EOS.strip)
199
199
  add_column :adverts, :notes, :text, null: false, limit: 4294967295
200
200
  add_column :adverts, :description, :text, null: false, limit: 255
@@ -266,7 +266,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
266
266
  end
267
267
  end
268
268
 
269
- up = Generators::DeclareSchema::Migration::Migrator.run.first
269
+ up, down = Generators::DeclareSchema::Migration::Migrator.run
270
270
  ActiveRecord::Migration.class_eval up
271
271
  Advert.connection.schema_cache.clear!
272
272
  Advert.reset_column_information
@@ -278,9 +278,6 @@ RSpec.describe 'DeclareSchema Migration Generator' do
278
278
 
279
279
  class Category < ActiveRecord::Base; end
280
280
  class Advert < ActiveRecord::Base
281
- fields do
282
- name :string, limit: 255, null: true
283
- end
284
281
  belongs_to :category
285
282
  end
286
283
 
@@ -301,10 +298,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
301
298
 
302
299
  class Category < ActiveRecord::Base; end
303
300
  class Advert < ActiveRecord::Base
304
- fields { }
305
301
  belongs_to :category, foreign_key: "c_id", class_name: 'Category'
306
302
  end
307
- up = Generators::DeclareSchema::Migration::Migrator.run.first
303
+ up, down = Generators::DeclareSchema::Migration::Migrator.run
308
304
  expect(up.gsub(/\n+/, "\n")).to eq(<<~EOS.strip)
309
305
  add_column :adverts, :c_id, :integer, limit: 8, null: false
310
306
  add_index :adverts, [:c_id], name: 'on_c_id'
@@ -317,10 +313,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
317
313
 
318
314
  class Category < ActiveRecord::Base; end
319
315
  class Advert < ActiveRecord::Base
320
- fields { }
321
316
  belongs_to :category, index: false
322
317
  end
323
- up = Generators::DeclareSchema::Migration::Migrator.run.first
318
+ up, down = Generators::DeclareSchema::Migration::Migrator.run
324
319
  expect(up.gsub(/\n+/, "\n")).to eq("add_column :adverts, :category_id, :integer, limit: 8, null: false")
325
320
 
326
321
  Advert.field_specs.delete(:category_id)
@@ -330,10 +325,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
330
325
 
331
326
  class Category < ActiveRecord::Base; end
332
327
  class Advert < ActiveRecord::Base
333
- fields { }
334
328
  belongs_to :category, index: 'my_index'
335
329
  end
336
- up = Generators::DeclareSchema::Migration::Migrator.run.first
330
+ up, down = Generators::DeclareSchema::Migration::Migrator.run
337
331
  expect(up.gsub(/\n+/, "\n")).to eq(<<~EOS.strip)
338
332
  add_column :adverts, :category_id, :integer, limit: 8, null: false
339
333
  add_index :adverts, [:category_id], name: 'my_index'
@@ -378,7 +372,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
378
372
  title :string, index: true, limit: 255, null: true
379
373
  end
380
374
  end
381
- up = Generators::DeclareSchema::Migration::Migrator.run.first
375
+ up, down = Generators::DeclareSchema::Migration::Migrator.run
382
376
  expect(up.gsub(/\n+/, "\n")).to eq(<<~EOS.strip)
383
377
  add_column :adverts, :title, :string, limit: 255
384
378
  add_index :adverts, [:title], name: 'on_title'
@@ -393,7 +387,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
393
387
  title :string, index: true, unique: true, null: true, limit: 255
394
388
  end
395
389
  end
396
- up = Generators::DeclareSchema::Migration::Migrator.run.first
390
+ up, down = Generators::DeclareSchema::Migration::Migrator.run
397
391
  expect(up.gsub(/\n+/, "\n")).to eq(<<~EOS.strip)
398
392
  add_column :adverts, :title, :string, limit: 255
399
393
  add_index :adverts, [:title], unique: true, name: 'on_title'
@@ -408,20 +402,20 @@ RSpec.describe 'DeclareSchema Migration Generator' do
408
402
  title :string, index: 'my_index', limit: 255, null: true
409
403
  end
410
404
  end
411
- up = Generators::DeclareSchema::Migration::Migrator.run.first
405
+ up, down = Generators::DeclareSchema::Migration::Migrator.run
412
406
  expect(up.gsub(/\n+/, "\n")).to eq(<<~EOS.strip)
413
407
  add_column :adverts, :title, :string, limit: 255
414
408
  add_index :adverts, [:title], name: 'my_index'
415
409
  EOS
416
410
 
417
- Advert.index_specs.delete_if { |spec| spec.fields==["title"] }
411
+ Advert.index_specs.delete_if {|spec| spec.fields==["title"]}
418
412
 
419
413
  # You can ask for an index outside of the fields block
420
414
 
421
415
  class Advert < ActiveRecord::Base
422
416
  index :title
423
417
  end
424
- up = Generators::DeclareSchema::Migration::Migrator.run.first
418
+ up, down = Generators::DeclareSchema::Migration::Migrator.run
425
419
  expect(up.gsub(/\n+/, "\n")).to eq(<<~EOS.strip)
426
420
  add_column :adverts, :title, :string, limit: 255
427
421
  add_index :adverts, [:title], name: 'on_title'
@@ -434,7 +428,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
434
428
  class Advert < ActiveRecord::Base
435
429
  index :title, unique: true, name: 'my_index'
436
430
  end
437
- up = Generators::DeclareSchema::Migration::Migrator.run.first
431
+ up, down = Generators::DeclareSchema::Migration::Migrator.run
438
432
  expect(up.gsub(/\n+/, "\n")).to eq(<<~EOS.strip)
439
433
  add_column :adverts, :title, :string, limit: 255
440
434
  add_index :adverts, [:title], unique: true, name: 'my_index'
@@ -447,7 +441,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
447
441
  class Advert < ActiveRecord::Base
448
442
  index [:title, :category_id]
449
443
  end
450
- up = Generators::DeclareSchema::Migration::Migrator.run.first
444
+ up, down = Generators::DeclareSchema::Migration::Migrator.run
451
445
  expect(up.gsub(/\n+/, "\n")).to eq(<<~EOS.strip)
452
446
  add_column :adverts, :title, :string, limit: 255
453
447
  add_index :adverts, [:title, :category_id], name: 'on_title_and_category_id'
@@ -550,7 +544,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
550
544
  title :string, default: "Untitled", limit: 255, null: true
551
545
  end
552
546
  end
553
- up = Generators::DeclareSchema::Migration::Migrator.run.first
547
+ up, down = Generators::DeclareSchema::Migration::Migrator.run
554
548
  ActiveRecord::Migration.class_eval(up)
555
549
 
556
550
  class FancyAdvert < Advert
@@ -619,7 +613,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
619
613
  created_at :datetime
620
614
  end
621
615
  end
622
- up = Generators::DeclareSchema::Migration::Migrator.run(adverts: :ads).first
616
+ up, down = Generators::DeclareSchema::Migration::Migrator.run(adverts: :ads)
623
617
  expect(up.gsub(/\n+/, "\n")).to eq(<<~EOS.strip)
624
618
  rename_table :adverts, :ads
625
619
  add_column :ads, :created_at, :datetime, null: false
@@ -688,48 +682,5 @@ RSpec.describe 'DeclareSchema Migration Generator' do
688
682
  ActiveRecord::Migration.class_eval(up)
689
683
  expect(Ad.field_specs['company'].options[:validates].inspect).to eq("{:presence=>true, :uniqueness=>{:case_sensitive=>false}}")
690
684
  end
691
-
692
- describe 'belongs_to' do
693
- before do
694
- unless defined?(AdCategory)
695
- class AdCategory < ActiveRecord::Base
696
- fields { }
697
- end
698
- end
699
-
700
- class Advert < ActiveRecord::Base
701
- fields do
702
- name :string, limit: 255, null: true
703
- category_id :integer, limit: 8
704
- nullable_category_id :integer, limit: 8, null: true
705
- end
706
- end
707
- up = Generators::DeclareSchema::Migration::Migrator.run.first
708
- ActiveRecord::Migration.class_eval(up)
709
- end
710
-
711
- it 'passes through optional:, if given' do
712
- class AdvertBelongsTo < ActiveRecord::Base
713
- self.table_name = 'adverts'
714
- fields { }
715
- reset_column_information
716
- belongs_to :ad_category, optional: true
717
- end
718
- expect(AdvertBelongsTo.reflections['ad_category'].options).to eq(optional: true)
719
- end
720
-
721
- [false, true].each do |nullable|
722
- context "nullable=#{nullable}" do
723
- it 'infers the optional: argument from null: option' do
724
- eval <<~EOS
725
- class AdvertBelongsTo < ActiveRecord::Base
726
- fields { }
727
- belongs_to :ad_category, null: #{nullable}
728
- end
729
- EOS
730
- expect(AdvertBelongsTo.reflections['ad_category'].options).to eq(optional: nullable)
731
- end
732
- end
733
- end
734
- end
735
685
  end
686
+
@@ -18,8 +18,6 @@ require "#{TESTAPP_PATH}/config/environment"
18
18
  require 'rails/generators'
19
19
  Rails::Generators.configure!(Rails.application.config.generators)
20
20
 
21
- ActiveRecord::Base.connection.schema_cache.clear!
22
-
23
21
  (ActiveRecord::Base.connection.tables - Generators::DeclareSchema::Migration::Migrator.always_ignore_tables).each do |table|
24
22
  ActiveRecord::Base.connection.execute("DROP TABLE #{ActiveRecord::Base.connection.quote_table_name(table)}")
25
23
  end
@@ -36,6 +36,36 @@ module Generators
36
36
  end
37
37
  end
38
38
  end
39
+
40
+ describe '#before_generating_migration' do
41
+ it 'requires a block be passed' do
42
+ expect { described_class.before_generating_migration }.to raise_error(ArgumentError, 'A block is required when setting the before_generating_migration callback')
43
+ end
44
+ end
45
+
46
+ describe 'load_rails_models' do
47
+ before do
48
+ expect(Rails.application).to receive(:eager_load!)
49
+ expect(Rails::Engine).to receive(:subclasses).and_return([])
50
+ end
51
+
52
+ subject { described_class.new.load_rails_models }
53
+
54
+ context 'when a before_generating_migration callback is configured' do
55
+ let(:dummy_proc) { -> {} }
56
+
57
+ before do
58
+ described_class.before_generating_migration(&dummy_proc)
59
+ expect(dummy_proc).to receive(:call).and_return(true)
60
+ end
61
+
62
+ it { should be_truthy }
63
+ end
64
+
65
+ context 'when no before_generating_migration callback is configured' do
66
+ it { should be_nil }
67
+ end
68
+ end
39
69
  end
40
70
  end
41
71
  end
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.3.0.pre.1
4
+ version: 0.3.0.pre.2
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: 2020-10-30 00:00:00.000000000 Z
11
+ date: 2020-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -84,7 +84,7 @@ homepage: https://github.com/Invoca/declare_schema
84
84
  licenses: []
85
85
  metadata:
86
86
  allowed_push_host: https://rubygems.org
87
- post_install_message:
87
+ post_install_message:
88
88
  rdoc_options: []
89
89
  require_paths:
90
90
  - lib
@@ -100,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
100
  version: 1.3.6
101
101
  requirements: []
102
102
  rubygems_version: 3.0.3
103
- signing_key:
103
+ signing_key:
104
104
  specification_version: 4
105
105
  summary: Database migration generator for Rails
106
106
  test_files: []