1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ConsoleTools\Controller; |
4
|
|
|
|
5
|
|
|
use Zend\Mvc\Controller\AbstractActionController; |
6
|
|
|
use Zend\Console\ColorInterface as Color; |
7
|
|
|
use Zend\Console\Adapter\AdapterInterface as Console; |
8
|
|
|
use Zend\Console\Exception\RuntimeException; |
9
|
|
|
use Zend\Db\Adapter\Adapter; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Controller for console operations as clean and apply schema |
14
|
|
|
* Clean current schema and apply sql file |
15
|
|
|
* |
16
|
|
|
* @author V.Leontiev <[email protected]> |
17
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
18
|
|
|
* @since php 5.6 or higher |
19
|
|
|
* @see https://github.com/newage/console-tools |
20
|
|
|
*/ |
21
|
|
|
class SchemaController extends AbstractActionController |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Folder to schema |
26
|
|
|
* Folder must be there for use completion-bash |
27
|
|
|
*/ |
28
|
|
|
const FOLDER_SCHEMA = '/data/schema/'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Sql folder destination |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $schemaFolder = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Clean current schema and apply sql dump file |
39
|
|
|
*/ |
40
|
|
|
public function cleanAction() |
41
|
|
|
{ |
42
|
|
|
$request = $this->getRequest(); |
43
|
|
|
$console = $this->getServiceLocator()->get('console'); |
44
|
|
|
$adapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter'); |
45
|
|
|
|
46
|
|
|
if (!$console instanceof Console) { |
47
|
|
|
throw new RuntimeException('Cannot obtain console adapter. Are we running in a console?'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$schemaName = $adapter->getCurrentSchema(); |
51
|
|
|
$dumpFileName = $request->getParam('file'); |
52
|
|
|
|
53
|
|
|
if (empty($dumpFileName)) { |
54
|
|
|
$dumpFileName = 'clean.sql'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$dumpFileDir = $this->getSchemaFolder(); |
58
|
|
|
$dumpFilePath = $dumpFileDir . $dumpFileName; |
59
|
|
|
if (!is_dir($dumpFileDir)) { |
60
|
|
|
mkdir($dumpFileDir, 0777); |
61
|
|
|
file_put_contents($dumpFilePath, '#Your default sql dump'); |
62
|
|
|
$console->writeLine('Created folder for clean schema: ' . $dumpFileName, Color::GREEN); |
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!file_exists($dumpFilePath)) { |
67
|
|
|
$console->writeLine('File don\'t exists: ' . $dumpFileName, Color::RED); |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$sql = 'DROP SCHEMA `' . $schemaName . '`'; |
72
|
|
|
$adapter->query($sql, Adapter::QUERY_MODE_EXECUTE); |
73
|
|
|
$console->writeLine('Droped current schema: ' . $schemaName, Color::GREEN); |
74
|
|
|
|
75
|
|
|
$sql = 'CREATE SCHEMA `' . $schemaName . '`'; |
76
|
|
|
$adapter->query($sql, Adapter::QUERY_MODE_EXECUTE); |
77
|
|
|
$console->writeLine('Created current schema: ' . $schemaName, Color::GREEN); |
78
|
|
|
|
79
|
|
|
$sql = 'USE `' . $schemaName . '`'; |
80
|
|
|
$adapter->query($sql, Adapter::QUERY_MODE_EXECUTE); |
81
|
|
|
|
82
|
|
|
$sql = file_get_contents($dumpFilePath); |
83
|
|
|
$adapter->query($sql, Adapter::QUERY_MODE_EXECUTE); |
84
|
|
|
$console->writeLine('Applied dump file: ' . $dumpFileName, Color::GREEN); |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get migration folder from config file |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
protected function getSchemaFolder() |
93
|
|
|
{ |
94
|
|
|
if ($this->schemaFolder === null) { |
95
|
|
|
$this->schemaFolder = getcwd() . self::FOLDER_SCHEMA; |
96
|
|
|
} |
97
|
|
|
return $this->schemaFolder; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|