Commit aca889fc authored by Jeremy Kerr's avatar Jeremy Kerr
Browse files

[sql] Perserve bundle ordering in bundle reorder migration script


Signed-off-by: default avatarJeremy Kerr <jk@ozlabs.org>
parent 5f20247c
BEGIN;
ALTER TABLE patchwork_bundle_patches RENAME TO patchwork_bundlepatch;
CREATE SEQUENCE bundlepatch_tmp_seq;
CREATE TABLE "patchwork_bundlepatch" (
"id" SERIAL NOT NULL PRIMARY KEY,
"patch_id" INTEGER NOT NULL
REFERENCES "patchwork_patch" ("id") DEFERRABLE INITIALLY DEFERRED,
"bundle_id" INTEGER NOT NULL
REFERENCES "patchwork_bundle" ("id") DEFERRABLE INITIALLY DEFERRED,
"order" SERIAL NOT NULL,
UNIQUE ("bundle_id", "patch_id")
);
-- we 'INSERT INTO ... SELECT' (rather than renaming and adding the order
-- column) here so that we can order by date
INSERT INTO patchwork_bundlepatch (id, patch_id, bundle_id)
SELECT patchwork_bundle_patches.id, patch_id, bundle_id
FROM patchwork_bundle_patches
INNER JOIN patchwork_patch
ON patchwork_patch.id = patchwork_bundle_patches.patch_id
ORDER BY bundle_id, patchwork_patch.date;
COMMIT;
BEGIN;
ALTER TABLE patchwork_bundlepatch
ADD COLUMN "order" INTEGER NOT NULL
DEFAULT nextval('bundlepatch_tmp_seq');
ALTER TABLE patchwork_bundlepatch ALTER COLUMN "order" DROP DEFAULT;
DROP SEQUENCE bundlepatch_tmp_seq;
ALTER TABLE patchwork_bundlepatch ADD UNIQUE("bundle_id", "order");
ALTER COLUMN "order" TYPE INTEGER;
-- normalise ordering: order should start with 1 in each bundle
UPDATE patchwork_bundlepatch SET "order" = 1 + "order" -
(SELECT min("order") FROM patchwork_bundlepatch AS p2
WHERE p2.bundle_id = patchwork_bundlepatch.bundle_id);
SELECT * FROM patchwork_bundlepatch;
COMMIT;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment