Skip to content

Commit

Permalink
Use sql/tables/*.sql for an update script [5/5]
Browse files Browse the repository at this point in the history
Use table creation DDL in generated upgrade script.

Only empty lines, comments and unnecessary type cast ("::regclass") 
are changed in the reference file:
updates/pg_partman--4.7.3--5.0.0-beta.sql
  • Loading branch information
vitaly-burovoy committed Jul 3, 2023
1 parent e3753e5 commit 340bebc
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 199 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ sql/$(EXTENSION)--$(EXTVERSION).sql: | $(wildcard sql/types/*.sql sql/tables/*.s

FUNCS_DIR := sql/functions
PROCS_DIR := sql/procedures
TABLS_DIR := sql/tables
INTRM_DIR := sql/_for_update

NEW_FUNCS := calculate_time_partition_info.sql
Expand Down Expand Up @@ -94,6 +95,9 @@ PROC_ALL_FILES := $(sort \
# TEMPORARY: hard to read; required to check existing migration is not touched; later will be simplified.
updates/$(EXTENSION)--$(PREV_VERSION)--$(EXTVERSION).sql: | \
$(INTRM_DIR)/01.tables.sql \
$(TABLS_DIR)/part_config.sql \
$(TABLS_DIR)/part_config_sub.sql \
$(INTRM_DIR)/04.tables-copy-back.sql \
$(INTRM_DIR)/08.hdr_new_functions.sql \
$(addprefix $(FUNCS_DIR)/,$(NEW_FUNCS)) \
$(INTRM_DIR)/08.hdr_altr_functions.sql \
Expand Down
192 changes: 0 additions & 192 deletions sql/_for_update/01.tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -204,195 +204,3 @@ BEGIN
RETURN v_result;
END
$$;

CREATE TABLE @extschema@.part_config (
parent_table text NOT NULL
, control text NOT NULL
, partition_interval text NOT NULL
, partition_type text NOT NULL
, premake int NOT NULL DEFAULT 4
, automatic_maintenance text NOT NULL DEFAULT 'on'
, template_table text
, retention text
, retention_schema text
, retention_keep_index boolean NOT NULL DEFAULT true
, retention_keep_table boolean NOT NULL DEFAULT true
, epoch text NOT NULL DEFAULT 'none'
, constraint_cols text[]
, optimize_constraint int NOT NULL DEFAULT 30
, infinite_time_partitions boolean NOT NULL DEFAULT false
, datetime_string text
, jobmon boolean NOT NULL DEFAULT true
, sub_partition_set_full boolean NOT NULL DEFAULT false
, undo_in_progress boolean NOT NULL DEFAULT false
, inherit_privileges boolean DEFAULT false
, constraint_valid boolean DEFAULT true NOT NULL
, subscription_refresh text
, ignore_default_data boolean NOT NULL DEFAULT true
, default_table boolean DEFAULT true
, date_trunc_interval text
, CONSTRAINT part_config_parent_table_pkey PRIMARY KEY (parent_table)
, CONSTRAINT positive_premake_check CHECK (premake > 0)
);

CREATE INDEX part_config_type_idx ON @extschema@.part_config (partition_type);
SELECT pg_catalog.pg_extension_config_dump('@extschema@.part_config'::regclass, '');

ALTER TABLE @extschema@.part_config ADD CONSTRAINT control_constraint_col_chk CHECK ((constraint_cols @> ARRAY[control]) <> true);
ALTER TABLE @extschema@.part_config ADD CONSTRAINT retention_schema_not_empty_chk CHECK (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
ADD CONSTRAINT part_config_epoch_check
CHECK (@extschema@.check_epoch_type(epoch));

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

CREATE TABLE @extschema@.part_config_sub (
sub_parent text
, sub_control text NOT NULL
, sub_partition_interval text NOT NULL
, sub_partition_type text NOT NULL
, sub_premake int NOT NULL DEFAULT 4
, sub_automatic_maintenance text NOT NULL DEFAULT 'on'
, sub_template_table text
, sub_retention text
, sub_retention_schema text
, sub_retention_keep_index boolean NOT NULL DEFAULT true
, sub_retention_keep_table boolean NOT NULL DEFAULT true
, sub_epoch text NOT NULL DEFAULT 'none'
, sub_constraint_cols text[]
, sub_optimize_constraint int NOT NULL DEFAULT 30
, sub_infinite_time_partitions boolean NOT NULL DEFAULT false
, sub_jobmon boolean NOT NULL DEFAULT true
, sub_inherit_privileges boolean DEFAULT false
, sub_constraint_valid boolean DEFAULT true NOT NULL
, sub_subscription_refresh text
, sub_ignore_default_data boolean NOT NULL DEFAULT true
, 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 positive_premake_check CHECK (sub_premake > 0)
);
SELECT pg_catalog.pg_extension_config_dump('@extschema@.part_config_sub'::regclass, '');

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_sub ADD CONSTRAINT retention_schema_not_empty_chk CHECK (sub_retention_schema <> '');

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_sub
ADD CONSTRAINT part_config_sub_epoch_check
CHECK (@extschema@.check_epoch_type(sub_epoch));

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

INSERT INTO @extschema@.part_config (
parent_table
, control
, partition_interval
, partition_type
, premake
, automatic_maintenance
, template_table
, retention
, retention_schema
, retention_keep_index
, retention_keep_table
, epoch
, constraint_cols
, optimize_constraint
, infinite_time_partitions
, datetime_string
, jobmon
, sub_partition_set_full
, undo_in_progress
, inherit_privileges
, constraint_valid
, subscription_refresh
, ignore_default_data
)
SELECT
parent_table
, control
, partition_interval
, CASE WHEN partition_type = 'native' THEN 'range' ELSE partition_type END
, premake
, automatic_maintenance
, template_table
, retention
, retention_schema
, retention_keep_index
, retention_keep_table
, epoch
, constraint_cols
, optimize_constraint
, infinite_time_partitions
, datetime_string
, jobmon
, sub_partition_set_full
, undo_in_progress
, inherit_privileges
, constraint_valid
, subscription_refresh
, ignore_default_data
FROM @extschema@.part_config_pre_500_data;


INSERT INTO @extschema@.part_config_sub (
sub_parent
, sub_control
, sub_partition_interval
, sub_partition_type
, sub_premake
, sub_automatic_maintenance
, sub_template_table
, sub_retention
, sub_retention_schema
, sub_retention_keep_index
, sub_retention_keep_table
, sub_epoch
, sub_constraint_cols
, sub_optimize_constraint
, sub_infinite_time_partitions
, sub_jobmon
, sub_inherit_privileges
, sub_constraint_valid
, sub_subscription_refresh
, sub_date_trunc_interval
, sub_ignore_default_data
)
SELECT
sub_parent
, sub_control
, sub_partition_interval
, CASE WHEN sub_partition_type = 'native' THEN 'range' ELSE sub_partition_type END
, sub_premake
, sub_automatic_maintenance
, sub_template_table
, sub_retention
, sub_retention_schema
, sub_retention_keep_index
, sub_retention_keep_table
, sub_epoch
, sub_constraint_cols
, sub_optimize_constraint
, sub_infinite_time_partitions
, sub_jobmon
, sub_inherit_privileges
, sub_constraint_valid
, sub_subscription_refresh
, sub_date_trunc_interval
, sub_ignore_default_data
FROM @extschema@.part_config_sub_pre_500_data;
98 changes: 98 additions & 0 deletions sql/_for_update/04.tables-copy-back.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
INSERT INTO @extschema@.part_config (
parent_table
, control
, partition_interval
, partition_type
, premake
, automatic_maintenance
, template_table
, retention
, retention_schema
, retention_keep_index
, retention_keep_table
, epoch
, constraint_cols
, optimize_constraint
, infinite_time_partitions
, datetime_string
, jobmon
, sub_partition_set_full
, undo_in_progress
, inherit_privileges
, constraint_valid
, subscription_refresh
, ignore_default_data
)
SELECT
parent_table
, control
, partition_interval
, CASE WHEN partition_type = 'native' THEN 'range' ELSE partition_type END
, premake
, automatic_maintenance
, template_table
, retention
, retention_schema
, retention_keep_index
, retention_keep_table
, epoch
, constraint_cols
, optimize_constraint
, infinite_time_partitions
, datetime_string
, jobmon
, sub_partition_set_full
, undo_in_progress
, inherit_privileges
, constraint_valid
, subscription_refresh
, ignore_default_data
FROM @extschema@.part_config_pre_500_data;


INSERT INTO @extschema@.part_config_sub (
sub_parent
, sub_control
, sub_partition_interval
, sub_partition_type
, sub_premake
, sub_automatic_maintenance
, sub_template_table
, sub_retention
, sub_retention_schema
, sub_retention_keep_index
, sub_retention_keep_table
, sub_epoch
, sub_constraint_cols
, sub_optimize_constraint
, sub_infinite_time_partitions
, sub_jobmon
, sub_inherit_privileges
, sub_constraint_valid
, sub_subscription_refresh
, sub_date_trunc_interval
, sub_ignore_default_data
)
SELECT
sub_parent
, sub_control
, sub_partition_interval
, CASE WHEN sub_partition_type = 'native' THEN 'range' ELSE sub_partition_type END
, sub_premake
, sub_automatic_maintenance
, sub_template_table
, sub_retention
, sub_retention_schema
, sub_retention_keep_index
, sub_retention_keep_table
, sub_epoch
, sub_constraint_cols
, sub_optimize_constraint
, sub_infinite_time_partitions
, sub_jobmon
, sub_inherit_privileges
, sub_constraint_valid
, sub_subscription_refresh
, sub_date_trunc_interval
, sub_ignore_default_data
FROM @extschema@.part_config_sub_pre_500_data;
12 changes: 5 additions & 7 deletions updates/pg_partman--4.7.3--5.0.0-beta.sql
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ BEGIN
RETURN v_result;
END
$$;

CREATE TABLE @extschema@.part_config (
parent_table text NOT NULL
, control text NOT NULL
Expand Down Expand Up @@ -234,10 +233,10 @@ CREATE TABLE @extschema@.part_config (
, CONSTRAINT part_config_parent_table_pkey PRIMARY KEY (parent_table)
, CONSTRAINT positive_premake_check CHECK (premake > 0)
);

CREATE INDEX part_config_type_idx ON @extschema@.part_config (partition_type);
SELECT pg_catalog.pg_extension_config_dump('@extschema@.part_config'::regclass, '');
SELECT pg_catalog.pg_extension_config_dump('@extschema@.part_config', '');

-- Ensure the control column cannot be one of the additional constraint columns.
ALTER TABLE @extschema@.part_config ADD CONSTRAINT control_constraint_col_chk CHECK ((constraint_cols @> ARRAY[control]) <> true);
ALTER TABLE @extschema@.part_config ADD CONSTRAINT retention_schema_not_empty_chk CHECK (retention_schema <> '');

Expand All @@ -252,7 +251,7 @@ CHECK (@extschema@.check_epoch_type(epoch));
ALTER TABLE @extschema@.part_config
ADD CONSTRAINT part_config_type_check
CHECK (@extschema@.check_partition_type(partition_type));

-- FK set deferrable because create_parent() & create_sub_parent() inserts to this table before part_config
CREATE TABLE @extschema@.part_config_sub (
sub_parent text
, sub_control text NOT NULL
Expand Down Expand Up @@ -280,10 +279,10 @@ CREATE TABLE @extschema@.part_config_sub (
, 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 positive_premake_check CHECK (sub_premake > 0)
);
SELECT pg_catalog.pg_extension_config_dump('@extschema@.part_config_sub'::regclass, '');
SELECT pg_catalog.pg_extension_config_dump('@extschema@.part_config_sub', '');

-- Ensure the control column cannot be one of the additional constraint columns.
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_sub ADD CONSTRAINT retention_schema_not_empty_chk CHECK (sub_retention_schema <> '');

ALTER TABLE @extschema@.part_config_sub
Expand All @@ -297,7 +296,6 @@ CHECK (@extschema@.check_epoch_type(sub_epoch));
ALTER TABLE @extschema@.part_config_sub
ADD CONSTRAINT part_config_sub_type_check
CHECK (@extschema@.check_partition_type(sub_partition_type));

INSERT INTO @extschema@.part_config (
parent_table
, control
Expand Down

0 comments on commit 340bebc

Please sign in to comment.