Skip to content

Commit

Permalink
simplify 5.0.1 update
Browse files Browse the repository at this point in the history
  • Loading branch information
keithf4 committed Dec 26, 2023
1 parent ee8653f commit 6faaaca
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 179 deletions.
31 changes: 30 additions & 1 deletion updates/pg_partman--4.7.4--5.0.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ CREATE TABLE @extschema@.part_config_sub (
, sub_default_table boolean default true
, sub_date_trunc_interval TEXT
, CONSTRAINT part_config_sub_pkey PRIMARY KEY (sub_parent)
, CONSTRAINT part_config_sub_sub_parent_fkey FOREIGN KEY (sub_parent) REFERENCES @extschema@.part_config (parent_table) ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED
, CONSTRAINT part_config_sub_sub_parent_fkey FOREIGN KEY (sub_parent) REFERENCES @extschema@.part_config (parent_table) ON DELETE CASCADE ON UPDATE CASCADE
, CONSTRAINT positive_premake_check CHECK (sub_premake > 0)
);
SELECT pg_catalog.pg_extension_config_dump('@extschema@.part_config_sub'::regclass, '');
Expand Down Expand Up @@ -328,6 +328,27 @@ SELECT
, sub_ignore_default_data
FROM @extschema@.part_config_sub_pre_500_data;

ALTER TABLE @extschema@.part_config ADD CONSTRAINT control_constraint_col_chk CHECK ((constraint_cols @> ARRAY[control]) <> true);
ALTER TABLE @extschema@.part_config_sub ADD CONSTRAINT control_constraint_col_chk CHECK ((sub_constraint_cols @> ARRAY[sub_control]) <> true);

ALTER TABLE @extschema@.part_config ADD CONSTRAINT retention_schema_not_empty_chk CHECK (retention_schema <> '');
ALTER TABLE @extschema@.part_config_sub ADD CONSTRAINT retention_schema_not_empty_chk CHECK (sub_retention_schema <> '');

ALTER TABLE @extschema@.part_config
ADD CONSTRAINT part_config_automatic_maintenance_check
CHECK (@extschema@.check_automatic_maintenance_value(automatic_maintenance));

ALTER TABLE @extschema@.part_config_sub
ADD CONSTRAINT part_config_sub_automatic_maintenance_check
CHECK (@extschema@.check_automatic_maintenance_value(sub_automatic_maintenance));

ALTER TABLE @extschema@.part_config
ADD CONSTRAINT part_config_epoch_check
CHECK (@extschema@.check_epoch_type(epoch));

ALTER TABLE @extschema@.part_config_sub
ADD CONSTRAINT part_config_sub_epoch_check
CHECK (@extschema@.check_epoch_type(sub_epoch));

UPDATE @extschema@.part_config SET partition_type = 'range' WHERE partition_type = 'native';
UPDATE @extschema@.part_config_sub SET sub_partition_type = 'range' WHERE sub_partition_type = 'native';
Expand All @@ -345,6 +366,14 @@ BEGIN
END
$$;

ALTER TABLE @extschema@.part_config
ADD CONSTRAINT part_config_type_check
CHECK (@extschema@.check_partition_type(partition_type));

ALTER TABLE @extschema@.part_config_sub
ADD CONSTRAINT part_config_sub_type_check
CHECK (@extschema@.check_partition_type(sub_partition_type));

-- #### Brand new functions ####

CREATE FUNCTION @extschema@.calculate_time_partition_info(
Expand Down
179 changes: 1 addition & 178 deletions updates/pg_partman--5.0.0--5.0.1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,182 +12,5 @@

-- Update 5.0.1 MUST be installed for version 5.x of pg_partman to work properly. As long as these updates are run within a few seconds of each other, there should be no issues.

-- Only recreate constraints if they don't already exist from a previous 5.0.0 update before 5.0.1 was available
DO $$
DECLARE
v_exists text;
BEGIN

SELECT conname INTO v_exists
FROM pg_catalog.pg_constraint t
JOIN pg_catalog.pg_class c ON t.conrelid = c.oid
JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
WHERE t.conname = 'control_constraint_col_chk'
AND c.relname = 'part_config'
AND n.nspname = '@extschema@';

IF v_exists IS NULL THEN
EXECUTE format('
ALTER TABLE @extschema@.part_config
ADD CONSTRAINT control_constraint_col_chk
CHECK ((constraint_cols @> ARRAY[control]) <> true)
');
END IF;
v_exists := NULL;

SELECT conname INTO v_exists
FROM pg_catalog.pg_constraint t
JOIN pg_catalog.pg_class c ON t.conrelid = c.oid
JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
WHERE t.conname = 'control_constraint_col_chk'
AND c.relname = 'part_config_sub'
AND n.nspname = '@extschema@';

IF v_exists IS NULL THEN
EXECUTE format('
ALTER TABLE @extschema@.part_config_sub
ADD CONSTRAINT control_constraint_col_chk
CHECK ((sub_constraint_cols @> ARRAY[sub_control]) <> true)
');
END IF;
v_exists := NULL;

SELECT conname INTO v_exists
FROM pg_catalog.pg_constraint t
JOIN pg_catalog.pg_class c ON t.conrelid = c.oid
JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
WHERE t.conname = 'retention_schema_not_empty_chk'
AND c.relname = 'part_config'
AND n.nspname = '@extschema@';

IF v_exists IS NULL THEN
EXECUTE format('
ALTER TABLE @extschema@.part_config
ADD CONSTRAINT retention_schema_not_empty_chk
CHECK (retention_schema <> %L)
', '');
END IF;
v_exists := NULL;

SELECT conname INTO v_exists
FROM pg_catalog.pg_constraint t
JOIN pg_catalog.pg_class c ON t.conrelid = c.oid
JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
WHERE t.conname = 'retention_schema_not_empty_chk'
AND c.relname = 'part_config_sub'
AND n.nspname = '@extschema@';

IF v_exists IS NULL THEN
EXECUTE format('
ALTER TABLE @extschema@.part_config_sub
ADD CONSTRAINT retention_schema_not_empty_chk
CHECK (sub_retention_schema <> %L)'
, '');
END IF;
v_exists := NULL;

SELECT conname INTO v_exists
FROM pg_catalog.pg_constraint t
JOIN pg_catalog.pg_class c ON t.conrelid = c.oid
JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
WHERE t.conname = 'part_config_automatic_maintenance_check'
AND c.relname = 'part_config'
AND n.nspname = '@extschema@';

IF v_exists IS NULL THEN
EXECUTE format('
ALTER TABLE @extschema@.part_config
ADD CONSTRAINT part_config_automatic_maintenance_check
CHECK (@extschema@.check_automatic_maintenance_value(automatic_maintenance));
');
END IF;
v_exists := NULL;

SELECT conname INTO v_exists
FROM pg_catalog.pg_constraint t
JOIN pg_catalog.pg_class c ON t.conrelid = c.oid
JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
WHERE t.conname = 'part_config_sub_automatic_maintenance_check'
AND c.relname = 'part_config_sub'
AND n.nspname = '@extschema@';

IF v_exists IS NULL THEN
EXECUTE format('
ALTER TABLE @extschema@.part_config_sub
ADD CONSTRAINT part_config_sub_automatic_maintenance_check
CHECK (@extschema@.check_automatic_maintenance_value(sub_automatic_maintenance));
');
END IF;
v_exists := NULL;

SELECT conname INTO v_exists
FROM pg_catalog.pg_constraint t
JOIN pg_catalog.pg_class c ON t.conrelid = c.oid
JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
WHERE t.conname = 'part_config_epoch_check'
AND c.relname = 'part_config'
AND n.nspname = '@extschema@';

IF v_exists IS NULL THEN
EXECUTE format('
ALTER TABLE @extschema@.part_config
ADD CONSTRAINT part_config_epoch_check
CHECK (@extschema@.check_epoch_type(epoch));
');
END IF;
v_exists := NULL;

SELECT conname INTO v_exists
FROM pg_catalog.pg_constraint t
JOIN pg_catalog.pg_class c ON t.conrelid = c.oid
JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
WHERE t.conname = 'part_config_sub_epoch_check'
AND c.relname = 'part_config_sub'
AND n.nspname = '@extschema@';

IF v_exists IS NULL THEN
EXECUTE format('
ALTER TABLE @extschema@.part_config_sub
ADD CONSTRAINT part_config_sub_epoch_check
CHECK (@extschema@.check_epoch_type(sub_epoch));
');
END IF;
v_exists := NULL;

SELECT conname INTO v_exists
FROM pg_catalog.pg_constraint t
JOIN pg_catalog.pg_class c ON t.conrelid = c.oid
JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
WHERE t.conname = 'part_config_type_check'
AND c.relname = 'part_config'
AND n.nspname = '@extschema@';

IF v_exists IS NULL THEN
EXECUTE format('
ALTER TABLE @extschema@.part_config
ADD CONSTRAINT part_config_type_check
CHECK (@extschema@.check_partition_type(partition_type));
');
END IF;
v_exists := NULL;

SELECT conname INTO v_exists
FROM pg_catalog.pg_constraint t
JOIN pg_catalog.pg_class c ON t.conrelid = c.oid
JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
WHERE t.conname = 'part_config_sub_type_check'
AND c.relname = 'part_config_sub'
AND n.nspname = '@extschema@';

IF v_exists IS NULL THEN
EXECUTE format('
ALTER TABLE @extschema@.part_config_sub
ADD CONSTRAINT part_config_sub_type_check
CHECK (@extschema@.check_partition_type(sub_partition_type));
');
END IF;
v_exists := NULL;


END
$$;
ALTER TABLE @extschema@.part_config_sub ALTER CONSTRAINT part_config_sub_sub_parent_fkey DEFERRABLE INITIALLY DEFERRED;

0 comments on commit 6faaaca

Please sign in to comment.