-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration.php
245 lines (194 loc) · 12.1 KB
/
migration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
mysql_connect('127.0.0.1', 'root', '');
mysql_select_db('argtech');
$query_count = 0;
function db_do($q)
{
global $query_count;
$query_count++;
echo $q . "\n";
$res = mysql_query($q);
if (mysql_error())
die(mysql_error());
return $res;
}
/* Get rid of stupid tables. */
db_do("DROP TABLE a_award");
db_do("DROP TABLE a_award_user");
db_do("DROP TABLE a_user");
db_do("DROP TABLE a_user_client");
db_do("DROP TABLE a_user_email");
db_do("DROP TABLE api_tool");
db_do("DROP TABLE api_tool_key");
db_do("DROP TABLE file_notes");
db_do("DROP TABLE post_alerts");
db_do("DROP TABLE randcounts");
db_do("DROP TABLE searches");
db_do("CREATE TABLE obj_allowed_mappings(id serial, left_type BIGINT(20) UNSIGNED NOT NULL, right_type BIGINT(20) UNSIGNED NOT NULL)");
/* Create the object type table, very important for a few reasons. */
db_do("CREATE TABLE obj_types(id SERIAL, slug VARCHAR(255), t VARCHAR(255), menu_title VARCHAR(255), privacy_setting ENUM('complex', 'public', 'project', 'parent'))");
db_do("ALTER TABLE obj_types ADD UNIQUE(slug)");
db_do("ALTER TABLE obj_types ADD UNIQUE(t)");
db_do("ALTER TABLE obj_types ADD UNIQUE(menu_title)");
/* Insert the user type into it. Our first type! */
db_do("INSERT INTO obj_types(slug, t, menu_title, privacy_setting) VALUES('user', 'user', 'Users', 'complex')");
db_do("INSERT INTO obj_types(slug, t, menu_title, privacy_setting) VALUES('project', 'project', 'Projects', 'complex')");
db_do("INSERT INTO obj_types(slug, t, menu_title, privacy_setting) VALUES('blog', 'blog', 'Blog', 'public')");
db_do("INSERT INTO obj_types(slug, t, menu_title, privacy_setting) VALUES('todo', 'todo', 'To Dos', 'project')");
db_do("INSERT INTO obj_types(slug, t, menu_title, privacy_setting) VALUES('character', 'character', 'Characters', 'project')");
db_do("INSERT INTO obj_types(slug, t, menu_title, privacy_setting) VALUES('comment', 'comment', 'Comments', 'parent')");
db_do("INSERT INTO obj_types(slug, t, menu_title, privacy_setting) VALUES('file', 'file', 'Files', 'parent')");
/* This stores any varchar values for the object. */
db_do("CREATE TABLE obj_string(id SERIAL, value VARCHAR(255))");
/* This stores any text values for the object. */
db_do("CREATE TABLE obj_text(id SERIAL, value TEXT)");
/* The base object table is the master table for all objects. This table will get big. */
db_do("CREATE TABLE base_object(id serial, creator BIGINT(20) UNSIGNED, parent BIGINT(20) UNSIGNED, project BIGINT(20) UNSIGNED, title BIGINT(20) UNSIGNED, created DATETIME, description BIGINT(20) UNSIGNED, buzz DECIMAL(5, 3) UNSIGNED NOT NULL DEFAULT 0.0, buzz_date DATETIME, image_id BIGINT(20) UNSIGNED NOT NULL, specific_id BIGINT(20) UNSIGNED NOT NULL)");
db_do("ALTER TABLE base_object ADD CONSTRAINT base_object_title_fk FOREIGN KEY (title) REFERENCES obj_string(id)");
db_do("ALTER TABLE base_object ADD CONSTRAINT base_object_description_fk FOREIGN KEY (description) REFERENCES obj_text(id)");
db_do("ALTER TABLE base_object ADD CONSTRAINT base_object_creator_fk FOREIGN KEY (creator) REFERENCES obj_static(id)");
db_do("ALTER TABLE base_object ADD CONSTRAINT base_object_project_fk FOREIGN KEY (project) REFERENCES obj_static(id)");
db_do("ALTER TABLE base_object ADD CONSTRAINT base_object_parent_fk FOREIGN KEY (parent) REFERENCES obj_static(id)");
db_do("CREATE TABLE obj_static(id serial, type BIGINT(20) UNSIGNED NOT NULL, current BIGINT(20) UNSIGNED NOT NULL, views BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, created DATETIME)");
db_do("ALTER TABLE obj_static ADD CONSTRAINT obj_static_current_fk FOREIGN KEY (current) REFERENCES base_obj(id)");
db_do("ALTER TABLE obj_static ADD CONSTRAINT obj_static_obj_type_fk FOREIGN KEY(type) REFERENCES obj_types(id)");
$user_old_to_new = array();
$res = db_do("SELECT * FROM users");
while ($row = mysql_fetch_assoc($res)) {
db_do("INSERT INTO obj_string(value) VALUES('" . mysql_real_escape_string($row['display_name']) . "')");
$title_id = mysql_insert_id();
db_do("INSERT INTO obj_text(value) VALUES('" . mysql_real_escape_string($row['bio']) . "')");
$bio_id = mysql_insert_id();
db_do("INSERT INTO base_object(title, created, description, buzz, buzz_date, specific_id) VALUES($title_id, '" . $row['#'] . "', $bio_id, '" . $row['buzz'] . "', '" . $row['buzz_date'] . "', '" . $row['id'] . "')");
$ver_id = mysql_insert_id();
db_do("INSERT INTO obj_static(type, current, created) VALUES(1, $ver_id, '" . $row['#'] . "')");
$user_old_to_new['a' . $row['id']] = mysql_insert_id();
db_do("UPDATE base_object SET parent = '" . $user_old_to_new['a' . $row['id']] . "' WHERE id = '" . $ver_id . "'");
}
/* Remind ourselves that the display_name has been taken care of. */
db_do("ALTER TABLE users DROP display_name, DROP #, DROP bio, DROP buzz, DROP buzz_date, MODIFY passhash VARCHAR(60)");
$project_old_to_new = array();
$res = db_do("SELECT * FROM project");
while ($row = mysql_fetch_assoc($res)) {
db_do("INSERT INTO obj_string(value) VALUES('" . mysql_real_escape_string($row['public_name']) . "')");
$title_id = mysql_insert_id();
db_do("INSERT INTO obj_text(value) VALUES('" . mysql_real_escape_string($row['bio']) . "')");
$bio_id = mysql_insert_id();
db_do("INSERT INTO base_object(creator, title, created, description, buzz, buzz_date) VALUES(" . $user_old_to_new['a' . $row['primary_contact']] . ", $title_id, '" . $row['created'] . "', $bio_id, '" . $row['buzz'] . "', '" . $row['buzz_date'] . "')");
$ver_id = mysql_insert_id();
db_do("INSERT INTO obj_static(type, created, current, views) VALUES(2, '" . $row['created'] . "', $ver_id, '" . $row['profile_views'] . "')");
$project_old_to_new['a' . $row['id']] = mysql_insert_id();
}
db_do("ALTER TABLE project DROP public_name, DROP private_name, DROP bio, DROP buzz, DROP buzz_date, DROP created, DROP profile_views, DROP primary_contact");
$blog_old_to_new = array();
$res = db_do("SELECT * FROM post");
while ($row = mysql_fetch_assoc($res)) {
db_do("INSERT INTO obj_string(value) VALUES('" . mysql_real_escape_string($row['title']) . "')");
$title_id = mysql_insert_id();
db_do("INSERT INTO obj_text(value) VALUES('" . mysql_real_escape_string($row['body']) . "')");
$body_id = mysql_insert_id();
db_do("INSERT INTO base_object(creator, title, created, description) VALUES(" . $user_old_to_new['a' . $row['user']] . ", $title_id, '" . $row['whenit'] . "', $body_id)");
$ver_id = mysql_insert_id();
db_do("INSERT INTO obj_static(type, current) VALUES(3, $ver_id)");
$blog_old_to_new['a' . $row['id']] = mysql_insert_id();
}
db_do("DROP TABLE post");
$res = db_do("SELECT * FROM todo");
$todo_old_to_new = array();
while ($row = mysql_fetch_assoc($res)) {
db_do("INSERT INTO obj_string(value) VALUES('" . mysql_real_escape_string($row['title']) . "')");
$title_id = mysql_insert_id();
db_do("INSERT INTO obj_text(value) VALUES('" . mysql_real_escape_string($row['body']) . "')");
$body_id = mysql_insert_id();
db_do("INSERT INTO base_object(creator, parent, title, created, description, project, specific_id) VALUES(" . $user_old_to_new['a' . $row['creator_id']] . ", " . $project_old_to_new['a' . $row['project_id']] . ", $title_id, '" . $row['created'] . "', $body_id, '" . $project_old_to_new['a' . $row['project_id']] . "', '" . $row['id'] . "')");
$ver_id = mysql_insert_id();
db_do("INSERT INTO obj_static(type, current) VALUES(4, $ver_id)");
$todo_old_to_new['a' . $row['id']] = mysql_insert_id();
}
db_do("ALTER TABLE todo DROP title, DROP body, DROP created, DROP creator_id, DROP project_id");
$res = db_do("SELECT * FROM game_character");
while ($row = mysql_fetch_assoc($res)) {
db_do("INSERT INTO obj_string(value) VALUES('" . mysql_real_escape_string($row['name']) . "')");
$title_id = mysql_insert_id();
db_do("INSERT INTO obj_text(value) VALUES('" . mysql_real_escape_string($row['body']) . "')");
$body_id = mysql_insert_id();
db_do("INSERT INTO base_object(creator, parent, title, created, description, project) VALUES(" . $user_old_to_new['a' . $row['creator']] . ", " . $project_old_to_new['a' . $row['project_id']] . ", $title_id, '" . $row['created'] . "', $body_id, '" . $project_old_to_new['a' . $row['project_id']] . "')");
$ver_id = mysql_insert_id();
db_do("INSERT INTO obj_static(type, current) VALUES(5, $ver_id)");
}
db_do("DROP TABLE game_character");
$res = db_do("SELECT * FROM comment");
while ($row = mysql_fetch_assoc($res)) {
if (!$row['verified'])
continue;
db_do("INSERT INTO obj_string(value) VALUES('" . mysql_real_escape_string($row['name']) . "')");
$title_id = mysql_insert_id();
db_do("INSERT INTO obj_text(value) VALUES('" . mysql_real_escape_string($row['body']) . "')");
$body_id = mysql_insert_id();
db_do("INSERT INTO base_object(parent, title, created, description) VALUES(" . $blog_old_to_new['a' . $row['post_id']] . ", $title_id, '" . $row['whenit'] . "', $body_id)");
$ver_id = mysql_insert_id();
db_do("INSERT INTO obj_static(type, current) VALUES(6, $ver_id)");
}
db_do("ALTER TABLE comment DROP name, DROP body, DROP whenit, DROP post_id");
$res = db_do("SELECT * FROM conversation ORDER BY id");
$conversation_old_to_new = array();
while ($row = mysql_fetch_assoc($res)) {
db_do("INSERT INTO obj_string(value) VALUES('" . mysql_real_escape_string($row['title']) . "')");
$title_id = mysql_insert_id();
db_do("INSERT INTO obj_text(value) VALUES('" . mysql_real_escape_string($row['body']) . "')");
$body_id = mysql_insert_id();
$parent = NULL;
$do_insert = true;
switch ($row['parent_type']) {
case 0:
$parent = $project_old_to_new['a' . $row['project_id']];
break;
case 1:
// This is really a comment. I was stupid.
$parent = $conversation_old_to_new['a' . $row['parent_id']];
db_do("INSERT INTO base_object(project, parent, title, created, description) VALUES('" . $project_old_to_new['a' . $row['project_id']] . "', $parent, $title_id, '" . $row['posted'] . "', $body_id)");
$ver_id = mysql_insert_id();
db_do("INSERT INTO obj_static(type, current) VALUES(6, $ver_id)");
$do_insert = false;
break;
case 3:
$parent = $todo_old_to_new['a' . $row['parent_id']];
break;
case 4:
// These are orphaned anyway. Sadface.
$do_insert = false;
break;
default:
die('Unknown parent type: ' . $row['parent_type'] . "\n" . print_r($row, 1) . "\n");
}
if ($do_insert) {
db_do("INSERT INTO base_object(creator, project, parent, title, created, description) VALUES(" . $user_old_to_new['a' . $row['user_id']] . ", " . $project_old_to_new['a' . $row['project_id']] . ", $parent, $title_id, '" . $row['posted'] . "', $body_id)");
$ver_id = mysql_insert_id();
db_do("INSERT INTO obj_static(type, current) VALUES(6, $ver_id)");
$conversation_old_to_new['a' . $row['id']] = mysql_insert_id();
}
}
db_do("ALTER TABLE conversation DROP title, DROP body, DROP user_id, DROP posted");
$res = db_do("SELECT * FROM file");
$files_old_to_new = array();
while ($row = mysql_fetch_assoc($res)) {
$files_old_to_new['a' . $row['id']] = $row;
db_do("UPDATE file SET project_id = '" . $project_old_to_new['a' . $row['project_id']] . "' WHERE id = '" . $row['id'] . "'");
}
$res = db_do("SELECT * FROM file_version");
while ($row = mysql_fetch_assoc($res)) {
db_do("INSERT INTO obj_string(value) VALUES('" . mysql_real_escape_string($row['shortdesc']) . "')");
$title_id = mysql_insert_id();
db_do("INSERT INTO obj_text(value) VALUES('" . mysql_real_escape_string($row['note']) . "')");
$body_id = mysql_insert_id();
db_do("INSERT INTO base_object(creator, title, created, description, parent) VALUES(" . $user_old_to_new['a' . $row['creator_id']] . ", $title_id, '" . $row['created'] . "', $body_id, '" . $project_old_to_new['a' . $files_old_to_new['a' . $row['file_id']]['project_id']] . "')");
$ver_id = mysql_insert_id();
db_do("INSERT INTO obj_static(type, current, views) VALUES(7, $ver_id, '" . $row['dl_count'] . "')");
}
db_do("ALTER TABLE file_version DROP shortdesc, DROP note, DROP creator_id, DROP created, DROP dl_count");
$res = db_do("SELECT * FROM project_user");
while ($row = mysql_fetch_assoc($res)) {
db_do("UPDATE project_user SET user_id = '" . $user_old_to_new['a' . $row['user_id']] .
"', project_id = '" . $project_old_to_new['a' . $row['project_id']] . "' WHERE id = '" . $row['id'] . "'");
}
echo $query_count . " queries\n";