db_schema 0.2.5 → 0.3.rc1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fd3abbf119d7700e83eedf09b10b100cfc744f9e
4
- data.tar.gz: d3e8cfd07c1fdcd902e705b337eed52f2bdf8829
3
+ metadata.gz: 60b581e6c318ed7bf979417435dce0ec4960a978
4
+ data.tar.gz: 0f296ac7525965a1bd3026e76cfa0cd2bb24b821
5
5
  SHA512:
6
- metadata.gz: 251c126151289b49ea663c2d57c46334d9ba98c9419110121acbb436aee006871eec48aeabbe3ea1a6e6369b1a76fd4ff07e5e9d3cbb0c88e12e13ba609269b2
7
- data.tar.gz: a6163f3a147938bad96b7d891f26990fc9c86068f716ec236e4647dda13019c1af9bf1a2ba566fe3c4c8f0b46d5edd6db29ed002eb3212d56590e62fc6baf84f
6
+ metadata.gz: 19375ec9fac1576a3c8164de6584ceacc21019d111bb2e584850713d89936a3714c394c0338c16eb5873c2f86d319286136690fad8cb28b2748dc911c625f3e8
7
+ data.tar.gz: b80cb8b4a448e5f0014763d89b1e2e2e376d48cd84f03380c55c823a71efe952d7187242888f021b27ccdbf3f85191b5286cefa11fd31d40ce7a3757f3d7dff4
@@ -1,10 +1,12 @@
1
+ dist: trusty
2
+ sudo: false
1
3
  language: ruby
2
4
  rvm:
3
- - 2.3.1
4
- before_install: gem install bundler -v 1.12.5
5
+ - 2.3.3
5
6
  services:
6
7
  - postgresql
7
8
  addons:
8
- postgresql: 9.4
9
+ postgresql: 9.6
9
10
  before_script:
10
11
  - psql -c 'CREATE DATABASE db_schema_test;' -U postgres
12
+ - psql -c 'CREATE DATABASE db_schema_test2;' -U postgres
data/README.md CHANGED
@@ -53,7 +53,7 @@ But you would lose it even with manual migrations.
53
53
  Add this line to your application's Gemfile:
54
54
 
55
55
  ``` ruby
56
- gem 'db_schema', '~> 0.2.5'
56
+ gem 'db_schema', '~> 0.3.rc1'
57
57
  ```
58
58
 
59
59
  And then execute:
@@ -65,7 +65,7 @@ $ bundle
65
65
  Or install it yourself as:
66
66
 
67
67
  ``` sh
68
- $ gem install db_schema
68
+ $ gem install db_schema --prerelease
69
69
  ```
70
70
 
71
71
  ## Usage
@@ -4,53 +4,51 @@ require 'yaml'
4
4
  require 'db_schema/configuration'
5
5
  require 'db_schema/utils'
6
6
  require 'db_schema/definitions'
7
+ require 'db_schema/migration'
8
+ require 'db_schema/operations'
7
9
  require 'db_schema/awesome_print'
8
10
  require 'db_schema/dsl'
9
11
  require 'db_schema/validator'
10
12
  require 'db_schema/normalizer'
11
13
  require 'db_schema/reader'
14
+ require 'db_schema/migrator'
12
15
  require 'db_schema/changes'
13
16
  require 'db_schema/runner'
14
17
  require 'db_schema/version'
15
18
 
16
19
  module DbSchema
17
20
  class << self
18
- def describe(&block)
19
- desired_schema = DSL.new(block).schema
20
- validate(desired_schema)
21
- Normalizer.new(desired_schema).normalize_tables
22
-
23
- actual_schema = Reader.read_schema
24
- changes = Changes.between(desired_schema, actual_schema)
25
- return if changes.empty?
26
-
27
- log_changes(changes) if configuration.log_changes?
28
- return if configuration.dry_run?
29
-
30
- Runner.new(changes).run!
31
-
32
- if configuration.post_check_enabled?
33
- perform_post_check(desired_schema)
34
- end
35
- end
21
+ attr_reader :current_schema
36
22
 
37
- def connection
38
- @connection ||= Sequel.connect(
39
- adapter: configuration.adapter,
40
- host: configuration.host,
41
- port: configuration.port,
42
- database: configuration.database,
43
- user: configuration.user,
44
- password: configuration.password
45
- ).tap do |db|
46
- db.extension :pg_enum
47
- db.extension :pg_array
23
+ def describe(&block)
24
+ with_connection do |connection|
25
+ desired = DSL.new(block)
26
+ validate(desired.schema)
27
+ Normalizer.new(desired.schema, connection).normalize_tables
28
+
29
+ connection.transaction do
30
+ actual_schema = run_migrations(desired.migrations, connection)
31
+ changes = Changes.between(desired.schema, actual_schema)
32
+ log_changes(changes) if configuration.log_changes?
33
+
34
+ if configuration.dry_run?
35
+ raise Sequel::Rollback
36
+ else
37
+ @current_schema = desired.schema
38
+ return if changes.empty?
39
+ end
40
+
41
+ Runner.new(changes, connection).run!
42
+
43
+ if configuration.post_check_enabled?
44
+ perform_post_check(desired.schema, connection)
45
+ end
46
+ end
48
47
  end
49
48
  end
50
49
 
51
- def configure(connection_parameters)
52
- @configuration = Configuration.new(connection_parameters)
53
- @connection = nil
50
+ def configure(params)
51
+ @configuration = configuration.merge(params)
54
52
  end
55
53
 
56
54
  def configure_from_yaml(yaml_path, environment, **other_options)
@@ -64,18 +62,43 @@ module DbSchema
64
62
  configure(renamed_data.merge(other_options))
65
63
  end
66
64
 
67
- def configuration
68
- raise 'You must call DbSchema.configure in order to connect to the database.' if @configuration.nil?
65
+ def connection=(external_connection)
66
+ @external_connection = external_connection
67
+ end
69
68
 
70
- @configuration
69
+ def configuration
70
+ @configuration ||= Configuration.new
71
71
  end
72
72
 
73
73
  def reset!
74
+ @external_connection = nil
74
75
  @configuration = nil
75
- @connection = nil
76
+ @current_schema = nil
76
77
  end
77
78
 
78
79
  private
80
+ def with_connection
81
+ raise ArgumentError unless block_given?
82
+
83
+ if @external_connection
84
+ yield @external_connection
85
+ else
86
+ Sequel.connect(
87
+ adapter: configuration.adapter,
88
+ host: configuration.host,
89
+ port: configuration.port,
90
+ database: configuration.database,
91
+ user: configuration.user,
92
+ password: configuration.password
93
+ ) do |db|
94
+ db.extension :pg_enum
95
+ db.extension :pg_array
96
+
97
+ yield db
98
+ end
99
+ end
100
+ end
101
+
79
102
  def validate(schema)
80
103
  validation_result = Validator.validate(schema)
81
104
 
@@ -90,6 +113,26 @@ module DbSchema
90
113
  end
91
114
  end
92
115
 
116
+ def run_migrations(migrations, connection)
117
+ @current_schema = Reader.read_schema(connection)
118
+
119
+ migrations.reduce(@current_schema) do |schema, migration|
120
+ migrator = Migrator.new(migration)
121
+
122
+ if migrator.applicable?(schema)
123
+ log_migration(migration) if configuration.log_changes?
124
+ migrator.run!(connection)
125
+ Reader.read_schema(connection)
126
+ else
127
+ schema
128
+ end
129
+ end
130
+ end
131
+
132
+ def log_migration(migration)
133
+ puts "DbSchema is running migration #{migration.name.ai}"
134
+ end
135
+
93
136
  def log_changes(changes)
94
137
  return if changes.empty?
95
138
 
@@ -101,8 +144,8 @@ module DbSchema
101
144
  end
102
145
  end
103
146
 
104
- def perform_post_check(desired_schema)
105
- unapplied_changes = Changes.between(desired_schema, Reader.read_schema)
147
+ def perform_post_check(desired_schema, connection)
148
+ unapplied_changes = Changes.between(desired_schema, Reader.read_schema(connection))
106
149
  return if unapplied_changes.empty?
107
150
 
108
151
  readable_changes = if unapplied_changes.respond_to?(:ai)
@@ -35,48 +35,49 @@ if defined?(AwesomePrint)
35
35
  :dbschema_enum
36
36
  when ::DbSchema::Definitions::Extension
37
37
  :dbschema_column_operation
38
- when ::DbSchema::Changes::CreateTable
38
+ when ::DbSchema::Operations::CreateTable
39
39
  :dbschema_create_table
40
- when ::DbSchema::Changes::DropTable
40
+ when ::DbSchema::Operations::DropTable
41
41
  :dbschema_drop_table
42
- when ::DbSchema::Changes::AlterTable
42
+ when ::DbSchema::Operations::AlterTable
43
43
  :dbschema_alter_table
44
- when ::DbSchema::Changes::CreateColumn
44
+ when ::DbSchema::Operations::CreateColumn
45
45
  :dbschema_create_column
46
- when ::DbSchema::Changes::DropColumn
46
+ when ::DbSchema::Operations::DropColumn
47
47
  :dbschema_column_operation
48
- when ::DbSchema::Changes::RenameColumn
49
- :dbschema_rename_column
50
- when ::DbSchema::Changes::AlterColumnType
48
+ when ::DbSchema::Operations::RenameTable,
49
+ ::DbSchema::Operations::RenameColumn
50
+ :dbschema_rename
51
+ when ::DbSchema::Operations::AlterColumnType
51
52
  :dbschema_alter_column_type
52
- when ::DbSchema::Changes::CreatePrimaryKey,
53
- ::DbSchema::Changes::DropPrimaryKey,
54
- ::DbSchema::Changes::AllowNull,
55
- ::DbSchema::Changes::DisallowNull
53
+ when ::DbSchema::Operations::CreatePrimaryKey,
54
+ ::DbSchema::Operations::DropPrimaryKey,
55
+ ::DbSchema::Operations::AllowNull,
56
+ ::DbSchema::Operations::DisallowNull
56
57
  :dbschema_column_operation
57
- when ::DbSchema::Changes::AlterColumnDefault
58
+ when ::DbSchema::Operations::AlterColumnDefault
58
59
  :dbschema_alter_column_default
59
- when ::DbSchema::Changes::CreateIndex
60
+ when ::DbSchema::Operations::CreateIndex
60
61
  :dbschema_create_index
61
- when ::DbSchema::Changes::DropIndex
62
+ when ::DbSchema::Operations::DropIndex
62
63
  :dbschema_column_operation
63
- when ::DbSchema::Changes::CreateCheckConstraint
64
+ when ::DbSchema::Operations::CreateCheckConstraint
64
65
  :dbschema_create_check_constraint
65
- when ::DbSchema::Changes::DropCheckConstraint
66
+ when ::DbSchema::Operations::DropCheckConstraint
66
67
  :dbschema_column_operation
67
- when ::DbSchema::Changes::CreateForeignKey
68
+ when ::DbSchema::Operations::CreateForeignKey
68
69
  :dbschema_create_foreign_key
69
- when ::DbSchema::Changes::DropForeignKey
70
+ when ::DbSchema::Operations::DropForeignKey
70
71
  :dbschema_drop_foreign_key
71
- when ::DbSchema::Changes::CreateEnum
72
+ when ::DbSchema::Operations::CreateEnum
72
73
  :dbschema_create_enum
73
- when ::DbSchema::Changes::DropEnum
74
+ when ::DbSchema::Operations::DropEnum
74
75
  :dbschema_column_operation
75
- when ::DbSchema::Changes::AlterEnumValues
76
+ when ::DbSchema::Operations::AlterEnumValues
76
77
  :dbschema_alter_enum_values
77
- when ::DbSchema::Changes::CreateExtension
78
+ when ::DbSchema::Operations::CreateExtension
78
79
  :dbschema_create_extension
79
- when ::DbSchema::Changes::DropExtension
80
+ when ::DbSchema::Operations::DropExtension
80
81
  :dbschema_column_operation
81
82
  else
82
83
  cast_without_dbschema(object, type)
@@ -202,27 +203,27 @@ if defined?(AwesomePrint)
202
203
  data << "checks: #{object.table.checks.ai}" if object.table.checks.any?
203
204
 
204
205
  data_string = indent_lines(data.join(', '))
205
- "#<DbSchema::Changes::CreateTable #{object.table.name.ai} #{data_string}>"
206
+ "#<DbSchema::Operations::CreateTable #{object.table.name.ai} #{data_string}>"
206
207
  end
207
208
 
208
209
  def awesome_dbschema_drop_table(object)
209
- "#<DbSchema::Changes::DropTable #{object.name.ai}>"
210
+ "#<DbSchema::Operations::DropTable #{object.name.ai}>"
210
211
  end
211
212
 
212
213
  def awesome_dbschema_alter_table(object)
213
- "#<DbSchema::Changes::AlterTable #{object.table_name.ai} #{indent_lines(object.changes.ai)}>"
214
+ "#<DbSchema::Operations::AlterTable #{object.table_name.ai} #{indent_lines(object.changes.ai)}>"
214
215
  end
215
216
 
216
217
  def awesome_dbschema_create_column(object)
217
- "#<DbSchema::Changes::CreateColumn #{object.field.ai}>"
218
+ "#<DbSchema::Operations::CreateColumn #{object.field.ai}>"
218
219
  end
219
220
 
220
221
  def awesome_dbschema_drop_column(object)
221
- "#<DbSchema::Changes::DropColumn #{object.name.ai}>"
222
+ "#<DbSchema::Operations::DropColumn #{object.name.ai}>"
222
223
  end
223
224
 
224
- def awesome_dbschema_rename_column(object)
225
- "#<DbSchema::Changes::RenameColumn #{object.old_name.ai} => #{object.new_name.ai}>"
225
+ def awesome_dbschema_rename(object)
226
+ "#<#{object.class} #{object.old_name.ai} => #{object.new_name.ai}>"
226
227
  end
227
228
 
228
229
  def awesome_dbschema_alter_column_type(object)
@@ -231,7 +232,7 @@ if defined?(AwesomePrint)
231
232
  "#{key} #{v.ai}"
232
233
  end.unshift(nil).join(', ')
233
234
 
234
- "#<DbSchema::Changes::AlterColumnType #{object.name.ai}, #{object.new_type.ai}#{attributes}>"
235
+ "#<DbSchema::Operations::AlterColumnType #{object.name.ai}, #{object.new_type.ai}#{attributes}>"
235
236
  end
236
237
 
237
238
  def awesome_dbschema_alter_column_default(object)
@@ -241,7 +242,7 @@ if defined?(AwesomePrint)
241
242
  object.new_default.ai
242
243
  end
243
244
 
244
- "#<DbSchema::Changes::AlterColumnDefault #{object.name.ai}, #{new_default}>"
245
+ "#<DbSchema::Operations::AlterColumnDefault #{object.name.ai}, #{new_default}>"
245
246
  end
246
247
 
247
248
  def awesome_dbschema_create_index(object)
@@ -260,11 +261,11 @@ if defined?(AwesomePrint)
260
261
  end
261
262
 
262
263
  def awesome_dbschema_create_foreign_key(object)
263
- "#<DbSchema::Changes::CreateForeignKey #{object.foreign_key.ai} on #{object.table_name.ai}>"
264
+ "#<DbSchema::Operations::CreateForeignKey #{object.foreign_key.ai} on #{object.table_name.ai}>"
264
265
  end
265
266
 
266
267
  def awesome_dbschema_drop_foreign_key(object)
267
- "#<DbSchema::Changes::DropForeignKey #{object.fkey_name.ai} on #{object.table_name.ai}>"
268
+ "#<DbSchema::Operations::DropForeignKey #{object.fkey_name.ai} on #{object.table_name.ai}>"
268
269
  end
269
270
 
270
271
  def awesome_dbschema_create_enum(object)
@@ -284,7 +285,7 @@ if defined?(AwesomePrint)
284
285
  colorize(value.to_s, :string)
285
286
  end.join(', ')
286
287
 
287
- "#<DbSchema::Changes::AlterEnumValues #{object.enum_name.ai} to (#{values})>"
288
+ "#<DbSchema::Operations::AlterEnumValues #{object.enum_name.ai} to (#{values})>"
288
289
  end
289
290
 
290
291
  def awesome_dbschema_create_extension(object)
@@ -12,17 +12,17 @@ module DbSchema
12
12
  actual = actual_schema.tables.find { |table| table.name == table_name }
13
13
 
14
14
  if desired && !actual
15
- changes << CreateTable.new(desired)
15
+ changes << Operations::CreateTable.new(desired)
16
16
 
17
17
  fkey_operations = desired.foreign_keys.map do |fkey|
18
- CreateForeignKey.new(table_name, fkey)
18
+ Operations::CreateForeignKey.new(table_name, fkey)
19
19
  end
20
20
  changes.concat(fkey_operations)
21
21
  elsif actual && !desired
22
- changes << DropTable.new(table_name)
22
+ changes << Operations::DropTable.new(table_name)
23
23
 
24
24
  actual.foreign_keys.each do |fkey|
25
- changes << DropForeignKey.new(table_name, fkey.name)
25
+ changes << Operations::DropForeignKey.new(table_name, fkey.name)
26
26
  end
27
27
  elsif actual != desired
28
28
  field_operations = field_changes(desired.fields, actual.fields)
@@ -31,9 +31,9 @@ module DbSchema
31
31
  fkey_operations = foreign_key_changes(table_name, desired.foreign_keys, actual.foreign_keys)
32
32
 
33
33
  if field_operations.any? || index_operations.any? || check_operations.any?
34
- changes << AlterTable.new(
34
+ changes << Operations::AlterTable.new(
35
35
  table_name,
36
- field_operations + index_operations + check_operations
36
+ sort_alter_table_changes(field_operations + index_operations + check_operations)
37
37
  )
38
38
  end
39
39
 
@@ -48,9 +48,9 @@ module DbSchema
48
48
  actual = actual_schema.enums.find { |enum| enum.name == enum_name }
49
49
 
50
50
  if desired && !actual
51
- changes << CreateEnum.new(desired)
51
+ changes << Operations::CreateEnum.new(desired)
52
52
  elsif actual && !desired
53
- changes << DropEnum.new(enum_name)
53
+ changes << Operations::DropEnum.new(enum_name)
54
54
  elsif actual != desired
55
55
  fields = actual_schema.tables.flat_map do |table|
56
56
  table.fields.select do |field|
@@ -73,17 +73,17 @@ module DbSchema
73
73
  end
74
74
  end
75
75
 
76
- changes << AlterEnumValues.new(enum_name, desired.values, fields)
76
+ changes << Operations::AlterEnumValues.new(enum_name, desired.values, fields)
77
77
  end
78
78
  end
79
79
 
80
80
  extension_changes = (desired_schema.extensions - actual_schema.extensions).map do |extension|
81
- CreateExtension.new(extension)
81
+ Operations::CreateExtension.new(extension)
82
82
  end + (actual_schema.extensions - desired_schema.extensions).map do |extension|
83
- DropExtension.new(extension.name)
83
+ Operations::DropExtension.new(extension.name)
84
84
  end
85
85
 
86
- table_changes + enum_changes + extension_changes
86
+ sort_all_changes(table_changes + enum_changes + extension_changes)
87
87
  end
88
88
 
89
89
  private
@@ -95,12 +95,12 @@ module DbSchema
95
95
  actual = actual_fields.find { |field| field.name == field_name }
96
96
 
97
97
  if desired && !actual
98
- table_changes << CreateColumn.new(desired)
98
+ table_changes << Operations::CreateColumn.new(desired)
99
99
  elsif actual && !desired
100
- table_changes << DropColumn.new(field_name)
100
+ table_changes << Operations::DropColumn.new(field_name)
101
101
  elsif actual != desired
102
102
  if (actual.type != desired.type) || (actual.attributes != desired.attributes)
103
- table_changes << AlterColumnType.new(
103
+ table_changes << Operations::AlterColumnType.new(
104
104
  field_name,
105
105
  new_type: desired.type,
106
106
  **desired.attributes
@@ -108,23 +108,23 @@ module DbSchema
108
108
  end
109
109
 
110
110
  if desired.primary_key? && !actual.primary_key?
111
- table_changes << CreatePrimaryKey.new(field_name)
111
+ table_changes << Operations::CreatePrimaryKey.new(field_name)
112
112
  end
113
113
 
114
114
  if actual.primary_key? && !desired.primary_key?
115
- table_changes << DropPrimaryKey.new(field_name)
115
+ table_changes << Operations::DropPrimaryKey.new(field_name)
116
116
  end
117
117
 
118
118
  if desired.null? && !actual.null?
119
- table_changes << AllowNull.new(field_name)
119
+ table_changes << Operations::AllowNull.new(field_name)
120
120
  end
121
121
 
122
122
  if actual.null? && !desired.null?
123
- table_changes << DisallowNull.new(field_name)
123
+ table_changes << Operations::DisallowNull.new(field_name)
124
124
  end
125
125
 
126
126
  if actual.default != desired.default
127
- table_changes << AlterColumnDefault.new(field_name, new_default: desired.default)
127
+ table_changes << Operations::AlterColumnDefault.new(field_name, new_default: desired.default)
128
128
  end
129
129
  end
130
130
  end
@@ -138,12 +138,12 @@ module DbSchema
138
138
  actual = actual_indices.find { |index| index.name == index_name }
139
139
 
140
140
  if desired && !actual
141
- table_changes << CreateIndex.new(desired)
141
+ table_changes << Operations::CreateIndex.new(desired)
142
142
  elsif actual && !desired
143
- table_changes << DropIndex.new(index_name)
143
+ table_changes << Operations::DropIndex.new(index_name)
144
144
  elsif actual != desired
145
- table_changes << DropIndex.new(index_name)
146
- table_changes << CreateIndex.new(desired)
145
+ table_changes << Operations::DropIndex.new(index_name)
146
+ table_changes << Operations::CreateIndex.new(desired)
147
147
  end
148
148
  end
149
149
  end
@@ -156,12 +156,12 @@ module DbSchema
156
156
  actual = actual_checks.find { |check| check.name == check_name }
157
157
 
158
158
  if desired && !actual
159
- table_changes << CreateCheckConstraint.new(desired)
159
+ table_changes << Operations::CreateCheckConstraint.new(desired)
160
160
  elsif actual && !desired
161
- table_changes << DropCheckConstraint.new(check_name)
161
+ table_changes << Operations::DropCheckConstraint.new(check_name)
162
162
  elsif actual != desired
163
- table_changes << DropCheckConstraint.new(check_name)
164
- table_changes << CreateCheckConstraint.new(desired)
163
+ table_changes << Operations::DropCheckConstraint.new(check_name)
164
+ table_changes << Operations::CreateCheckConstraint.new(desired)
165
165
  end
166
166
  end
167
167
  end
@@ -174,202 +174,53 @@ module DbSchema
174
174
  actual = actual_foreign_keys.find { |key| key.name == key_name }
175
175
 
176
176
  if desired && !actual
177
- table_changes << CreateForeignKey.new(table_name, desired)
177
+ table_changes << Operations::CreateForeignKey.new(table_name, desired)
178
178
  elsif actual && !desired
179
- table_changes << DropForeignKey.new(table_name, key_name)
179
+ table_changes << Operations::DropForeignKey.new(table_name, key_name)
180
180
  elsif actual != desired
181
- table_changes << DropForeignKey.new(table_name, key_name)
182
- table_changes << CreateForeignKey.new(table_name, desired)
181
+ table_changes << Operations::DropForeignKey.new(table_name, key_name)
182
+ table_changes << Operations::CreateForeignKey.new(table_name, desired)
183
183
  end
184
184
  end
185
185
  end
186
- end
187
-
188
- class CreateTable
189
- include Dry::Equalizer(:table)
190
- attr_reader :table
191
-
192
- def initialize(table)
193
- @table = table
194
- end
195
- end
196
-
197
- class DropTable
198
- include Dry::Equalizer(:name)
199
- attr_reader :name
200
-
201
- def initialize(name)
202
- @name = name
203
- end
204
- end
205
-
206
- class AlterTable
207
- include Dry::Equalizer(:table_name, :changes)
208
- attr_reader :table_name, :changes
209
-
210
- def initialize(table_name, changes)
211
- @table_name = table_name
212
- @changes = changes
213
- end
214
- end
215
-
216
- # Abstract base class for single-column toggle operations.
217
- class ColumnOperation
218
- include Dry::Equalizer(:name)
219
- attr_reader :name
220
-
221
- def initialize(name)
222
- @name = name
223
- end
224
- end
225
-
226
- class CreateColumn
227
- include Dry::Equalizer(:field)
228
- attr_reader :field
229
-
230
- def initialize(field)
231
- @field = field
232
- end
233
-
234
- def name
235
- field.name
236
- end
237
-
238
- def type
239
- field.type
240
- end
241
-
242
- def primary_key?
243
- field.primary_key?
244
- end
245
-
246
- def options
247
- field.options
248
- end
249
- end
250
-
251
- class DropColumn < ColumnOperation
252
- end
253
-
254
- class RenameColumn
255
- attr_reader :old_name, :new_name
256
-
257
- def initialize(old_name:, new_name:)
258
- @old_name = old_name
259
- @new_name = new_name
260
- end
261
- end
262
-
263
- class AlterColumnType
264
- include Dry::Equalizer(:name, :new_type, :new_attributes)
265
- attr_reader :name, :new_type, :new_attributes
266
-
267
- def initialize(name, new_type:, **new_attributes)
268
- @name = name
269
- @new_type = new_type
270
- @new_attributes = new_attributes
271
- end
272
- end
273
-
274
- class CreatePrimaryKey < ColumnOperation
275
- end
276
-
277
- class DropPrimaryKey < ColumnOperation
278
- end
279
-
280
- class AllowNull < ColumnOperation
281
- end
282
-
283
- class DisallowNull < ColumnOperation
284
- end
285
-
286
- class AlterColumnDefault
287
- include Dry::Equalizer(:name, :new_default)
288
- attr_reader :name, :new_default
289
-
290
- def initialize(name, new_default:)
291
- @name = name
292
- @new_default = new_default
293
- end
294
- end
295
-
296
- class CreateIndex
297
- include Dry::Equalizer(:index)
298
- attr_reader :index
299
-
300
- def initialize(index)
301
- @index = index
302
- end
303
- end
304
-
305
- class DropIndex < ColumnOperation
306
- end
307
-
308
- class CreateCheckConstraint
309
- include Dry::Equalizer(:check)
310
- attr_reader :check
311
-
312
- def initialize(check)
313
- @check = check
314
- end
315
- end
316
-
317
- class DropCheckConstraint < ColumnOperation
318
- end
319
186
 
320
- class CreateForeignKey
321
- include Dry::Equalizer(:table_name, :foreign_key)
322
- attr_reader :table_name, :foreign_key
323
-
324
- def initialize(table_name, foreign_key)
325
- @table_name = table_name
326
- @foreign_key = foreign_key
327
- end
328
- end
329
-
330
- class DropForeignKey
331
- include Dry::Equalizer(:table_name, :fkey_name)
332
- attr_reader :table_name, :fkey_name
333
-
334
- def initialize(table_name, fkey_name)
335
- @table_name = table_name
336
- @fkey_name = fkey_name
187
+ def sort_all_changes(changes)
188
+ Utils.sort_by_class(
189
+ changes,
190
+ [
191
+ Operations::CreateExtension,
192
+ Operations::DropForeignKey,
193
+ Operations::AlterEnumValues,
194
+ Operations::CreateEnum,
195
+ Operations::CreateTable,
196
+ Operations::AlterTable,
197
+ Operations::DropTable,
198
+ Operations::DropEnum,
199
+ Operations::CreateForeignKey,
200
+ Operations::DropExtension
201
+ ]
202
+ )
203
+ end
204
+
205
+ def sort_alter_table_changes(changes)
206
+ Utils.sort_by_class(
207
+ changes,
208
+ [
209
+ Operations::DropPrimaryKey,
210
+ Operations::DropCheckConstraint,
211
+ Operations::DropIndex,
212
+ Operations::DropColumn,
213
+ Operations::AlterColumnType,
214
+ Operations::AllowNull,
215
+ Operations::DisallowNull,
216
+ Operations::AlterColumnDefault,
217
+ Operations::CreateColumn,
218
+ Operations::CreateIndex,
219
+ Operations::CreateCheckConstraint,
220
+ Operations::CreatePrimaryKey
221
+ ]
222
+ )
337
223
  end
338
224
  end
339
-
340
- class CreateEnum
341
- include Dry::Equalizer(:enum)
342
- attr_reader :enum
343
-
344
- def initialize(enum)
345
- @enum = enum
346
- end
347
- end
348
-
349
- class DropEnum < ColumnOperation
350
- end
351
-
352
- class AlterEnumValues
353
- include Dry::Equalizer(:enum_name, :new_values, :enum_fields)
354
- attr_reader :enum_name, :new_values, :enum_fields
355
-
356
- def initialize(enum_name, new_values, enum_fields)
357
- @enum_name = enum_name
358
- @new_values = new_values
359
- @enum_fields = enum_fields
360
- end
361
- end
362
-
363
- class CreateExtension
364
- include Dry::Equalizer(:extension)
365
- attr_reader :extension
366
-
367
- def initialize(extension)
368
- @extension = extension
369
- end
370
- end
371
-
372
- class DropExtension < ColumnOperation
373
- end
374
225
  end
375
226
  end