/** * Run DB migrations from migration .sql files * @return int count of applied migrations */ public function runMigrations() { // Check we havy any if (!file_exists(DIR_MIGRATIONS)) { return 0; } // Check we have DB structure and any migration applied $migrated_files = []; if (SQL::getTables()) { // Have DB already? $migrations = new MigrationEntityRepository(); $migrated_files = $migrations->getPairs('filename', 'filename'); } $existing_files = FileSystem::scanDirs(DIR_MIGRATIONS); if (!$existing_files) { return 0; } $to_migrate = []; // Filters migrations that are already done foreach ($existing_files as $file) { if (isset($migrated_files[$file['name']])) { continue; } $to_migrate[] = $file['name']; } // Run new migrations foreach ($to_migrate as $file) { $this->runMigrationFile($file); $this->setMigrationFileAsCompleted($file); } return count($to_migrate); }