declare_schema 1.5.0.pre.1 → 2.1.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/.devcontainer/Dockerfile +19 -0
  3. data/.devcontainer/boot.sh +1 -0
  4. data/.devcontainer/devcontainer.json +55 -0
  5. data/.devcontainer/docker-compose.yml +40 -0
  6. data/.github/workflows/declare_schema_build.yml +35 -11
  7. data/Appraisals +2 -10
  8. data/CHANGELOG.md +8 -1
  9. data/CODE-OF-CONDUCT.md +16 -0
  10. data/CONTRIBUTING.md +46 -0
  11. data/Gemfile +6 -1
  12. data/Gemfile.lock +88 -78
  13. data/README.md +38 -14
  14. data/Rakefile +5 -15
  15. data/catalog-info.yaml +35 -0
  16. data/config/brakeman.ignore +2 -2
  17. data/declare_schema.gemspec +1 -1
  18. data/gemfiles/{rails_6_1_sqlite3.gemfile → rails_6_1.gemfile} +3 -0
  19. data/gemfiles/{rails_7_0_sqlite3.gemfile → rails_7_0.gemfile} +3 -0
  20. data/gemfiles/{rails_7_1_sqlite3.gemfile → rails_7_1.gemfile} +3 -0
  21. data/gemfiles/{rails_7_0_mysql2.gemfile → rails_7_2.gemfile} +4 -1
  22. data/lib/declare_schema/command.rb +2 -8
  23. data/lib/declare_schema/model/column.rb +1 -1
  24. data/lib/declare_schema/model/foreign_key_definition.rb +9 -12
  25. data/lib/declare_schema/model/index_definition.rb +16 -8
  26. data/lib/declare_schema/model.rb +10 -14
  27. data/lib/declare_schema/schema_change/base.rb +4 -0
  28. data/lib/declare_schema/schema_change/primary_key_change.rb +19 -6
  29. data/lib/declare_schema/version.rb +1 -1
  30. data/lib/declare_schema.rb +20 -8
  31. data/lib/generators/declare_schema/migration/migration_generator.rb +17 -8
  32. data/lib/generators/declare_schema/migration/migrator.rb +16 -9
  33. data/spec/fixtures/migrations/mysql2/will_generate_unique_constraint_names_rails_6.txt +15 -0
  34. data/spec/fixtures/migrations/mysql2/will_generate_unique_constraint_names_rails_7.txt +15 -0
  35. data/spec/fixtures/migrations/postgresql/will_generate_unique_constraint_names_rails_6.txt +15 -0
  36. data/spec/fixtures/migrations/postgresql/will_generate_unique_constraint_names_rails_7.txt +15 -0
  37. data/spec/fixtures/migrations/sqlite3/will_generate_unique_constraint_names_rails_6.txt +15 -0
  38. data/spec/fixtures/migrations/sqlite3/will_generate_unique_constraint_names_rails_7.txt +15 -0
  39. data/spec/lib/declare_schema/api_spec.rb +1 -7
  40. data/spec/lib/declare_schema/field_declaration_dsl_spec.rb +3 -3
  41. data/spec/lib/declare_schema/field_spec_spec.rb +68 -45
  42. data/spec/lib/declare_schema/generator_spec.rb +29 -71
  43. data/spec/lib/declare_schema/interactive_primary_key_spec.rb +3 -10
  44. data/spec/lib/declare_schema/migration_generator_spec.rb +249 -255
  45. data/spec/lib/declare_schema/model/column_spec.rb +89 -41
  46. data/spec/lib/declare_schema/model/foreign_key_definition_spec.rb +14 -8
  47. data/spec/lib/declare_schema/model/habtm_model_shim_spec.rb +4 -9
  48. data/spec/lib/declare_schema/model/index_definition_spec.rb +18 -10
  49. data/spec/lib/declare_schema/model/table_options_definition_spec.rb +11 -11
  50. data/spec/lib/declare_schema/schema_change/base_spec.rb +5 -7
  51. data/spec/lib/declare_schema/schema_change/column_add_spec.rb +1 -3
  52. data/spec/lib/declare_schema/schema_change/column_change_spec.rb +1 -3
  53. data/spec/lib/declare_schema/schema_change/column_remove_spec.rb +1 -3
  54. data/spec/lib/declare_schema/schema_change/column_rename_spec.rb +1 -3
  55. data/spec/lib/declare_schema/schema_change/foreign_key_add_spec.rb +1 -3
  56. data/spec/lib/declare_schema/schema_change/foreign_key_remove_spec.rb +1 -3
  57. data/spec/lib/declare_schema/schema_change/index_add_spec.rb +1 -3
  58. data/spec/lib/declare_schema/schema_change/index_remove_spec.rb +1 -3
  59. data/spec/lib/declare_schema/schema_change/primary_key_change_spec.rb +39 -15
  60. data/spec/lib/declare_schema/schema_change/table_add_spec.rb +1 -3
  61. data/spec/lib/declare_schema/schema_change/table_change_spec.rb +1 -3
  62. data/spec/lib/declare_schema/schema_change/table_remove_spec.rb +1 -3
  63. data/spec/lib/declare_schema/schema_change/table_rename_spec.rb +1 -3
  64. data/spec/lib/generators/declare_schema/migration/migrator_spec.rb +0 -4
  65. data/spec/spec_helper.rb +3 -0
  66. data/spec/support/adapter_specific_test_helpers.rb +25 -0
  67. data/spec/{lib/declare_schema → support}/prepare_testapp.rb +3 -1
  68. data/spec/support/test_app_spec_helpers.rb +7 -0
  69. metadata +24 -11
  70. data/gemfiles/rails_6_1_mysql2.gemfile +0 -23
  71. data/gemfiles/rails_7_1_mysql2.gemfile +0 -23
@@ -1,22 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- begin
4
- require 'mysql2'
5
- rescue LoadError
6
- end
7
-
8
3
  require_relative '../../../../lib/declare_schema/model/column'
9
4
 
10
5
  RSpec.describe DeclareSchema::Model::Column do
11
- before do
12
- load File.expand_path('../prepare_testapp.rb', __dir__)
13
- end
6
+ include_context 'prepare test app'
14
7
 
15
8
  describe 'class methods' do
16
9
  describe '.native_type?' do
17
10
  let(:native_types) { [:string, :text, :integer, :float, :decimal, :datetime, :time, :date, :binary, :boolean, :json] }
18
11
 
19
-
20
12
  it 'is falsey for :primary_key' do
21
13
  expect(described_class.native_type?(:primary_key)).to be_falsey
22
14
  end
@@ -40,60 +32,116 @@ RSpec.describe DeclareSchema::Model::Column do
40
32
  end
41
33
 
42
34
  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/)
35
+ subject(:native_types) { described_class.native_types }
36
+
37
+ describe 'primary_key' do
38
+ subject { native_types[:primary_key] }
39
+ let(:expected_name) do
40
+ case current_adapter
41
+ when 'mysql2'
42
+ 'bigint auto_increment PRIMARY KEY'
43
+ when 'postgresql'
44
+ 'bigserial primary key'
45
+ when 'sqlite3'
46
+ 'integer PRIMARY KEY AUTOINCREMENT NOT NULL'
47
+ end
48
+ end
49
+ it { is_expected.to eq(expected_name) }
47
50
  end
48
51
 
49
- it 'returns the native type for :string' do
50
- expect(subject.dig(:string, :name)).to eq('varchar')
52
+ describe 'string' do
53
+ subject { native_types[:string] }
54
+ let(:expected_name) { current_adapter == 'postgresql' ? 'character varying' : 'varchar' }
55
+ it { is_expected.to include(name: expected_name) }
51
56
  end
52
57
 
53
- it 'returns the native type for :integer' do
54
- expect(subject.dig(:integer, :name)).to match(/int/)
58
+ describe 'integer' do
59
+ subject { native_types[:integer] }
60
+ let(:expected_name) { /int/ }
61
+ it { is_expected.to include(name: expected_name) }
55
62
  end
56
63
 
57
- it 'returns the native type for :datetime' do
58
- expect(subject.dig(:datetime, :name)).to eq('datetime')
64
+ describe 'datetime' do
65
+ subject { native_types[:datetime] }
66
+ let(:expected_name) { current_adapter == 'postgresql' ? 'timestamp' : 'datetime' }
67
+ it { is_expected.to include(name: expected_name) }
59
68
  end
60
69
  end
61
70
 
62
71
  describe '.deserialize_default_value' do
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)
72
+ subject { described_class.deserialize_default_value(col_spec, column_type, default) }
73
+
74
+ context 'when deserializing a boolean' do
75
+ let(:column_type) { :boolean }
76
+ let(:col_spec) do
77
+ if current_adapter == 'postgresql'
78
+ instance_double(ActiveRecord::ConnectionAdapters::PostgreSQL::Column, type: :boolean, sql_type: "boolean", oid: 16, fmod: -1)
79
+ else
80
+ instance_double(ActiveRecord::ConnectionAdapters::Column, type: :boolean, sql_type: "boolean")
81
+ end
82
+ end
83
+
84
+ context 'when the default is true' do
85
+ let(:default) { 'true' }
86
+ it { is_expected.to eq(true) }
87
+ end
88
+
89
+ context 'when the default is false' do
90
+ let(:default) { 'false' }
91
+ it { is_expected.to eq(false) }
92
+ end
66
93
  end
67
94
 
68
- it 'deserializes :integer' do
69
- expect(described_class.deserialize_default_value(nil, :integer, '12')).to eq(12)
95
+ context 'when deserializing an integer' do
96
+ let(:column_type) { :integer }
97
+ let(:default) { '12' }
98
+ let(:col_spec) do
99
+ if current_adapter == 'postgresql'
100
+ instance_double(ActiveRecord::ConnectionAdapters::PostgreSQL::Column, type: :integer, sql_type: "integer", limit: 4, oid: 23, fmod: -1)
101
+ else
102
+ instance_double(ActiveRecord::ConnectionAdapters::Column, type: :integer, sql_type: "integer", limit: 4)
103
+ end
104
+ end
105
+
106
+ it { is_expected.to eq(12) }
70
107
  end
71
108
 
72
- it 'deserializes :json' do
73
- expect(described_class.deserialize_default_value(nil, :json, '{}')).to eq({})
109
+ context 'when deserializing json' do
110
+ let(:column_type) { :json }
111
+ let(:default) { '{}' }
112
+ let(:col_spec) do
113
+ if current_adapter == 'postgresql'
114
+ instance_double(ActiveRecord::ConnectionAdapters::PostgreSQL::Column, type: :json, sql_type: "json", oid: 114, fmod: -1)
115
+ else
116
+ instance_double(ActiveRecord::ConnectionAdapters::Column, type: :json, sql_type: "json")
117
+ end
118
+ end
119
+
120
+ it { is_expected.to eq({}) }
74
121
  end
75
122
  end
76
123
  end
77
124
 
78
125
  describe 'instance methods' do
126
+ subject { described_class.new(model, current_table_name, column) }
127
+
79
128
  let(:model) { ColumnTestModel }
80
129
  let(:type) { :integer }
81
130
  let(:current_table_name) { model.table_name }
82
- let(:column) { double("ActiveRecord Column",
83
- name: 'count',
84
- type: type,
85
- limit: nil,
86
- precision: nil,
87
- scale: nil,
88
- type_cast_from_database: nil,
89
- null: false,
90
- default: nil,
91
- sql_type_metadata: {}) }
92
- subject { described_class.new(model, current_table_name, column) }
131
+ let(:column) do
132
+ if current_adapter == 'postgresql'
133
+ instance_double(ActiveRecord::ConnectionAdapters::PostgreSQL::Column,
134
+ name: 'count', type: type, sql_type: type, limit: 4, precision: nil, scale: nil, null: false, default: nil,
135
+ oid: 23, fmod: -1)
136
+ else
137
+ instance_double(ActiveRecord::ConnectionAdapters::Column, name: 'count', type: type, sql_type: type, limit: nil,
138
+ precision: nil, scale: nil, null: false, default: nil)
139
+ end
140
+ end
93
141
 
94
142
  context 'Using declare_schema' do
95
143
  before do
96
- class ColumnTestModel < ActiveRecord::Base
144
+ class ColumnTestModel < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
97
145
  declare_schema do
98
146
  string :title, limit: 127, null: false
99
147
  integer :count, null: false
@@ -109,10 +157,10 @@ RSpec.describe DeclareSchema::Model::Column do
109
157
 
110
158
  describe '#schema_attributes' do
111
159
  it 'returns a hash with relevant key/values' do
112
- if defined?(Mysql2)
113
- expect(subject.schema_attributes).to eq(type: :integer, null: false, limit: 4)
114
- else
160
+ if current_adapter == 'sqlite3'
115
161
  expect(subject.schema_attributes).to eq(type: :integer, null: false)
162
+ else
163
+ expect(subject.schema_attributes).to eq(type: :integer, null: false, limit: 4)
116
164
  end
117
165
  end
118
166
  end
@@ -3,13 +3,13 @@
3
3
  require_relative '../../../../lib/declare_schema/model/foreign_key_definition'
4
4
 
5
5
  RSpec.describe DeclareSchema::Model::ForeignKeyDefinition do
6
+ include_context 'prepare test app'
7
+
6
8
  let(:model_class) { Network }
7
9
 
8
10
  context 'Using declare_schema' do
9
11
  before do
10
- load File.expand_path('../prepare_testapp.rb', __dir__)
11
-
12
- class Network < ActiveRecord::Base
12
+ class Network < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
13
13
  declare_schema do
14
14
  string :name, limit: 127, index: true
15
15
 
@@ -95,13 +95,19 @@ RSpec.describe DeclareSchema::Model::ForeignKeyDefinition do
95
95
  end
96
96
 
97
97
  describe 'class << self' do
98
- let(:connection) { instance_double(ActiveRecord::Base.connection.class) }
99
- let(:model) { instance_double('Model', table_name: 'models', connection: connection) }
98
+ let(:connection) { instance_double(ActiveRecord::Base.connection.class) }
99
+ let(:model) { instance_double('Model', table_name: 'models', connection: connection) }
100
100
  let(:old_table_name) { 'networks' }
101
+ let(:foreign_keys) do
102
+ [
103
+ instance_double(ActiveRecord::ConnectionAdapters::ForeignKeyDefinition,
104
+ column: 'network_id', name: 'constraint',
105
+ from_table: 'networks', to_table: 'models', on_delete: nil)
106
+ ]
107
+ end
108
+
101
109
  before do
102
- allow(connection).to receive(:quote_table_name).with('networks') { 'networks' }
103
- allow(connection).to receive(:select_rows) { [['CONSTRAINT `constraint` FOREIGN KEY (`network_id`) REFERENCES `networks` (`id`)']] }
104
- allow(connection).to receive(:index_name).with('models', column: 'network_id') { }
110
+ allow(connection).to receive(:foreign_keys).with(old_table_name) { foreign_keys }
105
111
  end
106
112
 
107
113
  describe '.for_table' do
@@ -1,26 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- begin
4
- require 'mysql2'
5
- rescue LoadError
6
- end
7
-
8
3
  require_relative '../../../../lib/declare_schema/model/habtm_model_shim'
9
4
 
10
5
  RSpec.describe DeclareSchema::Model::HabtmModelShim do
6
+ include_context 'prepare test app'
7
+
11
8
  let(:join_table) { "customers_users" }
12
9
  let(:foreign_keys) { ["user_id", "customer_id"] }
13
10
  let(:parent_table_names) { ["users", "customers"] }
14
11
  let(:connection) { instance_double(ActiveRecord::Base.connection.class, "connection") }
15
12
 
16
13
  before do
17
- load File.expand_path('../prepare_testapp.rb', __dir__)
18
-
19
- class User < ActiveRecord::Base
14
+ class User < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
20
15
  self.table_name = "users"
21
16
  end
22
17
 
23
- class Customer < ActiveRecord::Base
18
+ class Customer < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
24
19
  self.table_name = "customers"
25
20
  end
26
21
  end
@@ -14,10 +14,10 @@ RSpec.describe DeclareSchema::Model::IndexDefinition do
14
14
  let(:table_name) { model_class.table_name }
15
15
 
16
16
  context 'Using declare_schema' do
17
- before do
18
- load File.expand_path('../prepare_testapp.rb', __dir__)
17
+ include_context 'prepare test app'
19
18
 
20
- class IndexDefinitionTestModel < ActiveRecord::Base
19
+ before do
20
+ class IndexDefinitionTestModel < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
21
21
  declare_schema do
22
22
  string :name, limit: 127, index: true
23
23
 
@@ -25,7 +25,7 @@ RSpec.describe DeclareSchema::Model::IndexDefinition do
25
25
  end
26
26
  end
27
27
 
28
- class IndexDefinitionCompoundIndexModel < ActiveRecord::Base
28
+ class IndexDefinitionCompoundIndexModel < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
29
29
  declare_schema do
30
30
  integer :fk1_id
31
31
  integer :fk2_id
@@ -135,13 +135,13 @@ RSpec.describe DeclareSchema::Model::IndexDefinition do
135
135
  ActiveRecord::Base.connection.execute <<~EOS
136
136
  CREATE TABLE index_definition_test_models (
137
137
  id INTEGER NOT NULL PRIMARY KEY,
138
- name #{if defined?(SQLite3) then 'TEXT' else 'VARCHAR(255)' end} NOT NULL
138
+ name #{if current_adapter == 'sqlite3' then 'TEXT' else 'VARCHAR(255)' end} NOT NULL
139
139
  )
140
140
  EOS
141
141
  ActiveRecord::Base.connection.execute <<~EOS
142
142
  CREATE UNIQUE INDEX index_definition_test_models_on_name ON index_definition_test_models(name)
143
143
  EOS
144
- if defined?(Mysql2)
144
+ if current_adapter == 'mysql2'
145
145
  ActiveRecord::Base.connection.execute <<~EOS
146
146
  CREATE INDEX index_definition_test_models_on_name_partial ON index_definition_test_models(name(10))
147
147
  EOS
@@ -164,7 +164,7 @@ RSpec.describe DeclareSchema::Model::IndexDefinition do
164
164
  it 'returns the indexes for the model' do
165
165
  expect(subject.map(&:to_key)).to eq([
166
166
  ["index_definition_test_models_on_name", ["name"], { unique: true, where: nil, length: nil }],
167
- (["index_definition_test_models_on_name_partial", ["name"], { unique: false, where: nil, length: { name: 10 } }] if defined?(Mysql2)),
167
+ (["index_definition_test_models_on_name_partial", ["name"], { unique: false, where: nil, length: { name: 10 } }] if current_adapter == 'mysql2'),
168
168
  ["PRIMARY", ["id"], { unique: true, where: nil, length: nil }]
169
169
  ].compact)
170
170
  end
@@ -185,7 +185,7 @@ RSpec.describe DeclareSchema::Model::IndexDefinition do
185
185
 
186
186
  it 'skips the ignored index' do
187
187
  expect(subject.map(&:to_key)).to eq([
188
- (["index_definition_test_models_on_name_partial", ["name"], { unique: false, where: nil, length: { name: 10 } }] if defined?(Mysql2)),
188
+ (["index_definition_test_models_on_name_partial", ["name"], { unique: false, where: nil, length: { name: 10 } }] if current_adapter == 'mysql2'),
189
189
  ["PRIMARY", ["id"], { length: nil, unique: true, where: nil }]
190
190
  ].compact)
191
191
  end
@@ -214,12 +214,20 @@ RSpec.describe DeclareSchema::Model::IndexDefinition do
214
214
  end
215
215
 
216
216
  context 'with short max_index_and_constraint_name_length' do
217
+ include_context 'skip if' do
218
+ let(:adapter) { 'postgresql' }
219
+ end
220
+
217
221
  let(:max_index_and_constraint_name_length) { 40 }
218
222
 
219
223
  it { is_expected.to eq("users__last_name_first_name_middle_name") }
220
224
  end
221
225
 
222
226
  context 'with long table name' do
227
+ include_context 'skip if' do
228
+ let(:adapter) { 'postgresql' }
229
+ end
230
+
223
231
  let(:table_name2) { 'user_domains_extra' }
224
232
  {
225
233
  34 => '__last_name_first_name_middle_name',
@@ -279,7 +287,7 @@ RSpec.describe DeclareSchema::Model::IndexDefinition do
279
287
  context 'with multiple columns' do
280
288
  let(:columns) { ["last_name", "first_name"] }
281
289
 
282
- it { expect { subject }.to raise_exception(ArgumentError, /Index length of Integer only allowed when exactly one column; got 10 for \["last_name", "first_name"]/i) }
290
+ it { expect { subject }.to raise_exception(ArgumentError, /Index length of Integer only allowed when exactly one column; got 10 for \["last_name", "first_name"\]/i) }
283
291
  end
284
292
  end
285
293
 
@@ -320,7 +328,7 @@ RSpec.describe DeclareSchema::Model::IndexDefinition do
320
328
  context 'with an invalid length' do
321
329
  let(:length) { 10.5 }
322
330
 
323
- it { expect { subject }.to raise_exception(ArgumentError, /length must be nil or Integer or a Hash of column names to lengths; got 10\.5 for \[:last_name]/i) }
331
+ it { expect { subject }.to raise_exception(ArgumentError, /length must be nil or Integer or a Hash of column names to lengths; got 10\.5 for \[:last_name\]/i) }
324
332
  end
325
333
  end
326
334
  end
@@ -1,22 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- begin
4
- require 'mysql2'
5
- require 'active_record/connection_adapters/mysql2_adapter'
6
- rescue LoadError
7
- end
8
3
  require_relative '../../../../lib/declare_schema/model/table_options_definition'
9
4
 
10
5
  RSpec.describe DeclareSchema::Model::TableOptionsDefinition do
6
+ include_context 'prepare test app'
7
+
11
8
  let(:model_class) { TableOptionsDefinitionTestModel }
12
9
  let(:charset) { DeclareSchema.normalize_charset('utf8') }
13
10
  let(:collation) { DeclareSchema.normalize_collation('utf8_general') } # adapt so that tests will pass on MySQL 5.7 or 8+
14
11
 
15
12
  context 'Using declare_schema' do
16
13
  before do
17
- load File.expand_path('../prepare_testapp.rb', __dir__)
18
-
19
- class TableOptionsDefinitionTestModel < ActiveRecord::Base
14
+ class TableOptionsDefinitionTestModel < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
20
15
  declare_schema do
21
16
  string :name, limit: 127, index: true
22
17
  end
@@ -36,7 +31,11 @@ RSpec.describe DeclareSchema::Model::TableOptionsDefinition do
36
31
  subject { model.settings }
37
32
  it { is_expected.to eq("CHARACTER SET #{charset} COLLATE #{collation}") }
38
33
 
39
- if defined?(Mysql2)
34
+ context 'MySQL only' do
35
+ include_context 'skip unless' do
36
+ let(:adapter) { 'mysql2' }
37
+ end
38
+
40
39
  context 'when running in MySQL 8' do
41
40
  around do |spec|
42
41
  DeclareSchema.mysql_version = Gem::Version.new('8.0.21')
@@ -75,10 +74,11 @@ RSpec.describe DeclareSchema::Model::TableOptionsDefinition do
75
74
  describe '#for_model' do
76
75
  context 'when database migrated' do
77
76
  let(:options) do
78
- if defined?(Mysql2)
77
+ case current_adapter(model_class)
78
+ when 'mysql2'
79
79
  { charset: "utf8mb4", collation: "utf8mb4_bin" }
80
80
  else
81
- { }
81
+ {}
82
82
  end
83
83
  end
84
84
  subject { described_class.for_model(model_class) }
@@ -3,24 +3,22 @@
3
3
  require_relative '../../../../lib/declare_schema/schema_change/base'
4
4
 
5
5
  RSpec.describe DeclareSchema::SchemaChange::Base do
6
- before do
7
- load File.expand_path('../prepare_testapp.rb', __dir__)
8
- end
6
+ include_context 'prepare test app'
9
7
 
10
8
  before :all do
11
- class ChangeDefault < described_class
9
+ class ChangeDefault < described_class # rubocop:disable Lint/ConstantDefinitionInBlock
12
10
  attr_reader :up_command, :down_command
13
11
 
14
- def initialize(up:, down:)
12
+ def initialize(up:, down:) # rubocop:disable Naming/MethodParameterName
15
13
  @up_command = up
16
14
  @down_command = down
17
15
  end
18
16
  end
19
17
 
20
- class ChangeOverride < described_class
18
+ class ChangeOverride < described_class # rubocop:disable Lint/ConstantDefinitionInBlock
21
19
  attr_reader :up_command, :down_command
22
20
 
23
- def initialize(up:, down:)
21
+ def initialize(up:, down:) # rubocop:disable Naming/MethodParameterName
24
22
  @up_command = up
25
23
  @down_command = down
26
24
  end
@@ -3,9 +3,7 @@
3
3
  require_relative '../../../../lib/declare_schema/schema_change/column_add'
4
4
 
5
5
  RSpec.describe DeclareSchema::SchemaChange::ColumnAdd do
6
- before do
7
- load File.expand_path('../prepare_testapp.rb', __dir__)
8
- end
6
+ include_context 'prepare test app'
9
7
 
10
8
  let(:table_name) { 'networks' }
11
9
  let(:column_name) { 'title' }
@@ -3,9 +3,7 @@
3
3
  require_relative '../../../../lib/declare_schema/schema_change/column_change'
4
4
 
5
5
  RSpec.describe DeclareSchema::SchemaChange::ColumnChange do
6
- before do
7
- load File.expand_path('../prepare_testapp.rb', __dir__)
8
- end
6
+ include_context 'prepare test app'
9
7
 
10
8
  let(:table_name) { 'networks' }
11
9
  let(:column_name) { 'title' }
@@ -3,9 +3,7 @@
3
3
  require_relative '../../../../lib/declare_schema/schema_change/column_remove'
4
4
 
5
5
  RSpec.describe DeclareSchema::SchemaChange::ColumnRemove do
6
- before do
7
- load File.expand_path('../prepare_testapp.rb', __dir__)
8
- end
6
+ include_context 'prepare test app'
9
7
 
10
8
  let(:table_name) { 'networks' }
11
9
  let(:column_name) { 'title' }
@@ -3,9 +3,7 @@
3
3
  require_relative '../../../../lib/declare_schema/schema_change/column_rename'
4
4
 
5
5
  RSpec.describe DeclareSchema::SchemaChange::ColumnRename do
6
- before do
7
- load File.expand_path('../prepare_testapp.rb', __dir__)
8
- end
6
+ include_context 'prepare test app'
9
7
 
10
8
  let(:table_name) { 'networks' }
11
9
  let(:old_name) { 'title' }
@@ -3,9 +3,7 @@
3
3
  require_relative '../../../../lib/declare_schema/schema_change/foreign_key_add'
4
4
 
5
5
  RSpec.describe DeclareSchema::SchemaChange::ForeignKeyAdd do
6
- before do
7
- load File.expand_path('../prepare_testapp.rb', __dir__)
8
- end
6
+ include_context 'prepare test app'
9
7
 
10
8
  let(:table_name) { 'users' }
11
9
  let(:parent_table_name) { 'organization' }
@@ -3,9 +3,7 @@
3
3
  require_relative '../../../../lib/declare_schema/schema_change/foreign_key_remove'
4
4
 
5
5
  RSpec.describe DeclareSchema::SchemaChange::ForeignKeyRemove do
6
- before do
7
- load File.expand_path('../prepare_testapp.rb', __dir__)
8
- end
6
+ include_context 'prepare test app'
9
7
 
10
8
  let(:table_name) { 'users' }
11
9
  let(:parent_table_name) { 'organization' }
@@ -3,9 +3,7 @@
3
3
  require_relative '../../../../lib/declare_schema/schema_change/index_add'
4
4
 
5
5
  RSpec.describe DeclareSchema::SchemaChange::IndexAdd do
6
- before do
7
- load File.expand_path('../prepare_testapp.rb', __dir__)
8
- end
6
+ include_context 'prepare test app'
9
7
 
10
8
  let(:table_name) { 'users' }
11
9
  let(:column_names) { [:last_name, 'first_name'] }
@@ -3,9 +3,7 @@
3
3
  require_relative '../../../../lib/declare_schema/schema_change/index_remove'
4
4
 
5
5
  RSpec.describe DeclareSchema::SchemaChange::IndexRemove do
6
- before do
7
- load File.expand_path('../prepare_testapp.rb', __dir__)
8
- end
6
+ include_context 'prepare test app'
9
7
 
10
8
  let(:table_name) { 'users' }
11
9
  let(:column_names) { [:last_name, 'first_name'] }
@@ -3,9 +3,7 @@
3
3
  require_relative '../../../../lib/declare_schema/schema_change/primary_key_change'
4
4
 
5
5
  RSpec.describe DeclareSchema::SchemaChange::PrimaryKeyChange do
6
- before do
7
- load File.expand_path('../prepare_testapp.rb', __dir__)
8
- end
6
+ include_context 'prepare test app'
9
7
 
10
8
  let(:table_name) { 'users' }
11
9
  let(:old_column_names) { ['id'] }
@@ -17,15 +15,25 @@ RSpec.describe DeclareSchema::SchemaChange::PrimaryKeyChange do
17
15
  context 'when PRIMARY KEY set -> set' do
18
16
  describe '#up' do
19
17
  it 'responds with command' do
20
- command = "ALTER TABLE #{ActiveRecord::Base.connection.quote_table_name(table_name)} DROP PRIMARY KEY, ADD PRIMARY KEY (#{new_column_names.join(', ')})"
21
- expect(subject.up).to eq("execute #{command.inspect}\n")
18
+ if current_adapter == 'postgresql'
19
+ expect(subject.up.split("\n")).to include('execute "ALTER TABLE \"users\" DROP CONSTRAINT users_pkey;"')
20
+ expect(subject.up.split("\n")).to include('execute "ALTER TABLE \"users\" ADD PRIMARY KEY (last_name, first_name);"')
21
+ else
22
+ command = "ALTER TABLE #{ActiveRecord::Base.connection.quote_table_name(table_name)} DROP PRIMARY KEY, ADD PRIMARY KEY (last_name, first_name)"
23
+ expect(subject.up).to eq("execute #{command.inspect}\n")
24
+ end
22
25
  end
23
26
  end
24
27
 
25
28
  describe '#down' do
26
29
  it 'responds with command' do
27
- command = "ALTER TABLE #{ActiveRecord::Base.connection.quote_table_name(table_name)} DROP PRIMARY KEY, ADD PRIMARY KEY (#{old_column_names.join(', ')})"
28
- expect(subject.down).to eq("execute #{command.inspect}\n")
30
+ if current_adapter == 'postgresql'
31
+ expect(subject.down.split("\n")).to include('execute "ALTER TABLE \"users\" DROP CONSTRAINT users_pkey;"')
32
+ expect(subject.down.split("\n")).to include('execute "ALTER TABLE \"users\" ADD PRIMARY KEY (id);"')
33
+ else
34
+ command = "ALTER TABLE #{ActiveRecord::Base.connection.quote_table_name(table_name)} DROP PRIMARY KEY, ADD PRIMARY KEY (id)"
35
+ expect(subject.down).to eq("execute #{command.inspect}\n")
36
+ end
29
37
  end
30
38
  end
31
39
  end
@@ -35,15 +43,23 @@ RSpec.describe DeclareSchema::SchemaChange::PrimaryKeyChange do
35
43
 
36
44
  describe '#up' do
37
45
  it 'responds with command' do
38
- command = "ALTER TABLE #{ActiveRecord::Base.connection.quote_table_name(table_name)} ADD PRIMARY KEY (#{new_column_names.join(', ')})"
39
- expect(subject.up).to eq("execute #{command.inspect}\n")
46
+ if current_adapter == 'postgresql'
47
+ expect(subject.up.split("\n")).to include('execute "ALTER TABLE \"users\" ADD PRIMARY KEY (last_name, first_name);"')
48
+ else
49
+ command = "ALTER TABLE #{ActiveRecord::Base.connection.quote_table_name(table_name)} ADD PRIMARY KEY (#{new_column_names.join(', ')})"
50
+ expect(subject.up).to eq("execute #{command.inspect}\n")
51
+ end
40
52
  end
41
53
  end
42
54
 
43
55
  describe '#down' do
44
56
  it 'responds with command' do
45
- command = "ALTER TABLE #{ActiveRecord::Base.connection.quote_table_name(table_name)} DROP PRIMARY KEY"
46
- expect(subject.down).to eq("execute #{command.inspect}\n")
57
+ if current_adapter == 'postgresql'
58
+ expect(subject.down.split("\n")).to include('execute "ALTER TABLE \"users\" DROP CONSTRAINT users_pkey;"')
59
+ else
60
+ command = "ALTER TABLE #{ActiveRecord::Base.connection.quote_table_name(table_name)} DROP PRIMARY KEY"
61
+ expect(subject.down).to eq("execute #{command.inspect}\n")
62
+ end
47
63
  end
48
64
  end
49
65
  end
@@ -53,15 +69,23 @@ RSpec.describe DeclareSchema::SchemaChange::PrimaryKeyChange do
53
69
 
54
70
  describe '#up' do
55
71
  it 'responds with command' do
56
- command = "ALTER TABLE #{ActiveRecord::Base.connection.quote_table_name(table_name)} DROP PRIMARY KEY"
57
- expect(subject.up).to eq("execute #{command.inspect}\n")
72
+ if current_adapter == 'postgresql'
73
+ expect(subject.up.split("\n")).to include('execute "ALTER TABLE \"users\" DROP CONSTRAINT users_pkey;"')
74
+ else
75
+ command = "ALTER TABLE #{ActiveRecord::Base.connection.quote_table_name(table_name)} DROP PRIMARY KEY"
76
+ expect(subject.up).to eq("execute #{command.inspect}\n")
77
+ end
58
78
  end
59
79
  end
60
80
 
61
81
  describe '#down' do
62
82
  it 'responds with command' do
63
- command = "ALTER TABLE #{ActiveRecord::Base.connection.quote_table_name(table_name)} ADD PRIMARY KEY (#{old_column_names.join(', ')})"
64
- expect(subject.down).to eq("execute #{command.inspect}\n")
83
+ if current_adapter == 'postgresql'
84
+ expect(subject.down.split("\n")).to include('execute "ALTER TABLE \"users\" ADD PRIMARY KEY (id);"')
85
+ else
86
+ command = "ALTER TABLE #{ActiveRecord::Base.connection.quote_table_name(table_name)} ADD PRIMARY KEY (#{old_column_names.join(', ')})"
87
+ expect(subject.down).to eq("execute #{command.inspect}\n")
88
+ end
65
89
  end
66
90
  end
67
91
  end
@@ -3,9 +3,7 @@
3
3
  require_relative '../../../../lib/declare_schema/schema_change/table_add'
4
4
 
5
5
  RSpec.describe DeclareSchema::SchemaChange::TableAdd do
6
- before do
7
- load File.expand_path('../prepare_testapp.rb', __dir__)
8
- end
6
+ include_context 'prepare test app'
9
7
 
10
8
  let(:table_name) { 'networks' }
11
9
  let(:fields) { [[:string, :title, limit: 255, null: false ], [:boolean, :admin, null: false]] }
@@ -3,9 +3,7 @@
3
3
  require_relative '../../../../lib/declare_schema/schema_change/table_change'
4
4
 
5
5
  RSpec.describe DeclareSchema::SchemaChange::TableChange do
6
- before do
7
- load File.expand_path('../prepare_testapp.rb', __dir__)
8
- end
6
+ include_context 'prepare test app'
9
7
 
10
8
  let(:table_name) { 'networks' }
11
9
  let(:old_options) { { charset: 'utf8', collation: 'utf8_ci' } }