From 233a3308e2c11335eda5d02e0a89df4758a9b886 Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Mon, 15 Jul 2024 14:07:03 -0400 Subject: [PATCH 1/3] Add failing test --- .../Integration/Database/SchemaStateTest.php | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/Integration/Database/SchemaStateTest.php diff --git a/tests/Integration/Database/SchemaStateTest.php b/tests/Integration/Database/SchemaStateTest.php new file mode 100644 index 000000000000..019e2d3f1cc4 --- /dev/null +++ b/tests/Integration/Database/SchemaStateTest.php @@ -0,0 +1,48 @@ +getSchemaBuilder()->createDatabase( + DB::connection('sqlite')->getConfig('database') + ); + } + + public function testSchemaDumpOnSqlite() + { + if ($this->driver !== 'sqlite') { + $this->markTestSkipped('Test requires a SQLite connection.'); + } + + $connection = DB::connection('sqlite'); + + $this->createSqliteDatabase(); + + $this->app['files']->ensureDirectoryExists(database_path('schema')); + + $connection->statement('CREATE TABLE users(id integer primary key autoincrement not null, email varchar not null, name varchar not null);'); + $connection->statement('CREATE UNIQUE INDEX users_email_unique on users (email);'); + $connection->statement('INSERT INTO users (email, name) VALUES ("taylor@laravel.com", "Taylor Otwell");'); + $connection->statement('PRAGMA optimize;'); + + $connection->getSchemaState()->dump($connection, database_path('schema/sqlite-schema.sql')); + + $this->assertFileDoesNotContains([ + 'sqlite_sequence', + 'sqlite_stat1', + 'sqlite_stat4', + ], 'database/schema/sqlite-schema.sql'); + } +} From 6f395d97e1b710fd1c2302c14f7b3379bf4f4d1a Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Mon, 15 Jul 2024 14:07:25 -0400 Subject: [PATCH 2/3] Remove all `CREATE TABLE sqlite_*` statements from schema dump --- src/Illuminate/Database/Schema/SqliteSchemaState.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Database/Schema/SqliteSchemaState.php b/src/Illuminate/Database/Schema/SqliteSchemaState.php index 4b66542923e4..05bdb118ab74 100644 --- a/src/Illuminate/Database/Schema/SqliteSchemaState.php +++ b/src/Illuminate/Database/Schema/SqliteSchemaState.php @@ -21,9 +21,8 @@ public function dump(Connection $connection, $path) // ])); - $migrations = collect(preg_split("/\r\n|\n|\r/", $process->getOutput()))->filter(function ($line) { - return stripos($line, 'sqlite_sequence') === false && - strlen($line) > 0; + $migrations = collect(preg_split("/\r\n|\n|\r/", $process->getOutput()))->reject(function ($line) { + return str_starts_with($line, 'CREATE TABLE sqlite_'); })->all(); $this->files->put($path, implode(PHP_EOL, $migrations).PHP_EOL); From 2351a8fb15ef236d19d41e0103995277c7a90922 Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Mon, 15 Jul 2024 14:08:47 -0400 Subject: [PATCH 3/3] Wip --- tests/Integration/Database/SchemaStateTest.php | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/tests/Integration/Database/SchemaStateTest.php b/tests/Integration/Database/SchemaStateTest.php index 019e2d3f1cc4..22e0031609b4 100644 --- a/tests/Integration/Database/SchemaStateTest.php +++ b/tests/Integration/Database/SchemaStateTest.php @@ -13,13 +13,6 @@ class SchemaStateTest extends DatabaseTestCase 'database/schema', ]; - protected function createSqliteDatabase(): void - { - DB::connection('sqlite')->getSchemaBuilder()->createDatabase( - DB::connection('sqlite')->getConfig('database') - ); - } - public function testSchemaDumpOnSqlite() { if ($this->driver !== 'sqlite') { @@ -27,16 +20,15 @@ public function testSchemaDumpOnSqlite() } $connection = DB::connection('sqlite'); - - $this->createSqliteDatabase(); - - $this->app['files']->ensureDirectoryExists(database_path('schema')); + $connection->getSchemaBuilder()->createDatabase($connection->getConfig('database')); $connection->statement('CREATE TABLE users(id integer primary key autoincrement not null, email varchar not null, name varchar not null);'); $connection->statement('CREATE UNIQUE INDEX users_email_unique on users (email);'); $connection->statement('INSERT INTO users (email, name) VALUES ("taylor@laravel.com", "Taylor Otwell");'); $connection->statement('PRAGMA optimize;'); + $this->app['files']->ensureDirectoryExists(database_path('schema')); + $connection->getSchemaState()->dump($connection, database_path('schema/sqlite-schema.sql')); $this->assertFileDoesNotContains([