forked from drfraker/snipe-migrations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnipe.php
200 lines (169 loc) · 5.61 KB
/
Snipe.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
namespace Drfraker\SnipeMigrations;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Artisan;
class Snipe
{
/**
* Make sure the testing database is up to date.
*/
public function importSnapshot()
{
if ($this->usingInMemoryDatabase()) {
// Does not handle in memory databases yet.
return;
}
$this->databaseFileChanges()
? $this->newSnapshot()
: $this->importDatabase();
}
/**
* Determine if an in-memory database is being used.
* This isn't set up yet, and throws an error if the user is using :memory: sqlite.
*
* @return bool
*/
protected function usingInMemoryDatabase()
{
$default = config('database.default');
return config("database.connections.$default.database") === ':memory:';
}
/**
* Determine if there have been migration or (if enabled) seeder file changes since the last time the snapshot was updated.
*
* @return bool
*/
protected function databaseFileChanges()
{
if (! SnipeDatabaseState::$checkedForDatabaseFileChanges) {
$timeSum = config('snipe.seed-database', false)
? $this->migrationFileTimeSum() + $this->seederFileTimeSum()
: $this->migrationFileTimeSum();
if ($hasChanges = $this->databaseFilesHaveChanged($timeSum)) {
file_put_contents(config('snipe.snipefile-location'), $timeSum);
}
SnipeDatabaseState::$checkedForDatabaseFileChanges = true;
return $hasChanges;
}
}
/**
* Generate a new snapshot of the MySql database.
*/
protected function newSnapshot()
{
Artisan::call('migrate:fresh');
// Seed the database if required
if (config('snipe.seed-database', false)) {
Artisan::call('db:seed', [
'--class' => config('snipe.seed-class', 'DatabaseSeeder'),
]);
}
$storageLocation = config('snipe.snapshot-location');
// Store a snapshot of the db after migrations run.
exec("mysqldump -h {$this->getDbHost()} -u {$this->getDbUsername()} --password={$this->getDbPassword()} {$this->getDbName()} > {$storageLocation} 2>/dev/null");
}
/**
* Scan migration files for sum of last modified times.
*
* @return int
*/
protected function migrationFileTimeSum()
{
return collect(app()['migrator']->paths())
->concat([database_path('migrations')])
->map(function ($path) {
return collect(File::allFiles($path))
->sum(function ($file) {
return $file->getMTime();
});
})->sum();
}
/**
* Scan seeder files for sum of last modified times.
*
* @return int
*/
protected function seederFileTimeSum()
{
return collect([database_path('seeds')])
->map(function ($path) {
return collect(File::allFiles($path))
->sum(function ($file) {
return $file->getMTime();
});
})->sum();
}
/**
* Determine if any of the application's migration files have been updated since the last time a snapshot
* was created.
*
* @param $timeSum
* @return bool
*/
protected function databaseFilesHaveChanged($timeSum): bool
{
$snipeFile = config('snipe.snipefile-location');
$storedTimeSum = file_exists($snipeFile) ? file_get_contents($snipeFile) : 0;
return (int) $storedTimeSum !== $timeSum || ! file_exists(config('snipe.snapshot-location'));
}
/**
* Import the snapshot file into the database if it hasn't been imported yet.
*/
protected function importDatabase()
{
if (! SnipeDatabaseState::$importedDatabase) {
$dumpfile = config('snipe.snapshot-location');
exec("mysql -h {$this->getDbHost()} -u {$this->getDbUsername()} --password={$this->getDbPassword()} {$this->getDbName()} < {$dumpfile} 2>/dev/null");
SnipeDatabaseState::$importedDatabase = true;
}
}
/**
* Get the database connection from the config settings.
*
* @return \Illuminate\Config\Repository|mixed
*/
protected function getDatabaseConnection()
{
return config('database.default');
}
/**
* Get the Database host from the config settings.
*
* @return \Illuminate\Config\Repository|mixed
*/
protected function getDbHost()
{
$connection = $this->getDatabaseConnection();
return config("database.connections.{$connection}.host");
}
/**
* Get the Database username from the config settings.
*
* @return \Illuminate\Config\Repository|mixed
*/
protected function getDbUsername()
{
$connection = $this->getDatabaseConnection();
return config("database.connections.{$connection}.username");
}
/**
* Get the database password from the config settings.
*
* @return \Illuminate\Config\Repository|mixed
*/
protected function getDbPassword()
{
$connection = $this->getDatabaseConnection();
return config("database.connections.{$connection}.password");
}
/**
* Get the name of the database from config settings.
*
* @return \Illuminate\Config\Repository|mixed
*/
protected function getDbName()
{
$connection = $this->getDatabaseConnection();
return config("database.connections.{$connection}.database");
}
}