declare_schema 0.6.4 → 0.7.0

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.
@@ -47,6 +47,13 @@ RSpec.describe 'DeclareSchema Migration Generator' do
47
47
  ", id: :integer" unless Rails::VERSION::MAJOR < 5
48
48
  end
49
49
  end
50
+ let(:lock_version_limit) do
51
+ if defined?(Mysql2)
52
+ ", limit: 4"
53
+ else
54
+ ''
55
+ end
56
+ end
50
57
 
51
58
  # DeclareSchema - Migration Generator
52
59
  it 'generates migrations' do
@@ -74,7 +81,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
74
81
  expect(migrations).to(
75
82
  migrate_up(<<~EOS.strip)
76
83
  create_table :adverts, id: :bigint do |t|
77
- t.string :name, limit: 250#{charset_and_collation}
84
+ t.string :name, limit: 250, null: true#{charset_and_collation}
78
85
  end#{charset_alter_table}
79
86
  EOS
80
87
  .and migrate_down("drop_table :adverts")
@@ -107,8 +114,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
107
114
 
108
115
  expect(migrate).to(
109
116
  migrate_up(<<~EOS.strip)
110
- add_column :adverts, :body, :text#{text_limit}#{charset_and_collation}
111
- add_column :adverts, :published_at, :datetime
117
+ add_column :adverts, :body, :text#{text_limit}, null: true#{charset_and_collation}
118
+ add_column :adverts, :published_at, :datetime, null: true
112
119
  EOS
113
120
  .and migrate_down(<<~EOS.strip)
114
121
  remove_column :adverts, :body
@@ -126,7 +133,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
126
133
 
127
134
  expect(migrate).to(
128
135
  migrate_up("remove_column :adverts, :published_at").and(
129
- migrate_down("add_column :adverts, :published_at, :datetime#{datetime_precision}")
136
+ migrate_down("add_column :adverts, :published_at, :datetime#{datetime_precision}, null: true")
130
137
  )
131
138
  )
132
139
 
@@ -140,12 +147,12 @@ RSpec.describe 'DeclareSchema Migration Generator' do
140
147
 
141
148
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
142
149
  migrate_up(<<~EOS.strip)
143
- add_column :adverts, :title, :string, limit: 250#{charset_and_collation}
150
+ add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
144
151
  remove_column :adverts, :name
145
152
  EOS
146
153
  .and migrate_down(<<~EOS.strip)
147
154
  remove_column :adverts, :title
148
- add_column :adverts, :name, :string, limit: 250#{charset_and_collation}
155
+ add_column :adverts, :name, :string, limit: 250, null: true#{charset_and_collation}
149
156
  EOS
150
157
  )
151
158
 
@@ -165,8 +172,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
165
172
  end
166
173
 
167
174
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
168
- migrate_up("change_column :adverts, :title, :text#{text_limit}#{charset_and_collation}").and(
169
- migrate_down("change_column :adverts, :title, :string, limit: 250#{charset_and_collation}")
175
+ migrate_up("change_column :adverts, :title, :text#{text_limit}, null: true#{charset_and_collation}").and(
176
+ migrate_down("change_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}")
170
177
  )
171
178
  )
172
179
 
@@ -179,10 +186,10 @@ RSpec.describe 'DeclareSchema Migration Generator' do
179
186
 
180
187
  expect(migrate).to(
181
188
  migrate_up(<<~EOS.strip)
182
- change_column :adverts, :title, :string, limit: 250, default: "Untitled"#{charset_and_collation}
189
+ change_column :adverts, :title, :string, limit: 250, null: true, default: "Untitled"#{charset_and_collation}
183
190
  EOS
184
191
  .and migrate_down(<<~EOS.strip)
185
- change_column :adverts, :title, :string, limit: 250#{charset_and_collation}
192
+ change_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
186
193
  EOS
187
194
  )
188
195
 
@@ -195,7 +202,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
195
202
  end
196
203
 
197
204
  up, _ = Generators::DeclareSchema::Migration::Migrator.run.tap do |migrations|
198
- expect(migrations).to migrate_up("add_column :adverts, :price, :integer, limit: 2")
205
+ expect(migrations).to migrate_up("add_column :adverts, :price, :integer, limit: 2, null: true")
199
206
  end
200
207
 
201
208
  # Now run the migration, then change the limit:
@@ -209,24 +216,20 @@ RSpec.describe 'DeclareSchema Migration Generator' do
209
216
 
210
217
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
211
218
  migrate_up(<<~EOS.strip)
212
- change_column :adverts, :price, :integer, limit: 3
219
+ change_column :adverts, :price, :integer, limit: 3, null: true
213
220
  EOS
214
221
  .and migrate_down(<<~EOS.strip)
215
- change_column :adverts, :price, :integer, limit: 2
222
+ change_column :adverts, :price, :integer, limit: 2, null: true
216
223
  EOS
217
224
  )
218
225
 
219
- # Note that limit on a decimal column is ignored (use :scale and :precision)
220
-
221
226
  ActiveRecord::Migration.class_eval("remove_column :adverts, :price")
222
227
  class Advert < ActiveRecord::Base
223
228
  fields do
224
- price :decimal, null: true, limit: 4
229
+ price :decimal, precision: 4, scale: 1, null: true
225
230
  end
226
231
  end
227
232
 
228
- expect(Generators::DeclareSchema::Migration::Migrator.run).to migrate_up("add_column :adverts, :price, :decimal")
229
-
230
233
  # Limits are generally not needed for `text` fields, because by default, `text` fields will use the maximum size
231
234
  # allowed for that database type (0xffffffff for LONGTEXT in MySQL unlimited in Postgres, 1 billion in Sqlite).
232
235
  # If a `limit` is given, it will only be used in MySQL, to choose the smallest TEXT field that will accommodate
@@ -245,9 +248,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
245
248
 
246
249
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
247
250
  migrate_up(<<~EOS.strip)
248
- add_column :adverts, :price, :decimal
249
- add_column :adverts, :notes, :text, null: false#{text_limit}#{charset_and_collation}
250
- add_column :adverts, :description, :text, null: false#{', limit: 65535' if defined?(Mysql2)}#{charset_and_collation}
251
+ add_column :adverts, :price, :decimal, precision: 4, scale: 1, null: true
252
+ add_column :adverts, :notes, :text#{text_limit}, null: false#{charset_and_collation}
253
+ add_column :adverts, :description, :text#{', limit: 65535' if defined?(Mysql2)}, null: false#{charset_and_collation}
251
254
  EOS
252
255
  )
253
256
 
@@ -269,8 +272,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
269
272
 
270
273
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
271
274
  migrate_up(<<~EOS.strip)
272
- add_column :adverts, :notes, :text, null: false, limit: 4294967295#{charset_and_collation}
273
- add_column :adverts, :description, :text, null: false, limit: 255#{charset_and_collation}
275
+ add_column :adverts, :notes, :text, limit: 4294967295, null: false#{charset_and_collation}
276
+ add_column :adverts, :description, :text, limit: 255, null: false#{charset_and_collation}
274
277
  EOS
275
278
  )
276
279
 
@@ -313,7 +316,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
313
316
  change_column :adverts, :description, :text, limit: 4294967295, null: false#{charset_and_collation}
314
317
  EOS
315
318
  .and migrate_down(<<~EOS.strip)
316
- change_column :adverts, :description, :text#{', limit: 255' if defined?(Mysql2)}#{charset_and_collation}
319
+ change_column :adverts, :description, :text#{', limit: 255' if defined?(Mysql2)}, null: true#{charset_and_collation}
317
320
  EOS
318
321
  )
319
322
 
@@ -330,7 +333,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
330
333
  change_column :adverts, :description, :text, limit: 4294967295, null: false#{charset_and_collation}
331
334
  EOS
332
335
  .and migrate_down(<<~EOS.strip)
333
- change_column :adverts, :description, :text#{', limit: 255' if defined?(Mysql2)}#{charset_and_collation}
336
+ change_column :adverts, :description, :text#{', limit: 255' if defined?(Mysql2)}, null: true#{charset_and_collation}
334
337
  EOS
335
338
  )
336
339
  end
@@ -461,9 +464,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
461
464
 
462
465
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
463
466
  migrate_up(<<~EOS.strip)
464
- add_column :adverts, :created_at, :datetime
465
- add_column :adverts, :updated_at, :datetime
466
- add_column :adverts, :lock_version, :integer, null: false, default: 1
467
+ add_column :adverts, :created_at, :datetime, null: true
468
+ add_column :adverts, :updated_at, :datetime, null: true
469
+ add_column :adverts, :lock_version, :integer#{lock_version_limit}, null: false, default: 1
467
470
 
468
471
  #{"add_foreign_key(\"adverts\", \"categories\", column: \"category_id\", name: \"index_adverts_on_category_id\")\n" +
469
472
  "add_foreign_key(\"adverts\", \"categories\", column: \"c_id\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
@@ -494,7 +497,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
494
497
 
495
498
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
496
499
  migrate_up(<<~EOS.strip)
497
- add_column :adverts, :title, :string, limit: 250#{charset_and_collation}
500
+ add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
498
501
 
499
502
  add_index :adverts, [:title], name: 'on_title'
500
503
 
@@ -515,7 +518,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
515
518
 
516
519
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
517
520
  migrate_up(<<~EOS.strip)
518
- add_column :adverts, :title, :string, limit: 250#{charset_and_collation}
521
+ add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
519
522
 
520
523
  add_index :adverts, [:title], unique: true, name: 'on_title'
521
524
 
@@ -536,7 +539,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
536
539
 
537
540
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
538
541
  migrate_up(<<~EOS.strip)
539
- add_column :adverts, :title, :string, limit: 250#{charset_and_collation}
542
+ add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
540
543
 
541
544
  add_index :adverts, [:title], name: 'my_index'
542
545
 
@@ -555,7 +558,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
555
558
 
556
559
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
557
560
  migrate_up(<<~EOS.strip)
558
- add_column :adverts, :title, :string, limit: 250#{charset_and_collation}
561
+ add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
559
562
 
560
563
  add_index :adverts, [:title], name: 'on_title'
561
564
 
@@ -574,7 +577,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
574
577
 
575
578
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
576
579
  migrate_up(<<~EOS.strip)
577
- add_column :adverts, :title, :string, limit: 250#{charset_and_collation}
580
+ add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
578
581
 
579
582
  add_index :adverts, [:title], unique: true, name: 'my_index'
580
583
 
@@ -593,7 +596,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
593
596
 
594
597
  expect(Generators::DeclareSchema::Migration::Migrator.run).to(
595
598
  migrate_up(<<~EOS.strip)
596
- add_column :adverts, :title, :string, limit: 250#{charset_and_collation}
599
+ add_column :adverts, :title, :string, limit: 250, null: true#{charset_and_collation}
597
600
 
598
601
  add_index :adverts, [:title, :category_id], name: 'on_title_and_category_id'
599
602
 
@@ -627,8 +630,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
627
630
  migrate_up(<<~EOS.strip)
628
631
  rename_table :adverts, :ads
629
632
 
630
- add_column :ads, :title, :string, limit: 250#{charset_and_collation}
631
- add_column :ads, :body, :text#{', limit: 4294967295' if defined?(Mysql2)}#{charset_and_collation}
633
+ add_column :ads, :title, :string, limit: 250, null: true#{charset_and_collation}
634
+ add_column :ads, :body, :text#{', limit: 4294967295' if defined?(Mysql2)}, null: true#{charset_and_collation}
632
635
 
633
636
  #{if defined?(SQLite3)
634
637
  "add_index :ads, [:id], unique: true, name: 'PRIMARY'\n"
@@ -681,8 +684,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
681
684
  migrate_up(<<~EOS.strip)
682
685
  rename_table :adverts, :advertisements
683
686
 
684
- add_column :advertisements, :title, :string, limit: 250#{charset_and_collation}
685
- add_column :advertisements, :body, :text#{', limit: 4294967295' if defined?(Mysql2)}#{charset_and_collation}
687
+ add_column :advertisements, :title, :string, limit: 250, null: true#{charset_and_collation}
688
+ add_column :advertisements, :body, :text#{', limit: 4294967295' if defined?(Mysql2)}, null: true#{charset_and_collation}
686
689
  remove_column :advertisements, :name
687
690
 
688
691
  #{if defined?(SQLite3)
@@ -694,7 +697,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
694
697
  .and migrate_down(<<~EOS.strip)
695
698
  remove_column :advertisements, :title
696
699
  remove_column :advertisements, :body
697
- add_column :adverts, :name, :string, limit: 250#{charset_and_collation}
700
+ add_column :adverts, :name, :string, limit: 250, null: true#{charset_and_collation}
698
701
 
699
702
  rename_table :advertisements, :adverts
700
703
 
@@ -748,7 +751,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
748
751
  up, _ = Generators::DeclareSchema::Migration::Migrator.run do |migrations|
749
752
  expect(migrations).to(
750
753
  migrate_up(<<~EOS.strip)
751
- add_column :adverts, :type, :string, limit: 250#{charset_and_collation}
754
+ add_column :adverts, :type, :string, limit: 250, null: true#{charset_and_collation}
752
755
 
753
756
  add_index :adverts, [:type], name: 'on_type'
754
757
  EOS
@@ -795,11 +798,11 @@ RSpec.describe 'DeclareSchema Migration Generator' do
795
798
  expect(Generators::DeclareSchema::Migration::Migrator.run(adverts: { title: :name })).to(
796
799
  migrate_up(<<~EOS.strip)
797
800
  rename_column :adverts, :title, :name
798
- change_column :adverts, :name, :string, limit: 250, default: "No Name"#{charset_and_collation}
801
+ change_column :adverts, :name, :string, limit: 250, null: true, default: "No Name"#{charset_and_collation}
799
802
  EOS
800
803
  .and migrate_down(<<~EOS.strip)
801
804
  rename_column :adverts, :name, :title
802
- change_column :adverts, :title, :string, limit: 250, default: "Untitled"#{charset_and_collation}
805
+ change_column :adverts, :title, :string, limit: 250, null: true, default: "Untitled"#{charset_and_collation}
803
806
  EOS
804
807
  )
805
808
 
@@ -0,0 +1,141 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails'
4
+
5
+ begin
6
+ require 'mysql2'
7
+ rescue LoadError
8
+ end
9
+
10
+ require_relative '../../../../lib/declare_schema/model/column'
11
+
12
+ RSpec.describe DeclareSchema::Model::Column do
13
+ before do
14
+ load File.expand_path('../prepare_testapp.rb', __dir__)
15
+ end
16
+
17
+ describe 'class methods' do
18
+ describe '.native_type?' do
19
+ if Rails::VERSION::MAJOR >= 5
20
+ let(:native_types) { [:string, :text, :integer, :float, :decimal, :datetime, :time, :date, :binary, :boolean, :json] }
21
+ else
22
+ let(:native_types) { [:string, :text, :integer, :float, :decimal, :datetime, :time, :date, :binary, :boolean] }
23
+ end
24
+
25
+ it 'is falsey for :primary_key' do
26
+ expect(described_class.native_type?(:primary_key)).to be_falsey
27
+ end
28
+
29
+ it 'is truthy for native types' do
30
+ native_types.each do |type|
31
+ expect(described_class.native_type?(type)).to be_truthy, type.inspect
32
+ end
33
+ end
34
+
35
+ it 'is falsey for other types' do
36
+ [:email, :url].each do |type|
37
+ expect(described_class.native_type?(type)).to be_falsey
38
+ end
39
+ end
40
+ end
41
+
42
+ describe '.native_types' do
43
+ subject { described_class.native_types }
44
+
45
+ it 'returns the native type for :primary_key' do
46
+ expect(subject[:primary_key]).to match(/auto_increment PRIMARY KEY|PRIMARY KEY AUTOINCREMENT NOT NULL/)
47
+ end
48
+
49
+ it 'returns the native type for :string' do
50
+ expect(subject.dig(:string, :name)).to eq('varchar')
51
+ end
52
+
53
+ it 'returns the native type for :integer' do
54
+ expect(subject.dig(:integer, :name)).to match(/int/)
55
+ end
56
+
57
+ it 'returns the native type for :datetime' do
58
+ expect(subject.dig(:datetime, :name)).to eq('datetime')
59
+ end
60
+ end
61
+
62
+ describe '.sql_type' do
63
+ it 'returns the sql type for :string' do
64
+ expect(described_class.sql_type(:string)).to eq(:string)
65
+ end
66
+
67
+ it 'returns the sql type for :integer' do
68
+ expect(described_class.sql_type(:integer)).to match(:integer)
69
+ end
70
+
71
+ it 'returns the sql type for :datetime' do
72
+ expect(described_class.sql_type(:datetime)).to eq(:datetime)
73
+ end
74
+
75
+ it 'raises UnknownSqlType' do
76
+ expect do
77
+ described_class.sql_type(:email)
78
+ end.to raise_exception(::DeclareSchema::UnknownSqlTypeError, /:email for type :email/)
79
+ end
80
+ end
81
+
82
+ describe '.deserialize_default_value' do
83
+ require 'rails'
84
+
85
+ if ::Rails::VERSION::MAJOR >= 5
86
+ it 'deserializes :boolean' do
87
+ expect(described_class.deserialize_default_value(nil, :boolean, 'true')).to eq(true)
88
+ expect(described_class.deserialize_default_value(nil, :boolean, 'false')).to eq(false)
89
+ end
90
+
91
+ it 'deserializes :integer' do
92
+ expect(described_class.deserialize_default_value(nil, :integer, '12')).to eq(12)
93
+ end
94
+
95
+ it 'deserializes :json' do
96
+ expect(described_class.deserialize_default_value(nil, :json, '{}')).to eq({})
97
+ end
98
+ end
99
+ end
100
+ end
101
+
102
+ describe 'instance methods' do
103
+ before do
104
+ class ColumnTestModel < ActiveRecord::Base
105
+ fields do
106
+ title :string, limit: 127, null: false
107
+ count :integer, null: false
108
+ end
109
+ end
110
+ end
111
+ let(:model) { ColumnTestModel }
112
+ let(:current_table_name) { model.table_name }
113
+ let(:column) { double("ActiveRecord Column",
114
+ name: 'count',
115
+ type: :integer,
116
+ limit: nil,
117
+ precision: nil,
118
+ scale: nil,
119
+ type_cast_from_database: nil,
120
+ null: false,
121
+ default: nil,
122
+ sql_type_metadata: {}) }
123
+ subject { described_class.new(model, current_table_name, column) }
124
+
125
+ describe '#sql_type' do
126
+ it 'returns sql type' do
127
+ expect(subject.sql_type).to match(/int/)
128
+ end
129
+ end
130
+
131
+ describe '#schema_attributes' do
132
+ it 'returns a hash with relevant key/values' do
133
+ if defined?(Mysql2)
134
+ expect(subject.schema_attributes).to eq(type: :integer, null: false, limit: 4)
135
+ else
136
+ expect(subject.schema_attributes).to eq(type: :integer, null: false)
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
@@ -18,17 +18,8 @@ module Generators
18
18
  subject { described_class.new }
19
19
 
20
20
  describe 'format_options' do
21
- let(:mysql_longtext_limit) { 0xffff_ffff }
22
- let(:limit_option) do
23
- if defined?(Mysql2)
24
- ["limit: #{mysql_longtext_limit}"]
25
- else
26
- []
27
- end
28
- end
29
-
30
- it 'returns text limits if supported' do
31
- expect(subject.format_options({ limit: mysql_longtext_limit }, :text)).to eq(limit_option)
21
+ it 'returns an array of option .inspect strings, with symbols using the modern : hash notation' do
22
+ expect(subject.format_options({ limit: 4, 'key' => 'value "quoted"' })).to eq(["limit: 4", '"key" => "value \"quoted\""'])
32
23
  end
33
24
  end
34
25
 
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.6.4
4
+ version: 0.7.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-02-08 00:00:00.000000000 Z
11
+ date: 2021-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -61,6 +61,7 @@ files:
61
61
  - lib/declare_schema/extensions/module.rb
62
62
  - lib/declare_schema/field_declaration_dsl.rb
63
63
  - lib/declare_schema/model.rb
64
+ - lib/declare_schema/model/column.rb
64
65
  - lib/declare_schema/model/field_spec.rb
65
66
  - lib/declare_schema/model/foreign_key_definition.rb
66
67
  - lib/declare_schema/model/index_definition.rb
@@ -82,6 +83,7 @@ files:
82
83
  - spec/lib/declare_schema/generator_spec.rb
83
84
  - spec/lib/declare_schema/interactive_primary_key_spec.rb
84
85
  - spec/lib/declare_schema/migration_generator_spec.rb
86
+ - spec/lib/declare_schema/model/column_spec.rb
85
87
  - spec/lib/declare_schema/model/foreign_key_definition_spec.rb
86
88
  - spec/lib/declare_schema/model/index_definition_spec.rb
87
89
  - spec/lib/declare_schema/model/table_options_definition_spec.rb