declare_schema 0.10.1 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (29) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +23 -0
  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 +2 -3
  11. data/lib/declare_schema/model/habtm_model_shim.rb +2 -2
  12. data/lib/declare_schema/model/index_definition.rb +8 -6
  13. data/lib/declare_schema/schema_change/column_add.rb +3 -3
  14. data/lib/declare_schema/version.rb +1 -1
  15. data/lib/generators/declare_schema/migration/USAGE +14 -24
  16. data/lib/generators/declare_schema/migration/migration_generator.rb +40 -38
  17. data/lib/generators/declare_schema/migration/migrator.rb +18 -14
  18. data/lib/generators/declare_schema/migration/templates/migration.rb.erb +3 -3
  19. data/spec/lib/declare_schema/api_spec.rb +2 -4
  20. data/spec/lib/declare_schema/field_spec_spec.rb +3 -3
  21. data/spec/lib/declare_schema/generator_spec.rb +2 -2
  22. data/spec/lib/declare_schema/interactive_primary_key_spec.rb +8 -11
  23. data/spec/lib/declare_schema/migration_generator_spec.rb +352 -130
  24. data/spec/lib/declare_schema/model/column_spec.rb +2 -6
  25. data/spec/lib/declare_schema/model/foreign_key_definition_spec.rb +32 -8
  26. data/spec/lib/declare_schema/model/habtm_model_shim_spec.rb +4 -6
  27. data/spec/lib/declare_schema/model/index_definition_spec.rb +4 -4
  28. data/spec/spec_helper.rb +1 -1
  29. metadata +2 -2
@@ -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
@@ -57,7 +55,7 @@ RSpec.describe DeclareSchema::Model::HabtmModelShim do
57
55
 
58
56
  describe '#table_options' do
59
57
  it 'returns empty hash' do
60
- expect(subject.table_options).to eq({})
58
+ expect(subject._table_options).to eq({})
61
59
  end
62
60
  end
63
61
 
@@ -88,13 +86,13 @@ RSpec.describe DeclareSchema::Model::HabtmModelShim do
88
86
 
89
87
  describe '#primary_key' do
90
88
  it 'returns false' do
91
- expect(subject._defined_primary_key).to eq(false)
89
+ expect(subject._declared_primary_key).to eq(false)
92
90
  end
93
91
  end
94
92
 
95
- describe '#_defined_primary_key' do
93
+ describe '#_declared_primary_key' do
96
94
  it 'returns false' do
97
- expect(subject._defined_primary_key).to eq(false)
95
+ expect(subject._declared_primary_key).to eq(false)
98
96
  end
99
97
  end
100
98
 
@@ -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.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Invoca Development adapted from hobo_fields by Tom Locke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-18 00:00:00.000000000 Z
11
+ date: 2021-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails