Skip to content

Commit

Permalink
Delete some init
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-farries committed Dec 19, 2023
1 parent e3faa35 commit 17b02ae
Showing 1 changed file with 0 additions and 178 deletions.
178 changes: 0 additions & 178 deletions pkg/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,184 +55,6 @@ CREATE OR REPLACE FUNCTION %[1]s.is_active_migration_period(schemaname NAME) RET
AS $$ SELECT EXISTS (SELECT 1 FROM %[1]s.migrations WHERE schema=schemaname AND done=false) $$
LANGUAGE SQL
STABLE;
-- Get the latest version name (this is the one with child migrations)
CREATE OR REPLACE FUNCTION %[1]s.latest_version(schemaname NAME) RETURNS text
SECURITY DEFINER
SET search_path = %[1]s, pg_catalog, pg_temp
AS $$
SELECT p.name FROM %[1]s.migrations p
WHERE NOT EXISTS (
SELECT 1 FROM %[1]s.migrations c WHERE schema=schemaname AND c.parent=p.name
)
AND schema=schemaname $$
LANGUAGE SQL
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 $$
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 = 'inferred'
)
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;
-- Get the JSON representation of the current schema
CREATE OR REPLACE FUNCTION %[1]s.read_schema(schemaname text) RETURNS jsonb
LANGUAGE plpgsql AS $$
DECLARE
tables jsonb;
BEGIN
SELECT json_build_object(
'name', schemaname,
'tables', (
SELECT json_object_agg(t.relname, jsonb_build_object(
'name', t.relname,
'oid', t.oid,
'comment', descr.description,
'columns', (
SELECT json_object_agg(name, c) FROM (
SELECT
attr.attname AS name,
pg_get_expr(def.adbin, def.adrelid) AS default,
NOT (
attr.attnotnull
OR tp.typtype = 'd'
AND tp.typnotnull
) AS nullable,
CASE
WHEN 'character varying' :: regtype = ANY(ARRAY [attr.atttypid, tp.typelem]) THEN REPLACE(
format_type(attr.atttypid, attr.atttypmod),
'character varying',
'varchar'
)
WHEN 'timestamp with time zone' :: regtype = ANY(ARRAY [attr.atttypid, tp.typelem]) THEN REPLACE(
format_type(attr.atttypid, attr.atttypmod),
'timestamp with time zone',
'timestamptz'
)
ELSE format_type(attr.atttypid, attr.atttypmod)
END AS type,
descr.description AS comment
FROM
pg_attribute AS attr
INNER JOIN pg_type AS tp ON attr.atttypid = tp.oid
LEFT JOIN pg_attrdef AS def ON attr.attrelid = def.adrelid
AND attr.attnum = def.adnum
LEFT JOIN pg_description AS descr ON attr.attrelid = descr.objoid
AND attr.attnum = descr.objsubid
WHERE
attr.attnum > 0
AND NOT attr.attisdropped
AND attr.attrelid = t.oid
ORDER BY
attr.attnum
) c
),
'primaryKey', (
SELECT json_agg(pg_attribute.attname) AS primary_key_columns
FROM pg_index, pg_attribute
WHERE
indrelid = t.oid AND
nspname = schemaname AND
pg_attribute.attrelid = t.oid AND
pg_attribute.attnum = any(pg_index.indkey)
AND indisprimary
),
'indexes', (
SELECT json_object_agg(pi.indexrelid::regclass, json_build_object(
'name', pi.indexrelid::regclass
))
FROM pg_index pi
WHERE pi.indrelid = t.oid::regclass
)
)) FROM pg_class AS t
INNER JOIN pg_namespace AS ns ON t.relnamespace = ns.oid
LEFT JOIN pg_description AS descr ON t.oid = descr.objoid
AND descr.objsubid = 0
WHERE
ns.nspname = schemaname
AND t.relkind IN ('r', 'p') -- tables only (ignores views, materialized views & foreign tables)
)
)
INTO tables;
RETURN tables;
END;
$$;
CREATE OR REPLACE FUNCTION %[1]s.raw_migration() RETURNS event_trigger
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = %[1]s, pg_catalog, pg_temp AS $$
DECLARE
schemaname TEXT;
BEGIN
-- Ignore migrations done by pgroll
IF (pg_catalog.current_setting('pgroll.internal', 'TRUE') <> 'TRUE') THEN
RETURN;
END IF;
IF tg_event = 'sql_drop' THEN
-- Guess the schema from drop commands
SELECT schema_name INTO schemaname FROM pg_catalog.pg_event_trigger_dropped_objects() WHERE schema_name IS NOT NULL;
ELSIF tg_event = 'ddl_command_end' THEN
-- Guess the schema from ddl commands, ignore migrations that touch several schemas
IF (SELECT pg_catalog.count(DISTINCT schema_name) FROM pg_catalog.pg_event_trigger_ddl_commands() WHERE schema_name IS NOT NULL) > 1 THEN
RAISE NOTICE 'pgroll: ignoring migration that changes several schemas';
RETURN;
END IF;
SELECT schema_name INTO schemaname FROM pg_catalog.pg_event_trigger_ddl_commands() WHERE schema_name IS NOT NULL;
END IF;
IF schemaname IS NULL THEN
RAISE NOTICE 'pgroll: ignoring migration with null schema';
RETURN;
END IF;
-- Ignore migrations done during a migration period
IF %[1]s.is_active_migration_period(schemaname) THEN
RAISE NOTICE 'pgroll: ignoring migration during active migration period';
RETURN;
END IF;
-- Someone did a schema change without pgroll, include it in the history
INSERT INTO %[1]s.migrations (schema, name, migration, resulting_schema, done, parent, migration_type)
VALUES (
schemaname,
pg_catalog.format('sql_%%s',pg_catalog.substr(pg_catalog.md5(pg_catalog.random()::text), 0, 15)),
pg_catalog.json_build_object('sql', pg_catalog.json_build_object('up', pg_catalog.current_query())),
%[1]s.read_schema(schemaname),
true,
%[1]s.latest_version(schemaname),
'inferred'
);
END;
$$;
DROP EVENT TRIGGER IF EXISTS pg_roll_handle_ddl;
CREATE EVENT TRIGGER pg_roll_handle_ddl ON ddl_command_end
EXECUTE FUNCTION %[1]s.raw_migration();
DROP EVENT TRIGGER IF EXISTS pg_roll_handle_drop;
CREATE EVENT TRIGGER pg_roll_handle_drop ON sql_drop
EXECUTE FUNCTION %[1]s.raw_migration();
`

type State struct {
Expand Down

0 comments on commit 17b02ae

Please sign in to comment.