1 1. Drop the user ID foreign key from the "PackageComments" table:
3 `ALTER TABLE PackageComments DROP FOREIGN KEY PackageComments_ibfk_1;` should
4 work in most cases. Otherwise, check the output of `SHOW CREATE TABLE
5 PackageComments;` and use the foreign key name shown there.
7 2. Add support for anonymous comments:
10 ALTER TABLE PackageComments
11 MODIFY UsersID INTEGER UNSIGNED NULL DEFAULT NULL,
12 ADD FOREIGN KEY (UsersID) REFERENCES Users(ID) ON DELETE SET NULL;
15 3. Create the PackageBases table:
18 CREATE TABLE PackageBases (
19 ID INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
20 Name VARCHAR(64) NOT NULL,
21 CategoryID TINYINT UNSIGNED NOT NULL DEFAULT 1,
22 NumVotes INTEGER UNSIGNED NOT NULL DEFAULT 0,
23 OutOfDateTS BIGINT UNSIGNED NULL DEFAULT NULL,
24 SubmittedTS BIGINT UNSIGNED NOT NULL,
25 ModifiedTS BIGINT UNSIGNED NOT NULL,
26 SubmitterUID INTEGER UNSIGNED NULL DEFAULT NULL,
27 MaintainerUID INTEGER UNSIGNED NULL DEFAULT NULL,
28 PackagerUID INTEGER UNSIGNED NULL DEFAULT NULL,
34 INDEX (MaintainerUID),
36 FOREIGN KEY (CategoryID) REFERENCES PackageCategories(ID) ON DELETE NO ACTION,
37 -- deleting a user will cause packages to be orphaned, not deleted
38 FOREIGN KEY (SubmitterUID) REFERENCES Users(ID) ON DELETE SET NULL,
39 FOREIGN KEY (MaintainerUID) REFERENCES Users(ID) ON DELETE SET NULL,
40 FOREIGN KEY (PackagerUID) REFERENCES Users(ID) ON DELETE SET NULL
44 4. Migrate data from Packages to PackageBases:
47 INSERT INTO PackageBases
48 SELECT ID, Name, CategoryID, NumVotes, OutOfDateTS, SubmittedTS,
49 ModifiedTS, SubmitterUID, MaintainerUID, NULL FROM Packages;
52 5. Delete unneeded foreign keys from Packages:
54 First, drop the foreign keys on CategoryID, SubmitterUID and MaintainerUID. The
55 following queries should work in most cases:
59 DROP FOREIGN KEY Packages_ibfk_1,
60 DROP FOREIGN KEY Packages_ibfk_2,
61 DROP FOREIGN KEY Packages_ibfk_3;
64 You can use `SHOW CREATE TABLE Packages;` to check whether you should use
65 different names for your setup.
67 6. Delete unneeded fields from Packages:
71 DROP COLUMN CategoryID,
73 DROP COLUMN OutOfDateTS,
74 DROP COLUMN SubmittedTS,
75 DROP COLUMN ModifiedTS,
76 DROP COLUMN SubmitterUID,
77 DROP COLUMN MaintainerUID;
80 7. Add package base references to the Packages table:
83 ALTER TABLE Packages ADD COLUMN PackageBaseID INTEGER UNSIGNED NULL;
84 UPDATE Packages SET PackageBaseID = ID;
86 MODIFY PackageBaseID INTEGER UNSIGNED NOT NULL,
87 ADD FOREIGN KEY (PackageBaseID) REFERENCES PackageBases(ID) ON DELETE CASCADE;
90 8. Delete foreign keys from PackageVotes, PackageComments and CommentNotify:
93 ALTER TABLE PackageVotes
94 DROP FOREIGN KEY PackageVotes_ibfk_1,
95 DROP FOREIGN KEY PackageVotes_ibfk_2;
96 ALTER TABLE PackageComments
97 DROP FOREIGN KEY PackageComments_ibfk_3;
98 ALTER TABLE CommentNotify
99 DROP FOREIGN KEY CommentNotify_ibfk_1,
100 DROP FOREIGN KEY CommentNotify_ibfk_2;
103 We highly recommend to use `SHOW CREATE TABLE PackageVotes;` etc. to check
104 whether you should use different names for your setup.
106 9. Delete indexes from PackageVotes and CommentNotify:
109 ALTER TABLE PackageVotes DROP INDEX VoteUsersIDPackageID;
110 ALTER TABLE CommentNotify DROP INDEX NotifyUserIDPkgID;
113 10. Migrate PackageVotes, PackageComments and CommentNotify to refer to package
117 ALTER TABLE PackageVotes ADD COLUMN PackageBaseID INTEGER UNSIGNED NOT NULL;
118 UPDATE PackageVotes SET PackageBaseID = PackageID;
119 ALTER TABLE PackageVotes DROP COLUMN PackageID;
120 ALTER TABLE PackageComments ADD COLUMN PackageBaseID INTEGER UNSIGNED NOT NULL;
121 UPDATE PackageComments SET PackageBaseID = PackageID;
122 ALTER TABLE PackageComments DROP COLUMN PackageID;
123 ALTER TABLE CommentNotify ADD COLUMN PackageBaseID INTEGER UNSIGNED NOT NULL;
124 UPDATE CommentNotify SET PackageBaseID = PkgID;
125 ALTER TABLE CommentNotify DROP COLUMN PkgID;
128 11. Recreate missing foreign keys and indexes:
131 ALTER TABLE PackageVotes
132 ADD FOREIGN KEY (UsersID) REFERENCES Users(ID) ON DELETE CASCADE,
133 ADD FOREIGN KEY (PackageBaseID) REFERENCES PackageBases(ID) ON DELETE CASCADE;
134 ALTER TABLE PackageComments
135 ADD FOREIGN KEY (PackageBaseID) REFERENCES PackageBases(ID) ON DELETE CASCADE;
136 ALTER TABLE CommentNotify
137 ADD FOREIGN KEY (PackageBaseID) REFERENCES PackageBases(ID) ON DELETE CASCADE,
138 ADD FOREIGN KEY (UserID) REFERENCES Users(ID) ON DELETE CASCADE;
139 CREATE UNIQUE INDEX VoteUsersIDPackageID ON PackageVotes (UsersID, PackageBaseID);
140 CREATE UNIQUE INDEX NotifyUserIDPkgID ON CommentNotify (UserID, PackageBaseID);
143 12. Create a new table to store package dependency types:
146 CREATE TABLE DependencyTypes (
147 ID TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
148 Name VARCHAR(32) NOT NULL DEFAULT '',
151 INSERT INTO DependencyTypes VALUES (1, 'depends');
152 INSERT INTO DependencyTypes VALUES (2, 'makedepends');
153 INSERT INTO DependencyTypes VALUES (3, 'checkdepends');
154 INSERT INTO DependencyTypes VALUES (4, 'optdepends');
157 13. Add a field to store the dependency type to the PackageDepends table:
160 ALTER TABLE PackageDepends ADD COLUMN DepTypeID TINYINT UNSIGNED NOT NULL;
161 UPDATE PackageDepends SET DepTypeID = 1;
162 ALTER TABLE PackageDepends
163 ADD FOREIGN KEY (DepTypeID) REFERENCES DependencyTypes(ID) ON DELETE NO ACTION;
166 14. Resize the package dependency name field:
169 ALTER TABLE PackageDepends MODIFY DepName VARCHAR(255) NOT NULL;
172 15. Create a new table to store package relation types:
175 CREATE TABLE RelationTypes (
176 ID TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
177 Name VARCHAR(32) NOT NULL DEFAULT '',
180 INSERT INTO RelationTypes VALUES (1, 'conflicts');
181 INSERT INTO RelationTypes VALUES (2, 'provides');
182 INSERT INTO RelationTypes VALUES (3, 'replaces');
185 16. Create a new table to store package relations:
188 CREATE TABLE PackageRelations (
189 PackageID INTEGER UNSIGNED NOT NULL,
190 RelTypeID TINYINT UNSIGNED NOT NULL,
191 RelName VARCHAR(255) NOT NULL,
192 RelCondition VARCHAR(20),
195 FOREIGN KEY (PackageID) REFERENCES Packages(ID) ON DELETE CASCADE,
196 FOREIGN KEY (RelTypeID) REFERENCES RelationTypes(ID) ON DELETE NO ACTION
200 17. Create tables to store package groups:
203 CREATE TABLE Groups (
204 ID INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
205 Name VARCHAR(64) NOT NULL,
209 CREATE TABLE PackageGroups (
210 PackageID INTEGER UNSIGNED NOT NULL,
211 GroupID INTEGER UNSIGNED NOT NULL,
212 PRIMARY KEY (PackageID, GroupID),
213 FOREIGN KEY (PackageID) REFERENCES Packages(ID) ON DELETE CASCADE,
214 FOREIGN KEY (GroupID) REFERENCES Groups(ID) ON DELETE CASCADE
218 18. Create tables to store package licenses:
221 CREATE TABLE Licenses (
222 ID INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
223 Name VARCHAR(64) NOT NULL,
227 CREATE TABLE PackageLicenses (
228 PackageID INTEGER UNSIGNED NOT NULL,
229 LicenseID INTEGER UNSIGNED NOT NULL,
230 PRIMARY KEY (PackageID, LicenseID),
231 FOREIGN KEY (PackageID) REFERENCES Packages(ID) ON DELETE CASCADE,
232 FOREIGN KEY (LicenseID) REFERENCES Licenses(ID) ON DELETE CASCADE
236 19. Convert existing licenses to the new storage format:
239 INSERT INTO Licenses (Name) SELECT DISTINCT License FROM Packages;
240 INSERT INTO PackageLicenses (PackageID, LicenseID)
241 SELECT Packages.ID, Licenses.ID FROM Packages
242 INNER JOIN Licenses ON Licenses.Name = Packages.License;
245 20. Delete the license column from the Packages table:
248 ALTER TABLE Packages DROP COLUMN License;