-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBMigrationRevision.php
127 lines (100 loc) · 3.31 KB
/
DBMigrationRevision.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
<?php
namespace intelworx\dbvc;
/**
* Description of DBMigrationRevision
*
* @author JosephT
*/
abstract class DBMigrationRevision {
protected $id;
protected $title;
protected $file;
protected $checksum;
public function __construct($id, $file, $checksum, $title = null) {
$this->id = intval($id);
$this->title = $title;
$this->file = $file;
$this->checksum = $checksum;
}
public function getVersionId() {
return $this->id;
}
public function getId() {
return $this->id;
}
public function getTitle() {
return $this->title;
}
public function getFile() {
return $this->file;
}
public function getChecksum() {
return $this->checksum;
}
public function __toString() {
return '[' . $this->file . ':md5:' . $this->checksum . ']';
}
public function toFieldArray() {
return [$this->id, $this->file, $this->checksum, $this->title, date('Y-m-d H:i:s')];
}
/**
*
* @param array $line
* @return DBMigrationRevisionStored
*/
public static function fromLine($line) {
//$id, $file, $checksum, $file = null
return new DBMigrationRevisionStored($line[0], $line[1], $line[2], $line[3]);
}
/**
* Creates revision object from a file name.
* @param string $file
* @return null|DBMigrationRevisionFile
*/
public static function fromFile($file) {
$baseName = basename($file);
$matches = array();
if (!preg_match('/^(\d+)_(.+)\.sql$/i', $baseName, $matches)) {
DBMigration::el('skipping file', $file, 'it doesn\'t match pattern');
return null;
}
$version = $matches[1];
$description = preg_replace('/[^a-zA-Z0-9]+/', ' ', $matches[2]);
return new DBMigrationRevisionFile($version, $baseName, self::computeChecksum($file), $description, file_get_contents($file));
}
/**
* Computes checksum of a file
* @param string $file the path to the file
* @return string
*/
public static function computeChecksum($file) {
return md5_file($file);
}
}
class DBMigrationRevisionFile extends DBMigrationRevision {
private $baseChecksum;
public function __construct($id, $file, $checksum, $title = null, $content = '') {
parent::__construct($id, $file, $checksum, $title);
$this->baseChecksum = self::computeBaseChecksum($content);
}
public function matches(DBMigrationRevisionStored $storedRevision) {
return $this->file === $storedRevision->file &&
($this->checksum === $storedRevision->checksum || in_array($storedRevision->checksum, $this->baseChecksum));
}
/**
* Computes checksums without Line ending complications.
* @param string $content content of the original file.
* @return string
*/
public static function computeBaseChecksum($content) {
static $eol = '/\r\n|\r|\n/';
$checksums = array();
//covert to linux EOL
$checksums[] = md5(preg_replace($eol, "\n", $content));
$checksums[] = md5(preg_replace($eol, "\r\n", $content));
$checksums[] = md5(preg_replace($eol, "\r", $content));
return $checksums;
}
}
class DBMigrationRevisionStored extends DBMigrationRevision {
}