drop expression support

This commit is contained in:
Robert Lechte 2022-09-13 23:12:40 +10:00
parent 3ff01951b2
commit da6671acae
15 changed files with 86 additions and 8 deletions

View File

@ -276,7 +276,17 @@ def get_table_changes(
inheritance_status_changed = c.is_inherited != c_before.is_inherited
if inheritance_status_changed or generated_status_changed:
generated_status_removed = not c.is_generated and c_before.is_generated
can_drop_generated = (
generated_status_removed and c_before.can_drop_generated
)
drop_and_recreate_required = inheritance_status_changed or (
generated_status_changed and not can_drop_generated
)
if drop_and_recreate_required:
del c_modified[k]
if not c_before.is_inherited:
@ -285,6 +295,9 @@ def get_table_changes(
if not c.is_inherited:
c_added[k] = c
if generated_status_changed:
pass
for k, c in c_removed.items():
alter = v.alter_table_statement(c.drop_column_clause)
statements.append(alter)

6
poetry.lock generated
View File

@ -334,7 +334,7 @@ jupyter = ["ipywidgets (>=7.5.1,<8.0.0)"]
[[package]]
name = "schemainspect"
version = "3.1.1663062229"
version = "3.1.1663480743"
description = "Schema inspection for PostgreSQL (and possibly others)"
category = "main"
optional = false
@ -742,8 +742,8 @@ rich = [
{file = "rich-12.5.1.tar.gz", hash = "sha256:63a5c5ce3673d3d5fbbf23cd87e11ab84b6b451436f1b7f19ec54b6bc36ed7ca"},
]
schemainspect = [
{file = "schemainspect-3.1.1663062229-py3-none-any.whl", hash = "sha256:ca6ab497e90a1e4c4dd7406b1d38efdbf478d7c482408e3a8852b2dfa686a5f6"},
{file = "schemainspect-3.1.1663062229.tar.gz", hash = "sha256:625507658f4971a4a3cd0a4afe7ce40e66158fe5fcbd57f426e307434f5742e6"},
{file = "schemainspect-3.1.1663480743-py3-none-any.whl", hash = "sha256:852813d0ee38985242373708b8441c5fbcff611d30dae013afb5cb7d7148c95a"},
{file = "schemainspect-3.1.1663480743.tar.gz", hash = "sha256:b042676ceffd189983f38a42042dfb19d86925f8c4f8bdd6d40334582fdb116b"},
]
six = [
{file = "six-1.16.0-py2.py3-none-any.whl", hash = "sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254"},

View File

@ -14,7 +14,7 @@ python = ">=3.7,<4"
sqlbag = "*"
six = "*"
# schemainspect = {path="../schemainspect", develop=true}
schemainspect = ">=3.1.1658648837"
schemainspect = ">=3.1.1663480743"
psycopg2-binary = { version="*", optional = true }
[tool.poetry.dev-dependencies]

View File

@ -0,0 +1,15 @@
-- create table t(
-- a int,
-- adding int,
-- modifying int generated always as identity,
-- removing int generated by default as identity
-- );
create table t();
CREATE TABLE "demo_gencol" (
"id" serial PRIMARY KEY, -- PRIMARY KEY
"the_column" TEXT NULL GENERATED ALWAYS AS ('the original generated value') STORED, -- The column that is originally GENERATED, then changed not to be
"the_column2" text
);

View File

View File

@ -0,0 +1,17 @@
-- create table t(
-- a int,
-- adding int generated always as (1) stored,
-- modifying int generated always as (1) stored,
-- removing int
-- );
CREATE TABLE "demo_gencol" (
"id" serial PRIMARY KEY, -- PRIMARY KEY
"the_column" text,
"the_column2" TEXT NULL GENERATED ALWAYS AS ('the original generated value') STORED -- The column that is originally GENERATED, then changed not to be
);

View File

@ -0,0 +1,7 @@
drop table "public"."t";
alter table "public"."demo_gencol" drop column "the_column2";
alter table "public"."demo_gencol" add column "the_column2" text generated always as ('the original generated value'::text) stored;
alter table "public"."demo_gencol" alter column "the_column" drop expression;

View File

View File

@ -0,0 +1,13 @@
-- create table t(
-- a int,
-- adding int,
-- modifying int generated always as identity,
-- removing int generated by default as identity
-- );
create table t();
CREATE TABLE "demo_gencol" (
"id" serial PRIMARY KEY , -- PRIMARY KEY
"the_column" TEXT NULL -- The column that is originally GENERATED, then changed not to be
);

View File

@ -0,0 +1,13 @@
-- create table t(
-- a int,
-- adding int generated always as (1) stored,
-- modifying int generated always as (1) stored,
-- removing int
-- );
CREATE TABLE "demo_gencol" (
"id" serial PRIMARY KEY , -- PRIMARY KEY
"the_column" TEXT NULL -- The column that is originally GENERATED, then changed not to be
);

View File

@ -0,0 +1 @@
alter table "public"."demo_gencol" alter column "the_column" drop expression;

View File

@ -11,11 +11,9 @@ create table "public"."t2" (
alter table "public"."gen" drop column "adding";
alter table "public"."gen" drop column "removing";
alter table "public"."gen" add column "adding" integer generated always as (1) stored;
alter table "public"."gen" add column "removing" integer;
alter table "public"."gen" alter column "removing" drop expression;
alter table "public"."identchanges" alter column "c" drop default;

View File

@ -83,6 +83,7 @@ dependencies2
dependencies3
dependencies4
constraints
generated
""".split()