migra icon indicating copy to clipboard operation
migra copied to clipboard

Changing a "GENERATED" column to regular non-generated one causes the whole column to DROP

Open hi2u opened this issue 3 years ago • 0 comments

If you initially have a GENERATED column, and then later on remove the GENERATED part so that it's now just a regular column... rather than just removing the GENERATED part, migra DROPs the whole column and re-creates it.

Which means you lose all your data in that column.

Example is below:

Old schema:

CREATE TABLE "demo_gencol" (
            "id" UUID           PRIMARY KEY     DEFAULT uuid_generate_v4(), -- 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
);

New schema:

CREATE TABLE "demo_gencol" (
            "id" UUID           PRIMARY KEY     DEFAULT uuid_generate_v4(), -- PRIMARY KEY
    "the_column" TEXT               NULL -- The column that is originally GENERATED, then changed not to be
);

❌ Migra currently generates diff:

alter table "demo_gencol" drop column "the_column";

alter table "demo_gencol" add column "the_column" text;

...this was on version numbers:

pip3 list --format=columns | grep -iE "migra|psycopg|schemainspect"
migra               3.0.1652017683
psycopg2            2.8.6
psycopg2-binary     2.9.3
schemainspect       3.1.1652015119

✔️ Migra should instead generate diff:

ALTER TABLE "demo_gencol" ALTER COLUMN "the_column" DROP EXPRESSION;

This will simply remove the GENERATED feature from the column, and retain your data that was previously populated by it.

hi2u avatar Jun 25 '22 05:06 hi2u