Compare commits

...
1 Commits
Author SHA1 Message Date
Clemens Schwaighofer 0e5f637052 Update Serial to Identity function
Return status as varchar from change.

clean up edit table SQL files with too many empty lines
2024-12-05 12:11:07 +09:00
3 changed files with 36 additions and 37 deletions

No files matched your search

@@ -6,7 +6,8 @@
-- @param name col The column to be changed -- @param name col The column to be changed
-- @param varchar identity_type [default=a] Allowed a, d, assigned, default -- @param varchar identity_type [default=a] Allowed a, d, assigned, default
-- @param varchar col_type [default=''] Allowed smallint, int, bigint, int2, int4, int8 -- @param varchar col_type [default=''] Allowed smallint, int, bigint, int2, int4, int8
-- @raises EXCEPTON on column not found, no linked sequence, more than one linked sequence found -- @returns varchar status tring
-- @raises EXCEPTON on column not found, no linked sequence, more than one linked sequence found, invalid col type
-- --
CREATE OR REPLACE FUNCTION upgrade_serial_to_identity( CREATE OR REPLACE FUNCTION upgrade_serial_to_identity(
tbl regclass, tbl regclass,
@@ -14,17 +15,18 @@ CREATE OR REPLACE FUNCTION upgrade_serial_to_identity(
identity_type varchar = 'a', identity_type varchar = 'a',
col_type varchar = '' col_type varchar = ''
) )
RETURNS void RETURNS varchar
LANGUAGE plpgsql LANGUAGE plpgsql
AS $$ AS $$
DECLARE DECLARE
colnum smallint; colnum SMALLINT;
seqid oid; seqid OID;
count int; count INT;
col_type_oid int; col_type_oid INT;
col_type_len int; col_type_len INT;
current_col_atttypid oid; current_col_atttypid OID;
current_col_attlen int; current_col_attlen INT;
status_string VARCHAR;
BEGIN BEGIN
-- switch between always (default) or default identiy type -- switch between always (default) or default identiy type
IF identity_type NOT IN ('a', 'd', 'assigned', 'default') THEN IF identity_type NOT IN ('a', 'd', 'assigned', 'default') THEN
@@ -59,6 +61,10 @@ BEGIN
RAISE EXCEPTION 'more than one linked sequence found'; RAISE EXCEPTION 'more than one linked sequence found';
END IF; END IF;
IF col_type <> '' AND col_type NOT IN ('smallint', 'int', 'bigint', 'int2', 'int4', 'int8') THEN
RAISE EXCEPTION 'Invalid col type: %', col_type;
END IF;
-- drop the default -- drop the default
EXECUTE 'ALTER TABLE ' || tbl || ' ALTER COLUMN ' || quote_ident(col) || ' DROP DEFAULT'; EXECUTE 'ALTER TABLE ' || tbl || ' ALTER COLUMN ' || quote_ident(col) || ' DROP DEFAULT';
@@ -74,34 +80,31 @@ BEGIN
SET attidentity = identity_type SET attidentity = identity_type
WHERE attrelid = tbl WHERE attrelid = tbl
AND attname = col; AND attname = col;
RAISE NOTICE 'Update to identity for table "%" and columen "%" with type "%"', tbl, col, identity_type; status_string := 'Updated to identity for table "' || tbl || '" and columen "' || col || '" with type "' || identity_type || '"';
-- set type if requested and not empty -- set type if requested and not empty
IF col_type <> '' THEN IF col_type <> '' THEN
IF col_type IN ('smallint', 'int', 'bigint', 'int2', 'int4', 'int8') THEN -- rewrite smallint, int, bigint
-- rewrite smallint, int, bigint IF col_type = 'smallint' THEN
IF col_type = 'smallint' THEN col_type := 'int2';
col_type := 'int2'; ELSIF col_type = 'int' THEN
ELSIF col_type = 'int' THEN col_type := 'int4';
col_type := 'int4'; ELSIF col_type = 'bigint' THEN
ELSIF col_type = 'bigint' THEN col_type := 'int8';
col_type := 'int8'; END IF;
END IF; -- get the length and oid for selected
-- get the length and oid for selected SELECT oid, typlen INTO col_type_oid, col_type_len FROM pg_type WHERE typname = col_type;
SELECT oid, typlen INTO col_type_oid, col_type_len FROM pg_type WHERE typname = col_type; -- set only if diff or hight
-- set only if diff or hight IF current_col_atttypid <> col_type_oid AND col_type_len > current_col_attlen THEN
IF current_col_atttypid <> col_type_oid AND col_type_len > current_col_attlen THEN status_string := status_string || '. Change col type: ' || col_type;
RAISE NOTICE 'Change col type: %', col_type; -- update type
-- update type UPDATE pg_attribute
UPDATE pg_attribute SET
SET atttypid = col_type_oid, attlen = col_type_len
atttypid = col_type_oid, attlen = col_type_len WHERE attrelid = tbl
WHERE attrelid = tbl AND attname = col;
AND attname = col;
END IF;
ELSE
RAISE NOTICE 'Invalid col type: %', col_type;
END IF; END IF;
END IF; END IF;
RETURN status_string;
END; END;
$$; $$;
-2
View File
@@ -12,5 +12,3 @@ CREATE TABLE edit_menu_group (
flag VARCHAR, flag VARCHAR,
order_number INT NOT NULL order_number INT NOT NULL
) INHERITS (edit_generic) WITHOUT OIDS; ) INHERITS (edit_generic) WITHOUT OIDS;
-2
View File
@@ -16,5 +16,3 @@ CREATE TABLE edit_page_access (
FOREIGN KEY (edit_access_right_id) REFERENCES edit_access_right (edit_access_right_id) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY (edit_access_right_id) REFERENCES edit_access_right (edit_access_right_id) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE,
enabled SMALLINT NOT NULL DEFAULT 0 enabled SMALLINT NOT NULL DEFAULT 0
) INHERITS (edit_generic) WITHOUT OIDS; ) INHERITS (edit_generic) WITHOUT OIDS;