From 9b5c682e8ffb1d711f2848e990140383967380d2 Mon Sep 17 00:00:00 2001
From: Jeremy Kerr <jk@ozlabs.org>
Date: Sat, 7 Feb 2009 15:44:07 +1100
Subject: Complete bundle ordering migration script

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
---
 lib/sql/migration/005-bundle-patch-ordering.sql | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

(limited to 'lib/sql/migration/005-bundle-patch-ordering.sql')

diff --git a/lib/sql/migration/005-bundle-patch-ordering.sql b/lib/sql/migration/005-bundle-patch-ordering.sql
index 9f5bcf3..e0db398 100644
--- a/lib/sql/migration/005-bundle-patch-ordering.sql
+++ b/lib/sql/migration/005-bundle-patch-ordering.sql
@@ -1,9 +1,11 @@
 BEGIN;
 ALTER TABLE patchwork_bundle_patches RENAME TO patchwork_bundlepatch;
-ALTER TABLE patchwork_bundlepatch ADD COLUMN "order" INTEGER NULL;
-UPDATE patchwork_bundlepatch SET "order" =
-    (SELECT COALESCE(max("order"), 0) + 1 FROM patchwork_bundlepatch AS p2
-        WHERE p2.bundle_id = patchwork_bundlepatch.bundle_id);
-ALTER TABLE patchwork_bundlepatch ALTER COLUMN "order" SET NOT NULL;
+CREATE SEQUENCE bundlepatch_tmp_seq;
+
+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");
 COMMIT;
-- 
cgit v1.2.3

