dynamic_migrations 3.1.0 → 3.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +14 -0
- data/lib/dynamic_migrations/postgres/generator/column.rb +44 -19
- data/lib/dynamic_migrations/postgres/generator/foreign_key_constraint.rb +35 -14
- data/lib/dynamic_migrations/postgres/generator/fragment.rb +37 -2
- data/lib/dynamic_migrations/postgres/generator/function.rb +50 -25
- data/lib/dynamic_migrations/postgres/generator/index.rb +34 -14
- data/lib/dynamic_migrations/postgres/generator/migration.rb +175 -0
- data/lib/dynamic_migrations/postgres/generator/migration_dependency_sorter.rb +21 -0
- data/lib/dynamic_migrations/postgres/generator/primary_key.rb +16 -6
- data/lib/dynamic_migrations/postgres/generator/schema.rb +14 -6
- data/lib/dynamic_migrations/postgres/generator/schema_migration.rb +17 -0
- data/lib/dynamic_migrations/postgres/generator/table.rb +39 -19
- data/lib/dynamic_migrations/postgres/generator/table_migration.rb +51 -0
- data/lib/dynamic_migrations/postgres/generator/trigger.rb +34 -14
- data/lib/dynamic_migrations/postgres/generator/unique_constraint.rb +34 -14
- data/lib/dynamic_migrations/postgres/generator/validation.rb +38 -18
- data/lib/dynamic_migrations/postgres/generator.rb +163 -294
- data/lib/dynamic_migrations/postgres/server/database/connection.rb +15 -0
- data/lib/dynamic_migrations/version.rb +1 -1
- data/lib/dynamic_migrations.rb +4 -2
- metadata +6 -4
- data/lib/dynamic_migrations/postgres/generator/schema_migrations/section.rb +0 -37
- data/lib/dynamic_migrations/postgres/generator/schema_migrations.rb +0 -92
@@ -3,15 +3,23 @@ module DynamicMigrations
|
|
3
3
|
class Generator
|
4
4
|
module Schema
|
5
5
|
def create_schema schema, code_comment = nil
|
6
|
-
|
7
|
-
create_schema
|
8
|
-
|
6
|
+
add_fragment schema: schema,
|
7
|
+
migration_method: :create_schema,
|
8
|
+
object: schema,
|
9
|
+
code_comment: code_comment,
|
10
|
+
migration: <<~RUBY
|
11
|
+
create_schema :#{schema.name}
|
12
|
+
RUBY
|
9
13
|
end
|
10
14
|
|
11
15
|
def drop_schema schema, code_comment = nil
|
12
|
-
|
13
|
-
drop_schema
|
14
|
-
|
16
|
+
add_fragment schema: schema,
|
17
|
+
migration_method: :drop_schema,
|
18
|
+
object: schema,
|
19
|
+
code_comment: code_comment,
|
20
|
+
migration: <<~RUBY
|
21
|
+
drop_schema :#{schema.name}
|
22
|
+
RUBY
|
15
23
|
end
|
16
24
|
end
|
17
25
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module DynamicMigrations
|
2
|
+
module Postgres
|
3
|
+
class Generator
|
4
|
+
class SchemaMigration < Migration
|
5
|
+
# these sections are in order for which they will appear in a migration,
|
6
|
+
# note that removals come before additions, and that the order here optomizes
|
7
|
+
# for dependencies (i.e. columns have to be created before indexes are added and
|
8
|
+
# triggers are removed before functions are dropped)
|
9
|
+
add_structure_template [:remove_function_comment, :drop_function], "Remove Functions"
|
10
|
+
add_structure_template [:drop_schema], "Drop this schema"
|
11
|
+
add_structure_template [:create_schema], "Create this schema"
|
12
|
+
add_structure_template [:create_function], "Functions"
|
13
|
+
add_structure_template [:update_function, :set_function_comment], "Update Functions"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -13,20 +13,30 @@ module DynamicMigrations
|
|
13
13
|
raise NoTableCommentError, "Refusing to generate create_table migration, no description was provided for `#{table.schema.name}`.`#{table.name}`"
|
14
14
|
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
16
|
+
add_fragment schema: table.schema,
|
17
|
+
table: table,
|
18
|
+
migration_method: :create_table,
|
19
|
+
object: table,
|
20
|
+
code_comment: code_comment,
|
21
|
+
migration: <<~RUBY
|
22
|
+
table_comment = <<~COMMENT
|
23
|
+
#{indent table.description}
|
24
|
+
COMMENT
|
25
|
+
create_table :#{table.name}, #{table_options table} do |t|
|
26
|
+
#{indent table_columns(table.columns)}
|
27
|
+
end
|
28
|
+
RUBY
|
24
29
|
end
|
25
30
|
|
26
31
|
def drop_table table, code_comment = nil
|
27
|
-
|
28
|
-
|
29
|
-
|
32
|
+
add_fragment schema: table.schema,
|
33
|
+
table: table,
|
34
|
+
migration_method: :drop_table,
|
35
|
+
object: table,
|
36
|
+
code_comment: code_comment,
|
37
|
+
migration: <<~RUBY
|
38
|
+
drop_table :#{table.name}, force: true
|
39
|
+
RUBY
|
30
40
|
end
|
31
41
|
|
32
42
|
# add a comment to a table
|
@@ -37,18 +47,28 @@ module DynamicMigrations
|
|
37
47
|
raise MissingDescriptionError
|
38
48
|
end
|
39
49
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
50
|
+
add_fragment schema: table.schema,
|
51
|
+
table: table,
|
52
|
+
migration_method: :set_table_comment,
|
53
|
+
object: table,
|
54
|
+
code_comment: code_comment,
|
55
|
+
migration: <<~RUBY
|
56
|
+
set_table_comment :#{table.name}, <<~COMMENT
|
57
|
+
#{indent description}
|
58
|
+
COMMENT
|
59
|
+
RUBY
|
45
60
|
end
|
46
61
|
|
47
62
|
# remove the comment from a table
|
48
63
|
def remove_table_comment table, code_comment = nil
|
49
|
-
|
50
|
-
|
51
|
-
|
64
|
+
add_fragment schema: table.schema,
|
65
|
+
table: table,
|
66
|
+
migration_method: :remove_table_comment,
|
67
|
+
object: table,
|
68
|
+
code_comment: code_comment,
|
69
|
+
migration: <<~RUBY
|
70
|
+
remove_table_comment :#{table.name}
|
71
|
+
RUBY
|
52
72
|
end
|
53
73
|
|
54
74
|
private
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module DynamicMigrations
|
2
|
+
module Postgres
|
3
|
+
class Generator
|
4
|
+
class TableMigration < Migration
|
5
|
+
class UnexpectedTableError < StandardError
|
6
|
+
end
|
7
|
+
|
8
|
+
class MissingRequiredTableName < StandardError
|
9
|
+
end
|
10
|
+
|
11
|
+
# these sections are in order for which they will appear in a migration,
|
12
|
+
# note that removals come before additions, and that the order here optomizes
|
13
|
+
# for dependencies (i.e. columns have to be created before indexes are added and
|
14
|
+
# triggers are removed before functions are dropped)
|
15
|
+
add_structure_template [:remove_trigger_comment, :remove_trigger], "Remove Triggers"
|
16
|
+
add_structure_template [:remove_validation, :remove_unique_constraint], "Remove Validations"
|
17
|
+
add_structure_template [:remove_foreign_key], "Remove Foreign Keys"
|
18
|
+
add_structure_template [:remove_primary_key], "Remove Primary Keys"
|
19
|
+
add_structure_template [:remove_index, :remove_index_comment], "Remove Indexes"
|
20
|
+
add_structure_template [:remove_column], "Remove Columns"
|
21
|
+
add_structure_template [:drop_table], "Remove Tables"
|
22
|
+
add_structure_template [:create_table], "Create Table"
|
23
|
+
add_structure_template [:remove_table_comment, :set_table_comment], "Tables"
|
24
|
+
add_structure_template [:add_column], "Additional Columns"
|
25
|
+
add_structure_template [:change_column, :remove_column_comment, :set_column_comment], "Update Columns"
|
26
|
+
add_structure_template [:add_primary_key], "Primary Key"
|
27
|
+
add_structure_template [:add_index, :set_index_comment], "Indexes"
|
28
|
+
add_structure_template [:add_foreign_key, :set_foreign_key_constraint_comment, :remove_foreign_key_constraint_comment], "Foreign Keys"
|
29
|
+
add_structure_template [:add_validation, :add_unique_constraint, :set_validation_comment, :remove_validation_comment, :set_unique_constraint_comment, :remove_unique_constraint_comment], "Validations"
|
30
|
+
add_structure_template [:create_function], "Functions"
|
31
|
+
add_structure_template [:add_trigger, :set_trigger_comment], "Triggers"
|
32
|
+
add_structure_template [:update_function, :set_function_comment], "Update Functions"
|
33
|
+
|
34
|
+
attr_accessor :table_name
|
35
|
+
|
36
|
+
def initialize schema_name, table_name
|
37
|
+
raise MissingRequiredTableName unless table_name
|
38
|
+
super schema_name
|
39
|
+
@table_name = table_name
|
40
|
+
end
|
41
|
+
|
42
|
+
def add_fragment fragment
|
43
|
+
unless @table_name == fragment.table_name
|
44
|
+
raise UnexpectedTableError, "Frgment is for table `#{fragment.table_name}` but migration is for table `#{@table_name}`"
|
45
|
+
end
|
46
|
+
super
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -46,15 +46,25 @@ module DynamicMigrations
|
|
46
46
|
|
47
47
|
options_syntax = options.map { |k, v| "#{k}: #{v}" }.join(", ")
|
48
48
|
|
49
|
-
|
50
|
-
|
51
|
-
|
49
|
+
add_fragment schema: trigger.table.schema,
|
50
|
+
table: trigger.table,
|
51
|
+
migration_method: :add_trigger,
|
52
|
+
object: trigger,
|
53
|
+
code_comment: code_comment,
|
54
|
+
migration: <<~RUBY
|
55
|
+
#{method_name} :#{trigger.table.name}, #{options_syntax}
|
56
|
+
RUBY
|
52
57
|
end
|
53
58
|
|
54
59
|
def remove_trigger trigger, code_comment = nil
|
55
|
-
|
56
|
-
|
57
|
-
|
60
|
+
add_fragment schema: trigger.table.schema,
|
61
|
+
table: trigger.table,
|
62
|
+
migration_method: :remove_trigger,
|
63
|
+
object: trigger,
|
64
|
+
code_comment: code_comment,
|
65
|
+
migration: <<~RUBY
|
66
|
+
remove_trigger :#{trigger.table.name}, :#{trigger.name}
|
67
|
+
RUBY
|
58
68
|
end
|
59
69
|
|
60
70
|
def recreate_trigger original_trigger, updated_trigger
|
@@ -82,18 +92,28 @@ module DynamicMigrations
|
|
82
92
|
raise MissingDescriptionError
|
83
93
|
end
|
84
94
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
95
|
+
add_fragment schema: trigger.table.schema,
|
96
|
+
table: trigger.table,
|
97
|
+
migration_method: :set_trigger_comment,
|
98
|
+
object: trigger,
|
99
|
+
code_comment: code_comment,
|
100
|
+
migration: <<~RUBY
|
101
|
+
set_trigger_comment :#{trigger.table.name}, :#{trigger.name}, <<~COMMENT
|
102
|
+
#{indent description}
|
103
|
+
COMMENT
|
104
|
+
RUBY
|
90
105
|
end
|
91
106
|
|
92
107
|
# remove the comment from a trigger
|
93
108
|
def remove_trigger_comment trigger, code_comment = nil
|
94
|
-
|
95
|
-
|
96
|
-
|
109
|
+
add_fragment schema: trigger.table.schema,
|
110
|
+
table: trigger.table,
|
111
|
+
migration_method: :remove_trigger_comment,
|
112
|
+
object: trigger,
|
113
|
+
code_comment: code_comment,
|
114
|
+
migration: <<~RUBY
|
115
|
+
remove_trigger_comment :#{trigger.table.name}, :#{trigger.name}
|
116
|
+
RUBY
|
97
117
|
end
|
98
118
|
end
|
99
119
|
end
|
@@ -24,15 +24,25 @@ module DynamicMigrations
|
|
24
24
|
|
25
25
|
options_syntax = options.map { |k, v| "#{k}: #{v}" }.join(", ")
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
27
|
+
add_fragment schema: unique_constraint.table.schema,
|
28
|
+
table: unique_constraint.table,
|
29
|
+
migration_method: :add_unique_constraint,
|
30
|
+
object: unique_constraint,
|
31
|
+
code_comment: code_comment,
|
32
|
+
migration: <<~RUBY
|
33
|
+
add_unique_constraint :#{unique_constraint.table.name}, #{column_names}, #{options_syntax}
|
34
|
+
RUBY
|
30
35
|
end
|
31
36
|
|
32
37
|
def remove_unique_constraint unique_constraint, code_comment = nil
|
33
|
-
|
34
|
-
|
35
|
-
|
38
|
+
add_fragment schema: unique_constraint.table.schema,
|
39
|
+
table: unique_constraint.table,
|
40
|
+
migration_method: :remove_unique_constraint,
|
41
|
+
object: unique_constraint,
|
42
|
+
code_comment: code_comment,
|
43
|
+
migration: <<~RUBY
|
44
|
+
remove_unique_constraint :#{unique_constraint.table.name}, :#{unique_constraint.name}
|
45
|
+
RUBY
|
36
46
|
end
|
37
47
|
|
38
48
|
def recreate_unique_constraint original_unique_constraint, updated_unique_constraint
|
@@ -60,18 +70,28 @@ module DynamicMigrations
|
|
60
70
|
raise MissingDescriptionError
|
61
71
|
end
|
62
72
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
73
|
+
add_fragment schema: unique_constraint.table.schema,
|
74
|
+
table: unique_constraint.table,
|
75
|
+
migration_method: :set_unique_constraint_comment,
|
76
|
+
object: unique_constraint,
|
77
|
+
code_comment: code_comment,
|
78
|
+
migration: <<~RUBY
|
79
|
+
set_unique_constraint_comment :#{unique_constraint.table.name}, :#{unique_constraint.name}, <<~COMMENT
|
80
|
+
#{indent description}
|
81
|
+
COMMENT
|
82
|
+
RUBY
|
68
83
|
end
|
69
84
|
|
70
85
|
# remove the comment from a unique_constraint
|
71
86
|
def remove_unique_constraint_comment unique_constraint, code_comment = nil
|
72
|
-
|
73
|
-
|
74
|
-
|
87
|
+
add_fragment schema: unique_constraint.table.schema,
|
88
|
+
table: unique_constraint.table,
|
89
|
+
migration_method: :remove_unique_constraint_comment,
|
90
|
+
object: unique_constraint,
|
91
|
+
code_comment: code_comment,
|
92
|
+
migration: <<~RUBY
|
93
|
+
remove_unique_constraint_comment :#{unique_constraint.table.name}, :#{unique_constraint.name}
|
94
|
+
RUBY
|
75
95
|
end
|
76
96
|
end
|
77
97
|
end
|
@@ -28,19 +28,29 @@ module DynamicMigrations
|
|
28
28
|
validation_sql << ";"
|
29
29
|
end
|
30
30
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
31
|
+
add_fragment schema: validation.table.schema,
|
32
|
+
table: validation.table,
|
33
|
+
migration_method: :add_validation,
|
34
|
+
object: validation,
|
35
|
+
code_comment: code_comment,
|
36
|
+
migration: comment_sql + <<~RUBY
|
37
|
+
add_validation :#{validation.table.name}, #{options_syntax} do
|
38
|
+
<<~SQL
|
39
|
+
#{indent validation_sql}
|
40
|
+
SQL
|
41
|
+
end
|
42
|
+
RUBY
|
38
43
|
end
|
39
44
|
|
40
45
|
def remove_validation validation, code_comment = nil
|
41
|
-
|
42
|
-
|
43
|
-
|
46
|
+
add_fragment schema: validation.table.schema,
|
47
|
+
table: validation.table,
|
48
|
+
migration_method: :remove_validation,
|
49
|
+
object: validation,
|
50
|
+
code_comment: code_comment,
|
51
|
+
migration: <<~RUBY
|
52
|
+
remove_validation :#{validation.table.name}, :#{validation.name}
|
53
|
+
RUBY
|
44
54
|
end
|
45
55
|
|
46
56
|
def recreate_validation original_validation, updated_validation
|
@@ -68,18 +78,28 @@ module DynamicMigrations
|
|
68
78
|
raise MissingDescriptionError
|
69
79
|
end
|
70
80
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
81
|
+
add_fragment schema: validation.table.schema,
|
82
|
+
table: validation.table,
|
83
|
+
migration_method: :set_validation_comment,
|
84
|
+
object: validation,
|
85
|
+
code_comment: code_comment,
|
86
|
+
migration: <<~RUBY
|
87
|
+
set_validation_comment :#{validation.table.name}, :#{validation.name}, <<~COMMENT
|
88
|
+
#{indent description}
|
89
|
+
COMMENT
|
90
|
+
RUBY
|
76
91
|
end
|
77
92
|
|
78
93
|
# remove the comment from a validation
|
79
94
|
def remove_validation_comment validation, code_comment = nil
|
80
|
-
|
81
|
-
|
82
|
-
|
95
|
+
add_fragment schema: validation.table.schema,
|
96
|
+
table: validation.table,
|
97
|
+
migration_method: :remove_validation_comment,
|
98
|
+
object: validation,
|
99
|
+
code_comment: code_comment,
|
100
|
+
migration: <<~RUBY
|
101
|
+
remove_validation_comment :#{validation.table.name}, :#{validation.name}
|
102
|
+
RUBY
|
83
103
|
end
|
84
104
|
end
|
85
105
|
end
|