diff --git a/TODO.md b/TODO.md index a19f76b..e164357 100644 --- a/TODO.md +++ b/TODO.md @@ -6,3 +6,5 @@ - [x] Fix wrong `require_once` depth in `public/admin/actions/page.php` (`../../` → `../../../`) - [x] Fix same path depth bug in `formulaire.php` and `publish.php` - [x] Fix CSV import: imported theses not visible on public site (is_published defaulted to 0, access_type_id never set) +- [x] Fix DB routing: local dev (php -S) auto-uses test.db, production (nginx/fpm) uses posterg.db — no env var needed +- [x] migrate.sh auto-initialises DB from schema when absent/empty; existing DBs untouched diff --git a/scripts/migrate.sh b/scripts/migrate.sh index fe2c4b6..48c34b3 100755 --- a/scripts/migrate.sh +++ b/scripts/migrate.sh @@ -63,9 +63,13 @@ migrate_db() { local db="$1" local label="$2" - if [ ! -f "$db" ]; then - echo " [$label] database not found, skipping: $db" - return + # Auto-create from schema only when the file is absent or truly empty (no tables) + local table_count + table_count=$(sqlite3 "$db" "SELECT COUNT(*) FROM sqlite_master WHERE type='table';" 2>/dev/null || echo 0) + if [ "$table_count" -eq 0 ]; then + echo " [$label] initialising from schema…" + sqlite3 "$db" < "$REPO_ROOT/storage/schema.sql" + echo " [$label] schema applied." fi # Ensure tracking table exists diff --git a/src/config.php b/src/config.php index 0a117c6..87939ab 100644 --- a/src/config.php +++ b/src/config.php @@ -8,27 +8,33 @@ // Database paths relative to repository root define('DB_ROOT', __DIR__ . '/..'); -// Test database (used in development) +// Local dev database (php -S, never committed) define('DB_TEST_PATH', DB_ROOT . '/storage/test.db'); -// Production database (used on server) +// Production database (nginx/php-fpm on server) define('DB_PROD_PATH', DB_ROOT . '/storage/posterg.db'); /** * Determine which database to use. * - * Always defaults to the production database. - * Set DB_ENV=test explicitly to use the test database. + * - php built-in server (php -S …) → storage/test.db (local dev) + * - everything else (nginx/fpm) → storage/posterg.db (production) * - * export DB_ENV=test # use storage/test.db - * export DB_ENV=prod # use storage/posterg.db (default) - * - * The old file-existence auto-detection has been removed: a leftover - * test.db on a developer machine no longer silently redirects all - * requests to test data. + * The DB_ENV env-var can still override either way: + * DB_ENV=test → force test.db + * DB_ENV=prod → force posterg.db */ function getDatabasePath(): string { - if (getenv('DB_ENV') === 'test') { + $env = getenv('DB_ENV'); + if ($env === 'test') { + return DB_TEST_PATH; + } + if ($env === 'prod') { + return DB_PROD_PATH; + } + + // Auto-detect: php built-in CLI server == local development + if (php_sapi_name() === 'cli-server') { return DB_TEST_PATH; } @@ -36,8 +42,8 @@ function getDatabasePath(): string { } /** - * Check if running in test/development mode + * Check if running in local dev mode */ -function isTestMode() { +function isTestMode(): bool { return getDatabasePath() === DB_TEST_PATH; } diff --git a/storage/posterg.db b/storage/posterg.db index d1e8816..61c33a5 100644 Binary files a/storage/posterg.db and b/storage/posterg.db differ diff --git a/storage/test.db b/storage/test.db deleted file mode 100644 index a5901c5..0000000 Binary files a/storage/test.db and /dev/null differ