declare_schema 0.7.0 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -3
- data/Gemfile.lock +51 -49
- data/lib/declare_schema/field_declaration_dsl.rb +3 -4
- data/lib/declare_schema/model.rb +1 -2
- data/lib/declare_schema/model/column.rb +26 -27
- data/lib/declare_schema/model/field_spec.rb +2 -2
- data/lib/declare_schema/version.rb +1 -1
- data/lib/generators/declare_schema/migration/migrator.rb +4 -4
- data/spec/lib/declare_schema/field_spec_spec.rb +9 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6317ef2d7c038120278bbff6547c042a3418369d1e0bd8527f10ab57d58aef62
|
4
|
+
data.tar.gz: 66ce3ad03f6fde8365b287deb682d85b4fd7e983d283447af769aed0ade61571
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1a87ecf0479b94324f43c42d309f254e8a3ca10f3766cc90a756b94505cb559406871eee47a58fa376640ad85acda3e81d771cb03ab92dc08f5253d81bd88079
|
7
|
+
data.tar.gz: 4cd449cb9543819dd78e51267357f676a7439c89f2ad021f59a5d0f49664a8c8b8141eeebde4184e3eaf1f3f616dfef8a7092f59cc6382c73504de5ffeb41dc3
|
data/CHANGELOG.md
CHANGED
@@ -4,16 +4,22 @@ 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.7.
|
7
|
+
## [0.7.1] - 2021-02-17
|
8
|
+
### Fixed
|
9
|
+
- Exclude unknown options from FieldSpec#sql_options and #schema_attributes.
|
10
|
+
- Fixed a bug where fk_field_options were getting merged into spec_attrs after checking for equivalence,
|
11
|
+
leading to phantom migrations with no changes, or missing migrations when just the fk_field_options changed.
|
12
|
+
|
13
|
+
## [0.7.0] - 2021-02-14
|
8
14
|
### Changed
|
9
15
|
- Use `schema_attributes` for generating both up and down change migrations, so they are guaranteed to be symmetrical.
|
10
16
|
Note: Rails schema dumper is still used for the down migration to replace a model that has been dropped.
|
11
17
|
|
12
|
-
## [0.6.4] -
|
18
|
+
## [0.6.4] - 2021-02-08
|
13
19
|
- Fixed a bug where the generated call to add_foreign_key() was not setting `column:`,
|
14
20
|
so it only worked in cases where Rails could infer the foreign key by convention.
|
15
21
|
|
16
|
-
## [0.6.3] -
|
22
|
+
## [0.6.3] - 2021-01-21
|
17
23
|
### Added
|
18
24
|
- Added `add_foreign_key` native rails call in `DeclareSchema::Model::ForeignKeyDefinition#to_add_statement`.
|
19
25
|
|
@@ -119,6 +125,7 @@ using the appropriate Rails configuration attributes.
|
|
119
125
|
### Added
|
120
126
|
- Initial version from https://github.com/Invoca/hobo_fields v4.1.0.
|
121
127
|
|
128
|
+
[0.7.1]: https://github.com/Invoca/declare_schema/compare/v0.7.0...v0.7.1
|
122
129
|
[0.7.0]: https://github.com/Invoca/declare_schema/compare/v0.6.3...v0.7.0
|
123
130
|
[0.6.4]: https://github.com/Invoca/declare_schema/compare/v0.6.3...v0.6.4
|
124
131
|
[0.6.3]: https://github.com/Invoca/declare_schema/compare/v0.6.2...v0.6.3
|
data/Gemfile.lock
CHANGED
@@ -1,49 +1,49 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
declare_schema (0.7.
|
4
|
+
declare_schema (0.7.1)
|
5
5
|
rails (>= 4.2)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
actioncable (5.2.4.
|
11
|
-
actionpack (= 5.2.4.
|
10
|
+
actioncable (5.2.4.5)
|
11
|
+
actionpack (= 5.2.4.5)
|
12
12
|
nio4r (~> 2.0)
|
13
13
|
websocket-driver (>= 0.6.1)
|
14
|
-
actionmailer (5.2.4.
|
15
|
-
actionpack (= 5.2.4.
|
16
|
-
actionview (= 5.2.4.
|
17
|
-
activejob (= 5.2.4.
|
14
|
+
actionmailer (5.2.4.5)
|
15
|
+
actionpack (= 5.2.4.5)
|
16
|
+
actionview (= 5.2.4.5)
|
17
|
+
activejob (= 5.2.4.5)
|
18
18
|
mail (~> 2.5, >= 2.5.4)
|
19
19
|
rails-dom-testing (~> 2.0)
|
20
|
-
actionpack (5.2.4.
|
21
|
-
actionview (= 5.2.4.
|
22
|
-
activesupport (= 5.2.4.
|
20
|
+
actionpack (5.2.4.5)
|
21
|
+
actionview (= 5.2.4.5)
|
22
|
+
activesupport (= 5.2.4.5)
|
23
23
|
rack (~> 2.0, >= 2.0.8)
|
24
24
|
rack-test (>= 0.6.3)
|
25
25
|
rails-dom-testing (~> 2.0)
|
26
26
|
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
27
|
-
actionview (5.2.4.
|
28
|
-
activesupport (= 5.2.4.
|
27
|
+
actionview (5.2.4.5)
|
28
|
+
activesupport (= 5.2.4.5)
|
29
29
|
builder (~> 3.1)
|
30
30
|
erubi (~> 1.4)
|
31
31
|
rails-dom-testing (~> 2.0)
|
32
32
|
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
33
|
-
activejob (5.2.4.
|
34
|
-
activesupport (= 5.2.4.
|
33
|
+
activejob (5.2.4.5)
|
34
|
+
activesupport (= 5.2.4.5)
|
35
35
|
globalid (>= 0.3.6)
|
36
|
-
activemodel (5.2.4.
|
37
|
-
activesupport (= 5.2.4.
|
38
|
-
activerecord (5.2.4.
|
39
|
-
activemodel (= 5.2.4.
|
40
|
-
activesupport (= 5.2.4.
|
36
|
+
activemodel (5.2.4.5)
|
37
|
+
activesupport (= 5.2.4.5)
|
38
|
+
activerecord (5.2.4.5)
|
39
|
+
activemodel (= 5.2.4.5)
|
40
|
+
activesupport (= 5.2.4.5)
|
41
41
|
arel (>= 9.0)
|
42
|
-
activestorage (5.2.4.
|
43
|
-
actionpack (= 5.2.4.
|
44
|
-
activerecord (= 5.2.4.
|
42
|
+
activestorage (5.2.4.5)
|
43
|
+
actionpack (= 5.2.4.5)
|
44
|
+
activerecord (= 5.2.4.5)
|
45
45
|
marcel (~> 0.3.1)
|
46
|
-
activesupport (5.2.4.
|
46
|
+
activesupport (5.2.4.5)
|
47
47
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
48
48
|
i18n (>= 0.7, < 2)
|
49
49
|
minitest (~> 5.1)
|
@@ -60,19 +60,19 @@ GEM
|
|
60
60
|
byebug (11.1.3)
|
61
61
|
climate_control (0.2.0)
|
62
62
|
coderay (1.1.3)
|
63
|
-
concurrent-ruby (1.1.
|
63
|
+
concurrent-ruby (1.1.8)
|
64
64
|
crass (1.0.6)
|
65
65
|
diff-lcs (1.4.4)
|
66
|
-
erubi (1.
|
66
|
+
erubi (1.10.0)
|
67
67
|
ffi (1.14.2)
|
68
68
|
globalid (0.4.2)
|
69
69
|
activesupport (>= 4.2.0)
|
70
|
-
i18n (1.8.
|
70
|
+
i18n (1.8.9)
|
71
71
|
concurrent-ruby (~> 1.0)
|
72
72
|
listen (3.4.1)
|
73
73
|
rb-fsevent (~> 0.10, >= 0.10.3)
|
74
74
|
rb-inotify (~> 0.9, >= 0.9.10)
|
75
|
-
loofah (2.
|
75
|
+
loofah (2.9.0)
|
76
76
|
crass (~> 1.0.2)
|
77
77
|
nokogiri (>= 1.5.9)
|
78
78
|
mail (2.7.1)
|
@@ -82,12 +82,13 @@ GEM
|
|
82
82
|
method_source (1.0.0)
|
83
83
|
mimemagic (0.3.5)
|
84
84
|
mini_mime (1.0.2)
|
85
|
-
mini_portile2 (2.
|
86
|
-
minitest (5.14.
|
85
|
+
mini_portile2 (2.5.0)
|
86
|
+
minitest (5.14.3)
|
87
87
|
msgpack (1.4.2)
|
88
|
-
nio4r (2.5.
|
89
|
-
nokogiri (1.
|
90
|
-
mini_portile2 (~> 2.
|
88
|
+
nio4r (2.5.5)
|
89
|
+
nokogiri (1.11.1)
|
90
|
+
mini_portile2 (~> 2.5.0)
|
91
|
+
racc (~> 1.4)
|
91
92
|
parallel (1.19.2)
|
92
93
|
parser (2.7.1.4)
|
93
94
|
ast (~> 2.4.1)
|
@@ -97,35 +98,36 @@ GEM
|
|
97
98
|
pry-byebug (3.9.0)
|
98
99
|
byebug (~> 11.0)
|
99
100
|
pry (~> 0.13.0)
|
101
|
+
racc (1.5.2)
|
100
102
|
rack (2.2.3)
|
101
103
|
rack-test (1.1.0)
|
102
104
|
rack (>= 1.0, < 3)
|
103
|
-
rails (5.2.4.
|
104
|
-
actioncable (= 5.2.4.
|
105
|
-
actionmailer (= 5.2.4.
|
106
|
-
actionpack (= 5.2.4.
|
107
|
-
actionview (= 5.2.4.
|
108
|
-
activejob (= 5.2.4.
|
109
|
-
activemodel (= 5.2.4.
|
110
|
-
activerecord (= 5.2.4.
|
111
|
-
activestorage (= 5.2.4.
|
112
|
-
activesupport (= 5.2.4.
|
105
|
+
rails (5.2.4.5)
|
106
|
+
actioncable (= 5.2.4.5)
|
107
|
+
actionmailer (= 5.2.4.5)
|
108
|
+
actionpack (= 5.2.4.5)
|
109
|
+
actionview (= 5.2.4.5)
|
110
|
+
activejob (= 5.2.4.5)
|
111
|
+
activemodel (= 5.2.4.5)
|
112
|
+
activerecord (= 5.2.4.5)
|
113
|
+
activestorage (= 5.2.4.5)
|
114
|
+
activesupport (= 5.2.4.5)
|
113
115
|
bundler (>= 1.3.0)
|
114
|
-
railties (= 5.2.4.
|
116
|
+
railties (= 5.2.4.5)
|
115
117
|
sprockets-rails (>= 2.0.0)
|
116
118
|
rails-dom-testing (2.0.3)
|
117
119
|
activesupport (>= 4.2.0)
|
118
120
|
nokogiri (>= 1.6)
|
119
121
|
rails-html-sanitizer (1.3.0)
|
120
122
|
loofah (~> 2.3)
|
121
|
-
railties (5.2.4.
|
122
|
-
actionpack (= 5.2.4.
|
123
|
-
activesupport (= 5.2.4.
|
123
|
+
railties (5.2.4.5)
|
124
|
+
actionpack (= 5.2.4.5)
|
125
|
+
activesupport (= 5.2.4.5)
|
124
126
|
method_source
|
125
127
|
rake (>= 0.8.7)
|
126
128
|
thor (>= 0.19.0, < 2.0)
|
127
129
|
rainbow (3.0.0)
|
128
|
-
rake (13.0.
|
130
|
+
rake (13.0.3)
|
129
131
|
rb-fsevent (0.10.4)
|
130
132
|
rb-inotify (0.10.1)
|
131
133
|
ffi (~> 1.0)
|
@@ -167,9 +169,9 @@ GEM
|
|
167
169
|
activesupport (>= 4.0)
|
168
170
|
sprockets (>= 3.0.0)
|
169
171
|
sqlite3 (1.4.2)
|
170
|
-
thor (1.0
|
172
|
+
thor (1.1.0)
|
171
173
|
thread_safe (0.3.6)
|
172
|
-
tzinfo (1.2.
|
174
|
+
tzinfo (1.2.9)
|
173
175
|
thread_safe (~> 0.1)
|
174
176
|
unicode-display_width (1.7.0)
|
175
177
|
websocket-driver (0.7.3)
|
@@ -28,13 +28,12 @@ module DeclareSchema
|
|
28
28
|
field(:lock_version, :integer, default: 1, null: false)
|
29
29
|
end
|
30
30
|
|
31
|
-
def field(name, type, *args)
|
32
|
-
|
33
|
-
@model.declare_field(name, type, *(args + [@options.merge(options)]))
|
31
|
+
def field(name, type, *args, **options)
|
32
|
+
@model.declare_field(name, type, *[*args, @options.merge(options)])
|
34
33
|
end
|
35
34
|
|
36
35
|
def method_missing(name, *args)
|
37
|
-
field(name,
|
36
|
+
field(name, *args)
|
38
37
|
end
|
39
38
|
end
|
40
39
|
end
|
data/lib/declare_schema/model.rb
CHANGED
@@ -81,8 +81,7 @@ module DeclareSchema
|
|
81
81
|
# arguments. The arguments are forwarded to the #field_added
|
82
82
|
# callback, allowing custom metadata to be added to field
|
83
83
|
# declarations.
|
84
|
-
def declare_field(name, type, *args)
|
85
|
-
options = args.extract_options!
|
84
|
+
def declare_field(name, type, *args, **options)
|
86
85
|
try(:field_added, name, type, args, options)
|
87
86
|
add_serialize_for_field(name, type, options)
|
88
87
|
add_formatting_for_field(name, type)
|
@@ -73,44 +73,43 @@ module DeclareSchema
|
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
|
-
# Normalizes schema attributes for the
|
76
|
+
# Normalizes schema attributes for the given database adapter name.
|
77
77
|
# Note that the un-normalized attributes are still useful for generating migrations because those
|
78
78
|
# may be run with a different adapter.
|
79
|
-
# This method never mutates its argument.
|
80
|
-
def normalize_schema_attributes(schema_attributes)
|
81
|
-
schema_attributes[:type]
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
schema_attributes
|
96
|
-
else
|
97
|
-
schema_attributes
|
98
|
-
end
|
79
|
+
# This method never mutates its argument.
|
80
|
+
def normalize_schema_attributes(schema_attributes, db_adapter_name)
|
81
|
+
case schema_attributes[:type]
|
82
|
+
when :boolean
|
83
|
+
schema_attributes.reverse_merge(limit: 1)
|
84
|
+
when :integer
|
85
|
+
schema_attributes.reverse_merge(limit: 8) if db_adapter_name.match?(/sqlite/i)
|
86
|
+
when :float
|
87
|
+
schema_attributes.except(:limit)
|
88
|
+
when :text
|
89
|
+
schema_attributes.except(:limit) if db_adapter_name.match?(/sqlite/i)
|
90
|
+
when :datetime
|
91
|
+
schema_attributes.reverse_merge(precision: 0)
|
92
|
+
when NilClass
|
93
|
+
raise ArgumentError, ":type key not found; keys: #{schema_attributes.keys.inspect}"
|
94
|
+
end || schema_attributes
|
99
95
|
end
|
100
96
|
|
101
97
|
def equivalent_schema_attributes?(schema_attributes_lhs, schema_attributes_rhs)
|
102
|
-
|
98
|
+
db_adapter_name = ActiveRecord::Base.connection.class.name
|
99
|
+
normalized_lhs = normalize_schema_attributes(schema_attributes_lhs, db_adapter_name)
|
100
|
+
normalized_rhs = normalize_schema_attributes(schema_attributes_rhs, db_adapter_name)
|
101
|
+
|
102
|
+
normalized_lhs == normalized_rhs
|
103
103
|
end
|
104
104
|
end
|
105
105
|
|
106
|
+
attr_reader :sql_type
|
107
|
+
|
106
108
|
def initialize(model, current_table_name, column)
|
107
109
|
@model = model or raise ArgumentError, "must pass model"
|
108
110
|
@current_table_name = current_table_name or raise ArgumentError, "must pass current_table_name"
|
109
111
|
@column = column or raise ArgumentError, "must pass column"
|
110
|
-
|
111
|
-
|
112
|
-
def sql_type
|
113
|
-
@sql_type ||= self.class.sql_type(@column.type)
|
112
|
+
@sql_type = self.class.sql_type(@column.type)
|
114
113
|
end
|
115
114
|
|
116
115
|
SCHEMA_KEYS = [:type, :limit, :precision, :scale, :null, :default].freeze
|
@@ -121,7 +120,7 @@ module DeclareSchema
|
|
121
120
|
value =
|
122
121
|
case key
|
123
122
|
when :default
|
124
|
-
self.class.deserialize_default_value(@column, sql_type, @column.default)
|
123
|
+
self.class.deserialize_default_value(@column, @sql_type, @column.default)
|
125
124
|
else
|
126
125
|
col_value = @column.send(key)
|
127
126
|
if col_value.nil? && (native_type = self.class.native_types[@column.type])
|
@@ -112,14 +112,14 @@ module DeclareSchema
|
|
112
112
|
|
113
113
|
@options = Hash[@options.sort_by { |k, _v| OPTION_INDEXES[k] || 9999 }]
|
114
114
|
|
115
|
-
@sql_options = @options.
|
115
|
+
@sql_options = @options.slice(*SQL_OPTIONS)
|
116
116
|
end
|
117
117
|
|
118
118
|
# returns the attributes for schema migrations as a Hash
|
119
119
|
# omits name and position since those are meta-data above the schema
|
120
120
|
# omits keys with nil values
|
121
121
|
def schema_attributes(col_spec)
|
122
|
-
@
|
122
|
+
@sql_options.merge(type: @type).tap do |attrs|
|
123
123
|
attrs[:default] = Column.deserialize_default_value(col_spec, @sql_type, attrs[:default])
|
124
124
|
end.compact
|
125
125
|
end
|
@@ -444,12 +444,12 @@ module Generators
|
|
444
444
|
spec_attrs = spec.schema_attributes(column)
|
445
445
|
column_declaration = ::DeclareSchema::Model::Column.new(model, current_table_name, column)
|
446
446
|
col_attrs = column_declaration.schema_attributes
|
447
|
-
|
448
|
-
normalized_schema_attributes = spec_attrs.merge(fk_field_options(model, col_name_to_change))
|
447
|
+
normalized_schema_attrs = spec_attrs.merge(fk_field_options(model, col_name_to_change))
|
449
448
|
|
450
|
-
|
449
|
+
if !::DeclareSchema::Model::Column.equivalent_schema_attributes?(normalized_schema_attrs, col_attrs)
|
450
|
+
type = normalized_schema_attrs.delete(:type) or raise "no :type found in #{normalized_schema_attrs.inspect}"
|
451
451
|
changes << ["change_column #{new_table_name.to_sym.inspect}", col_name_to_change.to_sym.inspect,
|
452
|
-
type.to_sym.inspect, *format_options(
|
452
|
+
type.to_sym.inspect, *format_options(normalized_schema_attrs)].join(", ")
|
453
453
|
undo_changes << change_column_back(model, current_table_name, orig_col_name)
|
454
454
|
end
|
455
455
|
end
|
@@ -149,8 +149,8 @@ RSpec.describe DeclareSchema::Model::FieldSpec do
|
|
149
149
|
end
|
150
150
|
end
|
151
151
|
|
152
|
-
it 'returns the attributes except name, position' do
|
153
|
-
subject = described_class.new(model, :price, :bigint, null: true, default: 0, position: 2)
|
152
|
+
it 'returns the attributes except name, position, and non-SQL options' do
|
153
|
+
subject = described_class.new(model, :price, :bigint, null: true, default: 0, ruby_default: -> { }, encrypt_using: -> { }, position: 2)
|
154
154
|
expect(subject.schema_attributes(col_spec)).to eq(type: :integer, limit: 8, null: true, default: 0)
|
155
155
|
end
|
156
156
|
|
@@ -163,4 +163,11 @@ RSpec.describe DeclareSchema::Model::FieldSpec do
|
|
163
163
|
expect(bigint.schema_attributes(col_spec)).to eq(expected_attributes)
|
164
164
|
end
|
165
165
|
end
|
166
|
+
|
167
|
+
describe '#sql_options' do
|
168
|
+
subject { described_class.new(model, :price, :integer, limit: 4, null: true, default: 0, position: 2, encrypt_using: ->(field) { field }) }
|
169
|
+
it 'excludes non-sql options' do
|
170
|
+
expect(subject.sql_options).to eq(limit: 4, null: true, default: 0)
|
171
|
+
end
|
172
|
+
end
|
166
173
|
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.7.
|
4
|
+
version: 0.7.1
|
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-
|
11
|
+
date: 2021-02-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|