forked from GEWIS/gewisweb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimportdb.php
More file actions
82 lines (67 loc) · 2.27 KB
/
importdb.php
File metadata and controls
82 lines (67 loc) · 2.27 KB
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
<?php
/**
* This is a separate script that copies the GEWIS Report Database to the Web
* database.
*
* It is a simple PostgreSQL to MySQL copy script.
*/
// connections
$config = include 'config/autoload/gewisdb.local.php';
$pgconn = new PDO('pgsql:host=' . $config['host'] . ';dbname=' . $config['dbname']
. ';user=' . $config['user'] . ';password=' . $config['password']);
$pgconn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$doctrineConf = include 'config/autoload/doctrine.local.php';
$params = $doctrineConf['doctrine']['connection']['orm_default']['params'];
$myconn = new PDO(
'mysql:host=' . $params['host'] . ';dbname=' . $params['dbname'],
$params['user'],
$params['password']
);
$myconn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/* which tables to sync */
$tables = [
'Address',
'BoardMember',
'Decision',
'MailingList',
'Meeting',
'Member',
'members_mailinglists',
'Organ',
'OrganMember',
'organs_subdecisions',
'SubDecision'
];
// to not trip up InnoDB
$myconn->query('SET foreign_key_checks = 0');
$myconn->query('START TRANSACTION');
foreach ($tables as $table) {
$query = "SELECT * FROM $table";
$stmt = $pgconn->query($query);
echo "Table $table\n";
$truncate = "TRUNCATE TABLE $table";
$myconn->query($truncate);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$fields = '(' . implode(', ', array_keys($row)) . ')';
$values = '(' . implode(', ', array_map(function ($a) {
return ':' . $a;
}, array_keys($row))) . ')';
$data = $row;
// see if we can fetch about 256 more rows (gigantic speed increase)
for ($i = 0; $i < 256 && ($row2 = $stmt->fetch(PDO::FETCH_ASSOC)); $i++) {
$values .= ', (' . implode(', ', array_map(function ($a) use ($i) {
return ':' . $a . $i;
}, array_keys($row2))) . ')';
foreach ($row2 as $key => $value) {
$data[$key . $i] = $value;
}
}
$sql = "INSERT INTO $table $fields VALUES $values";
$stmtt = $myconn->prepare($sql);
$stmtt->execute($data);
echo '.';
}
echo "\n\n";
}
$myconn->query('COMMIT');
$myconn->query('SET foreign_key_checks = 1');