1 <?php # vim:ts=2:sw=2:et:
3 /* For licensing and copyright terms, see the file named LICENSE */
5 if (function_exists('date_default_timezone_set')) {
6 date_default_timezone_set('UTC');
8 ini_set('memory_limit', -1);
10 include_once dirname(__FILE__) . '/../inc/common.php';
12 if (count($argv) != 2) {
13 echo "Usage: bin/data-move.php 'pgsql:dbname=foo;user=bar'\n";
15 Reads your existing mtrack database (uses DSN information in config.ini).
16 Connects to the specified DSN and creates the mtrack schema, then populates
17 it from your existing mtrack database.
26 $sdsn = MTrackConfig::get('core', 'dsn');
28 $sdsn = 'sqlite:' . MTrackConfig::get('core', 'dblocation');
30 $sdb = new PDO($sdsn);
31 $sdb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
33 $ddb = new PDO($ddsn);
34 $ddb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
35 $ddb->exec("set client_encoding='utf-8'");
37 $driver = $ddb->getAttribute(PDO::ATTR_DRIVER_NAME);
38 $adapter_class = "MTrackDBSchema_$driver";
39 $adapter = new $adapter_class;
41 $adapter->setDB($ddb);
42 $vers = $adapter->determineVersion();
46 $ddb->beginTransaction();
50 foreach (glob(dirname(__FILE__) . '/../schema/*.xml') as $filename) {
51 $latest = new MTrackDBSchema($filename);
52 $schemata[$latest->version] = $latest;
55 echo "Applying schema version $latest->version\n";
56 foreach ($latest->tables as $t) {
57 $adapter->createTable($t);
61 foreach ($t->fields as $f) {
62 if ($f->type == 'autoinc') {
63 // Omit: we want the database to set this for us, otherwise
64 // sequence numbers won't get populated!
69 $pull = 'select ' . join(',', $names) . ' from ' . $t->name;
71 $push = 'insert into ' . $t->name . '(' . join(',', $names) . ') values (' .
72 str_repeat('?,', count($names) - 1) . '?)';
74 $sq = $sdb->query($pull, PDO::FETCH_NUM);
76 $dq = $ddb->prepare($push);
78 foreach ($sq as $row) {
79 /* postgres has stronger data validation requirements;
82 foreach ($names as $i => $fname) {
83 $f = $t->fields[$fname];
88 if (isset($f->nullable) && $f->nullable == '0') {
94 $dq->bindValue(1+$i, $row[$i]);
98 if (isset($f->nullable) && $f->nullable == '0') {
99 $dq->bindValue(1+$i, 0.0);
101 $dq->bindValue(1+$i, null);
104 /* avoid converting to double here, for sake of precision.
105 * Also, somehow we have commas in our data... fix that */
106 $dq->bindValue(1+$i, str_replace(",", ".", $row[$i]));
110 if (is_null($row[$i])) {
111 $dq->bindValue(1+$i, null);
113 $stm = fopen('php://memory', 'r+');
114 fwrite($stm, $row[$i]);
116 $dq->bindValue(1+$i, $stm, PDO::PARAM_LOB);
121 /* CSV import could have injected non-UTF-8 data */
122 if (is_null($row[$i])) {
123 $dq->bindValue(1+$i, null);
125 $enc = mb_detect_encoding($row[$i], 'UTF-8,ISO-8859-1');
126 if ($enc != 'UTF-8') {
128 mb_convert_encoding($row[$i], 'UTF-8', $enc));
130 $dq->bindValue(1+$i, $row[$i]);
137 } catch (Exception $e) {
142 foreach ($send as $d) {
143 echo bin2hex($d) . "\n";
149 if (isset($latest->post[$driver])) {
150 $ddb->exec($latest->post[$driver]);
152 $vers = $latest->version;
157 $ddb->exec('delete from mtrack_schema');
158 $q = $ddb->prepare('insert into mtrack_schema (version) values (?)');
159 $q->execute(array($latest->version));