modify enums before creating tables

This commit is contained in:
Robert Lechte 2020-07-28 21:31:41 +10:00
parent c97eeb1b2d
commit 2adbe8e0aa
5 changed files with 45 additions and 23 deletions

View File

@ -146,7 +146,9 @@ def statements_from_differences(
return statements
def get_enum_modifications(tables_from, tables_target, enums_from, enums_target):
def get_enum_modifications(
tables_from, tables_target, enums_from, enums_target, return_tuple=False
):
_, _, e_modified, _ = differences(enums_from, enums_target)
_, _, t_modified, _ = differences(tables_from, tables_target)
pre = Statements()
@ -190,7 +192,11 @@ def get_enum_modifications(tables_from, tables_target, enums_from, enums_target)
drop_statement = e.drop_statement_with_rename(unwanted_name)
post.append(drop_statement)
return pre + recreate + post
if return_tuple:
return pre, recreate + post
else:
return pre + recreate + post
def get_table_changes(
@ -206,14 +212,20 @@ def get_table_changes(
statements = Statements()
for t, v in removed.items():
statements.append(v.drop_statement)
enums_pre, enums_post = get_enum_modifications(
tables_from, tables_target, enums_from, enums_target, return_tuple=True
)
statements += enums_pre
for t, v in added.items():
statements.append(v.create_statement)
if v.rowsecurity:
rls_alter = v.alter_rls_statement
statements.append(rls_alter)
statements += get_enum_modifications(
tables_from, tables_target, enums_from, enums_target
)
statements += enums_post
for t, v in modified.items():
before = tables_from[t]

View File

@ -4,4 +4,6 @@ create table t(id integer primary key, category e);
create view v as select * from t;
create view v2 as select *, 'b'::e from t;
create view v2 as select *, 'b'::e from t;
create table created_with_e(id integer, category e);

View File

@ -6,6 +6,12 @@ alter type "public"."e" rename to "e__old_version_to_be_dropped";
create type "public"."e" as enum ('a', 'b', 'c', 'd');
create table "public"."created_with_e" (
"id" integer,
"category" e
);
alter table "public"."t" alter column category type "public"."e" using category::text::"public"."e";
drop type "public"."e__old_version_to_be_dropped";
@ -18,4 +24,6 @@ create or replace view "public"."v" as SELECT t.id,
create or replace view "public"."v2" as SELECT t.id,
t.category,
'b'::e AS e
FROM t;
FROM t;

View File

@ -44,6 +44,14 @@ drop table "public"."aunwanted";
drop table "public"."columnless_table";
alter type "public"."shipping_status" rename to "shipping_status__old_version_to_be_dropped";
create type "public"."shipping_status" as enum ('not shipped', 'shipped', 'delivered');
alter type "public"."unused_enum" rename to "unused_enum__old_version_to_be_dropped";
create type "public"."unused_enum" as enum ('a', 'b', 'c');
create table "public"."bug" (
"id" integer not null default nextval('bug_id_seq'::regclass),
"description" text,
@ -62,14 +70,6 @@ create table "public"."order_items" (
);
alter type "public"."shipping_status" rename to "shipping_status__old_version_to_be_dropped";
create type "public"."shipping_status" as enum ('not shipped', 'shipped', 'delivered');
alter type "public"."unused_enum" rename to "unused_enum__old_version_to_be_dropped";
create type "public"."unused_enum" as enum ('a', 'b', 'c');
alter table "public"."orders" alter column status type "public"."shipping_status" using status::text::"public"."shipping_status";
drop type "public"."shipping_status__old_version_to_be_dropped";

View File

@ -44,6 +44,14 @@ drop table "public"."aunwanted";
drop table "public"."columnless_table";
alter type "public"."shipping_status" rename to "shipping_status__old_version_to_be_dropped";
create type "public"."shipping_status" as enum ('not shipped', 'shipped', 'delivered');
alter type "public"."unused_enum" rename to "unused_enum__old_version_to_be_dropped";
create type "public"."unused_enum" as enum ('a', 'b', 'c');
create table "public"."bug" (
"id" integer not null default nextval('bug_id_seq'::regclass),
"description" text,
@ -62,14 +70,6 @@ create table "public"."order_items" (
);
alter type "public"."shipping_status" rename to "shipping_status__old_version_to_be_dropped";
create type "public"."shipping_status" as enum ('not shipped', 'shipped', 'delivered');
alter type "public"."unused_enum" rename to "unused_enum__old_version_to_be_dropped";
create type "public"."unused_enum" as enum ('a', 'b', 'c');
alter table "public"."orders" alter column status type "public"."shipping_status" using status::text::"public"."shipping_status";
drop type "public"."shipping_status__old_version_to_be_dropped";