declare_schema 0.13.2 → 0.14.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/declare_schema_build.yml +0 -4
- data/Appraisals +0 -10
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +2 -2
- data/declare_schema.gemspec +1 -1
- data/lib/declare_schema.rb +1 -6
- data/lib/declare_schema/model.rb +2 -14
- data/lib/declare_schema/model/column.rb +3 -11
- data/lib/declare_schema/model/index_definition.rb +0 -26
- data/lib/declare_schema/version.rb +1 -1
- data/lib/generators/declare_schema/migration/migration_generator.rb +2 -15
- data/lib/generators/declare_schema/migration/migrator.rb +3 -11
- 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 +56 -96
- 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 +4 -6
- 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: 444a7dd7bb3bac5706b293e51e2fa97bfa7e774cf0a5ef317b77122d991f10f2
|
4
|
+
data.tar.gz: 5c0d39fa31c10cb994bffc88fc312682896cb422f5b9bda45a7d5fa5cd64c131
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c39f52e48a8e79e75c97133aaac82f481c85e6fc762cba834021cc4dd5d77e8f9e4b78b551f73c9b2c28f9e83818cdc4cb142323a64146d6961ec52df4974382
|
7
|
+
data.tar.gz: 82b743c5c2e387e6a0b0f0c112fa24b699873412242a1cefea5c842c441d3e4169b8ed6a6364b667194d1b2fcfd35ba3567bfdac0ab8e5858e5ee0c04e770f53
|
@@ -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/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
@@ -3,6 +3,9 @@
|
|
3
3
|
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
|
+
## [0.14.0] - 2021-08-10
|
7
|
+
### Deprecated
|
8
|
+
- Dropped support for Rails versions less than 5.
|
6
9
|
|
7
10
|
## [0.13.2] - 2021-08-04
|
8
11
|
### Fixed
|
@@ -192,6 +195,7 @@ using the appropriate Rails configuration attributes.
|
|
192
195
|
### Added
|
193
196
|
- Initial version from https://github.com/Invoca/hobo_fields v4.1.0.
|
194
197
|
|
198
|
+
[0.14.0]: https://github.com/Invoca/declare_schema/compare/v0.13.1...v0.14.0
|
195
199
|
[0.13.1]: https://github.com/Invoca/declare_schema/compare/v0.13.0...v0.13.1
|
196
200
|
[0.13.0]: https://github.com/Invoca/declare_schema/compare/v0.12.1...v0.13.0
|
197
201
|
[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.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,
|
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}"
|
@@ -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
|
@@ -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)
|
@@ -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?
|
@@ -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
|
@@ -489,11 +489,7 @@ module Generators
|
|
489
489
|
parent_columns = connection.columns(parent_table) rescue []
|
490
490
|
pk_limit =
|
491
491
|
if (pk_column = parent_columns.find { |column| column.name.to_s == "id" }) # right now foreign keys assume id is the target
|
492
|
-
|
493
|
-
pk_column.cast_type.limit
|
494
|
-
else
|
495
|
-
pk_column.limit
|
496
|
-
end
|
492
|
+
pk_column.limit
|
497
493
|
else
|
498
494
|
8
|
499
495
|
end
|
@@ -555,12 +551,8 @@ module Generators
|
|
555
551
|
end
|
556
552
|
end
|
557
553
|
|
558
|
-
SchemaDumper =
|
559
|
-
|
560
|
-
ActiveRecord::SchemaDumper
|
561
|
-
else
|
562
|
-
ActiveRecord::ConnectionAdapters::SchemaDumper
|
563
|
-
end
|
554
|
+
SchemaDumper = ActiveRecord::ConnectionAdapters::SchemaDumper
|
555
|
+
|
564
556
|
|
565
557
|
def add_table_back(table)
|
566
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
|
@@ -809,26 +799,28 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
809
799
|
nuke_model_class(Advert)
|
810
800
|
ActiveRecord::Base.connection.execute("drop table `adverts`;")
|
811
801
|
|
812
|
-
|
813
|
-
|
814
|
-
|
815
|
-
|
802
|
+
if !defined?(SQLite3) && ActiveSupport::VERSION::MAJOR >= 5
|
803
|
+
class Advert < ActiveRecord::Base
|
804
|
+
def self.disable_auto_increment
|
805
|
+
true
|
806
|
+
end
|
816
807
|
|
817
|
-
|
818
|
-
|
808
|
+
fields do
|
809
|
+
description :text, limit: 250, null: true
|
810
|
+
end
|
819
811
|
end
|
820
|
-
end
|
821
812
|
|
822
|
-
|
823
|
-
|
824
|
-
|
825
|
-
|
826
|
-
|
827
|
-
|
828
|
-
|
829
|
-
|
830
|
-
|
831
|
-
|
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
|
832
824
|
end
|
833
825
|
|
834
826
|
## DSL
|
@@ -911,7 +903,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
911
903
|
nuke_model_class(Advertiser)
|
912
904
|
nuke_model_class(Affiliate)
|
913
905
|
end
|
914
|
-
end if !defined?(SQLite3)
|
906
|
+
end if !defined?(SQLite3)
|
915
907
|
|
916
908
|
describe 'serialize' do
|
917
909
|
before do
|
@@ -1098,13 +1090,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1098
1090
|
end
|
1099
1091
|
|
1100
1092
|
context "for Rails #{ActiveSupport::VERSION::MAJOR}" do
|
1101
|
-
|
1102
|
-
|
1103
|
-
let(:optional_false) { { optional: false } }
|
1104
|
-
else
|
1105
|
-
let(:optional_true) { {} }
|
1106
|
-
let(:optional_false) { {} }
|
1107
|
-
end
|
1093
|
+
let(:optional_true) { { optional: true } }
|
1094
|
+
let(:optional_false) { { optional: false } }
|
1108
1095
|
let(:optional_flag) { { false => optional_false, true => optional_true } }
|
1109
1096
|
|
1110
1097
|
describe 'belongs_to' do
|
@@ -1204,36 +1191,30 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1204
1191
|
migration_content = File.read(migrations.first)
|
1205
1192
|
first_line = migration_content.split("\n").first
|
1206
1193
|
base_class = first_line.split(' < ').last
|
1207
|
-
expect(base_class).to eq("(
|
1194
|
+
expect(base_class).to eq("(ActiveRecord::Migration[4.2])")
|
1208
1195
|
end
|
1209
1196
|
end
|
1210
1197
|
|
1211
1198
|
context 'Does not generate migrations' do
|
1212
1199
|
it 'for aliased fields bigint -> integer limit 8' do
|
1213
|
-
|
1214
|
-
|
1215
|
-
|
1216
|
-
price :bigint
|
1217
|
-
end
|
1200
|
+
class Advert < active_record_base_class.constantize
|
1201
|
+
fields do
|
1202
|
+
price :bigint
|
1218
1203
|
end
|
1204
|
+
end
|
1219
1205
|
|
1220
|
-
|
1221
|
-
|
1222
|
-
migrations = Dir.glob('db/migrate/*declare_schema_migration*.rb')
|
1223
|
-
expect(migrations.size).to eq(1), migrations.inspect
|
1206
|
+
generate_migrations '-n', '-m'
|
1224
1207
|
|
1225
|
-
|
1226
|
-
|
1227
|
-
end
|
1208
|
+
migrations = Dir.glob('db/migrate/*declare_schema_migration*.rb')
|
1209
|
+
expect(migrations.size).to eq(1), migrations.inspect
|
1228
1210
|
|
1229
|
-
|
1230
|
-
|
1231
|
-
|
1232
|
-
end
|
1211
|
+
class Advert < active_record_base_class.constantize
|
1212
|
+
fields do
|
1213
|
+
price :integer, limit: 8
|
1233
1214
|
end
|
1234
|
-
|
1235
|
-
expect { generate_migrations '-n', '-g' }.to output("Database and models match -- nothing to change\n").to_stdout
|
1236
1215
|
end
|
1216
|
+
|
1217
|
+
expect { generate_migrations '-n', '-g' }.to output("Database and models match -- nothing to change\n").to_stdout
|
1237
1218
|
end
|
1238
1219
|
end
|
1239
1220
|
end
|
@@ -1275,16 +1256,6 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
1275
1256
|
ActiveRecord::Migration.class_eval(up)
|
1276
1257
|
expect(Advert.columns.map(&:name)).to eq(["id", "name"])
|
1277
1258
|
|
1278
|
-
if ActiveSupport::VERSION::MAJOR < 5
|
1279
|
-
# Rails 4 drivers don't always create PK properly. Fix that by dropping and recreating.
|
1280
|
-
ActiveRecord::Base.connection.execute("drop table adverts")
|
1281
|
-
if defined?(Mysql2)
|
1282
|
-
ActiveRecord::Base.connection.execute("CREATE TABLE adverts (id integer PRIMARY KEY AUTO_INCREMENT NOT NULL, name varchar(250)) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin")
|
1283
|
-
else
|
1284
|
-
ActiveRecord::Base.connection.execute("CREATE TABLE adverts (id integer PRIMARY KEY AUTOINCREMENT NOT NULL, name varchar(250))")
|
1285
|
-
end
|
1286
|
-
end
|
1287
|
-
|
1288
1259
|
class Advert < ActiveRecord::Base
|
1289
1260
|
declare_schema do
|
1290
1261
|
string :name, limit: 250, null: true
|
@@ -2071,7 +2042,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
2071
2042
|
nuke_model_class(Advertiser)
|
2072
2043
|
nuke_model_class(Affiliate)
|
2073
2044
|
end
|
2074
|
-
end if !defined?(SQLite3)
|
2045
|
+
end if !defined?(SQLite3)
|
2075
2046
|
|
2076
2047
|
describe 'serialize' do
|
2077
2048
|
before do
|
@@ -2258,13 +2229,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
2258
2229
|
end
|
2259
2230
|
|
2260
2231
|
context "for Rails #{ActiveSupport::VERSION::MAJOR}" do
|
2261
|
-
|
2262
|
-
|
2263
|
-
let(:optional_false) { { optional: false } }
|
2264
|
-
else
|
2265
|
-
let(:optional_true) { {} }
|
2266
|
-
let(:optional_false) { {} }
|
2267
|
-
end
|
2232
|
+
let(:optional_true) { { optional: true } }
|
2233
|
+
let(:optional_false) { { optional: false } }
|
2268
2234
|
let(:optional_flag) { { false => optional_false, true => optional_true } }
|
2269
2235
|
|
2270
2236
|
describe 'belongs_to' do
|
@@ -2373,9 +2339,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
2373
2339
|
end
|
2374
2340
|
class Fk < ActiveRecord::Base
|
2375
2341
|
declare_schema { }
|
2376
|
-
belongs_to :id_default, (
|
2377
|
-
belongs_to :id4, (
|
2378
|
-
belongs_to :id8, (
|
2342
|
+
belongs_to :id_default, ({})
|
2343
|
+
belongs_to :id4, ({})
|
2344
|
+
belongs_to :id8, ({})
|
2379
2345
|
end
|
2380
2346
|
end
|
2381
2347
|
|
@@ -2486,36 +2452,30 @@ RSpec.describe 'DeclareSchema Migration Generator' do
|
|
2486
2452
|
migration_content = File.read(migrations.first)
|
2487
2453
|
first_line = migration_content.split("\n").first
|
2488
2454
|
base_class = first_line.split(' < ').last
|
2489
|
-
expect(base_class).to eq("(
|
2455
|
+
expect(base_class).to eq("(ActiveRecord::Migration[4.2])")
|
2490
2456
|
end
|
2491
2457
|
end
|
2492
2458
|
|
2493
2459
|
context 'Does not generate migrations' do
|
2494
2460
|
it 'for aliased fields bigint -> integer limit 8' do
|
2495
|
-
|
2496
|
-
|
2497
|
-
|
2498
|
-
bigint :price
|
2499
|
-
end
|
2461
|
+
class Advert < active_record_base_class.constantize
|
2462
|
+
declare_schema do
|
2463
|
+
bigint :price
|
2500
2464
|
end
|
2465
|
+
end
|
2501
2466
|
|
2502
|
-
|
2503
|
-
|
2504
|
-
migrations = Dir.glob('db/migrate/*declare_schema_migration*.rb')
|
2505
|
-
expect(migrations.size).to eq(1), migrations.inspect
|
2467
|
+
generate_migrations '-n', '-m'
|
2506
2468
|
|
2507
|
-
|
2508
|
-
|
2509
|
-
end
|
2469
|
+
migrations = Dir.glob('db/migrate/*declare_schema_migration*.rb')
|
2470
|
+
expect(migrations.size).to eq(1), migrations.inspect
|
2510
2471
|
|
2511
|
-
|
2512
|
-
|
2513
|
-
|
2514
|
-
end
|
2472
|
+
class Advert < active_record_base_class.constantize
|
2473
|
+
declare_schema do
|
2474
|
+
integer :price, limit: 8
|
2515
2475
|
end
|
2516
|
-
|
2517
|
-
expect { generate_migrations '-n', '-g' }.to output("Database and models match -- nothing to change\n").to_stdout
|
2518
2476
|
end
|
2477
|
+
|
2478
|
+
expect { generate_migrations '-n', '-g' }.to output("Database and models match -- nothing to change\n").to_stdout
|
2519
2479
|
end
|
2520
2480
|
end
|
2521
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.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-08-
|
11
|
+
date: 2021-08-10 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
|
@@ -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: "../"
|