CREATE OR REPLACE FUNCTION add_version_column_to_all_tables()
RETURNS VOID
AS $$
DECLARE
my_row RECORD;
BEGIN
FOR my_row IN
SELECT *
FROM information_schema.tables
WHERE table_schema = 'public' AND table_type='BASE TABLE'
LOOP
IF NOT EXISTS
(
SELECT attname FROM pg_attribute WHERE attrelid =
(SELECT oid FROM pg_class WHERE relname = my_row.table_name )
AND attname = 'subtenant'
)
THEN
IF EXISTS
(
SELECT attname FROM pg_attribute WHERE attrelid =
(SELECT oid FROM pg_class WHERE relname = my_row.table_name )
AND attname = 'tenantid'
)
THEN
EXECUTE('ALTER TABLE ' || my_row.table_name || ' ADD COLUMN subtenant text;');
EXECUTE('UPDATE ' || my_row.table_name || ' SET subtenant = tenantid;');
END IF;
END IF;
END LOOP;
END
$$
LANGUAGE plpgsql;
SELECT add_version_column_to_all_tables();
No comments:
Post a Comment