Skip to content

Commit

Permalink
[11.x] Fix SQLite schema dumps containing internal sqlite_* objects (
Browse files Browse the repository at this point in the history
…#52135)

* Add failing test

* Remove all `CREATE TABLE sqlite_*` statements from schema dump

* Wip
  • Loading branch information
bakerkretzmar authored Jul 15, 2024
1 parent 2cefbbd commit 094d4eb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/Illuminate/Database/Schema/SqliteSchemaState.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
40 changes: 40 additions & 0 deletions tests/Integration/Database/SchemaStateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Illuminate\Tests\Integration\Database;

use Illuminate\Support\Facades\DB;
use Orchestra\Testbench\Concerns\InteractsWithPublishedFiles;

class SchemaStateTest extends DatabaseTestCase
{
use InteractsWithPublishedFiles;

protected $files = [
'database/schema',
];

public function testSchemaDumpOnSqlite()
{
if ($this->driver !== 'sqlite') {
$this->markTestSkipped('Test requires a SQLite connection.');
}

$connection = DB::connection('sqlite');
$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 ("[email protected]", "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([
'sqlite_sequence',
'sqlite_stat1',
'sqlite_stat4',
], 'database/schema/sqlite-schema.sql');
}
}

0 comments on commit 094d4eb

Please sign in to comment.