-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.php
132 lines (107 loc) · 3.74 KB
/
migrate.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
<?php
require_once('config.inc.php');
function query($query) {
global $conn;
$result = mysql_query($query, $conn);
if (!$result) {
if (mysql_error($conn) != 'Query was empty') {
echo "SQL Error: " . mysql_error($conn) . "\n";
mysql_query('ROLLBACK', $conn);
mysql_close($conn);
exit;
}
}
return $result;
}
// Establish connection to database
$conn = mysql_connect(DATABASE_SERVER, DATABASE_USER, DATABASE_PASS);
if (!$conn) {
echo "Error connecting to database: " . mysql_error($conn);
exit;
}
// The database we are connecting to might not exist. If so lets create it.
if(mysql_num_rows(query("SHOW databases LIKE '" . DATABASE_NAME . "'"))==0) {
query('CREATE DATABASE `' . DATABASE_NAME . '` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;');
}
// Then lets connect to it
mysql_select_db(DATABASE_NAME, $conn);
// If the migrations table doesn't exist, lets create it.
if(mysql_num_rows(query("SHOW TABLES LIKE 'migrations'"))==0) {
query('CREATE TABLE `migrations` (`version` int(11) NOT NULL,`file` text NOT NULL,`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP) ENGINE=InnoDB DEFAULT CHARSET=latin1; ');
}
$migrated = Array();
$latest_version = 0;
// Lets query the migrations table to get a list of files already migrated.
$result = query('select * from migrations', $conn);
while($row=mysql_fetch_array($result)) {
$migrated[$row['file']] = true;
if ($row['version'] > $latest_version) {
$latest_version = $row['version']; // Set the version to be the latest.
}
}
// Find all the migration files in the directory and return the sorted.#
$files = array();
$dir = opendir(SQL_FOLDER);
while ($file = readdir($dir)) {
if (($file != '.') && ($file != '..')) {
$file_info = explode('__', $file);
if ((isset($file_info[1])) && (is_numeric($file_info[0]))){
$files[] = $file;
} else {
echo "File name error: " . $file . ". The format should be versin__name (double underscore)";
}
}
}
asort($files);
// Check no files have the same version
$previous_file = '';
foreach ($files as $file) {
$current_file_info = explode('__', $file);
$previous_file_info = explode('__', $previous_file);
if ($previous_file_info[0] == $current_file_info[0]) {
echo $file . " and " . $previous_file . " have the same version";
exit;
}
$previous_file = $file;
}
$new_count = 0;
// Lets now import the sql from all the files
foreach ($files as $file) {
$file_info = explode('__', $file);
// Do we allow out of order file versions?
if (OUT_OF_ORDER == true) {
if (isset($migrated[$file])) {
continue;
}
} else {
if ($file_info[0] <= $latest_version) {
if (!isset($migrated[$file])) {
echo "File " . $file . " is older than version " . $latest_version . " but hasn't been migrated";
exit;
}
continue;
}
}
// Read the content of the SQL file
$sql_file = file_get_contents(SQL_FOLDER . $file);
// Split the entries by the ; char (might be an issue if that char is inside the sql query (into insert data);
$sql_entries = explode(';', $sql_file);
echo "Process file: " . $file . " (" . count($sql_entries) . " changes.\n";
query('BEGIN');
// Loop through the entries of the file. skipping any that are blank (the ; split will cause a [0] to exist on a file with no content)
for ($i = 0; $i < count($sql_entries); $i++) {
if (!empty($sql_entries[$i])) {
query($sql_entries[$i]);
}
}
query('COMMIT');
$latest_version = $file_info[0];
$new_count++;
// As this was successful, lets write this file to the database.
query("INSERT INTO `migrations` (`version`, `file`) VALUES ('" . $file_info[0] . "', '" . $file . "')");
}
// Have we made any changes?
if ($new_count > 0) {
echo $new_count . " files successfully processed.\n";
}
mysql_close($conn);