From 7cb6c631c7a64f94b7a0ac39dbad776313c819a0 Mon Sep 17 00:00:00 2001 From: Andrew Farries Date: Tue, 19 Dec 2023 12:49:25 +0000 Subject: [PATCH] Delete some init --- pkg/state/state.go | 202 --------------------------------------------- 1 file changed, 202 deletions(-) diff --git a/pkg/state/state.go b/pkg/state/state.go index bf5072947..95b0e22d8 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -31,208 +31,6 @@ CREATE TABLE IF NOT EXISTS %[1]s.migrations ( PRIMARY KEY (schema, name), FOREIGN KEY (schema, parent) REFERENCES %[1]s.migrations(schema, name) ); - --- Only one migration can be active at a time -CREATE UNIQUE INDEX IF NOT EXISTS only_one_active ON %[1]s.migrations (schema, name, done) WHERE done = false; - --- Only first migration can exist without parent -CREATE UNIQUE INDEX IF NOT EXISTS only_first_migration_without_parent ON %[1]s.migrations (schema) WHERE parent IS NULL; - --- History is linear -CREATE UNIQUE INDEX IF NOT EXISTS history_is_linear ON %[1]s.migrations (schema, parent); - --- Add a column to tell whether the row represents an auto-detected DDL capture or a pgroll migration -ALTER TABLE %[1]s.migrations ADD COLUMN IF NOT EXISTS migration_type - VARCHAR(32) - DEFAULT 'pgroll' - CONSTRAINT migration_type_check CHECK (migration_type IN ('pgroll', 'inferred') -); - --- Helper functions - --- Are we in the middle of a migration? -CREATE OR REPLACE FUNCTION %[1]s.is_active_migration_period(schemaname NAME) RETURNS boolean - 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 {