dynamic_migrations 2.1.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +23 -0
- data/lib/dynamic_migrations/active_record/migrators/column.rb +21 -0
- data/lib/dynamic_migrations/active_record/migrators/foreign_key_constraint.rb +112 -0
- data/lib/dynamic_migrations/active_record/migrators/function.rb +108 -0
- data/lib/dynamic_migrations/active_record/migrators/index.rb +27 -0
- data/lib/dynamic_migrations/active_record/migrators/schema.rb +21 -0
- data/lib/dynamic_migrations/active_record/migrators/table.rb +21 -0
- data/lib/dynamic_migrations/active_record/migrators/trigger.rb +109 -0
- data/lib/dynamic_migrations/active_record/migrators/unique_constraint.rb +63 -0
- data/lib/dynamic_migrations/active_record/migrators/validation.rb +67 -0
- data/lib/dynamic_migrations/active_record/migrators.rb +64 -0
- data/lib/dynamic_migrations/name_helper.rb +13 -0
- data/lib/dynamic_migrations/postgres/generator/column.rb +92 -0
- data/lib/dynamic_migrations/postgres/generator/foreign_key_constraint.rb +84 -0
- data/lib/dynamic_migrations/postgres/generator/fragment.rb +30 -0
- data/lib/dynamic_migrations/postgres/generator/function.rb +77 -0
- data/lib/dynamic_migrations/postgres/generator/index.rb +101 -0
- data/lib/dynamic_migrations/postgres/generator/primary_key.rb +55 -0
- data/lib/dynamic_migrations/postgres/generator/schema.rb +19 -0
- data/lib/dynamic_migrations/postgres/generator/schema_migrations/section.rb +37 -0
- data/lib/dynamic_migrations/postgres/generator/schema_migrations.rb +92 -0
- data/lib/dynamic_migrations/postgres/generator/table.rb +122 -0
- data/lib/dynamic_migrations/postgres/generator/trigger.rb +101 -0
- data/lib/dynamic_migrations/postgres/generator/unique_constraint.rb +79 -0
- data/lib/dynamic_migrations/postgres/generator/validation.rb +87 -0
- data/lib/dynamic_migrations/postgres/generator.rb +359 -0
- data/lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/functions.rb +68 -0
- data/lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/columns.rb +72 -0
- data/lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/foreign_key_constraints.rb +73 -0
- data/lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/indexes.rb +73 -0
- data/lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/primary_key.rb +49 -0
- data/lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/triggers.rb +73 -0
- data/lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/unique_constraints.rb +73 -0
- data/lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/validations.rb +73 -0
- data/lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables.rb +80 -0
- data/lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas.rb +48 -0
- data/lib/dynamic_migrations/postgres/server/database/differences/to_migrations.rb +59 -0
- data/lib/dynamic_migrations/postgres/server/database/differences.rb +76 -16
- data/lib/dynamic_migrations/postgres/server/database/keys_and_unique_constraints_loader.rb +35 -9
- data/lib/dynamic_migrations/postgres/server/database/loaded_schemas_builder.rb +50 -26
- data/lib/dynamic_migrations/postgres/server/database/schema/function.rb +69 -0
- data/lib/dynamic_migrations/postgres/server/database/schema/functions.rb +63 -0
- data/lib/dynamic_migrations/postgres/server/database/schema/table/column.rb +6 -44
- data/lib/dynamic_migrations/postgres/server/database/schema/table/columns.rb +1 -1
- data/lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraint.rb +40 -5
- data/lib/dynamic_migrations/postgres/server/database/schema/table/index.rb +23 -9
- data/lib/dynamic_migrations/postgres/server/database/schema/table/primary_key.rb +21 -6
- data/lib/dynamic_migrations/postgres/server/database/schema/table/trigger.rb +151 -0
- data/lib/dynamic_migrations/postgres/server/database/schema/table/triggers.rb +66 -0
- data/lib/dynamic_migrations/postgres/server/database/schema/table/unique_constraint.rb +19 -9
- data/lib/dynamic_migrations/postgres/server/database/schema/table/validation.rb +20 -1
- data/lib/dynamic_migrations/postgres/server/database/schema/table.rb +15 -5
- data/lib/dynamic_migrations/postgres/server/database/schema/tables.rb +63 -0
- data/lib/dynamic_migrations/postgres/server/database/schema.rb +3 -49
- data/lib/dynamic_migrations/postgres/server/database/source.rb +21 -0
- data/lib/dynamic_migrations/postgres/server/database/structure_loader.rb +22 -112
- data/lib/dynamic_migrations/postgres/server/database/triggers_and_functions_loader.rb +131 -0
- data/lib/dynamic_migrations/postgres/server/database/validations_loader.rb +10 -4
- data/lib/dynamic_migrations/postgres/server/database.rb +2 -1
- data/lib/dynamic_migrations/postgres/server.rb +6 -0
- data/lib/dynamic_migrations/postgres.rb +1 -1
- data/lib/dynamic_migrations/version.rb +1 -1
- data/lib/dynamic_migrations.rb +47 -4
- metadata +44 -3
- data/lib/dynamic_migrations/postgres/data_types.rb +0 -320
@@ -7,88 +7,41 @@ module DynamicMigrations
|
|
7
7
|
module StructureLoader
|
8
8
|
def create_database_structure_cache
|
9
9
|
connection.exec(<<~SQL)
|
10
|
-
CREATE MATERIALIZED VIEW public.dynamic_migrations_structure_cache
|
10
|
+
CREATE MATERIALIZED VIEW public.dynamic_migrations_structure_cache AS
|
11
11
|
SELECT
|
12
12
|
-- Name of the schema containing the table
|
13
13
|
schemata.schema_name,
|
14
14
|
-- Name of the table
|
15
15
|
tables.table_name,
|
16
16
|
-- The comment which has been added to the table (if any)
|
17
|
-
table_description.description
|
17
|
+
table_description.description AS table_description,
|
18
18
|
-- Name of the column
|
19
19
|
columns.column_name,
|
20
20
|
-- The comment which has been added to the column (if any)
|
21
|
-
column_description.description
|
21
|
+
column_description.description AS column_description,
|
22
22
|
-- Default expression of the column
|
23
23
|
columns.column_default,
|
24
24
|
-- YES if the column is possibly nullable, NO if
|
25
25
|
-- it is known not nullable
|
26
26
|
columns.is_nullable,
|
27
|
-
--
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
-- If data_type identifies a character or bit string type,
|
37
|
-
-- the declared maximum length; null for all other data
|
38
|
-
-- types or if no maximum length was declared.
|
39
|
-
columns.character_maximum_length,
|
40
|
-
-- If data_type identifies a character type, the maximum
|
41
|
-
-- possible length in octets (bytes) of a datum; null for
|
42
|
-
-- all other data types. The maximum octet length depends
|
43
|
-
-- on the declared character maximum length (see above)
|
44
|
-
-- and the server encoding.
|
45
|
-
columns.character_octet_length,
|
46
|
-
-- If data_type identifies a numeric type, this column
|
47
|
-
-- contains the (declared or implicit) precision of the type
|
48
|
-
-- for this column. The precision indicates the number of
|
49
|
-
-- significant digits. It can be expressed in decimal (base 10)
|
50
|
-
-- or binary (base 2) terms, as specified in the column
|
51
|
-
-- numeric_precision_radix. For all other data types, this
|
52
|
-
-- column is null.
|
53
|
-
columns.numeric_precision,
|
54
|
-
-- If data_type identifies a numeric type, this column indicates
|
55
|
-
-- in which base the values in the columns numeric_precision and
|
56
|
-
-- numeric_scale are expressed. The value is either 2 or 10. For
|
57
|
-
-- all other data types, this column is null.
|
58
|
-
columns.numeric_precision_radix,
|
59
|
-
-- If data_type identifies an exact numeric type, this column
|
60
|
-
-- contains the (declared or implicit) scale of the type for this
|
61
|
-
-- column. The scale indicates the number of significant digits to
|
62
|
-
-- the right of the decimal point. It can be expressed in decimal
|
63
|
-
-- (base 10) or binary (base 2) terms, as specified in the column
|
64
|
-
-- numeric_precision_radix. For all other data types, this column
|
65
|
-
-- is null.
|
66
|
-
columns.numeric_scale,
|
67
|
-
-- If data_type identifies a date, time, timestamp, or interval
|
68
|
-
-- type, this column contains the (declared or implicit) fractional
|
69
|
-
-- seconds precision of the type for this column, that is, the
|
70
|
-
-- number of decimal digits maintained following the decimal point
|
71
|
-
-- in the seconds value. For all other data types, this column is
|
72
|
-
-- null.
|
73
|
-
columns.datetime_precision,
|
27
|
+
-- The formatted data type (such as integer, char(5) or numeric(12,2)[])
|
28
|
+
CASE
|
29
|
+
WHEN tables.table_name IS NOT NULL THEN
|
30
|
+
(
|
31
|
+
SELECT format_type(atttypid,atttypmod) FROM pg_attribute a
|
32
|
+
WHERE a.attrelid = concat('"', schemata.schema_name, '"', '.', '"', tables.table_name, '"')::regclass
|
33
|
+
AND attnum = columns.ordinal_position
|
34
|
+
)
|
35
|
+
END AS data_type,
|
74
36
|
-- If data_type identifies an interval type, this column contains
|
75
37
|
-- the specification which fields the intervals include for this
|
76
38
|
-- column, e.g., YEAR TO MONTH, DAY TO SECOND, etc. If no field
|
77
39
|
-- restrictions were specified (that is, the interval accepts all
|
78
|
-
|
79
|
-
columns.interval_type
|
80
|
-
-- Name of the schema that the column data type (the underlying
|
81
|
-
--type of the domain, if applicable) is defined in
|
82
|
-
columns.udt_schema,
|
83
|
-
-- Name of the column data type (the underlying type of the domain,
|
84
|
-
-- if applicable)
|
85
|
-
columns.udt_name,
|
86
|
-
-- YES if the column is updatable, NO if not (Columns in base tables
|
87
|
-
-- are always updatable, columns in views not necessarily)
|
88
|
-
columns.is_updatable
|
40
|
+
-- fields), and for all other data types, this field is null.
|
41
|
+
columns.interval_type
|
89
42
|
FROM information_schema.schemata
|
90
43
|
LEFT JOIN information_schema.tables ON schemata.schema_name = tables.table_schema AND left(tables.table_name, 3) != 'pg_'
|
91
|
-
LEFT JOIN information_schema.columns ON tables.table_name = columns.table_name
|
44
|
+
LEFT JOIN information_schema.columns ON tables.table_name = columns.table_name AND schemata.schema_name = columns.table_schema
|
92
45
|
-- required for the column and table description/comment joins
|
93
46
|
LEFT JOIN pg_catalog.pg_statio_all_tables ON pg_statio_all_tables.schemaname = schemata.schema_name AND pg_statio_all_tables.relname = tables.table_name
|
94
47
|
-- required for the table description/comment
|
@@ -110,12 +63,12 @@ module DynamicMigrations
|
|
110
63
|
# useful hash representing the structure of your database
|
111
64
|
def fetch_structure
|
112
65
|
begin
|
113
|
-
rows = connection.
|
66
|
+
rows = connection.exec(<<~SQL)
|
114
67
|
SELECT * FROM public.dynamic_migrations_structure_cache
|
115
68
|
SQL
|
116
69
|
rescue PG::UndefinedTable
|
117
70
|
create_database_structure_cache
|
118
|
-
rows = connection.
|
71
|
+
rows = connection.exec(<<~SQL)
|
119
72
|
SELECT * FROM public.dynamic_migrations_structure_cache
|
120
73
|
SQL
|
121
74
|
end
|
@@ -142,16 +95,7 @@ module DynamicMigrations
|
|
142
95
|
column[:null] = row["is_nullable"] == "YES"
|
143
96
|
column[:default] = row["column_default"]
|
144
97
|
column[:description] = row["column_description"]
|
145
|
-
column[:character_maximum_length] = row["character_maximum_length"].nil? ? nil : row["character_maximum_length"].to_i
|
146
|
-
column[:character_octet_length] = row["character_octet_length"].nil? ? nil : row["character_octet_length"].to_i
|
147
|
-
column[:numeric_precision] = row["numeric_precision"].nil? ? nil : row["numeric_precision"].to_i
|
148
|
-
column[:numeric_precision_radix] = row["numeric_precision_radix"].nil? ? nil : row["numeric_precision_radix"].to_i
|
149
|
-
column[:numeric_scale] = row["numeric_scale"].nil? ? nil : row["numeric_scale"].to_i
|
150
|
-
column[:datetime_precision] = row["datetime_precision"].nil? ? nil : row["datetime_precision"].to_i
|
151
98
|
column[:interval_type] = row["interval_type"].nil? ? nil : row["interval_type"].to_sym
|
152
|
-
column[:udt_schema] = row["udt_schema"].to_sym
|
153
|
-
column[:udt_name] = row["udt_name"].to_sym
|
154
|
-
column[:updatable] = row["is_updatable"] == "YES"
|
155
99
|
end
|
156
100
|
end
|
157
101
|
end
|
@@ -168,29 +112,11 @@ module DynamicMigrations
|
|
168
112
|
table = schema.add_table table_name, table_definition[:description]
|
169
113
|
|
170
114
|
table_definition[:columns].each do |column_name, column_definition|
|
171
|
-
# we only need these for arrays and user-defined types
|
172
|
-
# (user-defined is usually ENUMS)
|
173
|
-
if [:ARRAY, :"USER-DEFINED"].include? column_definition[:data_type]
|
174
|
-
udt_schema = column_definition[:udt_schema]
|
175
|
-
udt_name = column_definition[:udt_name]
|
176
|
-
else
|
177
|
-
udt_schema = nil
|
178
|
-
udt_name = nil
|
179
|
-
end
|
180
|
-
|
181
115
|
table.add_column column_name, column_definition[:data_type],
|
182
116
|
null: column_definition[:null],
|
183
117
|
default: column_definition[:default],
|
184
118
|
description: column_definition[:description],
|
185
|
-
|
186
|
-
character_octet_length: column_definition[:character_octet_length],
|
187
|
-
numeric_precision: column_definition[:numeric_precision],
|
188
|
-
numeric_precision_radix: column_definition[:numeric_precision_radix],
|
189
|
-
numeric_scale: column_definition[:numeric_scale],
|
190
|
-
datetime_precision: column_definition[:datetime_precision],
|
191
|
-
udt_schema: udt_schema,
|
192
|
-
udt_name: udt_name,
|
193
|
-
updatable: column_definition[:updatable]
|
119
|
+
interval_type: column_definition[:interval_type]
|
194
120
|
end
|
195
121
|
end
|
196
122
|
end
|
@@ -198,7 +124,7 @@ module DynamicMigrations
|
|
198
124
|
|
199
125
|
# returns a list of the schema names in this database
|
200
126
|
def fetch_schema_names
|
201
|
-
rows = connection.exec(
|
127
|
+
rows = connection.exec(<<~SQL)
|
202
128
|
SELECT schema_name
|
203
129
|
FROM information_schema.schemata;
|
204
130
|
SQL
|
@@ -211,30 +137,14 @@ module DynamicMigrations
|
|
211
137
|
|
212
138
|
# returns a list of the table names in the provided schema
|
213
139
|
def fetch_table_names schema_name
|
214
|
-
rows = connection.exec_params(
|
215
|
-
|
216
|
-
|
140
|
+
rows = connection.exec_params(<<~SQL, [schema_name.to_s])
|
141
|
+
SELECT table_name FROM information_schema.tables
|
142
|
+
WHERE table_schema = $1
|
217
143
|
SQL
|
218
144
|
table_names = rows.map { |row| row["table_name"] }
|
219
145
|
table_names.reject! { |table_name| table_name.start_with? "pg_" }
|
220
146
|
table_names.sort
|
221
147
|
end
|
222
|
-
|
223
|
-
# returns a list of columns definitions for the provided table
|
224
|
-
def fetch_columns schema_name, table_name
|
225
|
-
rows = connection.exec_params(<<-SQL, [schema_name.to_s, table_name.to_s])
|
226
|
-
SELECT column_name, is_nullable, data_type, character_octet_length, column_default, numeric_precision, numeric_precision_radix, numeric_scale, udt_schema, udt_name
|
227
|
-
FROM information_schema.columns
|
228
|
-
WHERE table_schema = $1
|
229
|
-
AND table_name = $2;
|
230
|
-
SQL
|
231
|
-
rows.map do |row|
|
232
|
-
{
|
233
|
-
column_name: row["column_name"].to_sym,
|
234
|
-
type: row["data_type"].to_sym
|
235
|
-
}
|
236
|
-
end
|
237
|
-
end
|
238
148
|
end
|
239
149
|
end
|
240
150
|
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DynamicMigrations
|
4
|
+
module Postgres
|
5
|
+
class Server
|
6
|
+
class Database
|
7
|
+
module TriggersAndFunctionsLoader
|
8
|
+
class EventTriggerProcedureSchemaMismatchError < StandardError
|
9
|
+
end
|
10
|
+
|
11
|
+
# fetch all columns from the database and build and return a
|
12
|
+
# useful hash representing the triggers_and_functions of your database
|
13
|
+
#
|
14
|
+
# this query is very fast, so does not need cached (a materialized view)
|
15
|
+
def fetch_triggers_and_functions
|
16
|
+
rows = connection.exec(<<~SQL)
|
17
|
+
SELECT
|
18
|
+
n.nspname AS trigger_schema,
|
19
|
+
t.tgname AS trigger_name,
|
20
|
+
em.text AS event_manipulation,
|
21
|
+
n.nspname AS event_object_schema,
|
22
|
+
c.relname AS event_object_table,
|
23
|
+
rank() OVER (
|
24
|
+
PARTITION BY (n.nspname),
|
25
|
+
(c.relname),
|
26
|
+
em.num,
|
27
|
+
(t.tgtype & 1),
|
28
|
+
(t.tgtype & 66)
|
29
|
+
ORDER BY
|
30
|
+
t.tgname
|
31
|
+
) AS action_order,
|
32
|
+
CASE WHEN pg_has_role(c.relowner, 'USAGE') THEN (
|
33
|
+
regexp_match(
|
34
|
+
pg_get_triggerdef(t.oid),
|
35
|
+
'.{35,} WHEN ((.+)) EXECUTE FUNCTION'
|
36
|
+
)
|
37
|
+
) [1] ELSE NULL END AS action_condition,
|
38
|
+
p_n.nspname AS function_schema,
|
39
|
+
p.proname AS function_name,
|
40
|
+
p.prosrc AS function_definition,
|
41
|
+
SUBSTRING(
|
42
|
+
pg_get_triggerdef(t.oid)
|
43
|
+
FROM
|
44
|
+
POSITION(
|
45
|
+
('EXECUTE FUNCTION') IN (
|
46
|
+
SUBSTRING(
|
47
|
+
pg_get_triggerdef(t.oid)
|
48
|
+
FROM
|
49
|
+
48
|
50
|
+
)
|
51
|
+
)
|
52
|
+
) + 47
|
53
|
+
) AS action_statement,
|
54
|
+
CASE t.tgtype & 1 WHEN 1 THEN 'row' ELSE 'statement' END AS action_orientation,
|
55
|
+
CASE t.tgtype & 66 WHEN 2 THEN 'before' WHEN 64 THEN 'instead_of' ELSE 'after' END AS action_timing,
|
56
|
+
t.tgoldtable AS action_reference_old_table,
|
57
|
+
t.tgnewtable AS action_reference_new_table,
|
58
|
+
obj_description(t.oid, 'pg_trigger') as description,
|
59
|
+
obj_description(p.oid, 'pg_proc') as function_description
|
60
|
+
FROM
|
61
|
+
-- trigger tables
|
62
|
+
pg_namespace n,
|
63
|
+
pg_class c,
|
64
|
+
pg_trigger t,
|
65
|
+
-- procedure tables
|
66
|
+
pg_proc p,
|
67
|
+
pg_namespace p_n,
|
68
|
+
(
|
69
|
+
VALUES
|
70
|
+
(4, 'insert'),
|
71
|
+
(8, 'delete'),
|
72
|
+
(16, 'update')
|
73
|
+
) em(num, text)
|
74
|
+
WHERE
|
75
|
+
n.oid = c.relnamespace
|
76
|
+
AND c.oid = t.tgrelid
|
77
|
+
AND p.oid = t.tgfoid
|
78
|
+
AND p_n.oid = p.pronamespace
|
79
|
+
AND (t.tgtype & em.num) <> 0
|
80
|
+
AND NOT t.tgisinternal
|
81
|
+
AND NOT pg_is_other_temp_schema(n.oid)
|
82
|
+
AND (
|
83
|
+
pg_has_role(c.relowner, 'USAGE')
|
84
|
+
OR has_table_privilege(
|
85
|
+
c.oid, 'INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER'
|
86
|
+
)
|
87
|
+
OR has_any_column_privilege(
|
88
|
+
c.oid, 'INSERT, UPDATE, REFERENCES'
|
89
|
+
)
|
90
|
+
);
|
91
|
+
SQL
|
92
|
+
|
93
|
+
schemas = {}
|
94
|
+
rows.each do |row|
|
95
|
+
trigger_name = row["trigger_name"].to_sym
|
96
|
+
event_object_schema = row["event_object_schema"].to_sym
|
97
|
+
event_object_table = row["event_object_table"].to_sym
|
98
|
+
|
99
|
+
schema = schemas[event_object_schema] ||= {}
|
100
|
+
table = schema[event_object_table] ||= {}
|
101
|
+
|
102
|
+
# by convention (and to simplify things) we place these all in the same schema
|
103
|
+
unless row["trigger_schema"] == row["function_schema"] && row["function_schema"] == row["event_object_schema"]
|
104
|
+
raise EventTriggerProcedureSchemaMismatchError, "Expected trigger, procedure and event_object to be in the same schema for trigger '#{trigger_name}'"
|
105
|
+
end
|
106
|
+
|
107
|
+
table[trigger_name] = {
|
108
|
+
trigger_schema: row["trigger_schema"].to_sym,
|
109
|
+
event_manipulation: row["event_manipulation"].to_sym,
|
110
|
+
action_order: row["action_order"].to_i,
|
111
|
+
action_condition: row["action_condition"],
|
112
|
+
function_schema: row["function_schema"].to_sym,
|
113
|
+
function_name: row["function_name"].to_sym,
|
114
|
+
function_definition: row["function_definition"],
|
115
|
+
action_statement: row["action_statement"],
|
116
|
+
action_orientation: row["action_orientation"].to_sym,
|
117
|
+
action_timing: row["action_timing"].to_sym,
|
118
|
+
# `action_reference_old_table` and `action_reference_new_table` can be null
|
119
|
+
action_reference_old_table: row["action_reference_old_table"]&.to_sym,
|
120
|
+
action_reference_new_table: row["action_reference_new_table"]&.to_sym,
|
121
|
+
description: row["description"],
|
122
|
+
function_description: row["function_description"]
|
123
|
+
}
|
124
|
+
end
|
125
|
+
schemas
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
@@ -12,7 +12,9 @@ module DynamicMigrations
|
|
12
12
|
table_constraints.table_name,
|
13
13
|
array_agg(col.column_name ORDER BY col.column_name) AS columns,
|
14
14
|
table_constraints.constraint_name as validation_name,
|
15
|
-
|
15
|
+
pg_get_expr(conbin, conrelid, true) as check_clause,
|
16
|
+
obj_description(pgc.oid, 'pg_constraint') as description,
|
17
|
+
-- in case we need to update this query in a later version of DynamicMigrations
|
16
18
|
1 as table_version
|
17
19
|
FROM information_schema.table_constraints
|
18
20
|
JOIN information_schema.check_constraints
|
@@ -30,6 +32,7 @@ module DynamicMigrations
|
|
30
32
|
AND table_constraints.constraint_schema != 'postgis'
|
31
33
|
AND left(table_constraints.constraint_schema, 3) != 'pg_'
|
32
34
|
GROUP BY
|
35
|
+
pgc.oid,
|
33
36
|
table_constraints.table_schema,
|
34
37
|
table_constraints.table_name,
|
35
38
|
table_constraints.constraint_name,
|
@@ -47,12 +50,12 @@ module DynamicMigrations
|
|
47
50
|
# useful hash representing the validations of your database
|
48
51
|
def fetch_validations
|
49
52
|
begin
|
50
|
-
rows = connection.
|
53
|
+
rows = connection.exec(<<~SQL)
|
51
54
|
SELECT * FROM public.dynamic_migrations_validations_cache
|
52
55
|
SQL
|
53
56
|
rescue PG::UndefinedTable
|
54
57
|
create_database_validations_cache
|
55
|
-
rows = connection.
|
58
|
+
rows = connection.exec(<<~SQL)
|
56
59
|
SELECT * FROM public.dynamic_migrations_validations_cache
|
57
60
|
SQL
|
58
61
|
end
|
@@ -69,7 +72,10 @@ module DynamicMigrations
|
|
69
72
|
|
70
73
|
table[validation_name] = {
|
71
74
|
columns: row["columns"].gsub(/\A\{/, "").gsub(/\}\Z/, "").split(",").map { |column_name| column_name.to_sym },
|
72
|
-
check_clause: row["check_clause"]
|
75
|
+
check_clause: row["check_clause"],
|
76
|
+
description: row["description"],
|
77
|
+
deferrable: row["deferrable"] == "TRUE",
|
78
|
+
initially_deferred: row["initially_deferred"] == "TRUE"
|
73
79
|
}
|
74
80
|
end
|
75
81
|
schemas
|
@@ -13,6 +13,7 @@ module DynamicMigrations
|
|
13
13
|
include StructureLoader
|
14
14
|
include ValidationsLoader
|
15
15
|
include KeysAndUniqueConstraintsLoader
|
16
|
+
include TriggersAndFunctionsLoader
|
16
17
|
include LoadedSchemas
|
17
18
|
include ConfiguredSchemas
|
18
19
|
include LoadedSchemasBuilder
|
@@ -46,7 +47,7 @@ module DynamicMigrations
|
|
46
47
|
end
|
47
48
|
|
48
49
|
def differences
|
49
|
-
Differences.new(self)
|
50
|
+
Differences.new(self)
|
50
51
|
end
|
51
52
|
end
|
52
53
|
end
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
module DynamicMigrations
|
4
4
|
# This module exists only to namespace Postgres functionality and
|
5
|
-
# make it possible to add other database/storage types in the
|
5
|
+
# make it possible to add other database/storage types in the future.
|
6
6
|
module Postgres
|
7
7
|
end
|
8
8
|
end
|
data/lib/dynamic_migrations.rb
CHANGED
@@ -14,31 +14,74 @@ require "dynamic_migrations/postgres/server/database/connection"
|
|
14
14
|
require "dynamic_migrations/postgres/server/database/structure_loader"
|
15
15
|
require "dynamic_migrations/postgres/server/database/validations_loader"
|
16
16
|
require "dynamic_migrations/postgres/server/database/keys_and_unique_constraints_loader"
|
17
|
+
require "dynamic_migrations/postgres/server/database/triggers_and_functions_loader"
|
17
18
|
require "dynamic_migrations/postgres/server/database/loaded_schemas_builder"
|
18
|
-
require "dynamic_migrations/postgres/server/database/differences"
|
19
19
|
require "dynamic_migrations/postgres/server/database/loaded_schemas"
|
20
20
|
require "dynamic_migrations/postgres/server/database/configured_schemas"
|
21
|
+
|
22
|
+
require "dynamic_migrations/postgres/server/database/differences"
|
23
|
+
require "dynamic_migrations/postgres/server/database/differences/to_migrations/schemas"
|
24
|
+
require "dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables"
|
25
|
+
require "dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/columns"
|
26
|
+
require "dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/foreign_key_constraints"
|
27
|
+
require "dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/indexes"
|
28
|
+
require "dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/primary_key"
|
29
|
+
require "dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/triggers"
|
30
|
+
require "dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/unique_constraints"
|
31
|
+
require "dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/validations"
|
32
|
+
require "dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/functions"
|
33
|
+
require "dynamic_migrations/postgres/server/database/differences/to_migrations"
|
34
|
+
|
21
35
|
require "dynamic_migrations/postgres/server/database"
|
22
36
|
require "dynamic_migrations/postgres/server/database/source"
|
37
|
+
|
38
|
+
require "dynamic_migrations/postgres/server/database/schema/tables"
|
39
|
+
require "dynamic_migrations/postgres/server/database/schema/functions"
|
23
40
|
require "dynamic_migrations/postgres/server/database/schema"
|
41
|
+
require "dynamic_migrations/postgres/server/database/schema/function"
|
24
42
|
require "dynamic_migrations/postgres/server/database/schema/table/validations"
|
25
43
|
require "dynamic_migrations/postgres/server/database/schema/table/indexes"
|
26
44
|
require "dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraints"
|
27
45
|
require "dynamic_migrations/postgres/server/database/schema/table/unique_constraints"
|
28
46
|
require "dynamic_migrations/postgres/server/database/schema/table/columns"
|
47
|
+
require "dynamic_migrations/postgres/server/database/schema/table/triggers"
|
29
48
|
require "dynamic_migrations/postgres/server/database/schema/table"
|
30
49
|
require "dynamic_migrations/postgres/server/database/schema/table/column"
|
31
50
|
require "dynamic_migrations/postgres/server/database/schema/table/validation"
|
32
51
|
require "dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraint"
|
33
52
|
require "dynamic_migrations/postgres/server/database/schema/table/index"
|
34
53
|
require "dynamic_migrations/postgres/server/database/schema/table/primary_key"
|
54
|
+
require "dynamic_migrations/postgres/server/database/schema/table/trigger"
|
35
55
|
require "dynamic_migrations/postgres/server/database/schema/table/unique_constraint"
|
36
56
|
|
37
57
|
require "dynamic_migrations/postgres/server"
|
38
58
|
require "dynamic_migrations/postgres/connections"
|
39
|
-
|
59
|
+
|
60
|
+
require "dynamic_migrations/postgres/generator/schema"
|
61
|
+
require "dynamic_migrations/postgres/generator/table"
|
62
|
+
require "dynamic_migrations/postgres/generator/column"
|
63
|
+
require "dynamic_migrations/postgres/generator/foreign_key_constraint"
|
64
|
+
require "dynamic_migrations/postgres/generator/function"
|
65
|
+
require "dynamic_migrations/postgres/generator/index"
|
66
|
+
require "dynamic_migrations/postgres/generator/primary_key"
|
67
|
+
require "dynamic_migrations/postgres/generator/trigger"
|
68
|
+
require "dynamic_migrations/postgres/generator/unique_constraint"
|
69
|
+
require "dynamic_migrations/postgres/generator/validation"
|
70
|
+
require "dynamic_migrations/postgres/generator"
|
71
|
+
require "dynamic_migrations/postgres/generator/fragment"
|
72
|
+
require "dynamic_migrations/postgres/generator/schema_migrations"
|
73
|
+
require "dynamic_migrations/postgres/generator/schema_migrations/section"
|
74
|
+
|
75
|
+
require "dynamic_migrations/active_record/migrators/schema"
|
76
|
+
require "dynamic_migrations/active_record/migrators/validation"
|
77
|
+
require "dynamic_migrations/active_record/migrators/foreign_key_constraint"
|
78
|
+
require "dynamic_migrations/active_record/migrators/unique_constraint"
|
79
|
+
require "dynamic_migrations/active_record/migrators/function"
|
80
|
+
require "dynamic_migrations/active_record/migrators/trigger"
|
81
|
+
require "dynamic_migrations/active_record/migrators/table"
|
82
|
+
require "dynamic_migrations/active_record/migrators/index"
|
83
|
+
require "dynamic_migrations/active_record/migrators/column"
|
84
|
+
require "dynamic_migrations/active_record/migrators"
|
40
85
|
|
41
86
|
module DynamicMigrations
|
42
|
-
class Error < StandardError
|
43
|
-
end
|
44
87
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dynamic_migrations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Craig Ulliott
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-08-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pg
|
@@ -65,24 +65,61 @@ files:
|
|
65
65
|
- LICENSE.txt
|
66
66
|
- README.md
|
67
67
|
- lib/dynamic_migrations.rb
|
68
|
+
- lib/dynamic_migrations/active_record/migrators.rb
|
69
|
+
- lib/dynamic_migrations/active_record/migrators/column.rb
|
70
|
+
- lib/dynamic_migrations/active_record/migrators/foreign_key_constraint.rb
|
71
|
+
- lib/dynamic_migrations/active_record/migrators/function.rb
|
72
|
+
- lib/dynamic_migrations/active_record/migrators/index.rb
|
73
|
+
- lib/dynamic_migrations/active_record/migrators/schema.rb
|
74
|
+
- lib/dynamic_migrations/active_record/migrators/table.rb
|
75
|
+
- lib/dynamic_migrations/active_record/migrators/trigger.rb
|
76
|
+
- lib/dynamic_migrations/active_record/migrators/unique_constraint.rb
|
77
|
+
- lib/dynamic_migrations/active_record/migrators/validation.rb
|
68
78
|
- lib/dynamic_migrations/expected_boolean_error.rb
|
69
79
|
- lib/dynamic_migrations/expected_integer_error.rb
|
70
80
|
- lib/dynamic_migrations/expected_string_error.rb
|
71
81
|
- lib/dynamic_migrations/expected_symbol_error.rb
|
72
82
|
- lib/dynamic_migrations/invalid_source_error.rb
|
73
83
|
- lib/dynamic_migrations/module_included_into_unexpected_target_error.rb
|
84
|
+
- lib/dynamic_migrations/name_helper.rb
|
74
85
|
- lib/dynamic_migrations/postgres.rb
|
75
86
|
- lib/dynamic_migrations/postgres/connections.rb
|
76
|
-
- lib/dynamic_migrations/postgres/
|
87
|
+
- lib/dynamic_migrations/postgres/generator.rb
|
88
|
+
- lib/dynamic_migrations/postgres/generator/column.rb
|
89
|
+
- lib/dynamic_migrations/postgres/generator/foreign_key_constraint.rb
|
90
|
+
- lib/dynamic_migrations/postgres/generator/fragment.rb
|
91
|
+
- lib/dynamic_migrations/postgres/generator/function.rb
|
92
|
+
- lib/dynamic_migrations/postgres/generator/index.rb
|
93
|
+
- lib/dynamic_migrations/postgres/generator/primary_key.rb
|
94
|
+
- lib/dynamic_migrations/postgres/generator/schema.rb
|
95
|
+
- lib/dynamic_migrations/postgres/generator/schema_migrations.rb
|
96
|
+
- lib/dynamic_migrations/postgres/generator/schema_migrations/section.rb
|
97
|
+
- lib/dynamic_migrations/postgres/generator/table.rb
|
98
|
+
- lib/dynamic_migrations/postgres/generator/trigger.rb
|
99
|
+
- lib/dynamic_migrations/postgres/generator/unique_constraint.rb
|
100
|
+
- lib/dynamic_migrations/postgres/generator/validation.rb
|
77
101
|
- lib/dynamic_migrations/postgres/server.rb
|
78
102
|
- lib/dynamic_migrations/postgres/server/database.rb
|
79
103
|
- lib/dynamic_migrations/postgres/server/database/configured_schemas.rb
|
80
104
|
- lib/dynamic_migrations/postgres/server/database/connection.rb
|
81
105
|
- lib/dynamic_migrations/postgres/server/database/differences.rb
|
106
|
+
- lib/dynamic_migrations/postgres/server/database/differences/to_migrations.rb
|
107
|
+
- lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas.rb
|
108
|
+
- lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/functions.rb
|
109
|
+
- lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables.rb
|
110
|
+
- lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/columns.rb
|
111
|
+
- lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/foreign_key_constraints.rb
|
112
|
+
- lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/indexes.rb
|
113
|
+
- lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/primary_key.rb
|
114
|
+
- lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/triggers.rb
|
115
|
+
- lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/unique_constraints.rb
|
116
|
+
- lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/validations.rb
|
82
117
|
- lib/dynamic_migrations/postgres/server/database/keys_and_unique_constraints_loader.rb
|
83
118
|
- lib/dynamic_migrations/postgres/server/database/loaded_schemas.rb
|
84
119
|
- lib/dynamic_migrations/postgres/server/database/loaded_schemas_builder.rb
|
85
120
|
- lib/dynamic_migrations/postgres/server/database/schema.rb
|
121
|
+
- lib/dynamic_migrations/postgres/server/database/schema/function.rb
|
122
|
+
- lib/dynamic_migrations/postgres/server/database/schema/functions.rb
|
86
123
|
- lib/dynamic_migrations/postgres/server/database/schema/table.rb
|
87
124
|
- lib/dynamic_migrations/postgres/server/database/schema/table/column.rb
|
88
125
|
- lib/dynamic_migrations/postgres/server/database/schema/table/columns.rb
|
@@ -91,12 +128,16 @@ files:
|
|
91
128
|
- lib/dynamic_migrations/postgres/server/database/schema/table/index.rb
|
92
129
|
- lib/dynamic_migrations/postgres/server/database/schema/table/indexes.rb
|
93
130
|
- lib/dynamic_migrations/postgres/server/database/schema/table/primary_key.rb
|
131
|
+
- lib/dynamic_migrations/postgres/server/database/schema/table/trigger.rb
|
132
|
+
- lib/dynamic_migrations/postgres/server/database/schema/table/triggers.rb
|
94
133
|
- lib/dynamic_migrations/postgres/server/database/schema/table/unique_constraint.rb
|
95
134
|
- lib/dynamic_migrations/postgres/server/database/schema/table/unique_constraints.rb
|
96
135
|
- lib/dynamic_migrations/postgres/server/database/schema/table/validation.rb
|
97
136
|
- lib/dynamic_migrations/postgres/server/database/schema/table/validations.rb
|
137
|
+
- lib/dynamic_migrations/postgres/server/database/schema/tables.rb
|
98
138
|
- lib/dynamic_migrations/postgres/server/database/source.rb
|
99
139
|
- lib/dynamic_migrations/postgres/server/database/structure_loader.rb
|
140
|
+
- lib/dynamic_migrations/postgres/server/database/triggers_and_functions_loader.rb
|
100
141
|
- lib/dynamic_migrations/postgres/server/database/validations_loader.rb
|
101
142
|
- lib/dynamic_migrations/version.rb
|
102
143
|
homepage:
|