diff --git a/db/migrate.php b/db/migrate.php new file mode 100644 index 0000000..5c58844 --- /dev/null +++ b/db/migrate.php @@ -0,0 +1,47 @@ +connect_error) { + break; + } + fwrite(STDERR, "Waiting for database ($i/30): {$conn->connect_error}\n"); + sleep(2); +} + +if (!$conn || $conn->connect_error) { + fwrite(STDERR, "Database connection failed.\n"); + exit(1); +} + +$conn->set_charset('utf8mb4'); +$sql = file_get_contents($schema); + +if (!$conn->multi_query($sql)) { + fwrite(STDERR, "Migration failed: {$conn->error}\n"); + exit(1); +} + +do { + if ($result = $conn->store_result()) { + $result->free(); + } + if ($conn->more_results() && !$conn->next_result()) { + fwrite(STDERR, "Migration failed: {$conn->error}\n"); + exit(1); + } +} while ($conn->more_results()); + +echo "Database migration completed.\n"; diff --git a/docker-compose.yml b/docker-compose.yml index 8e17bd7..ec0d1a8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -33,23 +33,8 @@ services: DB_USER: webgis DB_PASSWORD: webgis_password DB_NAME: webgis_project - depends_on: - migrate: - condition: service_completed_successfully - - migrate: - build: - context: . - dockerfile_inline: | - FROM mariadb:10.11 - COPY db/schema.sql /schema.sql - environment: - DB_HOST: db - DB_USER: webgis - DB_PASSWORD: webgis_password - DB_NAME: webgis_project command: > - sh -c "mariadb --host=db --user=webgis --password=webgis_password webgis_project < /schema.sql" + bash -lc "php /var/www/html/db/migrate.php && apache2-foreground" depends_on: db: condition: service_healthy