Skip to content

Commit

Permalink
Replace implementation of previous_version func
Browse files Browse the repository at this point in the history
Use a recursive CTE to find the first non-'manual' parent of the current
version.
  • Loading branch information
andrew-farries committed Nov 2, 2023
1 parent 21eafcf commit fdc8972
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion pkg/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,20 @@ STABLE;
-- Get the name of the previous version of the schema, or NULL if there is none.
CREATE OR REPLACE FUNCTION %[1]s.previous_version(schemaname NAME) RETURNS text
AS $$
SELECT parent FROM %[1]s.migrations WHERE name = (SELECT %[1]s.latest_version(schemaname)) AND schema=schemaname;
WITH RECURSIVE find_ancestor AS (
SELECT schema, name, parent, migration_type FROM pgroll.migrations
WHERE name = (SELECT %[1]s.latest_version(schemaname)) AND schema = schemaname
UNION ALL
SELECT m.schema, m.name, m.parent, m.migration_type FROM pgroll.migrations m
INNER JOIN find_ancestor fa ON fa.parent = m.name AND fa.schema = m.schema
WHERE m.migration_type = 'manual'
)
SELECT a.parent
FROM find_ancestor AS a
JOIN pgroll.migrations AS b ON a.parent = b.name AND a.schema = b.schema
WHERE b.migration_type = 'pgroll';
$$
LANGUAGE SQL
STABLE;
Expand Down

0 comments on commit fdc8972

Please sign in to comment.