Skip to content

Commit c1b0f90

Browse files
Simplify previous_migration function (#922)
Get rid of the recursive CTE in the `previous_migration` function - we only need to find the parent of the latest migration, which can be done with a simple query. The need for recursion was removed because this function used to find the previous migration with an existent version schema, but that functionality is now in the `previous_version` function, allowing `previous_migration` to be simplified.
1 parent 31dd8d1 commit c1b0f90

File tree

1 file changed

+7
-34
lines changed

1 file changed

+7
-34
lines changed

pkg/state/init.sql

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -98,42 +98,15 @@ STABLE;
9898
CREATE OR REPLACE FUNCTION placeholder.previous_migration (schemaname name)
9999
RETURNS text
100100
AS $$
101-
WITH RECURSIVE ancestors AS (
102-
SELECT
103-
name,
104-
schema,
105-
parent,
106-
migration_type,
107-
0 AS depth
108-
FROM
109-
placeholder.migrations
110-
WHERE
111-
name = placeholder.latest_migration (schemaname)
112-
AND SCHEMA = schemaname
113-
UNION ALL
114-
SELECT
115-
m.name,
116-
m.schema,
117-
m.parent,
118-
m.migration_type,
119-
a.depth + 1
120-
FROM
121-
placeholder.migrations m
122-
JOIN ancestors a ON m.name = a.parent
123-
AND m.schema = a.schema
124-
)
125-
SELECT
126-
a.name
127-
FROM
128-
ancestors a
101+
SELECT
102+
parent
103+
FROM
104+
placeholder.migrations
129105
WHERE
130-
a.depth > 0
131-
ORDER BY
132-
a.depth ASC
133-
LIMIT 1;
106+
SCHEMA = schemaname
107+
AND name = placeholder.latest_migration (schemaname);
134108
$$
135-
LANGUAGE SQL
136-
STABLE;
109+
LANGUAGE SQL;
137110

138111
-- find_version_schema finds a recent version schema for a given schema name.
139112
-- How recent is determined by the minDepth parameter: for a minDepth of 0, it

0 commit comments

Comments
 (0)