0

After reading the sqitch-rework documentation and the Altering Table with Sqitch Rework command stackoverflow question, I am aware that sqitch-rework was intended for idempotent changes.

But why?

It seems that my use of sqitch-rework works perfectly fine with a non-idempotent change.

Here is how I used it with PostgreSQL:

original deploy:

-- Deploy myproject:database/facility/schema/public/table/mytable to pg
-- requires: database/facility/schema/public

SET ROLE :"owner_role";

BEGIN;

CREATE TABLE public."MyTable"(
  "Date" timestamp without time zone NOT NULL
);

COMMIT;

original revert:

-- Revert myproject:database/facility/schema/public/table/mytable from pg

SET ROLE :"owner_role";

BEGIN;

DROP TABLE public."MyTable";

COMMIT;

reworked deploy:

-- Deploy myproject:database/facility/schema/public/table/mytable to pg
-- requires: database/facility/schema/public

SET ROLE :"owner_role";

BEGIN;

LOCK TABLE public."MyTable" IN ACCESS EXCLUSIVE MODE;

ALTER TABLE public."MyTable" RENAME COLUMN "Date" TO "Complete time";

ALTER TABLE public."MyTable" ADD COLUMN "Start time" timestamp without time zone;
UPDATE public."MyTable" SET "Start time" = "Complete time";
ALTER TABLE public."MyTable" ALTER COLUMN "Start time" SET NOT NULL;

COMMIT;

reworked revert:

-- Revert myproject:database/facility/schema/public/table/mytable from pg

SET ROLE :"owner_role";

BEGIN;

LOCK TABLE public."MyTable" IN ACCESS EXCLUSIVE MODE;

ALTER TABLE public."MyTable" DROP COLUMN "Start time";
ALTER TABLE public."MyTable" RENAME COLUMN "Complete time" TO "Date";

COMMIT;

Am I not understanding something? Isn't this a valid use of sqitch-rework?

soliz
  • 735
  • 1
  • 7
  • 17
  • @theory any comment? – soliz Jan 31 '22 at 10:18
  • The key phrase is: "It is best used only under the following circumstances ...". One of those being `idempotent`. It is not a requirement, just a best practice to avoid surprises. – Adrian Klaver Feb 11 '22 at 17:34
  • "It is best used only under …" combined with the 3rd point "…must…" is confusing. What would the surprises be with my use? – soliz Feb 14 '22 at 07:34
  • I missed the must. In any case there is no way for Sqitch to 'know' what is idempotent or not. That is for the user to verify. So back to, you can create non-idempotent changes and apply them. See the [rework](https://sqitch.org/docs/manual/sqitchtutorial/) section here Sqitch tutorial. My suggestions 1) In original deploy script add `DROP TABLE IF EXISTS public."MyTable"` before the `CREATE TABLE ...` to get back to a known state before creating the table. 2) Not sure that the `LOCK TABLE public."MyTable" IN ACCESS EXCLUSIVE MODE;` are needed as that is taken out by `ALTER TABLE` anyway. – Adrian Klaver Feb 14 '22 at 17:35

0 Answers0