declare_schema 0.13.1 → 0.14.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 +4 -4
- data/.github/workflows/declare_schema_build.yml +0 -4
- data/.ruby-version +1 -1
- data/Appraisals +0 -10
- data/CHANGELOG.md +19 -0
- data/Gemfile.lock +2 -2
- data/declare_schema.gemspec +1 -1
- data/lib/declare_schema/dsl.rb +1 -1
- data/lib/declare_schema/model/column.rb +3 -11
- data/lib/declare_schema/model/index_definition.rb +0 -26
- data/lib/declare_schema/model.rb +4 -16
- data/lib/declare_schema/version.rb +1 -1
- data/lib/declare_schema.rb +1 -6
- data/lib/generators/declare_schema/migration/migration_generator.rb +2 -15
- data/lib/generators/declare_schema/migration/migrator.rb +7 -13
- data/lib/generators/declare_schema/migration/templates/migration.rb.erb +1 -1
- data/spec/lib/declare_schema/field_spec_spec.rb +2 -12
- data/spec/lib/declare_schema/generator_spec.rb +1 -15
- data/spec/lib/declare_schema/interactive_primary_key_spec.rb +2 -2
- data/spec/lib/declare_schema/migration_generator_spec.rb +64 -79
- data/spec/lib/declare_schema/model/column_spec.rb +11 -16
- data/spec/lib/declare_schema/model/index_definition_spec.rb +0 -20
- data/spec/spec_helper.rb +1 -5
- metadata +5 -7
- data/gemfiles/rails_4_mysql.gemfile +0 -22
- data/gemfiles/rails_4_sqlite.gemfile +0 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2804b7a11446f5570e967f4ea65be3ee2da5af62a54c3a345acfd2d9b9f58d7b
|
4
|
+
data.tar.gz: 1c1787e5388145ec1a8ae81276fda0488be7fc2d7e192355aaf8d76ef0c00005
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2eaff1f5f1e0e0ded155db135646df64a9e38a04c64511550e98ed7857e182b63f45558b309dcbaf93d3a22bff6781b95b94418aa90604fbdafda076384bbde5
|
7
|
+
data.tar.gz: a8e380d8e2b1fadf9aeceeea58d3a2a1503330191dcb8f4f4fc507f7b6d191b4dec2f191e94820d7fb21efacfc95c571e5589612517904c48d91dde2d2866973
|
@@ -11,15 +11,11 @@ jobs:
|
|
11
11
|
matrix:
|
12
12
|
ruby: [ 2.4.5, 2.5.8, 2.6.5, 2.7.1 ]
|
13
13
|
gemfile:
|
14
|
-
- gemfiles/rails_4_mysql.gemfile
|
15
|
-
- gemfiles/rails_4_sqlite.gemfile
|
16
14
|
- gemfiles/rails_5_mysql.gemfile
|
17
15
|
- gemfiles/rails_5_sqlite.gemfile
|
18
16
|
- gemfiles/rails_6_mysql.gemfile
|
19
17
|
- gemfiles/rails_6_sqlite.gemfile
|
20
18
|
exclude:
|
21
|
-
- { gemfile: gemfiles/rails_4_mysql.gemfile, ruby: 2.7.1 }
|
22
|
-
- { gemfile: gemfiles/rails_4_sqlite.gemfile, ruby: 2.7.1 }
|
23
19
|
- { gemfile: gemfiles/rails_5_mysql.gemfile, ruby: 2.4.5 }
|
24
20
|
- { gemfile: gemfiles/rails_5_sqlite.gemfile, ruby: 2.4.5 }
|
25
21
|
- { gemfile: gemfiles/rails_6_mysql.gemfile, ruby: 2.4.5 }
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.7.0
|
data/Appraisals
CHANGED
@@ -1,15 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
appraise 'rails-4-sqlite' do
|
4
|
-
gem 'rails', '~> 4.2'
|
5
|
-
gem 'sqlite3', '~> 1.3.0'
|
6
|
-
end
|
7
|
-
|
8
|
-
appraise 'rails-4-mysql' do
|
9
|
-
gem 'rails', '~> 4.2'
|
10
|
-
gem 'mysql2'
|
11
|
-
end
|
12
|
-
|
13
3
|
appraise 'rails-5-sqlite' do
|
14
4
|
gem 'rails', '~> 5.2'
|
15
5
|
gem 'sqlite3'
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +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.14.2] - 2021-09-01
|
8
|
+
### Fixed
|
9
|
+
- Fixed another Ruby 2.7 warning about needing `**options` when calling a method that has `**options` in its signature.
|
10
|
+
|
11
|
+
## [0.14.1] - 2021-09-01
|
12
|
+
### Fixed
|
13
|
+
- Fixed Ruby 2.7 warning about needing `**options` when calling a method that has `**options` in its signature.
|
14
|
+
|
15
|
+
## [0.14.0] - 2021-08-10
|
16
|
+
### Removed
|
17
|
+
- Dropped support for Rails versions less than 5.
|
18
|
+
|
19
|
+
## [0.13.2] - 2021-08-04
|
20
|
+
### Fixed
|
21
|
+
- Fixed issue with disable_auto_increment support for new tables.
|
22
|
+
|
7
23
|
## [0.13.1] - 2021-08-02
|
8
24
|
### Fixed
|
9
25
|
- Fixed migration file indentation bug in Rails 5 where the first line was indented an extra 4 characters.
|
@@ -188,6 +204,9 @@ using the appropriate Rails configuration attributes.
|
|
188
204
|
### Added
|
189
205
|
- Initial version from https://github.com/Invoca/hobo_fields v4.1.0.
|
190
206
|
|
207
|
+
[0.14.2]: https://github.com/Invoca/declare_schema/compare/v0.14.1...v0.14.2
|
208
|
+
[0.14.1]: https://github.com/Invoca/declare_schema/compare/v0.14.0...v0.14.1
|
209
|
+
[0.14.0]: https://github.com/Invoca/declare_schema/compare/v0.13.1...v0.14.0
|
191
210
|
[0.13.1]: https://github.com/Invoca/declare_schema/compare/v0.13.0...v0.13.1
|
192
211
|
[0.13.0]: https://github.com/Invoca/declare_schema/compare/v0.12.1...v0.13.0
|
193
212
|
[0.12.1]: https://github.com/Invoca/declare_schema/compare/v0.12.0...v0.12.1
|
data/Gemfile.lock
CHANGED
data/declare_schema.gemspec
CHANGED
data/lib/declare_schema/dsl.rb
CHANGED
@@ -36,7 +36,7 @@ module DeclareSchema
|
|
36
36
|
def method_missing(*args, **options)
|
37
37
|
args.count(&:itself) >= 2 or raise ::ArgumentError, "fields in declare_schema block must be declared as: type name, [:required], options (got #{args.inspect}, #{options.inspect})"
|
38
38
|
type, name, *required = args
|
39
|
-
field(name, type, *required, options)
|
39
|
+
field(name, type, *required, **options)
|
40
40
|
end
|
41
41
|
end
|
42
42
|
end
|
@@ -52,17 +52,9 @@ module DeclareSchema
|
|
52
52
|
|
53
53
|
def deserialize_default_value(column, type, default_value)
|
54
54
|
type or raise ArgumentError, "must pass type; got #{type.inspect}"
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
# TODO: Delete this Rails 4 support ASAP! This could be wrong, since it's using the type of the old column...which
|
59
|
-
# might be getting migrated to a new type. We should be using just type as below. -Colin
|
60
|
-
column.type_cast_from_database(default_value)
|
61
|
-
else
|
62
|
-
cast_type = ActiveRecord::Base.connection.send(:lookup_cast_type, type.to_s) or
|
63
|
-
raise "cast_type not found for #{type}"
|
64
|
-
cast_type.deserialize(default_value)
|
65
|
-
end
|
55
|
+
cast_type = ActiveRecord::Base.connection.send(:lookup_cast_type, type.to_s) or
|
56
|
+
raise "cast_type not found for #{type}"
|
57
|
+
cast_type.deserialize(default_value)
|
66
58
|
end
|
67
59
|
|
68
60
|
# Normalizes schema attributes for the given database adapter name.
|
@@ -63,32 +63,6 @@ module DeclareSchema
|
|
63
63
|
def index_name(columns)
|
64
64
|
"on_#{Array(columns).join("_and_")}"
|
65
65
|
end
|
66
|
-
|
67
|
-
private
|
68
|
-
|
69
|
-
# This is the old approach which is still needed for MySQL in Rails 4 and SQLite
|
70
|
-
def fallback_find_primary_key(model, table)
|
71
|
-
ActiveRecord::Base.connection.class.name.match?(/SQLite3Adapter/) || ActiveSupport::VERSION::MAJOR < 5 or return nil
|
72
|
-
|
73
|
-
connection = model.connection.dup
|
74
|
-
|
75
|
-
class << connection # defeat Rails MySQL driver code that skips the primary key by changing its name to a symbol
|
76
|
-
def each_hash(result)
|
77
|
-
super do |hash|
|
78
|
-
if hash[:Key_name] == PRIMARY_KEY_NAME
|
79
|
-
hash[:Key_name] = PRIMARY_KEY_NAME.to_sym
|
80
|
-
end
|
81
|
-
yield hash
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
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
|
91
|
-
end
|
92
66
|
end
|
93
67
|
|
94
68
|
def primary_key?
|
data/lib/declare_schema/model.rb
CHANGED
@@ -131,11 +131,7 @@ module DeclareSchema
|
|
131
131
|
|
132
132
|
fk_options[:dependent] = options.delete(:far_end_dependent) if options.has_key?(:far_end_dependent)
|
133
133
|
|
134
|
-
|
135
|
-
super
|
136
|
-
else
|
137
|
-
super(name, scope, options.except(:optional))
|
138
|
-
end
|
134
|
+
super
|
139
135
|
|
140
136
|
refl = reflections[name.to_s] or raise "Couldn't find reflection #{name} in #{reflections.keys}"
|
141
137
|
fkey = refl.foreign_key or raise "Couldn't find foreign_key for #{name} in #{refl.inspect}"
|
@@ -156,7 +152,7 @@ module DeclareSchema
|
|
156
152
|
end
|
157
153
|
end
|
158
154
|
|
159
|
-
declare_field(fkey.to_sym, :bigint, fkey_id_column_options)
|
155
|
+
declare_field(fkey.to_sym, :bigint, **fkey_id_column_options)
|
160
156
|
|
161
157
|
if refl.options[:polymorphic]
|
162
158
|
foreign_type = options[:foreign_type] || "#{name}_type"
|
@@ -190,12 +186,6 @@ module DeclareSchema
|
|
190
186
|
end
|
191
187
|
end
|
192
188
|
|
193
|
-
if ::ActiveSupport::VERSION::MAJOR < 5
|
194
|
-
def primary_key
|
195
|
-
super || 'id'
|
196
|
-
end
|
197
|
-
end
|
198
|
-
|
199
189
|
# returns the primary key (String) as declared with primary_key =
|
200
190
|
# unlike the `primary_key` method, DOES NOT query the database to find the actual primary key in use right now
|
201
191
|
# if no explicit primary key set, returns the _default_declared_primary_key
|
@@ -224,7 +214,7 @@ module DeclareSchema
|
|
224
214
|
# Declares the "foo_type" field that accompanies the "foo_id"
|
225
215
|
# field for a polymorphic belongs_to
|
226
216
|
def _declare_polymorphic_type_field(foreign_type, column_options)
|
227
|
-
declare_field(foreign_type, :string, column_options.merge(limit: 255))
|
217
|
+
declare_field(foreign_type, :string, **column_options.merge(limit: 255))
|
228
218
|
# FIXME: Before declare_schema was extracted, this used to now do:
|
229
219
|
# never_show(type_col)
|
230
220
|
# That needs doing somewhere
|
@@ -268,10 +258,8 @@ module DeclareSchema
|
|
268
258
|
ActiveRecord::Coders::JSON
|
269
259
|
elsif [:load, :dump].all? { |x| class_name_or_coder.respond_to?(x) }
|
270
260
|
class_name_or_coder
|
271
|
-
|
261
|
+
else
|
272
262
|
ActiveRecord::Coders::YAMLColumn.new(attr_name, class_name_or_coder)
|
273
|
-
else
|
274
|
-
ActiveRecord::Coders::YAMLColumn.new(class_name_or_coder)
|
275
263
|
end
|
276
264
|
|
277
265
|
if default == coder.load(nil)
|
data/lib/declare_schema.rb
CHANGED
@@ -28,12 +28,7 @@ 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
|
+
@db_migrate_command = "bundle exec rails db:migrate"
|
37
32
|
|
38
33
|
class << self
|
39
34
|
attr_reader :default_charset, :default_collation, :default_text_limit, :default_string_limit, :default_null,
|
@@ -83,11 +83,7 @@ module DeclareSchema
|
|
83
83
|
if options[:migrate]
|
84
84
|
say db_migrate_command
|
85
85
|
bare_rails_command = db_migrate_command.sub(/\Abundle exec +/, '').sub(/\Arake +|rails +/, '')
|
86
|
-
|
87
|
-
rake(bare_rails_command)
|
88
|
-
else
|
89
|
-
rails_command(bare_rails_command)
|
90
|
-
end
|
86
|
+
rails_command(bare_rails_command)
|
91
87
|
else
|
92
88
|
say "\nNot running migration since --migrate not given. When you are ready, run:\n\n #{db_migrate_command}\n\n"
|
93
89
|
end
|
@@ -97,24 +93,15 @@ module DeclareSchema
|
|
97
93
|
|
98
94
|
private
|
99
95
|
|
100
|
-
if ActiveSupport::VERSION::MAJOR < 5
|
101
|
-
def indent(string, columns)
|
102
|
-
whitespace = ' ' * columns
|
103
|
-
string.gsub("\n", "\n#{whitespace}").gsub(/ +\n/, "\n")
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
96
|
def migrations_pending?
|
108
97
|
migrations = case ActiveSupport::VERSION::MAJOR
|
109
|
-
when 4
|
110
|
-
ActiveRecord::Migrator.migrations('db/migrate')
|
111
98
|
when 5
|
112
99
|
ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths).migrations
|
113
100
|
else
|
114
101
|
ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths, ActiveRecord::SchemaMigration).migrations
|
115
102
|
end
|
116
103
|
pending_migrations = case ActiveSupport::VERSION::MAJOR
|
117
|
-
when
|
104
|
+
when 5
|
118
105
|
ActiveRecord::Migrator.new(:up, migrations).pending_migrations
|
119
106
|
else
|
120
107
|
ActiveRecord::Migrator.new(:up, migrations, ActiveRecord::SchemaMigration).pending_migrations
|
@@ -226,7 +226,9 @@ module Generators
|
|
226
226
|
|
227
227
|
primary_key_definition =
|
228
228
|
if disable_auto_increment
|
229
|
-
[:integer, :id, limit: 8, auto_increment: false, primary_key: true]
|
229
|
+
[[:integer, :id, limit: 8, auto_increment: false, primary_key: true]]
|
230
|
+
else
|
231
|
+
[]
|
230
232
|
end
|
231
233
|
|
232
234
|
field_definitions = model.field_specs.values.sort_by(&:position).map do |f|
|
@@ -237,7 +239,7 @@ module Generators
|
|
237
239
|
table_options = create_table_options(model, disable_auto_increment)
|
238
240
|
|
239
241
|
table_add = ::DeclareSchema::SchemaChange::TableAdd.new(t,
|
240
|
-
|
242
|
+
primary_key_definition + field_definitions,
|
241
243
|
table_options,
|
242
244
|
sql_options: table_options_definition.settings)
|
243
245
|
[
|
@@ -487,11 +489,7 @@ module Generators
|
|
487
489
|
parent_columns = connection.columns(parent_table) rescue []
|
488
490
|
pk_limit =
|
489
491
|
if (pk_column = parent_columns.find { |column| column.name.to_s == "id" }) # right now foreign keys assume id is the target
|
490
|
-
|
491
|
-
pk_column.cast_type.limit
|
492
|
-
else
|
493
|
-
pk_column.limit
|
494
|
-
end
|
492
|
+
pk_column.limit
|
495
493
|
else
|
496
494
|
8
|
497
495
|
end
|
@@ -553,12 +551,8 @@ module Generators
|
|
553
551
|
end
|
554
552
|
end
|
555
553
|
|
556
|
-
SchemaDumper =
|
557
|
-
|
558
|
-
ActiveRecord::SchemaDumper
|
559
|
-
else
|
560
|
-
ActiveRecord::ConnectionAdapters::SchemaDumper
|
561
|
-
end
|
554
|
+
SchemaDumper = ActiveRecord::ConnectionAdapters::SchemaDumper
|
555
|
+
|
562
556
|
|
563
557
|
def add_table_back(table)
|
564
558
|
dumped_schema_stream = StringIO.new
|
@@ -11,10 +11,6 @@ RSpec.describe DeclareSchema::Model::FieldSpec do
|
|
11
11
|
|
12
12
|
before do
|
13
13
|
load File.expand_path('prepare_testapp.rb', __dir__)
|
14
|
-
|
15
|
-
if ActiveSupport::VERSION::MAJOR < 5
|
16
|
-
allow(col_spec).to receive(:type_cast_from_database, &:itself)
|
17
|
-
end
|
18
14
|
end
|
19
15
|
|
20
16
|
describe '#initialize' do
|
@@ -226,14 +222,8 @@ RSpec.describe DeclareSchema::Model::FieldSpec do
|
|
226
222
|
|
227
223
|
describe '#schema_attributes' do
|
228
224
|
let(:col_spec) do
|
229
|
-
|
230
|
-
|
231
|
-
cast_type = ActiveRecord::Type::Integer.new(limit: 8)
|
232
|
-
ActiveRecord::ConnectionAdapters::Column.new("price", nil, cast_type, "integer(8)", false)
|
233
|
-
else
|
234
|
-
sql_type_metadata = ActiveRecord::ConnectionAdapters::SqlTypeMetadata.new(sql_type: "integer(8)", type: :integer, limit: 8)
|
235
|
-
ActiveRecord::ConnectionAdapters::Column.new("price", nil, sql_type_metadata, false, "adverts")
|
236
|
-
end
|
225
|
+
sql_type_metadata = ActiveRecord::ConnectionAdapters::SqlTypeMetadata.new(sql_type: "integer(8)", type: :integer, limit: 8)
|
226
|
+
ActiveRecord::ConnectionAdapters::Column.new("price", nil, sql_type_metadata, false, "adverts")
|
237
227
|
end
|
238
228
|
|
239
229
|
it 'returns the attributes except name, position, and non-SQL options' do
|
@@ -28,7 +28,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
28
28
|
EOS
|
29
29
|
|
30
30
|
case ActiveSupport::VERSION::MAJOR
|
31
|
-
when
|
31
|
+
when 5
|
32
32
|
expect_test_definition_to_eq('alpha/beta', <<~EOS)
|
33
33
|
require "test_helper"
|
34
34
|
|
@@ -51,20 +51,6 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
51
51
|
end
|
52
52
|
|
53
53
|
case ActiveSupport::VERSION::MAJOR
|
54
|
-
when 4
|
55
|
-
expect_test_fixture_to_eq('alpha/beta', <<~EOS)
|
56
|
-
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
57
|
-
|
58
|
-
# This model initially had no columns defined. If you add columns to the
|
59
|
-
# model remove the '{}' from the fixture names and add the columns immediately
|
60
|
-
# below each fixture, per the syntax in the comments below
|
61
|
-
#
|
62
|
-
one: {}
|
63
|
-
# column: value
|
64
|
-
#
|
65
|
-
two: {}
|
66
|
-
# column: value
|
67
|
-
EOS
|
68
54
|
when 5
|
69
55
|
expect_test_fixture_to_eq('alpha/beta', <<~EOS)
|
70
56
|
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
@@ -62,7 +62,7 @@ RSpec.describe 'DeclareSchema Migration Generator interactive primary key' do
|
|
62
62
|
ActiveRecord::Base.connection.execute("CREATE TABLE foos (id integer PRIMARY KEY AUTOINCREMENT NOT NULL)")
|
63
63
|
end
|
64
64
|
|
65
|
-
if
|
65
|
+
if !defined?(Mysql2) # TODO TECH-4814 Put this test back for Mysql2
|
66
66
|
# replace custom primary_key
|
67
67
|
class Foo < ActiveRecord::Base
|
68
68
|
fields do
|
@@ -128,7 +128,7 @@ RSpec.describe 'DeclareSchema Migration Generator interactive primary key' do
|
|
128
128
|
ActiveRecord::Base.connection.execute("CREATE TABLE foos (id integer PRIMARY KEY AUTOINCREMENT NOT NULL)")
|
129
129
|
end
|
130
130
|
|
131
|
-
if
|
131
|
+
if !defined?(Mysql2) # TODO TECH-4814 Put this test back for Mysql2
|
132
132
|
# replace custom primary_key
|
133
133
|
class Foo < ActiveRecord::Base
|
134
134
|
declare_schema do
|
@@ -30,7 +30,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
let(:datetime_precision) do
|
33
|
-
if defined?(Mysql2)
|
33
|
+
if defined?(Mysql2)
|
34
34
|
', precision: 0'
|
35
35
|
end
|
36
36
|
end
|
@@ -43,7 +43,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
43
43
|
''
|
44
44
|
end
|
45
45
|
else
|
46
|
-
", id: :integer"
|
46
|
+
", id: :integer"
|
47
47
|
end
|
48
48
|
end
|
49
49
|
let(:lock_version_limit) do
|
@@ -91,16 +91,6 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
91
91
|
ActiveRecord::Migration.class_eval(up)
|
92
92
|
expect(Advert.columns.map(&:name)).to eq(["id", "name"])
|
93
93
|
|
94
|
-
if ActiveSupport::VERSION::MAJOR < 5
|
95
|
-
# Rails 4 drivers don't always create PK properly. Fix that by dropping and recreating.
|
96
|
-
ActiveRecord::Base.connection.execute("drop table adverts")
|
97
|
-
if defined?(Mysql2)
|
98
|
-
ActiveRecord::Base.connection.execute("CREATE TABLE adverts (id integer PRIMARY KEY AUTO_INCREMENT NOT NULL, name varchar(250)) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin")
|
99
|
-
else
|
100
|
-
ActiveRecord::Base.connection.execute("CREATE TABLE adverts (id integer PRIMARY KEY AUTOINCREMENT NOT NULL, name varchar(250))")
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
94
|
class Advert < ActiveRecord::Base
|
105
95
|
fields do
|
106
96
|
name :string, limit: 250, null: true
|
@@ -803,9 +793,36 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
803
793
|
EOS
|
804
794
|
)
|
805
795
|
|
796
|
+
Advert.connection.schema_cache.clear!
|
797
|
+
Advert.reset_column_information
|
798
|
+
|
806
799
|
nuke_model_class(Advert)
|
807
800
|
ActiveRecord::Base.connection.execute("drop table `adverts`;")
|
808
801
|
|
802
|
+
if !defined?(SQLite3) && ActiveSupport::VERSION::MAJOR >= 5
|
803
|
+
class Advert < ActiveRecord::Base
|
804
|
+
def self.disable_auto_increment
|
805
|
+
true
|
806
|
+
end
|
807
|
+
|
808
|
+
fields do
|
809
|
+
description :text, limit: 250, null: true
|
810
|
+
end
|
811
|
+
end
|
812
|
+
|
813
|
+
up, _ = Generators::DeclareSchema::Migration::Migrator.run.tap do |migrations|
|
814
|
+
expect(migrations).to(
|
815
|
+
migrate_up(<<~EOS.strip)
|
816
|
+
create_table :adverts, id: false, options: "CHARACTER SET utf8mb4 COLLATE utf8mb4_bin" do |t|
|
817
|
+
t.integer :id, limit: 8, auto_increment: false, primary_key: true
|
818
|
+
t.text :description, limit: 255, null: true, charset: "utf8mb4", collation: "utf8mb4_bin"
|
819
|
+
end
|
820
|
+
EOS
|
821
|
+
.and migrate_down("drop_table :adverts")
|
822
|
+
)
|
823
|
+
end
|
824
|
+
end
|
825
|
+
|
809
826
|
## DSL
|
810
827
|
|
811
828
|
# The DSL allows lambdas and constants
|
@@ -886,7 +903,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
886
903
|
nuke_model_class(Advertiser)
|
887
904
|
nuke_model_class(Affiliate)
|
888
905
|
end
|
889
|
-
end if !defined?(SQLite3)
|
906
|
+
end if !defined?(SQLite3)
|
890
907
|
|
891
908
|
describe 'serialize' do
|
892
909
|
before do
|
@@ -1073,13 +1090,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1073
1090
|
end
|
1074
1091
|
|
1075
1092
|
context "for Rails #{ActiveSupport::VERSION::MAJOR}" do
|
1076
|
-
|
1077
|
-
|
1078
|
-
let(:optional_false) { { optional: false } }
|
1079
|
-
else
|
1080
|
-
let(:optional_true) { {} }
|
1081
|
-
let(:optional_false) { {} }
|
1082
|
-
end
|
1093
|
+
let(:optional_true) { { optional: true } }
|
1094
|
+
let(:optional_false) { { optional: false } }
|
1083
1095
|
let(:optional_flag) { { false => optional_false, true => optional_true } }
|
1084
1096
|
|
1085
1097
|
describe 'belongs_to' do
|
@@ -1179,36 +1191,30 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1179
1191
|
migration_content = File.read(migrations.first)
|
1180
1192
|
first_line = migration_content.split("\n").first
|
1181
1193
|
base_class = first_line.split(' < ').last
|
1182
|
-
expect(base_class).to eq("(
|
1194
|
+
expect(base_class).to eq("(ActiveRecord::Migration[4.2])")
|
1183
1195
|
end
|
1184
1196
|
end
|
1185
1197
|
|
1186
1198
|
context 'Does not generate migrations' do
|
1187
1199
|
it 'for aliased fields bigint -> integer limit 8' do
|
1188
|
-
|
1189
|
-
|
1190
|
-
|
1191
|
-
price :bigint
|
1192
|
-
end
|
1200
|
+
class Advert < active_record_base_class.constantize
|
1201
|
+
fields do
|
1202
|
+
price :bigint
|
1193
1203
|
end
|
1204
|
+
end
|
1194
1205
|
|
1195
|
-
|
1196
|
-
|
1197
|
-
migrations = Dir.glob('db/migrate/*declare_schema_migration*.rb')
|
1198
|
-
expect(migrations.size).to eq(1), migrations.inspect
|
1206
|
+
generate_migrations '-n', '-m'
|
1199
1207
|
|
1200
|
-
|
1201
|
-
|
1202
|
-
end
|
1208
|
+
migrations = Dir.glob('db/migrate/*declare_schema_migration*.rb')
|
1209
|
+
expect(migrations.size).to eq(1), migrations.inspect
|
1203
1210
|
|
1204
|
-
|
1205
|
-
|
1206
|
-
|
1207
|
-
end
|
1211
|
+
class Advert < active_record_base_class.constantize
|
1212
|
+
fields do
|
1213
|
+
price :integer, limit: 8
|
1208
1214
|
end
|
1209
|
-
|
1210
|
-
expect { generate_migrations '-n', '-g' }.to output("Database and models match -- nothing to change\n").to_stdout
|
1211
1215
|
end
|
1216
|
+
|
1217
|
+
expect { generate_migrations '-n', '-g' }.to output("Database and models match -- nothing to change\n").to_stdout
|
1212
1218
|
end
|
1213
1219
|
end
|
1214
1220
|
end
|
@@ -1250,16 +1256,6 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1250
1256
|
ActiveRecord::Migration.class_eval(up)
|
1251
1257
|
expect(Advert.columns.map(&:name)).to eq(["id", "name"])
|
1252
1258
|
|
1253
|
-
if ActiveSupport::VERSION::MAJOR < 5
|
1254
|
-
# Rails 4 drivers don't always create PK properly. Fix that by dropping and recreating.
|
1255
|
-
ActiveRecord::Base.connection.execute("drop table adverts")
|
1256
|
-
if defined?(Mysql2)
|
1257
|
-
ActiveRecord::Base.connection.execute("CREATE TABLE adverts (id integer PRIMARY KEY AUTO_INCREMENT NOT NULL, name varchar(250)) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin")
|
1258
|
-
else
|
1259
|
-
ActiveRecord::Base.connection.execute("CREATE TABLE adverts (id integer PRIMARY KEY AUTOINCREMENT NOT NULL, name varchar(250))")
|
1260
|
-
end
|
1261
|
-
end
|
1262
|
-
|
1263
1259
|
class Advert < ActiveRecord::Base
|
1264
1260
|
declare_schema do
|
1265
1261
|
string :name, limit: 250, null: true
|
@@ -2046,7 +2042,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
2046
2042
|
nuke_model_class(Advertiser)
|
2047
2043
|
nuke_model_class(Affiliate)
|
2048
2044
|
end
|
2049
|
-
end if !defined?(SQLite3)
|
2045
|
+
end if !defined?(SQLite3)
|
2050
2046
|
|
2051
2047
|
describe 'serialize' do
|
2052
2048
|
before do
|
@@ -2233,13 +2229,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
2233
2229
|
end
|
2234
2230
|
|
2235
2231
|
context "for Rails #{ActiveSupport::VERSION::MAJOR}" do
|
2236
|
-
|
2237
|
-
|
2238
|
-
let(:optional_false) { { optional: false } }
|
2239
|
-
else
|
2240
|
-
let(:optional_true) { {} }
|
2241
|
-
let(:optional_false) { {} }
|
2242
|
-
end
|
2232
|
+
let(:optional_true) { { optional: true } }
|
2233
|
+
let(:optional_false) { { optional: false } }
|
2243
2234
|
let(:optional_flag) { { false => optional_false, true => optional_true } }
|
2244
2235
|
|
2245
2236
|
describe 'belongs_to' do
|
@@ -2348,9 +2339,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
2348
2339
|
end
|
2349
2340
|
class Fk < ActiveRecord::Base
|
2350
2341
|
declare_schema { }
|
2351
|
-
belongs_to :id_default, (
|
2352
|
-
belongs_to :id4, (
|
2353
|
-
belongs_to :id8, (
|
2342
|
+
belongs_to :id_default, ({})
|
2343
|
+
belongs_to :id4, ({})
|
2344
|
+
belongs_to :id8, ({})
|
2354
2345
|
end
|
2355
2346
|
end
|
2356
2347
|
|
@@ -2461,36 +2452,30 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
2461
2452
|
migration_content = File.read(migrations.first)
|
2462
2453
|
first_line = migration_content.split("\n").first
|
2463
2454
|
base_class = first_line.split(' < ').last
|
2464
|
-
expect(base_class).to eq("(
|
2455
|
+
expect(base_class).to eq("(ActiveRecord::Migration[4.2])")
|
2465
2456
|
end
|
2466
2457
|
end
|
2467
2458
|
|
2468
2459
|
context 'Does not generate migrations' do
|
2469
2460
|
it 'for aliased fields bigint -> integer limit 8' do
|
2470
|
-
|
2471
|
-
|
2472
|
-
|
2473
|
-
bigint :price
|
2474
|
-
end
|
2461
|
+
class Advert < active_record_base_class.constantize
|
2462
|
+
declare_schema do
|
2463
|
+
bigint :price
|
2475
2464
|
end
|
2465
|
+
end
|
2476
2466
|
|
2477
|
-
|
2478
|
-
|
2479
|
-
migrations = Dir.glob('db/migrate/*declare_schema_migration*.rb')
|
2480
|
-
expect(migrations.size).to eq(1), migrations.inspect
|
2467
|
+
generate_migrations '-n', '-m'
|
2481
2468
|
|
2482
|
-
|
2483
|
-
|
2484
|
-
end
|
2469
|
+
migrations = Dir.glob('db/migrate/*declare_schema_migration*.rb')
|
2470
|
+
expect(migrations.size).to eq(1), migrations.inspect
|
2485
2471
|
|
2486
|
-
|
2487
|
-
|
2488
|
-
|
2489
|
-
end
|
2472
|
+
class Advert < active_record_base_class.constantize
|
2473
|
+
declare_schema do
|
2474
|
+
integer :price, limit: 8
|
2490
2475
|
end
|
2491
|
-
|
2492
|
-
expect { generate_migrations '-n', '-g' }.to output("Database and models match -- nothing to change\n").to_stdout
|
2493
2476
|
end
|
2477
|
+
|
2478
|
+
expect { generate_migrations '-n', '-g' }.to output("Database and models match -- nothing to change\n").to_stdout
|
2494
2479
|
end
|
2495
2480
|
end
|
2496
2481
|
|
@@ -14,11 +14,8 @@ RSpec.describe DeclareSchema::Model::Column do
|
|
14
14
|
|
15
15
|
describe 'class methods' do
|
16
16
|
describe '.native_type?' do
|
17
|
-
|
18
|
-
|
19
|
-
else
|
20
|
-
let(:native_types) { [:string, :text, :integer, :float, :decimal, :datetime, :time, :date, :binary, :boolean] }
|
21
|
-
end
|
17
|
+
let(:native_types) { [:string, :text, :integer, :float, :decimal, :datetime, :time, :date, :binary, :boolean, :json] }
|
18
|
+
|
22
19
|
|
23
20
|
it 'is falsey for :primary_key' do
|
24
21
|
expect(described_class.native_type?(:primary_key)).to be_falsey
|
@@ -63,19 +60,17 @@ RSpec.describe DeclareSchema::Model::Column do
|
|
63
60
|
end
|
64
61
|
|
65
62
|
describe '.deserialize_default_value' do
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
end
|
63
|
+
it 'deserializes :boolean' do
|
64
|
+
expect(described_class.deserialize_default_value(nil, :boolean, 'true')).to eq(true)
|
65
|
+
expect(described_class.deserialize_default_value(nil, :boolean, 'false')).to eq(false)
|
66
|
+
end
|
71
67
|
|
72
|
-
|
73
|
-
|
74
|
-
|
68
|
+
it 'deserializes :integer' do
|
69
|
+
expect(described_class.deserialize_default_value(nil, :integer, '12')).to eq(12)
|
70
|
+
end
|
75
71
|
|
76
|
-
|
77
|
-
|
78
|
-
end
|
72
|
+
it 'deserializes :json' do
|
73
|
+
expect(described_class.deserialize_default_value(nil, :json, '{}')).to eq({})
|
79
74
|
end
|
80
75
|
end
|
81
76
|
end
|
@@ -110,16 +110,6 @@ 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 ActiveSupport::VERSION::MAJOR < 5
|
114
|
-
expect(model_class.connection).to receive(:primary_key).with('index_definition_compound_index_models').and_return(nil)
|
115
|
-
connection_stub = instance_double(ActiveRecord::Base.connection.class, "connection")
|
116
|
-
expect(connection_stub).to receive(:indexes).
|
117
|
-
with('index_definition_compound_index_models').
|
118
|
-
and_return([DeclareSchema::Model::IndexDefinition.new(model_class, ['fk1_id', 'fk2_id'], name: 'PRIMARY')])
|
119
|
-
|
120
|
-
expect(model_class.connection).to receive(:dup).and_return(connection_stub)
|
121
|
-
end
|
122
|
-
|
123
113
|
expect(subject.size).to eq(1), subject.inspect
|
124
114
|
expect([:name, :columns, :unique].map { |attr| subject[0].send(attr) }).to eq(
|
125
115
|
['PRIMARY', ['fk1_id', 'fk2_id'], true]
|
@@ -219,16 +209,6 @@ RSpec.describe DeclareSchema::Model::IndexDefinition do
|
|
219
209
|
let(:model_class) { IndexDefinitionCompoundIndexModel }
|
220
210
|
|
221
211
|
it 'returns the indexes for the model' do
|
222
|
-
if ActiveSupport::VERSION::MAJOR < 5
|
223
|
-
expect(model_class.connection).to receive(:primary_key).with('index_definition_compound_index_models').and_return(nil)
|
224
|
-
connection_stub = instance_double(ActiveRecord::Base.connection.class, "connection")
|
225
|
-
expect(connection_stub).to receive(:indexes).
|
226
|
-
with('index_definition_compound_index_models').
|
227
|
-
and_return([DeclareSchema::Model::IndexDefinition.new(model_class, ['fk1_id', 'fk2_id'], name: 'PRIMARY')])
|
228
|
-
|
229
|
-
expect(model_class.connection).to receive(:dup).and_return(connection_stub)
|
230
|
-
end
|
231
|
-
|
232
212
|
expect(subject.size).to eq(1), subject.inspect
|
233
213
|
expect([:name, :columns, :unique].map { |attr| subject[0].send(attr) }).to eq(
|
234
214
|
['PRIMARY', ['fk1_id', 'fk2_id'], true]
|
data/spec/spec_helper.rb
CHANGED
@@ -27,11 +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
|
-
|
31
|
-
'ActiveRecord::Base'
|
32
|
-
else
|
33
|
-
'ApplicationRecord'
|
34
|
-
end
|
30
|
+
'ApplicationRecord'
|
35
31
|
end
|
36
32
|
|
37
33
|
def migrate(renames = {})
|
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.
|
4
|
+
version: 0.14.2
|
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-
|
11
|
+
date: 2021-09-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '5.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '5.0'
|
27
27
|
description: Declare your Rails/active_record model schemas and have database migrations
|
28
28
|
generated for you!
|
29
29
|
email: development@invoca.com
|
@@ -49,8 +49,6 @@ files:
|
|
49
49
|
- bin/declare_schema
|
50
50
|
- declare_schema.gemspec
|
51
51
|
- gemfiles/.bundle/config
|
52
|
-
- gemfiles/rails_4_mysql.gemfile
|
53
|
-
- gemfiles/rails_4_sqlite.gemfile
|
54
52
|
- gemfiles/rails_5_mysql.gemfile
|
55
53
|
- gemfiles/rails_5_sqlite.gemfile
|
56
54
|
- gemfiles/rails_6_mysql.gemfile
|
@@ -143,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
143
141
|
- !ruby/object:Gem::Version
|
144
142
|
version: 1.3.6
|
145
143
|
requirements: []
|
146
|
-
rubygems_version: 3.
|
144
|
+
rubygems_version: 3.1.2
|
147
145
|
signing_key:
|
148
146
|
specification_version: 4
|
149
147
|
summary: Database schema declaration and migration generator for Rails
|
@@ -1,22 +0,0 @@
|
|
1
|
-
# This file was generated by Appraisal
|
2
|
-
|
3
|
-
source "https://rubygems.org"
|
4
|
-
|
5
|
-
gem "appraisal"
|
6
|
-
gem "bundler", "< 2"
|
7
|
-
gem "climate_control", "~> 0.2"
|
8
|
-
gem "pry"
|
9
|
-
gem "pry-byebug"
|
10
|
-
gem "rails", "~> 4.2"
|
11
|
-
gem "responders"
|
12
|
-
gem "rspec"
|
13
|
-
gem "rubocop"
|
14
|
-
gem "yard"
|
15
|
-
gem "mysql2"
|
16
|
-
|
17
|
-
group :testapp do
|
18
|
-
gem "bootsnap", ">= 1.1.0", require: false
|
19
|
-
gem "listen"
|
20
|
-
end
|
21
|
-
|
22
|
-
gemspec path: "../"
|
@@ -1,22 +0,0 @@
|
|
1
|
-
# This file was generated by Appraisal
|
2
|
-
|
3
|
-
source "https://rubygems.org"
|
4
|
-
|
5
|
-
gem "appraisal"
|
6
|
-
gem "bundler", "< 2"
|
7
|
-
gem "climate_control", "~> 0.2"
|
8
|
-
gem "pry"
|
9
|
-
gem "pry-byebug"
|
10
|
-
gem "rails", "~> 4.2"
|
11
|
-
gem "responders"
|
12
|
-
gem "rspec"
|
13
|
-
gem "rubocop"
|
14
|
-
gem "yard"
|
15
|
-
gem "sqlite3", "~> 1.3.0"
|
16
|
-
|
17
|
-
group :testapp do
|
18
|
-
gem "bootsnap", ">= 1.1.0", require: false
|
19
|
-
gem "listen"
|
20
|
-
end
|
21
|
-
|
22
|
-
gemspec path: "../"
|