diff --git a/.github/workflows/build-release.yml b/.github/workflows/build-release.yml index cee3a0b03..600084c14 100644 --- a/.github/workflows/build-release.yml +++ b/.github/workflows/build-release.yml @@ -179,9 +179,6 @@ jobs: exit 1 } - - name: Initialize database - run: docker exec -i mysql mysql -u root -proot ospos < app/Database/database.sql - - name: Run PHPUnit tests env: CI_ENVIRONMENT: testing diff --git a/app/Database/Migrations/20170501000000_initial_schema.php b/app/Database/Migrations/20170501000000_initial_schema.php new file mode 100644 index 000000000..aa1935a0c --- /dev/null +++ b/app/Database/Migrations/20170501000000_initial_schema.php @@ -0,0 +1,83 @@ +db->listTables(); + + if (!empty($tables)) { + // Database already populated - skip initial schema + // This is an existing installation upgrading from older version + return; + } + + // Fresh install - load initial schema + helper('migration'); + $schemaPath = APPPATH . 'Database/database.sql'; + + if (file_exists($schemaPath)) { + $sql = file_get_contents($schemaPath); + + // Split by semicolons and execute each statement + // Handle multi-line statements and DELIMITER changes + $this->executeSqlScript($sql); + } + } + + /** + * Execute SQL script handling multi-line statements + */ + private function executeSqlScript(string $sql): void + { + // Remove comments + $sql = preg_replace('/--.*$/m', '', $sql); + $sql = preg_replace('/\/\*.*?\*\//s', '', $sql); + + // Split by semicolons at end of lines + $statements = preg_split('/;\s*\n/', $sql); + + foreach ($statements as $statement) { + $statement = trim($statement); + if (!empty($statement)) { + try { + $this->db->query($statement); + } catch (\Exception $e) { + // Log but continue - some statements may fail on duplicate indexes etc. + log_message('debug', 'Migration statement warning: ' . $e->getMessage()); + } + } + } + } + + /** + * Revert a migration step. + * Cannot revert initial schema - would lose all data. + */ + public function down(): void + { + // Cannot safely revert initial schema + // Would require dropping all tables which would lose all data + $this->db->query('SET FOREIGN_KEY_CHECKS = 0'); + + foreach ($this->db->listTables() as $table) { + $this->db->query('DROP TABLE IF EXISTS `' . $table . '`'); + } + + $this->db->query('SET FOREIGN_KEY_CHECKS = 1'); + } +} \ No newline at end of file