~launchpad-pqm/launchpad/devel

7675.360.1 by Muharem Hrnjadovic
Package sets and distro series schema patch (including data migration) complete, testing it next.
1
-- Copyright 2009 Canonical Ltd.  This software is licensed under the
2
-- GNU Affero General Public License version 3 (see the file LICENSE).
3
4
SET client_min_messages=ERROR;
5
6
-- ** PART 1 ** Create the 'packagesetgroup' table and the
7
--              'packageset.packagesetgroup' foreign key,
8
--              populate the 'packagesetgroup' table
9
10
-- This table keeps track of package sets that are equivalent across
11
-- distro series boundaries.
7675.360.2 by Muharem Hrnjadovic
Patch tested and working.
12
CREATE SEQUENCE packagesetgroup_id_seq
13
    START WITH 1
14
    INCREMENT BY 1
15
    NO MAXVALUE
16
    NO MINVALUE
17
    CACHE 1;
7675.360.1 by Muharem Hrnjadovic
Package sets and distro series schema patch (including data migration) complete, testing it next.
18
CREATE TABLE packagesetgroup (
7675.360.2 by Muharem Hrnjadovic
Patch tested and working.
19
    id integer NOT NULL DEFAULT nextval('packagesetgroup_id_seq'),
7675.360.1 by Muharem Hrnjadovic
Package sets and distro series schema patch (including data migration) complete, testing it next.
20
    date_created timestamp without time zone DEFAULT timezone('UTC'::text, now()) NOT NULL,
21
    owner integer NOT NULL,
7675.360.2 by Muharem Hrnjadovic
Patch tested and working.
22
    -- Please note: the 'name' column is only here to ease the data migration
23
    -- and will be dropped at the end of this patch.
24
    name text NOT NULL
7675.360.1 by Muharem Hrnjadovic
Package sets and distro series schema patch (including data migration) complete, testing it next.
25
);
7675.360.2 by Muharem Hrnjadovic
Patch tested and working.
26
ALTER SEQUENCE packagesetgroup_id_seq OWNED BY packagesetgroup.id;
7675.360.1 by Muharem Hrnjadovic
Package sets and distro series schema patch (including data migration) complete, testing it next.
27
ALTER TABLE ONLY packagesetgroup
28
    ADD CONSTRAINT packagesetgroup_pkey PRIMARY KEY (id);
29
ALTER TABLE ONLY packagesetgroup
30
    ADD CONSTRAINT packagesetgroup__owner__fk
31
    FOREIGN KEY (owner) REFERENCES person(id);
32
33
-- Package sets and their clones belong to the same package set group.
34
ALTER TABLE ONLY packageset ADD COLUMN packagesetgroup integer;
35
ALTER TABLE ONLY packageset
36
  ADD CONSTRAINT packageset__packagesetgroup__fk
37
  FOREIGN KEY (packagesetgroup) REFERENCES packagesetgroup(id);
38
39
-- Create a group for each of the original (karmic koala) package sets.
7675.360.2 by Muharem Hrnjadovic
Patch tested and working.
40
INSERT INTO packagesetgroup(owner, name)
41
SELECT packageset.owner, packageset.name
7675.360.1 by Muharem Hrnjadovic
Package sets and distro series schema patch (including data migration) complete, testing it next.
42
FROM packageset WHERE NOT packageset.name LIKE('lucid-%');
43
44
45
-- ** PART 2 ** Associate the karmic koala package sets and their lucid lynx
46
--              clones with the appropriate package set groups
47
48
-- Update the karmic koala package sets so they reference their groups.
49
UPDATE packageset SET packagesetgroup = packagesetgroup.id
50
FROM packagesetgroup WHERE packageset.name = packagesetgroup.name;
51
52
-- Update the lucid lynx package set *clones* so they reference their groups
53
-- as well.
54
UPDATE packageset SET packagesetgroup = packagesetgroup.id
55
FROM packagesetgroup WHERE packageset.name = 'lucid-' || packagesetgroup.name;
56
57
-- ** PART 3 ** Add the 'packageset.distroseries' foreign key and
13194.4.8 by Gavin Panella
These changes could not be landed in devel, so they'll land here.
58
--              initialize it for the existing package sets.
7675.360.1 by Muharem Hrnjadovic
Package sets and distro series schema patch (including data migration) complete, testing it next.
59
60
-- A package set lives in a distro series context.
61
ALTER TABLE ONLY packageset ADD COLUMN distroseries integer;
62
63
-- Define the foreign key constraint.
64
ALTER TABLE ONLY packageset
65
  ADD CONSTRAINT packageset__distroseries__fk
66
  FOREIGN KEY (distroseries) REFERENCES distroseries(id);
67
68
-- First migrate the original package sets created for the karmic koala.
69
UPDATE packageset SET distroseries = distroseries.id FROM distroseries
70
WHERE distroseries.name = 'karmic' AND NOT packageset.name LIKE('lucid-%');
71
72
-- Migrate the lucid lynx package sets next.
73
UPDATE packageset SET distroseries = distroseries.id FROM distroseries
74
WHERE distroseries.name = 'lucid' AND packageset.name LIKE('lucid-%');
75
76
-- Make the 'distroseries' foreign key mandatory.
77
ALTER TABLE ONLY packageset ALTER COLUMN distroseries SET NOT NULL;
78
79
-- The package set name is now only unique in conjunction with a distro series.
80
ALTER TABLE ONLY packageset
81
    DROP CONSTRAINT packageset_name_key;
82
ALTER TABLE ONLY packageset
83
    ADD CONSTRAINT packageset__name__distroseries__key UNIQUE (name, distroseries);
84
85
-- ** PART 4 ** Strip off the 'lucid-' prefix of the lucid lynx
86
--              package set names
87
UPDATE packageset SET name = substring(name FROM length('lucid-')+1)
88
WHERE name LIKE('lucid-%');
89
90
-- ** PART 5 ** Create package set groups for package sets that were added in
91
--              lucid lynx but do not exist in the karmic koala,
92
--              associate these package sets with their newly created groups
7675.360.2 by Muharem Hrnjadovic
Patch tested and working.
93
INSERT INTO packagesetgroup(owner, name)
94
SELECT packageset.owner, packageset.name
7675.360.1 by Muharem Hrnjadovic
Package sets and distro series schema patch (including data migration) complete, testing it next.
95
FROM packageset, distroseries WHERE
96
    packageset.packagesetgroup IS NULL
97
    AND packageset.distroseries = distroseries.id
98
    AND distroseries.name = 'lucid';
99
100
UPDATE packageset SET packagesetgroup = packagesetgroup.id
101
FROM packagesetgroup, distroseries
102
WHERE
103
    packageset.packagesetgroup IS NULL
104
    AND packageset.distroseries = distroseries.id
105
    AND distroseries.name = 'lucid'
106
    AND packageset.name = packagesetgroup.name;
107
108
-- ** PART 6 ** Make the 'packageset.packagesetgroup' foreign key mandatory
109
ALTER TABLE ONLY packageset ALTER COLUMN packagesetgroup SET NOT NULL;
110
7675.360.2 by Muharem Hrnjadovic
Patch tested and working.
111
-- ** PART 7 ** Drop the 'packagesetgroup.name' column that was only added
112
--              for data migration purposes.
113
ALTER TABLE ONLY packagesetgroup DROP COLUMN name;
114
7675.360.1 by Muharem Hrnjadovic
Package sets and distro series schema patch (including data migration) complete, testing it next.
115
-- Define indices on the newly added foreign keys.
116
CREATE INDEX packageset__packagesetgroup__idx
117
    ON packageset(packagesetgroup);
118
CREATE INDEX packageset__distroseries__idx
119
    ON packageset(distroseries);
7675.360.6 by Muharem Hrnjadovic
stub's review comments.
120
CREATE INDEX packagesetgroup__owner__idx ON PackageSetGroup(owner);
7675.360.1 by Muharem Hrnjadovic
Package sets and distro series schema patch (including data migration) complete, testing it next.
121
122
INSERT INTO LaunchpadDatabaseRevision VALUES (2207, 06, 0);