68
68
LANGUAGE SQL
69
69
STABLE;
70
70
71
- -- Get the latest version schema name
72
- CREATE OR REPLACE FUNCTION placeholder .latest_version (schemaname name)
73
- RETURNS text
74
- SECURITY DEFINER
75
- SET search_path = placeholder, pg_catalog, pg_temp
76
- AS $$
77
- SELECT
78
- COALESCE(p .migration - >> ' version_schema' , p .name )
79
- FROM
80
- placeholder .migrations p
81
- WHERE
82
- NOT EXISTS (
83
- SELECT
84
- 1
85
- FROM
86
- placeholder .migrations c
87
- WHERE
88
- SCHEMA = schemaname
89
- AND c .parent = p .name )
90
- AND SCHEMA = schemaname
91
- $$
92
- LANGUAGE SQL
93
- STABLE;
94
-
95
- -- Get the name of the latest version schema, or NULL if there is none.
71
+ -- Get the name of the latest migration, or NULL if there is none.
96
72
-- This will be the same as the version-schema name of the migration in most
97
73
-- cases, unless the migration sets its `versionSchema` field.
98
74
CREATE OR REPLACE FUNCTION placeholder .latest_migration (schemaname name)
118
94
LANGUAGE SQL
119
95
STABLE;
120
96
121
- -- Get the previous version schema name, or NULL if there is none.
122
- -- This ignores previous versions for which no version schema exists, such as
123
- -- versions corresponding to inferred migrations.
124
- CREATE OR REPLACE FUNCTION placeholder .previous_version (schemaname name, includeInferred boolean )
97
+ -- Get the name of the previous migration, or NULL if there is none.
98
+ CREATE OR REPLACE FUNCTION placeholder .previous_migration (schemaname name)
125
99
RETURNS text
126
100
AS $$
127
101
WITH RECURSIVE ancestors AS (
128
102
SELECT
129
103
name,
130
- migration - >> ' version_schema' AS versionSchema,
131
104
schema,
132
105
parent,
133
106
migration_type,
@@ -140,7 +113,6 @@ CREATE OR REPLACE FUNCTION placeholder.previous_version (schemaname name, includ
140
113
UNION ALL
141
114
SELECT
142
115
m .name ,
143
- m .migration - >> ' version_schema' AS versionSchema,
144
116
m .schema ,
145
117
m .parent ,
146
118
m .migration_type ,
@@ -151,68 +123,92 @@ CREATE OR REPLACE FUNCTION placeholder.previous_version (schemaname name, includ
151
123
AND m .schema = a .schema
152
124
)
153
125
SELECT
154
- COALESCE( a . versionSchema , a . name )
126
+ a . name
155
127
FROM
156
128
ancestors a
157
129
WHERE
158
130
a .depth > 0
159
- AND (includeInferred
160
- OR (a .migration_type = ' pgroll'
161
- AND EXISTS (
162
- SELECT
163
- s .schema_name
164
- FROM
165
- information_schema .schemata s
166
- WHERE
167
- s .schema_name = schemaname || ' _' || COALESCE(a .versionSchema , a .name ))))
168
131
ORDER BY
169
132
a .depth ASC
170
133
LIMIT 1 ;
171
134
$$
172
135
LANGUAGE SQL
173
136
STABLE;
174
137
175
- -- Get the name of the previous migration, or NULL if there is none.
176
- CREATE OR REPLACE FUNCTION placeholder .previous_migration (schemaname name)
138
+ -- find_version_schema finds a recent version schema for a given schema name.
139
+ -- How recent is determined by the minDepth parameter: for a minDepth of 0, it
140
+ -- returns the latest version schema, for a minDepth of 1, it returns the
141
+ -- previous version schema, and so on.
142
+ -- Only version schemas that exist in the database are considered; migrations
143
+ -- without version schema (such as inferred migrations) are ignored.
144
+ CREATE OR REPLACE FUNCTION find_version_schema (p_schema_name name, p_depth integer DEFAULT 0 )
177
145
RETURNS text
178
146
AS $$
179
147
WITH RECURSIVE ancestors AS (
180
148
SELECT
181
149
name,
150
+ COALESCE(migration - >> ' version_schema' , name) AS version_schema,
182
151
schema,
183
152
parent,
184
- migration_type,
185
153
0 AS depth
186
154
FROM
187
155
placeholder .migrations
188
156
WHERE
189
- name = placeholder .latest_migration (schemaname )
190
- AND SCHEMA = schemaname
157
+ name = placeholder .latest_migration (p_schema_name )
158
+ AND SCHEMA = p_schema_name
191
159
UNION ALL
192
160
SELECT
193
161
m .name ,
162
+ COALESCE(m .migration - >> ' version_schema' , m .name ) AS version_schema,
194
163
m .schema ,
195
164
m .parent ,
196
- m .migration_type ,
197
165
a .depth + 1
198
166
FROM
199
167
placeholder .migrations m
200
168
JOIN ancestors a ON m .name = a .parent
201
169
AND m .schema = a .schema
202
170
)
203
171
SELECT
204
- a .name
172
+ a .version_schema
205
173
FROM
206
174
ancestors a
207
175
WHERE
208
- a .depth > 0
176
+ EXISTS (
177
+ SELECT
178
+ 1
179
+ FROM
180
+ information_schema .schemata s
181
+ WHERE
182
+ s .schema_name = p_schema_name || ' _' || a .version_schema )
209
183
ORDER BY
210
- a .depth ASC
184
+ a .depth ASC OFFSET p_depth
211
185
LIMIT 1 ;
212
186
$$
213
187
LANGUAGE SQL
214
188
STABLE;
215
189
190
+ -- previous_version returns the name of the previous version schema for a given
191
+ -- schema name or NULL if there is no previous version schema.
192
+ CREATE OR REPLACE FUNCTION previous_version (schemaname name)
193
+ RETURNS text
194
+ AS $$
195
+ SELECT
196
+ placeholder .find_version_schema (schemaname, 1 );
197
+ $$
198
+ LANGUAGE SQL
199
+ STABLE;
200
+
201
+ -- latest_version returns the name of the latest version schema for a given
202
+ -- schema name or NULL if there are no version schema.
203
+ CREATE OR REPLACE FUNCTION latest_version (schemaname name)
204
+ RETURNS text
205
+ AS $$
206
+ SELECT
207
+ placeholder .find_version_schema (schemaname, 0 );
208
+ $$
209
+ LANGUAGE SQL
210
+ STABLE;
211
+
216
212
-- Get the JSON representation of the current schema
217
213
CREATE OR REPLACE FUNCTION placeholder .read_schema (schemaname text )
218
214
RETURNS jsonb
0 commit comments