1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PSFS\services; |
4
|
|
|
|
5
|
|
|
use Propel\Generator\Config\GeneratorConfig; |
6
|
|
|
use Propel\Generator\Manager\MigrationManager; |
7
|
|
|
use Propel\Generator\Model\Database; |
8
|
|
|
use Propel\Generator\Model\IdMethod; |
9
|
|
|
use PSFS\base\Logger; |
10
|
|
|
use PSFS\base\types\SimpleService; |
11
|
|
|
use PSFS\base\types\traits\Generator\PropelHelperTrait; |
12
|
|
|
use Symfony\Component\Console\Output\Output; |
13
|
|
|
|
14
|
|
|
class MigrationService extends SimpleService |
15
|
|
|
{ |
16
|
|
|
use PropelHelperTrait; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string $module |
21
|
|
|
* @param string $path |
22
|
|
|
* @return array |
23
|
|
|
* @throws \PSFS\base\exception\GeneratorException |
24
|
|
|
*/ |
25
|
|
|
public function getConnectionManager(string $module, string $path): array |
26
|
|
|
{ |
27
|
|
|
$modulePath = str_replace(CORE_DIR . DIRECTORY_SEPARATOR, '', $path . $module); |
28
|
|
|
$generatorConfig = $this->getConfigGenerator($modulePath); |
29
|
|
|
|
30
|
|
|
$manager = new MigrationManager(); |
31
|
|
|
$manager->setGeneratorConfig($generatorConfig); |
32
|
|
|
$manager->setSchemas($this->getSchemas( |
33
|
|
|
$generatorConfig->getSection('paths')['schemaDir'], |
34
|
|
|
$generatorConfig->getSection('generator')['recursive']) |
35
|
|
|
); |
36
|
|
|
$manager->setConnections($generatorConfig->getBuildConnections()); |
37
|
|
|
$manager->setMigrationTable($generatorConfig->getConfigProperty('migrations.tableName')); |
|
|
|
|
38
|
|
|
$manager->setWorkingDirectory($generatorConfig->getSection('paths')['migrationDir']); |
39
|
|
|
return [$manager, $generatorConfig]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param MigrationManager $manager |
44
|
|
|
* @param GeneratorConfig $generatorConfig |
45
|
|
|
* @param Database $appDatabase |
46
|
|
|
* @param array $connections |
47
|
|
|
* @param bool $debugLogger |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
public function checkSourceDatabase(MigrationManager $manager, GeneratorConfig $generatorConfig, Database $appDatabase, array $connections, bool $debugLogger = false): array |
51
|
|
|
{ |
52
|
|
|
$name = $appDatabase->getName(); |
53
|
|
|
$params = $connections[$name] ?? []; |
54
|
|
|
if (!$params) { |
55
|
|
|
Logger::log(sprintf('<info>No connection configured for database "%s"</info>', $name)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($debugLogger) { |
59
|
|
|
Logger::log(sprintf('Connecting to database "%s" using DSN "%s"', $name, $params['dsn'])); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
list($conn, $platform) = $this->getPlatformAndConnection($manager, $name, $generatorConfig); |
63
|
|
|
|
64
|
|
|
$appDatabase->setPlatform($platform); |
65
|
|
|
|
66
|
|
|
if ($platform && !$platform->supportsMigrations()) { |
67
|
|
|
Logger::log(sprintf('Skipping database "%s" since vendor "%s" does not support migrations', $name, $platform->getDatabaseType())); |
68
|
|
|
return [null, 0]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$additionalTables = []; |
72
|
|
|
foreach ($appDatabase->getTables() as $table) { |
73
|
|
|
if ($table->getSchema() && $table->getSchema() != $appDatabase->getSchema()) { |
74
|
|
|
$additionalTables[] = $table; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$database = new Database($name); |
79
|
|
|
$database->setPlatform($platform); |
80
|
|
|
$database->setSchema($appDatabase->getSchema()); |
81
|
|
|
$database->setDefaultIdMethod(IdMethod::NATIVE); |
82
|
|
|
|
83
|
|
|
$parser = $generatorConfig->getConfiguredSchemaParser($conn, $name); |
84
|
|
|
$nbTables = $parser->parse($database, $additionalTables); |
85
|
|
|
|
86
|
|
|
if ($debugLogger) { |
87
|
|
|
Logger::log(sprintf('%d tables found in database "%s"', $nbTables, $name), Output::VERBOSITY_VERBOSE); |
88
|
|
|
} |
89
|
|
|
return [$database, $nbTables]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param MigrationManager $manager |
94
|
|
|
* @param string|null $name |
95
|
|
|
* @param GeneratorConfig $generatorConfig |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
public function getPlatformAndConnection(MigrationManager $manager, ?string $name, GeneratorConfig $generatorConfig): array |
99
|
|
|
{ |
100
|
|
|
$conn = $manager->getAdapterConnection($name); |
|
|
|
|
101
|
|
|
$platform = $generatorConfig->getConfiguredPlatform($conn, $name); |
102
|
|
|
return [$conn, $platform]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param MigrationManager $manager |
107
|
|
|
* @param array $migrationsUp |
108
|
|
|
* @param array $migrationsDown |
109
|
|
|
* @param GeneratorConfig $generatorConfig |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
|
|
public function generateMigrationFile(MigrationManager $manager, array $migrationsUp, array $migrationsDown, GeneratorConfig $generatorConfig): void |
113
|
|
|
{ |
114
|
|
|
$timestamp = time(); |
115
|
|
|
$migrationFileName = $manager->getMigrationFileName($timestamp); |
116
|
|
|
$migrationClassBody = $manager->getMigrationClassBody($migrationsUp, $migrationsDown, $timestamp); |
117
|
|
|
|
118
|
|
|
$file = $generatorConfig->getSection('paths')['migrationDir'] . DIRECTORY_SEPARATOR . $migrationFileName; |
119
|
|
|
file_put_contents($file, $migrationClassBody); |
120
|
|
|
|
121
|
|
|
Logger::log(sprintf('"%s" file successfully created.', $file)); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|