|
@@ -16,17 +16,14 @@ use think\Model;
|
|
|
|
|
|
|
|
class Migrate
|
|
class Migrate
|
|
|
{
|
|
{
|
|
|
|
|
+ protected ?array $migrations = null;
|
|
|
|
|
+ protected AdapterInterface $adapter;
|
|
|
|
|
+ protected App $app;
|
|
|
private string $moduleName;
|
|
private string $moduleName;
|
|
|
-
|
|
|
|
|
private string $path;
|
|
private string $path;
|
|
|
- protected ?array $migrations = null;
|
|
|
|
|
private $input;
|
|
private $input;
|
|
|
private $output;
|
|
private $output;
|
|
|
|
|
|
|
|
- protected AdapterInterface $adapter;
|
|
|
|
|
-
|
|
|
|
|
- protected App $app;
|
|
|
|
|
-
|
|
|
|
|
public function __construct(App $app, string $moduleName)
|
|
public function __construct(App $app, string $moduleName)
|
|
|
{
|
|
{
|
|
|
$this->app = $app;
|
|
$this->app = $app;
|
|
@@ -37,65 +34,6 @@ class Migrate
|
|
|
$this->output = null;
|
|
$this->output = null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function install(): array
|
|
|
|
|
- {
|
|
|
|
|
- $migrations = $this->getMigrations();
|
|
|
|
|
- $versions = $this->getVersions();
|
|
|
|
|
- $currentVersion = $this->getCurrentVersion();
|
|
|
|
|
- if (empty($versions) && empty($migrations)) {
|
|
|
|
|
- return [];
|
|
|
|
|
- }
|
|
|
|
|
- ksort($migrations);
|
|
|
|
|
- $installVersions = [];
|
|
|
|
|
- foreach ($migrations as $migration) {
|
|
|
|
|
- if ($migration->getVersion() <= $currentVersion) {
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
- if (!in_array($migration->getVersion(), $versions)) {
|
|
|
|
|
- $installVersions[] = $migration->getVersion();
|
|
|
|
|
- $this->executeMigration($migration);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return $installVersions;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public function uninstall(): void
|
|
|
|
|
- {
|
|
|
|
|
- $migrations = $this->getMigrations();
|
|
|
|
|
- $versionLog = $this->getVersionLog();
|
|
|
|
|
- $versions = array_keys($versionLog);
|
|
|
|
|
-
|
|
|
|
|
- ksort($migrations);
|
|
|
|
|
- sort($versions);
|
|
|
|
|
- if (empty($versions)) {
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- krsort($migrations);
|
|
|
|
|
- foreach ($migrations as $migration) {
|
|
|
|
|
- if (in_array($migration->getVersion(), $versions)) {
|
|
|
|
|
- if (isset($versionLog[$migration->getVersion()]) && 0 != $versionLog[$migration->getVersion()]['breakpoint']) {
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
- $this->executeMigration($migration, MigrationInterface::DOWN);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public function getMigrationList(): array
|
|
|
|
|
- {
|
|
|
|
|
- $migrations = $this->getMigrations();
|
|
|
|
|
- MigrationsModel::maker(function (Model $model) {
|
|
|
|
|
- if ($model instanceof MigrationsModel) {
|
|
|
|
|
- $model->setOption('suffix', $this->moduleName);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- $versionLog = MigrationsModel::column('*', 'version');
|
|
|
|
|
- foreach ($migrations as $key => $migration) {
|
|
|
|
|
- $migrations[$key] = $versionLog[$key] ?? ['version'=> $key];
|
|
|
|
|
- }
|
|
|
|
|
- return array_values($migrations);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
protected function getMigrations(): ?array
|
|
protected function getMigrations(): ?array
|
|
|
{
|
|
{
|
|
|
if (null === $this->migrations) {
|
|
if (null === $this->migrations) {
|
|
@@ -157,14 +95,31 @@ class Migrate
|
|
|
return $this->migrations;
|
|
return $this->migrations;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- protected function getVersions()
|
|
|
|
|
|
|
+ public function install(): array
|
|
|
{
|
|
{
|
|
|
- return $this->getAdapter()->getVersions();
|
|
|
|
|
|
|
+ $migrations = $this->getMigrations();
|
|
|
|
|
+ $versions = $this->getVersions();
|
|
|
|
|
+ $currentVersion = $this->getCurrentVersion();
|
|
|
|
|
+ if (empty($versions) && empty($migrations)) {
|
|
|
|
|
+ return [];
|
|
|
|
|
+ }
|
|
|
|
|
+ ksort($migrations);
|
|
|
|
|
+ $installVersions = [];
|
|
|
|
|
+ foreach ($migrations as $migration) {
|
|
|
|
|
+ if ($migration->getVersion() <= $currentVersion) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!in_array($migration->getVersion(), $versions)) {
|
|
|
|
|
+ $installVersions[] = $migration->getVersion();
|
|
|
|
|
+ $this->executeMigration($migration);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return $installVersions;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- protected function getVersionLog()
|
|
|
|
|
|
|
+ protected function getVersions()
|
|
|
{
|
|
{
|
|
|
- return $this->getAdapter()->getVersionLog();
|
|
|
|
|
|
|
+ return $this->getAdapter()->getVersions();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function getAdapter()
|
|
public function getAdapter()
|
|
@@ -290,4 +245,46 @@ class Migrate
|
|
|
$this->getAdapter()
|
|
$this->getAdapter()
|
|
|
->migrated($migration, $direction, date('Y-m-d H:i:s', $startTime), date('Y-m-d H:i:s', time()));
|
|
->migrated($migration, $direction, date('Y-m-d H:i:s', $startTime), date('Y-m-d H:i:s', time()));
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public function uninstall(): void
|
|
|
|
|
+ {
|
|
|
|
|
+ $migrations = $this->getMigrations();
|
|
|
|
|
+ $versionLog = $this->getVersionLog();
|
|
|
|
|
+ $versions = array_keys($versionLog);
|
|
|
|
|
+
|
|
|
|
|
+ ksort($migrations);
|
|
|
|
|
+ sort($versions);
|
|
|
|
|
+ if (empty($versions)) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ krsort($migrations);
|
|
|
|
|
+ foreach ($migrations as $migration) {
|
|
|
|
|
+ if (in_array($migration->getVersion(), $versions)) {
|
|
|
|
|
+ if (isset($versionLog[$migration->getVersion()]) && 0 != $versionLog[$migration->getVersion()]['breakpoint']) {
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ $this->executeMigration($migration, MigrationInterface::DOWN);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected function getVersionLog()
|
|
|
|
|
+ {
|
|
|
|
|
+ return $this->getAdapter()->getVersionLog();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function getMigrationList(): array
|
|
|
|
|
+ {
|
|
|
|
|
+ $migrations = $this->getMigrations();
|
|
|
|
|
+ MigrationsModel::maker(function (Model $model) {
|
|
|
|
|
+ if ($model instanceof MigrationsModel) {
|
|
|
|
|
+ $model->setOption('suffix', $this->moduleName);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ $versionLog = MigrationsModel::column('*', 'version');
|
|
|
|
|
+ foreach ($migrations as $key => $migration) {
|
|
|
|
|
+ $migrations[$key] = $versionLog[$key] ?? ['version' => $key];
|
|
|
|
|
+ }
|
|
|
|
|
+ return array_values($migrations);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|