-
Notifications
You must be signed in to change notification settings - Fork 11.1k
/
MySqlSchemaState.php
99 lines (88 loc) · 2.95 KB
/
MySqlSchemaState.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
<?php
namespace Illuminate\Database\Schema;
class MySqlSchemaState extends SchemaState
{
/**
* Dump the database's schema into a file.
*
* @param string $path
* @return void
*/
public function dump($path)
{
$this->makeProcess(
$this->baseDumpCommand().' --routines --result-file=$LARAVEL_LOAD_PATH --no-data'
)->mustRun($this->output, array_merge($this->baseVariables($this->connection->getConfig()), [
'LARAVEL_LOAD_PATH' => $path,
]));
$this->removeAutoIncrementingState($path);
$this->appendMigrationData($path);
}
/**
* Remove the auto-incrementing state from the given schema dump.
*
* @param string $path
* @return void
*/
protected function removeAutoIncrementingState(string $path)
{
$this->files->put($path, preg_replace(
'/\s+AUTO_INCREMENT=[0-9]+/iu',
'',
$this->files->get($path)
));
}
/**
* Append the migration data to the schema dump.
*
* @param string $path
* @return void
*/
protected function appendMigrationData(string $path)
{
with($process = $this->makeProcess(
$this->baseDumpCommand().' migrations --no-create-info --skip-extended-insert --skip-routines --compact'
))->mustRun(null, array_merge($this->baseVariables($this->connection->getConfig()), [
//
]));
$this->files->append($path, $process->getOutput());
}
/**
* Load the given schema file into the database.
*
* @param string $path
* @return void
*/
public function load($path)
{
$process = $this->makeProcess('mysql --host=$LARAVEL_LOAD_HOST --port=$LARAVEL_LOAD_PORT --user=$LARAVEL_LOAD_USER --password=$LARAVEL_LOAD_PASSWORD --database=$LARAVEL_LOAD_DATABASE < $LARAVEL_LOAD_PATH');
$process->mustRun(null, array_merge($this->baseVariables($this->connection->getConfig()), [
'LARAVEL_LOAD_PATH' => $path,
]));
}
/**
* Get the base dump command arguments for MySQL as a string.
*
* @return string
*/
protected function baseDumpCommand()
{
return 'mysqldump --set-gtid-purged=OFF --skip-add-drop-table --skip-add-locks --skip-comments --skip-set-charset --tz-utc --host=$LARAVEL_LOAD_HOST --port=$LARAVEL_LOAD_PORT --user=$LARAVEL_LOAD_USER --password=$LARAVEL_LOAD_PASSWORD $LARAVEL_LOAD_DATABASE';
}
/**
* Get the base variables for a dump / load command.
*
* @param array $config
* @return array
*/
protected function baseVariables(array $config)
{
return [
'LARAVEL_LOAD_HOST' => $config['host'],
'LARAVEL_LOAD_PORT' => $config['port'],
'LARAVEL_LOAD_USER' => $config['username'],
'LARAVEL_LOAD_PASSWORD' => $config['password'],
'LARAVEL_LOAD_DATABASE' => $config['database'],
];
}
}