annotate managedb.php @ 630:b84fc6e7847e

Suppress PHP errors.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 01 Nov 2014 07:11:13 +0200
parents 894a4a6bfb5f
children 81807c56153e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 #!/usr/bin/php
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 <?
175
8df523e6326a User require_once instead of require.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
3 require_once "mconfig.inc.php";
8df523e6326a User require_once instead of require.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
4 require_once "msite.inc.php";
555
206196b610fd Move site specific things to dbdefs.inc.php
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
5 require_once "dbdefs.inc.php";
8
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6
479
86ee2b42a995 Move CLI helper functions to msitegen, again.
Matti Hamalainen <ccr@tnsp.org>
parents: 426
diff changeset
7 stCheckCLIExec();
50
184a4188555c Move CLI helper functions to createdb.php, as they are now only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 47
diff changeset
8
555
206196b610fd Move site specific things to dbdefs.inc.php
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
9
542
8c1a53532be0 Rename various database tables and column names for better SQL compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 541
diff changeset
10 $currVersion = -1;
555
206196b610fd Move site specific things to dbdefs.inc.php
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
11 $dbMetaData = array(
199
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
12 "dbVersion" => array(VT_INT, $dbVersion, "Database version"),
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
13 );
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
14
620
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
15 // Add database meta table into tables to be created
555
206196b610fd Move site specific things to dbdefs.inc.php
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
16 $sqlTables["dbmeta"] = array(
206196b610fd Move site specific things to dbdefs.inc.php
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
17 array("key" , "VARCHAR(32)", "PRIMARY KEY"),
206196b610fd Move site specific things to dbdefs.inc.php
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
18 array("vtype" , "INT"),
206196b610fd Move site specific things to dbdefs.inc.php
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
19 array("vstr" , "VARCHAR(128)"),
206196b610fd Move site specific things to dbdefs.inc.php
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
20 array("vtext" , "TEXT"),
206196b610fd Move site specific things to dbdefs.inc.php
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
21 array("vint" , "INT"),
206196b610fd Move site specific things to dbdefs.inc.php
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
22 array("sdesc" , "VARCHAR(128)"),
8
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 );
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24
515
2fc7bd9e209f Add comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 511
diff changeset
25
88
784953d60ba5 More work on test data generation.
Matti Hamalainen <ccr@tnsp.org>
parents: 85
diff changeset
26
8
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 //
36
ace577c92a32 Comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
28 // Helper functions
8
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 //
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
30 function stCreateTables($dbh, $upgrade)
8
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 {
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 global $sqlTables;
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 echo "Creating tables...\n";
503
3232f682f0d2 Change how table schemas are defined, in preparation for future database
Matti Hamalainen <ccr@tnsp.org>
parents: 502
diff changeset
34 foreach ($sqlTables as $name => $schemaData)
8
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 {
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 echo " - '".$name."'\n";
540
ab08640b4302 Improve database upgrade code.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
37 stDBExecSQL($dbh, "DROP TABLE IF EXISTS ".$name);
620
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
38 if (!stDBCreateOneTable($dbh, $name, stDBGetTableSchema($dbh, $schemaData)))
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
39 {
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
40 if (!$upgrade)
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
41 return FALSE;
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
42 else
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
43 echo "\nFailed to create table '".$name."'\n";
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
44 }
8
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 }
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 return TRUE;
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 }
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49
540
ab08640b4302 Improve database upgrade code.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
50 function stUpgradeMap($type, $name)
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
51 {
540
ab08640b4302 Improve database upgrade code.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
52 global $upgradeMappings, $currVersion, $dbVersion;
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
53
540
ab08640b4302 Improve database upgrade code.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
54 $converted = FALSE;
ab08640b4302 Improve database upgrade code.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
55 $tmpName = $name;
ab08640b4302 Improve database upgrade code.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
56
ab08640b4302 Improve database upgrade code.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
57 for ($ver = $currVersion; $ver <= $dbVersion; $ver++)
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
58 {
540
ab08640b4302 Improve database upgrade code.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
59 if (isset($upgradeMappings[$ver]) &&
590
94b9bf155fdb Revert a completely broken change. :S
Matti Hamalainen <ccr@tnsp.org>
parents: 586
diff changeset
60 isset($upgradeMappings[$ver][$tmpName]) &&
94b9bf155fdb Revert a completely broken change. :S
Matti Hamalainen <ccr@tnsp.org>
parents: 586
diff changeset
61 isset($upgradeMappings[$ver][$tmpName][$type]))
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
62 {
590
94b9bf155fdb Revert a completely broken change. :S
Matti Hamalainen <ccr@tnsp.org>
parents: 586
diff changeset
63 $tmpName = $upgradeMappings[$ver][$tmpName][$type];
540
ab08640b4302 Improve database upgrade code.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
64 $converted = TRUE;
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
65 }
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
66 }
540
ab08640b4302 Improve database upgrade code.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
67
ab08640b4302 Improve database upgrade code.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
68 if ($converted)
ab08640b4302 Improve database upgrade code.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
69 return $tmpName;
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
70 else
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
71 switch ($type)
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
72 {
540
ab08640b4302 Improve database upgrade code.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
73 case "table": return $name;
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
74 case "key": return $name;
540
ab08640b4302 Improve database upgrade code.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
75 default: die("Invalid upgrade type '".$type."'.\n");
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
76 }
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
77 }
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
78
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
79
620
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
80 function stDoAddSettings($inDB, $outDB, $settings, $table, $upgrade, $group = FALSE)
8
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 {
519
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
82 $status = TRUE;
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
83
139
75cf14ee99a7 More work on party information system.
Matti Hamalainen <ccr@tnsp.org>
parents: 123
diff changeset
84 foreach ($settings as $key => $data)
8
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 {
551
bed441ced78e Improve migration and upgrade procedures.
Matti Hamalainen <ccr@tnsp.org>
parents: 549
diff changeset
86 $sql = false;
bed441ced78e Improve migration and upgrade procedures.
Matti Hamalainen <ccr@tnsp.org>
parents: 549
diff changeset
87
515
2fc7bd9e209f Add comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 511
diff changeset
88 // Get setting type
80
e51205f2bdd0 Rename a variable.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
89 switch ($data[0])
8
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 {
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 case VT_TEXT: $type = "%s"; $var = "vtext"; break;
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 case VT_STR: $type = "%s"; $var = "vstr"; break;
72
63a6caf59e5c Fix creation of boolean settings, and add '%b' formatter into stPrepareSQL().
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
93 case VT_BOOL: $type = "%b"; $var = "vint"; break;
8
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 case VT_INT: $type = "%d"; $var = "vint"; break;
80
e51205f2bdd0 Rename a variable.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
95 default: die("Invalid type in default settings '".$key."', type=".$data[0]."\n");
8
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 }
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97
620
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
98 $sqlFmt =
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
99 "INSERT INTO ".$table." (key,vtype,".$var.",sdesc".
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
100 ($group !== FALSE ? ",vgroup" : "").") VALUES (%s,%d,".$type.",%s".
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
101 ($group !== FALSE ? ",%d" : "").")";
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
102
515
2fc7bd9e209f Add comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 511
diff changeset
103 // Check if we are upgrading
551
bed441ced78e Improve migration and upgrade procedures.
Matti Hamalainen <ccr@tnsp.org>
parents: 549
diff changeset
104 if ($upgrade)
199
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
105 {
551
bed441ced78e Improve migration and upgrade procedures.
Matti Hamalainen <ccr@tnsp.org>
parents: 549
diff changeset
106 // Map the key, in case the name has changed
bed441ced78e Improve migration and upgrade procedures.
Matti Hamalainen <ccr@tnsp.org>
parents: 549
diff changeset
107 $inKey = stUpgradeMap("key", $key);
bed441ced78e Improve migration and upgrade procedures.
Matti Hamalainen <ccr@tnsp.org>
parents: 549
diff changeset
108 $inTable = stUpgradeMap("table", $table);
199
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
109
551
bed441ced78e Improve migration and upgrade procedures.
Matti Hamalainen <ccr@tnsp.org>
parents: 549
diff changeset
110 if (($res = stDBFetchSQL($inDB, stDBPrepareSQL($inDB, "SELECT * FROM ".$inTable." WHERE key=%s", $inKey))) !== FALSE)
bed441ced78e Improve migration and upgrade procedures.
Matti Hamalainen <ccr@tnsp.org>
parents: 549
diff changeset
111 {
bed441ced78e Improve migration and upgrade procedures.
Matti Hamalainen <ccr@tnsp.org>
parents: 549
diff changeset
112 // Yup, upgrade the data, if we can
bed441ced78e Improve migration and upgrade procedures.
Matti Hamalainen <ccr@tnsp.org>
parents: 549
diff changeset
113 if ($res["vtype"] != $data[0])
bed441ced78e Improve migration and upgrade procedures.
Matti Hamalainen <ccr@tnsp.org>
parents: 549
diff changeset
114 die("Oops! Data type of '".$key."' does not match in table '".$table.". DB upgrade failed.\n");
bed441ced78e Improve migration and upgrade procedures.
Matti Hamalainen <ccr@tnsp.org>
parents: 549
diff changeset
115
620
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
116 $sql = stDBPrepareSQL($outDB, $sqlFmt,
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
117 $key, $data[0], $res[$var], $data[2], $group);
551
bed441ced78e Improve migration and upgrade procedures.
Matti Hamalainen <ccr@tnsp.org>
parents: 549
diff changeset
118 }
199
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
119 }
551
bed441ced78e Improve migration and upgrade procedures.
Matti Hamalainen <ccr@tnsp.org>
parents: 549
diff changeset
120
bed441ced78e Improve migration and upgrade procedures.
Matti Hamalainen <ccr@tnsp.org>
parents: 549
diff changeset
121 if ($sql === false)
199
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
122 {
515
2fc7bd9e209f Add comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 511
diff changeset
123 // Normal insertion of default data
620
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
124 $sql = stDBPrepareSQL($outDB, $sqlFmt,
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
125 $key, $data[0], $data[1], $data[2], $group);
199
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
126 }
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
127
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
128 if (stDBExecSQL($outDB, $sql) === FALSE)
519
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
129 {
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
130 $status = FALSE;
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
131 break;
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
132 }
199
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
133 }
519
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
134
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
135 return $status;
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
136 }
199
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
137
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
138
620
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
139 function stAddSettingsGroup($inDB, $outDB, $groups, $groupData, $groupTable, $table, $upgrade)
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
140 {
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
141 echo ($upgrade ? "Adding settings to" : "Upgrading settings in")." '".$table."' table.\n";
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
142 foreach ($groups as $groupID => $settings)
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
143 {
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
144 if (!isset($groupData[$groupID]))
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
145 {
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
146 echo "Group data not set for settings group ID '".$groupID."'.\n";
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
147 return FALSE;
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
148 }
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
149
629
894a4a6bfb5f Better fix for the DB migration issue.
Matti Hamalainen <ccr@tnsp.org>
parents: 628
diff changeset
150 // Add group, if needed
894a4a6bfb5f Better fix for the DB migration issue.
Matti Hamalainen <ccr@tnsp.org>
parents: 628
diff changeset
151 echo " - Group '".$groupID."' -> ".$groupData[$groupID][1].".";
894a4a6bfb5f Better fix for the DB migration issue.
Matti Hamalainen <ccr@tnsp.org>
parents: 628
diff changeset
152 $sql = stDBPrepareSQL($outDB,
894a4a6bfb5f Better fix for the DB migration issue.
Matti Hamalainen <ccr@tnsp.org>
parents: 628
diff changeset
153 "INSERT INTO ".$groupTable." (name,description) VALUES (%s,%s)",
894a4a6bfb5f Better fix for the DB migration issue.
Matti Hamalainen <ccr@tnsp.org>
parents: 628
diff changeset
154 $groupData[$groupID][0], $groupData[$groupID][1]);
894a4a6bfb5f Better fix for the DB migration issue.
Matti Hamalainen <ccr@tnsp.org>
parents: 628
diff changeset
155
894a4a6bfb5f Better fix for the DB migration issue.
Matti Hamalainen <ccr@tnsp.org>
parents: 628
diff changeset
156 if (($gid = stDBExecSQLInsert($outDB, $sql)) === false)
628
914d57c80059 Fix database migrations now that we create settings groups.
Matti Hamalainen <ccr@tnsp.org>
parents: 620
diff changeset
157 {
629
894a4a6bfb5f Better fix for the DB migration issue.
Matti Hamalainen <ccr@tnsp.org>
parents: 628
diff changeset
158 echo "\nFailed to add group '".$groupID."'\n";
894a4a6bfb5f Better fix for the DB migration issue.
Matti Hamalainen <ccr@tnsp.org>
parents: 628
diff changeset
159 return FALSE;
620
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
160 }
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
161
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
162 // Add settings to the group
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
163 stDBBeginTransaction($outDB);
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
164 if (stDoAddSettings($inDB, $outDB, $settings, $table, $upgrade, $gid) === false)
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
165 {
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
166 echo "\nFailed to add settings to group '".$groupID."'\n";
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
167 return FALSE;
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
168 }
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
169 stDBCommitTransaction($outDB);
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
170 echo "\n";
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
171 }
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
172
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
173 return TRUE;
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
174 }
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
175
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
176
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
177 function stAddSettingsNormal($inDB, $outDB, $settings, $table, $upgrade)
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
178 {
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
179 stDBBeginTransaction($outDB);
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
180 echo ($upgrade ? "Adding settings to" : "Upgrading settings in")." '".$table."' table.";
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
181 $status = stDoAddSettings($inDB, $outDB, $settings, $table, $upgrade);
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
182 echo "\n";
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
183 if ($status !== FALSE)
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
184 stDBCommitTransaction($outDB);
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
185 return $status;
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
186 }
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
187
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
188
519
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
189 function stAddTestData($outDB)
85
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
190 {
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
191 global $siteTestData;
519
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
192
85
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
193 echo "Adding test data.\n";
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
194
519
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
195 $status = TRUE;
581
Matti Hamalainen <ccr@tnsp.org>
parents: 577
diff changeset
196 stDBBeginTransaction($outDB);
519
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
197
85
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
198 foreach ($siteTestData as $table => $data)
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
199 {
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
200 echo " - ".$table."...\n";
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
201 if (count($data) >= 3)
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
202 {
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
203 for ($n = 2; $n < count($data); $n++)
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
204 {
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
205 $arr = array_merge(
519
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
206 array($outDB, "INSERT INTO ".$table." (".$data[0].") VALUES (".$data[1].")"),
85
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
207 $data[$n]);
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
208
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
209 $sql = call_user_func_array('stDBPrepareSQL', $arr);
519
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
210 if (stDBExecSQL($outDB, $sql) === false)
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
211 {
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
212 $status = false;
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
213 break;
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
214 }
85
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
215 }
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
216 }
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
217 else
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
218 {
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
219 echo " Invalid table / data definition.\n";
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
220 }
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
221 }
519
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
222
581
Matti Hamalainen <ccr@tnsp.org>
parents: 577
diff changeset
223 stDBCommitTransaction($outDB);
519
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
224 return $status;
85
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
225 }
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
226
499
408effd4e9ea Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
227
504
a491865e0684 Implement tables migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 503
diff changeset
228 function stGetSQLTypeParam($dbh, $def, $value)
a491865e0684 Implement tables migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 503
diff changeset
229 {
a491865e0684 Implement tables migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 503
diff changeset
230 switch (substr($def, 0, 3))
a491865e0684 Implement tables migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 503
diff changeset
231 {
a491865e0684 Implement tables migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 503
diff changeset
232 case "INT":
a491865e0684 Implement tables migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 503
diff changeset
233 return intval($value);
a491865e0684 Implement tables migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 503
diff changeset
234
a491865e0684 Implement tables migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 503
diff changeset
235 case "VAR":
a491865e0684 Implement tables migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 503
diff changeset
236 case "TEX":
a491865e0684 Implement tables migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 503
diff changeset
237 return $dbh->quote($value);
a491865e0684 Implement tables migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 503
diff changeset
238
a491865e0684 Implement tables migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 503
diff changeset
239 default: die("Unknown type ".$col[1].".\n");
a491865e0684 Implement tables migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 503
diff changeset
240 }
a491865e0684 Implement tables migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 503
diff changeset
241 }
a491865e0684 Implement tables migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 503
diff changeset
242
519
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
243
551
bed441ced78e Improve migration and upgrade procedures.
Matti Hamalainen <ccr@tnsp.org>
parents: 549
diff changeset
244 function stMigrateTables($inDB, $outDB, $upgrade, $excluded)
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
245 {
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
246 global $sqlTables;
519
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
247
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
248 echo "Migrating tables...\n";
519
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
249 $status = TRUE;
553
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
250 $dbName = $outDB->getAttribute(PDO::ATTR_DRIVER_NAME);
581
Matti Hamalainen <ccr@tnsp.org>
parents: 577
diff changeset
251 stDBBeginTransaction($outDB);
519
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
252
553
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
253 foreach ($sqlTables as $table => $schema)
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
254 {
553
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
255 // Do not migrate excluded tables (or excluded tables with old names)
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
256 $sequences = array();
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
257 $inTable = $upgrade ? stUpgradeMap("table", $table) : $table;
620
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
258 if ($inTable === FALSE || in_array($inTable, $excluded) || in_array($table, $excluded))
553
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
259 continue;
504
a491865e0684 Implement tables migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 503
diff changeset
260
553
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
261 // Process each row of the input table
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
262 echo " - '".$table."' ";
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
263 foreach (stDBExecSQL($inDB, "SELECT * FROM ".$inTable) as $row)
504
a491865e0684 Implement tables migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 503
diff changeset
264 {
553
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
265 // Convert to new schema, as needed
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
266 $avals = array();
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
267 $acols = array();
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
268
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
269 foreach ($schema as $col)
504
a491865e0684 Implement tables migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 503
diff changeset
270 {
553
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
271 // If input has schema column, add it to output
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
272 if (isset($row[$col[0]]))
504
a491865e0684 Implement tables migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 503
diff changeset
273 {
553
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
274 $avals[] = stGetSQLTypeParam($outDB, $col[1], $row[$col[0]]);
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
275 $acols[] = $col[0];
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
276 }
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
277
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
278 // If the column should have autoincrement, add it to sequences
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
279 if (in_array("AUTOINCREMENT", $col))
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
280 $sequences[] = $col[0];
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
281 }
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
282
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
283 $sql = "INSERT INTO ".$table." (".implode(",", $acols).") VALUES (".implode(",", $avals).")";
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
284 if (stDBExecSQL($outDB, $sql) === false)
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
285 {
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
286 $status = FALSE;
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
287 break;
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
288 }
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
289 echo ".";
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
290 }
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
291
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
292 // Perform some specialities here
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
293 switch ($dbName)
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
294 {
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
295 case "pgsql":
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
296 // For PostgreSQL we need to update sequences
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
297 foreach ($sequences as $seq)
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
298 {
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
299 if (($max_id = stDBFetchSQLColumn($outDB, "SELECT MAX(".$seq.") FROM ".$table)) !== false &&
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
300 ($seq_id = stDBFetchSQLColumn($outDB, "SELECT nextval('".$table."_".$seq."_seq')")) !== false &&
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
301 ($seq_id <= $max_id))
540
ab08640b4302 Improve database upgrade code.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
302 {
553
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
303 echo "SEQFIX";
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
304 stDBExecSQL($outDB, "SELECT setval('".$table."_".$seq."_seq', ".$max_id.")");
540
ab08640b4302 Improve database upgrade code.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
305 }
504
a491865e0684 Implement tables migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 503
diff changeset
306 }
553
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
307 break;
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
308 }
504
a491865e0684 Implement tables migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 503
diff changeset
309
553
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
310 echo "\n";
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
311 }
519
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
312
581
Matti Hamalainen <ccr@tnsp.org>
parents: 577
diff changeset
313 stDBCommitTransaction($outDB);
519
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
314
6aa9356ead52 Improve migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 518
diff changeset
315 return $status;
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
316 }
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
317
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
318
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
319 function stSetDBPermissions($spec)
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
320 {
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
321 if (substr($spec, 0, 7) == "sqlite:")
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
322 {
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
323 $filename = substr($spec, 7);
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
324 echo
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
325 "NOTICE! It seems you have SQLite database in use, changing permission ".
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
326 "of the target file '".$filename."' to 0600, for security. You may have to ".
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
327 "loosen up that for the things to actually work, but be careful. Having your ".
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
328 "database world-readable in the web is NOT good.\n";
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
329
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
330 if (chmod($filename, 0600) === FALSE)
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
331 {
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
332 echo "ERROR! Could not set permissions of '".$filename."!\n";
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
333 }
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
334 }
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
335 }
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
336
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
337
36
ace577c92a32 Comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
338 //
ace577c92a32 Comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
339 // Main program starts
ace577c92a32 Comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
340 //
8
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
341 if ($argc < 2)
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
342 {
482
e0fd4c7ae8fb Update commandline help, remove the commandline option for target database
Matti Hamalainen <ccr@tnsp.org>
parents: 479
diff changeset
343 echo
529
ddbc84031a7b Rename createdb.php to managedb.php
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
344 "ManageDB - Manage FAPWeb SQL database\n".
565
ed2247111fdd Bump copyright years.
Matti Hamalainen <ccr@tnsp.org>
parents: 555
diff changeset
345 "(C) Copyright 2012-2014 ccr/TNSP\n".
482
e0fd4c7ae8fb Update commandline help, remove the commandline option for target database
Matti Hamalainen <ccr@tnsp.org>
parents: 479
diff changeset
346 "\n".
e0fd4c7ae8fb Update commandline help, remove the commandline option for target database
Matti Hamalainen <ccr@tnsp.org>
parents: 479
diff changeset
347 "Usage: ".$argv[0]." <mode> [args]\n".
8
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
348 "Where mode is one of following:\n".
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
349 "\n".
525
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
350 " new <dbspec> Create a new database with given PDO spec\n".
8
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
351 " or default to the one in mconfig.inc.php\n".
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
352 "\n".
525
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
353 " test <dbspec> Like new, but add initial test data.\n".
199
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
354 "\n".
525
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
355 " upgrade <input_dbspec> <output_dbspec>\n".
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
356 " Upgrade current database, if possible.\n".
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
357 " Output to new database (DO NOT USE SAME as current!)\n".
516
664915ff8520 Add 'migrate' option.
Matti Hamalainen <ccr@tnsp.org>
parents: 515
diff changeset
358 "\n".
525
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
359 " migrate <input_dbspec> <output_dbspec>\n".
516
664915ff8520 Add 'migrate' option.
Matti Hamalainen <ccr@tnsp.org>
parents: 515
diff changeset
360 " Like upgrade, but no version check. Creates\n".
664915ff8520 Add 'migrate' option.
Matti Hamalainen <ccr@tnsp.org>
parents: 515
diff changeset
361 " a copy of the database to the output spec.\n".
8
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
362 "\n";
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
363 exit;
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
364 }
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
365
541
e5a088dd3a4b Validate current table schema before actions.
Matti Hamalainen <ccr@tnsp.org>
parents: 540
diff changeset
366 // Validate tables data
e5a088dd3a4b Validate current table schema before actions.
Matti Hamalainen <ccr@tnsp.org>
parents: 540
diff changeset
367 $errors = FALSE;
e5a088dd3a4b Validate current table schema before actions.
Matti Hamalainen <ccr@tnsp.org>
parents: 540
diff changeset
368 foreach ($sqlTables as $table => $data)
e5a088dd3a4b Validate current table schema before actions.
Matti Hamalainen <ccr@tnsp.org>
parents: 540
diff changeset
369 {
e5a088dd3a4b Validate current table schema before actions.
Matti Hamalainen <ccr@tnsp.org>
parents: 540
diff changeset
370 if (strtolower($table) != $table)
e5a088dd3a4b Validate current table schema before actions.
Matti Hamalainen <ccr@tnsp.org>
parents: 540
diff changeset
371 {
e5a088dd3a4b Validate current table schema before actions.
Matti Hamalainen <ccr@tnsp.org>
parents: 540
diff changeset
372 echo "Invalid table definition '".$table."', table name must be lower case.\n";
e5a088dd3a4b Validate current table schema before actions.
Matti Hamalainen <ccr@tnsp.org>
parents: 540
diff changeset
373 $errors = TRUE;
e5a088dd3a4b Validate current table schema before actions.
Matti Hamalainen <ccr@tnsp.org>
parents: 540
diff changeset
374 }
e5a088dd3a4b Validate current table schema before actions.
Matti Hamalainen <ccr@tnsp.org>
parents: 540
diff changeset
375
e5a088dd3a4b Validate current table schema before actions.
Matti Hamalainen <ccr@tnsp.org>
parents: 540
diff changeset
376 foreach ($data as $def)
e5a088dd3a4b Validate current table schema before actions.
Matti Hamalainen <ccr@tnsp.org>
parents: 540
diff changeset
377 if (strtolower($def[0]) != $def[0])
e5a088dd3a4b Validate current table schema before actions.
Matti Hamalainen <ccr@tnsp.org>
parents: 540
diff changeset
378 {
e5a088dd3a4b Validate current table schema before actions.
Matti Hamalainen <ccr@tnsp.org>
parents: 540
diff changeset
379 echo "Invalid table definition '".$table."', column key '".$def[0]."' must be lower case.\n";
e5a088dd3a4b Validate current table schema before actions.
Matti Hamalainen <ccr@tnsp.org>
parents: 540
diff changeset
380 $errors = TRUE;
e5a088dd3a4b Validate current table schema before actions.
Matti Hamalainen <ccr@tnsp.org>
parents: 540
diff changeset
381 }
e5a088dd3a4b Validate current table schema before actions.
Matti Hamalainen <ccr@tnsp.org>
parents: 540
diff changeset
382 }
e5a088dd3a4b Validate current table schema before actions.
Matti Hamalainen <ccr@tnsp.org>
parents: 540
diff changeset
383
e5a088dd3a4b Validate current table schema before actions.
Matti Hamalainen <ccr@tnsp.org>
parents: 540
diff changeset
384 if ($errors)
e5a088dd3a4b Validate current table schema before actions.
Matti Hamalainen <ccr@tnsp.org>
parents: 540
diff changeset
385 die("Errors in table definitions.\n");
e5a088dd3a4b Validate current table schema before actions.
Matti Hamalainen <ccr@tnsp.org>
parents: 540
diff changeset
386
8
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
387
85
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
388 // Act according to specified command
489
aa65fe63072e Rename a variable.
Matti Hamalainen <ccr@tnsp.org>
parents: 482
diff changeset
389 $addTestData = FALSE;
551
bed441ced78e Improve migration and upgrade procedures.
Matti Hamalainen <ccr@tnsp.org>
parents: 549
diff changeset
390 $upgrade = FALSE;
bed441ced78e Improve migration and upgrade procedures.
Matti Hamalainen <ccr@tnsp.org>
parents: 549
diff changeset
391
27
b27204653615 Work on adding support for entry submission.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
392 switch (stCArgLC(1))
8
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
393 {
85
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
394 case "test":
489
aa65fe63072e Rename a variable.
Matti Hamalainen <ccr@tnsp.org>
parents: 482
diff changeset
395 $addTestData = TRUE;
85
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
396
8
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
397 case "new":
525
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
398 // Try to connect to database
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
399 if (($inSpec = stCArg(2)) === false)
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
400 die("No PDO database spec specified.\n");
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
401
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
402 if (($inDB = stConnectSQLDBSpec($inSpec)) === false)
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
403 die("Could not connect to SQL database '".$inSpec."'.\n");
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
404
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
405 echo "Using database spec '".$inSpec."'.\n";
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
406
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
407 // Create tables, add defaults
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
408 if (stCreateTables($inDB, FALSE))
139
75cf14ee99a7 More work on party information system.
Matti Hamalainen <ccr@tnsp.org>
parents: 123
diff changeset
409 {
620
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
410 stAddSettingsNormal($inDB, $inDB, $dbMetaData, "dbmeta", FALSE, FALSE);
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
411 stAddSettingsGroup($inDB, $inDB, $siteDefaultSettings, $siteSettingsGroups, "settings_groups", "settings", FALSE);
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
412 stAddSettingsNormal($inDB, $inDB, $siteDisplayVars, "display_vars", FALSE, FALSE);
139
75cf14ee99a7 More work on party information system.
Matti Hamalainen <ccr@tnsp.org>
parents: 123
diff changeset
413 }
27
b27204653615 Work on adding support for entry submission.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
414
489
aa65fe63072e Rename a variable.
Matti Hamalainen <ccr@tnsp.org>
parents: 482
diff changeset
415 if ($addTestData)
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
416 stAddTestData($inDB);
85
50d6c69836a1 Add test data mode for database creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
417
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
418 stSetDBPermissions($inSpec);
8
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
419 break;
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
420
199
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
421 case "upgrade":
551
bed441ced78e Improve migration and upgrade procedures.
Matti Hamalainen <ccr@tnsp.org>
parents: 549
diff changeset
422 $upgrade = TRUE;
bed441ced78e Improve migration and upgrade procedures.
Matti Hamalainen <ccr@tnsp.org>
parents: 549
diff changeset
423
516
664915ff8520 Add 'migrate' option.
Matti Hamalainen <ccr@tnsp.org>
parents: 515
diff changeset
424 case "migrate":
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
425 //
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
426 // Attempt to upgrade database
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
427 //
525
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
428 if ($argc < 4)
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
429 die("Usage: ".$argv[0]." upgrade <input_dbspec> <output_dbspec>\n");
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
430
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
431 $inSpec = stCArg(2);
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
432 $outSpec = stCArg(3);
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
433
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
434 if ($outSpec == $inSpec)
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
435 die("The input and output databases CAN NOT BE SAME.\nBe VERY CAREFUL to not accidentally specify same db!\n");
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
436
620
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
437 echo "Using INPUT database spec '".$inSpec."'.\n";
525
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
438
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
439 if (($inDB = stConnectSQLDBSpec($inSpec)) === false)
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
440 die("Could not connect to SQL database '".$inSpec."'.\n");
d7302b3a479e Change database tool commandline a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
441
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
442 // Check the current version first ...
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
443 if (($currVersion = stGetDBMeta($inDB, "dbVersion")) === FALSE)
199
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
444 $currVersion = -1;
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
445
516
664915ff8520 Add 'migrate' option.
Matti Hamalainen <ccr@tnsp.org>
parents: 515
diff changeset
446 if ($currVersion == $dbVersion && stCArgLC(1) == "upgrade")
199
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
447 {
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
448 echo "Database is already version ".$dbVersion.", no upgrading needed.\n";
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
449 }
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
450 else
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
451 {
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
452 // Okay, we shall create an upgraded version ..
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
453 if (($outDB = stConnectSQLDBSpec($outSpec)) === false)
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
454 die("Could not connect to SQL database '".$outSpec."'.\n");
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
455
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
456 echo "Database at version ".$currVersion.", upgrading to ".$dbVersion."\n";
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
457 echo "Using OUTPUT database spec '".$outSpec."'.\n";
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
458
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
459 // Possibly bail out incompatible upgrades here
199
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
460
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
461 // Create tables
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
462 if (!stCreateTables($outDB, TRUE))
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
463 exit;
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
464
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
465 // Migrate data from setting tables ..
620
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
466 if (!stAddSettingsGroup($inDB, $outDB, $siteDefaultSettings, $siteSettingsGroups, "settings_groups", "settings", TRUE))
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
467 exit;
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
468
620
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
469 if (!stAddSettingsNormal($inDB, $outDB, $siteDisplayVars, "display_vars", TRUE))
199
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
470 exit;
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
471
620
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
472 stAddSettingsNormal($inDB, $outDB, $dbMetaData, "dbmeta", TRUE);
199
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
473
503
3232f682f0d2 Change how table schemas are defined, in preparation for future database
Matti Hamalainen <ccr@tnsp.org>
parents: 502
diff changeset
474 // Migrate other tables
629
894a4a6bfb5f Better fix for the DB migration issue.
Matti Hamalainen <ccr@tnsp.org>
parents: 628
diff changeset
475 if (!stMigrateTables($inDB, $outDB, $upgrade, array("settings", "display_vars", "dbmeta", "settings_groups")))
503
3232f682f0d2 Change how table schemas are defined, in preparation for future database
Matti Hamalainen <ccr@tnsp.org>
parents: 502
diff changeset
476 exit;
553
f13e7a513db1 Fix sequences after database upgrade or migration.
Matti Hamalainen <ccr@tnsp.org>
parents: 552
diff changeset
477
616
36e614bf864f Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 590
diff changeset
478 // Set new database version
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
479 echo "Setting dbVersion.\n";
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
480 stSetDBMeta($outDB, "dbVersion", $dbVersion);
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
481 echo "Upgrade complete.\n";
199
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
482
502
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
483 stSetDBPermissions($inSpec);
d2a38070e18f Partially refactor database upgrading .. not finished yet, and the
Matti Hamalainen <ccr@tnsp.org>
parents: 500
diff changeset
484 stSetDBPermissions($outSpec);
199
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
485 }
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
486 break;
dbc6f214b825 Work on a database upgrade function.
Matti Hamalainen <ccr@tnsp.org>
parents: 185
diff changeset
487
27
b27204653615 Work on adding support for entry submission.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
488 default:
b27204653615 Work on adding support for entry submission.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
489 echo "ERROR! Invalid operation mode '".stCArg(1)."'.\n";
b27204653615 Work on adding support for entry submission.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
490 break;
8
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
491 }
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
492
28
bf567e6402bf Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
493
bf567e6402bf Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
494 //
bf567e6402bf Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
495 // Clean up permissions
bf567e6402bf Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
496 //
620
8444fe96120e Implement setting groups for admin interface settings. This is just the
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
497 foreach (array("managedb.php", "wanktool.php") as $filename)
28
bf567e6402bf Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
498 {
630
b84fc6e7847e Suppress PHP errors.
Matti Hamalainen <ccr@tnsp.org>
parents: 629
diff changeset
499 if (@chmod($filename, 0700) === FALSE)
28
bf567e6402bf Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
500 {
490
505f46b8bb14 Separate variable from string flow.
Matti Hamalainen <ccr@tnsp.org>
parents: 489
diff changeset
501 echo "ERROR! Could not set permissions for '".$filename."'!\n";
28
bf567e6402bf Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
502 }
bf567e6402bf Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
503 }
bf567e6402bf Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
504
8
4c5f651aa107 Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
505 ?>