-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSchemaManager.php
More file actions
executable file
·164 lines (141 loc) · 4.6 KB
/
SchemaManager.php
File metadata and controls
executable file
·164 lines (141 loc) · 4.6 KB
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
<?php
namespace Foolz\FoolFrame\Model;
use Foolz\Plugin\Hook;
class SchemaManager
{
/**
* The database connection
*
* @var \Doctrine\DBAL\Connection
*/
protected $connection;
/**
* Set a prefix to ignore all the tables that aren't prefixed by this
*
* @var string
*/
protected $prefix = null;
/**
* The schema that holds what the code explains
*
* @var \Doctrine\DBAL\Schema\Schema
*/
protected $coded_schema;
/**
* The current database schema
*
* @var \Doctrine\DBAL\Schema\Schema
*/
protected $database_schema;
/**
* Creates a schema manager for testing if the modules are up to date
*
* @param \Doctrine\DBAL\Connection $connection The doctrine database connection
* @param string $prefix The prefix used for the database (will ignore any other prefix)
* @param array $prefixes_ignored Prefix in the database that should be ignored between the ones with the selected $prefix. Do not prepend $prefix.
*
* @return \Foolz\FoolFrame\Model\SchemaManager A new SchemaManager
*/
public static function forge(\Doctrine\DBAL\Connection $connection, $prefix = '', $prefixes_ignored = [])
{
$new = new static();
$new->connection = $connection;
$new->prefix = $prefix;
$sm = $new->connection->getSchemaManager();
$tables = $sm->listTables();
// get rid of the tables that don't have the same prefix
if ($prefix !== null) {
foreach ($tables as $key => $table) {
if (strpos($table->getName(), $new->prefix) !== 0) {
unset($tables[$key]);
}
}
}
// get more prefixes ignored
$prefixes_ignored = Hook::forge('Foolz\FoolFrame\Model\SchemaManager::forge#var.ignorePrefix')
->setObject($new)
->setParam('prefixes_ignored', $prefixes_ignored)
->execute()
->get($prefixes_ignored);
// get rid of the ignored prefixes (in example ff_plugin_)
if (count($prefixes_ignored)) {
foreach ($tables as $key => $table) {
foreach ($prefixes_ignored as $prefix_ignored) {
if (strpos($table->getName(), $new->prefix.$prefix_ignored) === 0) {
unset($tables[$key]);
}
}
}
}
// get more tables ignored
$tables = Hook::forge('Foolz\FoolFrame\Model\SchemaManager::forge#var.tables')
->setObject($new)
->setParam('tables', $tables)
->execute()
->get($tables);
// create a database "how it is now"
$new->database_schema = new \Doctrine\DBAL\Schema\Schema($tables, [], $sm->createSchemaConfig());
// make an empty schema
$new->coded_schema = new \Doctrine\DBAL\Schema\Schema([], [], $sm->createSchemaConfig());
return $new;
}
/**
* Returns the Doctrine Connection
*
* @return \Doctrine\DBAL\Connection
*/
public function getConnection()
{
return $this->connection;
}
/**
* Get the prefix for the database
*
* @return null|string null if there's no prefix, string if set
*/
public function getPrefix()
{
return $this->prefix;
}
/**
* Returns the database schema that has to be edited to the destination one
*
* @return \Doctrine\DBAL\Schema\Schema Returns an empty (or edited) schema for you to edit
*/
public function getCodedSchema()
{
return $this->coded_schema;
}
/**
* Returns the live database schema
*
* @return \Doctrine\DBAL\Schema\Schema Returns the schema that was generated out of the database
*/
public function getDatabaseSchema()
{
return $this->database_schema;
}
/**
* Returns the array of changes that will take place if commit is run
*
* @return array An array with SQL queries that correspond to the changes
*/
public function getChanges()
{
return $this->coded_schema->getMigrateFromSql(
$this->database_schema,
$this->connection->getSchemaManager()->getDatabasePlatform()
);
}
/**
* Runs the changes to the schema
*/
public function commit()
{
$this->connection->beginTransaction();
foreach ($this->getChanges() as $sql) {
$this->connection->query($sql);
}
$this->connection->commit();
}
}