|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace yentu\commands; |
|
4
|
|
|
|
|
5
|
|
|
use clearice\io\Io; |
|
6
|
|
|
use yentu\CodeWriter; |
|
7
|
|
|
use yentu\exceptions\CommandException; |
|
8
|
|
|
use yentu\factories\DatabaseManipulatorFactory; |
|
9
|
|
|
use yentu\Migrations; |
|
10
|
|
|
|
|
11
|
|
|
class Import extends Command implements Reversible |
|
12
|
|
|
{ |
|
13
|
|
|
private $db; |
|
14
|
|
|
private $code; |
|
15
|
|
|
private $hasSchema = false; |
|
16
|
|
|
private $foreignKeys = array(); |
|
17
|
|
|
private $newVersion; |
|
18
|
|
|
private $manipulatorFactory; |
|
19
|
|
|
private $migrations; |
|
20
|
|
|
private $io; |
|
21
|
|
|
|
|
22
|
11 |
|
public function __construct(Migrations $migrations, DatabaseManipulatorFactory $manipulatorFactory, Io $io, CodeWriter $codeWriter) |
|
23
|
|
|
{ |
|
24
|
11 |
|
$this->manipulatorFactory = $manipulatorFactory; |
|
25
|
11 |
|
$this->migrations = $migrations; |
|
26
|
11 |
|
$this->io = $io; |
|
27
|
11 |
|
$this->code = $codeWriter; |
|
28
|
11 |
|
} |
|
29
|
|
|
|
|
30
|
11 |
|
public function run() |
|
31
|
|
|
{ |
|
32
|
11 |
|
$this->db = $this->manipulatorFactory->createManipulator(); |
|
33
|
11 |
|
$files = scandir($this->migrations->getPath("migrations")); |
|
34
|
11 |
|
if (count($files) > 2) { |
|
35
|
3 |
|
throw new CommandException("Cannot run imports. Your migrations directory is not empty"); |
|
36
|
|
|
} |
|
37
|
8 |
|
$description = $this->db->getDescription(); |
|
38
|
|
|
|
|
39
|
8 |
|
$this->code->add('begin()'); |
|
40
|
8 |
|
if (isset($description['schemata'])) { |
|
41
|
8 |
|
$this->importSchemata($description['schemata']); |
|
42
|
|
|
} |
|
43
|
8 |
|
if (isset($description['tables'])) { |
|
44
|
8 |
|
$this->importTables($description['tables']); |
|
45
|
|
|
} |
|
46
|
8 |
|
if (isset($description['views'])) { |
|
47
|
8 |
|
$this->importViews($description['views']); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
8 |
|
$this->importForeignKeys(); |
|
51
|
8 |
|
$this->code->add('->end();'); |
|
52
|
|
|
|
|
53
|
8 |
|
$this->newVersion = date('YmdHis', time()); |
|
54
|
8 |
|
$path = $this->migrations->getPath("migrations/{$this->newVersion}_import.php"); |
|
55
|
8 |
|
file_put_contents($path, $this->code); |
|
56
|
8 |
|
$this->io->output("Created `$path`\n"); |
|
57
|
8 |
|
if (!$this->db->getAssertor()->doesTableExist('yentu_history')) { |
|
58
|
1 |
|
$this->db->createHistory(); |
|
59
|
|
|
} |
|
60
|
8 |
|
$this->db->setVersion($this->newVersion); |
|
61
|
8 |
|
$this->db->disconnect(); |
|
62
|
|
|
|
|
63
|
8 |
|
return $description; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
7 |
|
public function getNewVersion() |
|
67
|
|
|
{ |
|
68
|
7 |
|
return $this->newVersion; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
7 |
|
private function generateSchemaCode($description, $ref = false, $prefix = '') |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
7 |
|
$refprefix = $ref === true ? 'ref' : '->'; |
|
74
|
7 |
|
if ($description["{$prefix}schema"] == false) { |
|
75
|
6 |
|
return "{$refprefix}table('{$description["{$prefix}table"]}')"; |
|
76
|
|
|
} else { |
|
77
|
1 |
|
return "{$refprefix}schema('{$description["{$prefix}schema"]}')->table('{$description["{$prefix}table"]}')"; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
8 |
|
protected function importForeignKeys() |
|
82
|
|
|
{ |
|
83
|
8 |
|
foreach ($this->foreignKeys as $name => $foreignKey) { |
|
84
|
7 |
|
$this->code->add($this->generateSchemaCode($foreignKey)); |
|
85
|
7 |
|
$this->code->addIndent(); |
|
86
|
7 |
|
$this->code->add("->foreignKey('" . implode("','", $foreignKey['columns']) . "')"); |
|
87
|
7 |
|
$reference = $this->generateSchemaCode($foreignKey, true, 'foreign_'); |
|
88
|
7 |
|
$this->code->add("->references({$reference})"); |
|
89
|
7 |
|
$this->code->add("->columns('" . implode("','", $foreignKey['foreign_columns']) . "')"); |
|
90
|
|
|
|
|
91
|
7 |
|
if ($foreignKey['on_delete'] != '') { |
|
92
|
7 |
|
$this->code->add("->onDelete('{$foreignKey['on_delete']}')"); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
7 |
|
if ($foreignKey['on_update'] != '') { |
|
96
|
7 |
|
$this->code->add("->onUpdate('{$foreignKey['on_update']}')"); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
7 |
|
$this->code->add("->name('$name')"); |
|
100
|
7 |
|
$this->code->decreaseIndent(); |
|
101
|
7 |
|
$this->code->ln(); |
|
102
|
|
|
} |
|
103
|
8 |
|
} |
|
104
|
|
|
|
|
105
|
7 |
|
protected function importColumns($columns) |
|
106
|
|
|
{ |
|
107
|
7 |
|
foreach ($columns as $column) { |
|
108
|
7 |
|
$this->code->addNoLn("->column('{$column['name']}')"); |
|
109
|
7 |
|
$this->code->addNoIndent("->type('{$column['type']}')"); |
|
110
|
7 |
|
$this->code->addNoIndent("->nulls(" . ($column['nulls'] === true ? 'true' : 'false') . ")"); |
|
111
|
|
|
|
|
112
|
7 |
|
if ($column['length'] != '') { |
|
113
|
5 |
|
$this->code->addNoIndent("->length({$column['length']})"); |
|
114
|
|
|
} |
|
115
|
7 |
|
if ($column['default'] != '') { |
|
116
|
3 |
|
$this->code->addNoIndent("->defaultValue(\"{$column['default']}\")"); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
7 |
|
$this->code->ln(); |
|
120
|
|
|
} |
|
121
|
7 |
|
} |
|
122
|
|
|
|
|
123
|
8 |
|
protected function importSchemata($schemata) |
|
124
|
|
|
{ |
|
125
|
8 |
|
$this->code->add('// Schemata'); |
|
126
|
|
|
|
|
127
|
8 |
|
if (count($schemata) > 0) { |
|
128
|
1 |
|
$this->hasSchema = true; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
8 |
|
foreach ($schemata as $schema) { |
|
132
|
1 |
|
$this->code->add("->schema('{$schema['name']}')"); |
|
133
|
1 |
|
$this->code->addIndent(); |
|
134
|
1 |
|
$this->importTables($schema['tables']); |
|
135
|
1 |
|
$this->importViews($schema['views']); |
|
136
|
1 |
|
$this->code->decreaseIndent(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
8 |
|
$this->hasSchema = false; |
|
140
|
8 |
|
} |
|
141
|
|
|
|
|
142
|
8 |
|
protected function importViews($views) |
|
143
|
|
|
{ |
|
144
|
8 |
|
foreach ($views as $view) { |
|
145
|
4 |
|
$definition = sprintf('->definition("%s")', str_replace('"', '\"', $view['definition'])); |
|
146
|
4 |
|
$this->code->add("->view('{$view['name']}')$definition"); |
|
147
|
4 |
|
$this->code->ln(); |
|
148
|
|
|
} |
|
149
|
8 |
|
} |
|
150
|
|
|
|
|
151
|
8 |
|
protected function importTables($tables) |
|
152
|
|
|
{ |
|
153
|
8 |
|
foreach ($tables as $table) { |
|
154
|
7 |
|
if ($table['name'] == 'yentu_history') |
|
155
|
7 |
|
continue; |
|
156
|
|
|
|
|
157
|
7 |
|
$this->code->add("->table('{$table['name']}')"); |
|
158
|
7 |
|
$this->code->addIndent(); |
|
159
|
|
|
|
|
160
|
7 |
|
$this->importColumns($table['columns']); |
|
161
|
7 |
|
$this->importConstraints('primaryKey', $table['primary_key']); |
|
162
|
|
|
|
|
163
|
7 |
|
if ($table['auto_increment']) { |
|
164
|
6 |
|
$this->code->add("->autoIncrement()"); |
|
165
|
|
|
} |
|
166
|
7 |
|
$this->importConstraints('unique', $table['unique_keys']); |
|
167
|
7 |
|
$this->importConstraints('index', $table['indices']); |
|
168
|
|
|
|
|
169
|
7 |
|
$this->code->decreaseIndent(); |
|
170
|
7 |
|
$this->code->ln(); |
|
171
|
|
|
|
|
172
|
7 |
|
if (count($table['foreign_keys']) > 0) { |
|
173
|
7 |
|
$this->foreignKeys = array_merge($this->foreignKeys, $table['foreign_keys']); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
8 |
|
} |
|
177
|
|
|
|
|
178
|
7 |
|
protected function importConstraints($type, $constraints) |
|
179
|
|
|
{ |
|
180
|
7 |
|
foreach ($constraints as $name => $constraint) { |
|
181
|
7 |
|
$constraint = implode("','", $constraint['columns']); |
|
182
|
7 |
|
$this->code->add("->$type('$constraint')->name('$name')"); |
|
183
|
|
|
} |
|
184
|
7 |
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function reverseActions() |
|
187
|
|
|
{ |
|
188
|
|
|
|
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
} |
|
192
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.