public function testHasChanges() { $this->assertEquals(0, count($this->m->getMigrations())); $this->assertTrue($this->m->hasChanges()); $this->assertTrue($this->m->save()); $this->assertEquals(1, count($this->m->getMigrations())); $this->assertFalse($this->m->hasChanges()); $this->m = new Migration(new TestChanged(), $this->migrationPath); $this->m->setName('Test'); $this->assertTrue($this->m->hasChanges()); sleep(1); $this->assertTrue($this->m->save()); $this->assertEquals(2, count($this->m->getMigrations())); }
public function actionMigrate($module, $model, $name = '', $db = null) { $className = strtr("\\Modules\\{module}\\Models\\{model}", ['{module}' => ucfirst($module), '{model}' => ucfirst($model)]); if (class_exists($className) === false) { echo "Model not found in namespace: " . $className . PHP_EOL; exit(1); } $path = Alias::get('App.Modules.' . ucfirst($module) . '.Migrations'); if (!is_dir($path)) { echo "Migrations not found" . PHP_EOL; die(1); } $isUp = $this->isToUpMigration($module, $model, $name, $db); $modelInstance = new $className(); $migration = new Migration($modelInstance, $path); $migrationModel = ModelMigration::objects()->last()->get(['module' => ucfirst($module), 'model' => ucfirst($model)]); $migrations = $migration->getMigrations(); if (!$isUp) { rsort($migrations); } foreach ($migrations as $migrationFile) { $fileName = basename($migrationFile); list($name, $timestamp) = explode('_', str_replace('.json', '', $fileName)); if ($migrationModel) { if ($isUp && $migrationModel->timestamp >= $timestamp) { continue; } else { if ($migrationModel->timestamp > $timestamp) { continue; } } } $migrationClassName = strtr("\\Modules\\{module}\\Migrations\\{migration}", ['{module}' => ucfirst($module), '{migration}' => str_replace('.json', '', $fileName)]); include_once $path . DIRECTORY_SEPARATOR . str_replace('.json', '', $fileName) . '.php'; /** @var $migrationInstance \Mindy\Query\Migration */ $migrationInstance = new $migrationClassName(); echo "Process: " . str_replace('.json', '', $fileName) . PHP_EOL; if ($isUp) { $migrationInstance->up(); ModelMigration::objects()->create(['module' => ucfirst($module), 'model' => ucfirst($model), 'timestamp' => $timestamp]); } else { $migrationInstance->down(); ModelMigration::objects()->delete(['module' => ucfirst($module), 'model' => ucfirst($model), 'timestamp' => $timestamp]); } if (!empty($name) && $fileName == $name) { break; } } echo "Complete" . PHP_EOL; }