feat(console): add db:migrate CLI for idempotent core schema updates
Existing systems used to have no automatic mechanism for the db/updates/*.sql idempotent updates after `git pull` — devs had to remember which files were already applied and run new ones manually with `mariadb < ...`. Mirror module:migrate to fix this: - New `bin/console db:migrate` walks db/updates/*.sql in alphabetical order, skipping anything already recorded in the new core_migrations tracking table. Each file runs in its own transaction; failure rolls back so the file is retried on the next run. - New `db:migrate --status` lists applied vs. pending files without running anything. Useful for ops debugging "schema seems stale" symptoms. - module:sync now runs db:migrate as its first step, so the standard post-pull command stays a single invocation. README's 3-step setup is unchanged — module:sync now also covers core schema updates. CoreMigrationRepository owns its own mysqli connection (using the same DB credentials as MintyPHP\DB) and runs raw `$mysqli->query()` instead of going through DB::query's prepared statement path — MariaDB rejects some DDL (e.g. ALTER TABLE … ADD CONSTRAINT CHECK) in the prepared protocol, which the existing 18 db/updates files trip on. ModuleMigrationRepository is left untouched (no module migration uses such DDL today and changing the vendor pattern is out of scope). End-to-end verified: against an existing DB the first run applies all 19 idempotent files as no-ops and records them, second run reports "all updates already applied". Against a freshly init'd DB the same thing happens — no double work, no surprises. All encrypted seed secrets keep decrypting because APP_CRYPTO_KEY is unchanged. Tests: tests/Console/CoreCommandsTest covers success/failure/status output shapes against a FakeModuleRunner (mirror of the existing ModuleCommandsTest pattern). 5 stale baseline entries in phpstan-baseline removed (covered by the new @api annotation on the test fixture class). Docs: CLAUDE.md and docs/reference-cli-commands.md document the new command + --status flag; docs/howto-fehlerbehebung.md gains a "schema seems stale after git pull" section pointing at db:migrate --status. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
127
core/Repository/Database/CoreMigrationRepository.php
Normal file
127
core/Repository/Database/CoreMigrationRepository.php
Normal file
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
namespace MintyPHP\Repository\Database;
|
||||
|
||||
use MintyPHP\DB;
|
||||
use MintyPHP\DBError;
|
||||
use mysqli;
|
||||
|
||||
/**
|
||||
* Manages the core_migrations tracking table and applies raw SQL statements.
|
||||
*
|
||||
* Owns its own mysqli connection (using the same credentials as MintyPHP\DB)
|
||||
* so we can run statements via mysqli->query() — bypassing prepared statements,
|
||||
* which MariaDB rejects for some DDL (e.g. ALTER TABLE … ADD CONSTRAINT CHECK).
|
||||
*
|
||||
* All SQL for core migration tracking lives here (strict layering).
|
||||
*/
|
||||
final class CoreMigrationRepository implements CoreMigrationRepositoryInterface
|
||||
{
|
||||
private ?mysqli $mysqli = null;
|
||||
|
||||
public function ensureTrackingTable(): void
|
||||
{
|
||||
$this->executeStatement(
|
||||
'CREATE TABLE IF NOT EXISTS `core_migrations` (
|
||||
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
`filename` VARCHAR(255) NOT NULL,
|
||||
`applied_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uq_core_migration` (`filename`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, true> Filenames already applied, keyed by filename
|
||||
*/
|
||||
public function getAppliedFilenames(): array
|
||||
{
|
||||
$applied = [];
|
||||
$result = $this->connection()->query('SELECT filename FROM core_migrations');
|
||||
if ($result === false) {
|
||||
throw new DBError('Failed to query core_migrations: ' . $this->connection()->error);
|
||||
}
|
||||
while (($row = $result->fetch_assoc()) !== null) {
|
||||
$filename = trim((string) ($row['filename'] ?? ''));
|
||||
if ($filename !== '') {
|
||||
$applied[$filename] = true;
|
||||
}
|
||||
}
|
||||
$result->free();
|
||||
return $applied;
|
||||
}
|
||||
|
||||
public function recordApplied(string $filename): void
|
||||
{
|
||||
$stmt = $this->connection()->prepare('INSERT INTO core_migrations (filename) VALUES (?)');
|
||||
if ($stmt === false) {
|
||||
throw new DBError('Failed to prepare INSERT into core_migrations: ' . $this->connection()->error);
|
||||
}
|
||||
$stmt->bind_param('s', $filename);
|
||||
$stmt->execute();
|
||||
if ($stmt->errno !== 0) {
|
||||
$error = $stmt->error;
|
||||
$stmt->close();
|
||||
throw new DBError('Failed to insert into core_migrations: ' . $error);
|
||||
}
|
||||
$stmt->close();
|
||||
}
|
||||
|
||||
public function beginTransaction(): void
|
||||
{
|
||||
$this->executeStatement('START TRANSACTION');
|
||||
}
|
||||
|
||||
public function commit(): void
|
||||
{
|
||||
$this->executeStatement('COMMIT');
|
||||
}
|
||||
|
||||
public function rollback(): void
|
||||
{
|
||||
$this->executeStatement('ROLLBACK');
|
||||
}
|
||||
|
||||
public function executeStatement(string $sql): void
|
||||
{
|
||||
$mysqli = $this->connection();
|
||||
$result = $mysqli->query($sql);
|
||||
if ($result === false) {
|
||||
throw new DBError($mysqli->error);
|
||||
}
|
||||
// For SELECT-style results, free the result set so the connection
|
||||
// doesn't get stuck in "commands out of sync" state.
|
||||
if ($result instanceof \mysqli_result) {
|
||||
$result->free();
|
||||
}
|
||||
}
|
||||
|
||||
private function connection(): mysqli
|
||||
{
|
||||
if ($this->mysqli instanceof mysqli) {
|
||||
return $this->mysqli;
|
||||
}
|
||||
|
||||
// Reuse the MintyPHP\DB connection settings (public statics) so we
|
||||
// hit the same database the rest of the app uses, but with our own
|
||||
// socket — the vendor DB class always uses prepared statements,
|
||||
// which MariaDB rejects for some DDL.
|
||||
$args = array_filter(
|
||||
[DB::$host, DB::$username, DB::$password, DB::$database, DB::$port, DB::$socket],
|
||||
static fn ($v): bool => $v !== null
|
||||
);
|
||||
|
||||
$reflect = new \ReflectionClass(mysqli::class);
|
||||
$mysqli = $reflect->newInstanceArgs($args);
|
||||
if ($mysqli->connect_errno !== 0) {
|
||||
throw new DBError('Failed to connect to database for core migrations: ' . $mysqli->connect_error);
|
||||
}
|
||||
if (!$mysqli->set_charset('utf8mb4')) {
|
||||
throw new DBError('Failed to set charset for core migrations connection: ' . $mysqli->error);
|
||||
}
|
||||
|
||||
$this->mysqli = $mysqli;
|
||||
return $mysqli;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace MintyPHP\Repository\Database;
|
||||
|
||||
/** Contract for core migration tracking: table creation, applied-file queries, and statement execution. */
|
||||
interface CoreMigrationRepositoryInterface
|
||||
{
|
||||
public function ensureTrackingTable(): void;
|
||||
|
||||
/**
|
||||
* @return array<string, true>
|
||||
*/
|
||||
public function getAppliedFilenames(): array;
|
||||
|
||||
public function recordApplied(string $filename): void;
|
||||
|
||||
public function beginTransaction(): void;
|
||||
|
||||
public function commit(): void;
|
||||
|
||||
public function rollback(): void;
|
||||
|
||||
public function executeStatement(string $sql): void;
|
||||
}
|
||||
Reference in New Issue
Block a user