diff --git a/.github/workflows/databases.yml b/.github/workflows/databases.yml index 5db77fab35bc..46cf7a225d67 100644 --- a/.github/workflows/databases.yml +++ b/.github/workflows/databases.yml @@ -238,3 +238,41 @@ jobs: DB_DATABASE: master DB_USERNAME: SA DB_PASSWORD: Forge123 + + sqlite: + runs-on: ubuntu-20.04 + + strategy: + fail-fast: true + + name: SQLite + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.2 + extensions: dom, curl, libxml, mbstring, zip, pcntl, sqlsrv, pdo, pdo_sqlsrv, odbc, pdo_odbc, :php-psr + tools: composer:v2 + coverage: none + + - name: Set Framework version + run: composer config version "11.x-dev" + + - name: Install dependencies + uses: nick-fields/retry@v3 + with: + timeout_minutes: 5 + max_attempts: 5 + command: composer update --prefer-stable --prefer-dist --no-interaction --no-progress + + - name: Setup SQLite Database + run: php vendor/bin/testbench package:create-sqlite-db + + - name: Execute tests + run: vendor/bin/phpunit tests/Integration/Database/Sqlite + env: + DB_CONNECTION: sqlite diff --git a/src/Illuminate/Database/Schema/SqliteSchemaState.php b/src/Illuminate/Database/Schema/SqliteSchemaState.php index a7c0edfeff02..d7195f495b47 100644 --- a/src/Illuminate/Database/Schema/SqliteSchemaState.php +++ b/src/Illuminate/Database/Schema/SqliteSchemaState.php @@ -16,16 +16,14 @@ class SqliteSchemaState extends SchemaState public function dump(Connection $connection, $path) { with($process = $this->makeProcess( - $this->baseCommand().' .schema' + $this->baseCommand().' ".schema --indent"' ))->setTimeout(null)->mustRun(null, array_merge($this->baseVariables($this->connection->getConfig()), [ // ])); - $migrations = collect(preg_split("/\r\n|\n|\r/", $process->getOutput()))->reject(function ($line) { - return str_starts_with($line, 'CREATE TABLE sqlite_'); - })->all(); + $migrations = preg_replace('/CREATE TABLE sqlite_.+\);[\r\n]+/is', '', $process->getOutput()); - $this->files->put($path, implode(PHP_EOL, $migrations).PHP_EOL); + $this->files->put($path, $migrations.PHP_EOL); if ($this->hasMigrationTable()) { $this->appendMigrationData($path); diff --git a/tests/Integration/Database/Sqlite/DatabaseSqliteConnectionTest.php b/tests/Integration/Database/Sqlite/DatabaseSqliteConnectionTest.php index d95b6b97f98b..0afec4bd7d1d 100644 --- a/tests/Integration/Database/Sqlite/DatabaseSqliteConnectionTest.php +++ b/tests/Integration/Database/Sqlite/DatabaseSqliteConnectionTest.php @@ -10,9 +10,11 @@ class DatabaseSqliteConnectionTest extends DatabaseTestCase { - protected function getEnvironmentSetUp($app) + protected function defineEnvironment($app) { - if (getenv('DB_CONNECTION') !== 'testing') { + parent::defineEnvironment($app); + + if ($this->driver !== 'sqlite') { $this->markTestSkipped('Test requires a Sqlite connection.'); } diff --git a/tests/Integration/Database/Sqlite/DatabaseSqliteSchemaBuilderTest.php b/tests/Integration/Database/Sqlite/DatabaseSqliteSchemaBuilderTest.php index c7e4f44c4a55..36fe9611357a 100644 --- a/tests/Integration/Database/Sqlite/DatabaseSqliteSchemaBuilderTest.php +++ b/tests/Integration/Database/Sqlite/DatabaseSqliteSchemaBuilderTest.php @@ -9,9 +9,11 @@ class DatabaseSqliteSchemaBuilderTest extends DatabaseTestCase { - protected function getEnvironmentSetUp($app) + protected function defineEnvironment($app) { - if (getenv('DB_CONNECTION') !== 'testing') { + parent::defineEnvironment($app); + + if ($this->driver !== 'sqlite') { $this->markTestSkipped('Test requires a Sqlite connection.'); } diff --git a/tests/Integration/Database/Sqlite/EloquentModelConnectionsTest.php b/tests/Integration/Database/Sqlite/EloquentModelConnectionsTest.php index ba296463329a..9ecd22172801 100644 --- a/tests/Integration/Database/Sqlite/EloquentModelConnectionsTest.php +++ b/tests/Integration/Database/Sqlite/EloquentModelConnectionsTest.php @@ -10,9 +10,11 @@ class EloquentModelConnectionsTest extends TestCase { - protected function getEnvironmentSetUp($app) + protected function defineEnvironment($app) { - if (getenv('DB_CONNECTION') !== 'testing') { + $connection = $app['config']->get('database.default'); + + if ($app['config']->get("database.connections.$connection.driver") !== 'sqlite') { $this->markTestSkipped('Test requires a Sqlite connection.'); } diff --git a/tests/Integration/Database/Sqlite/EscapeTest.php b/tests/Integration/Database/Sqlite/EscapeTest.php index bcf6c2f0d5c2..aba82ca0e3a3 100644 --- a/tests/Integration/Database/Sqlite/EscapeTest.php +++ b/tests/Integration/Database/Sqlite/EscapeTest.php @@ -7,9 +7,11 @@ class EscapeTest extends DatabaseTestCase { - protected function getEnvironmentSetUp($app) + protected function defineEnvironment($app) { - if (getenv('DB_CONNECTION') !== 'testing') { + parent::defineEnvironment($app); + + if ($this->driver !== 'sqlite') { $this->markTestSkipped('Test requires a Sqlite connection.'); } diff --git a/tests/Integration/Database/Sqlite/SchemaStateTest.php b/tests/Integration/Database/Sqlite/SchemaStateTest.php index 8c25810b6992..5bb30f1b34a4 100644 --- a/tests/Integration/Database/Sqlite/SchemaStateTest.php +++ b/tests/Integration/Database/Sqlite/SchemaStateTest.php @@ -3,14 +3,13 @@ namespace Illuminate\Tests\Integration\Database\Sqlite; use Illuminate\Support\Facades\DB; -use Illuminate\Tests\Integration\Database\DatabaseTestCase; -use Orchestra\Testbench\Attributes\WithMigration; use Orchestra\Testbench\Concerns\InteractsWithPublishedFiles; -use Orchestra\Testbench\Factories\UserFactory; +use Orchestra\Testbench\TestCase; use PHPUnit\Framework\Attributes\RequiresOperatingSystem; -#[WithMigration] -class SchemaStateTest extends DatabaseTestCase +use function Orchestra\Testbench\remote; + +class SchemaStateTest extends TestCase { use InteractsWithPublishedFiles; @@ -18,26 +17,53 @@ class SchemaStateTest extends DatabaseTestCase 'database/schema', ]; + protected function setUp(): void + { + parent::setUp(); + + remote('migrate:install')->mustRun(); + } + + protected function tearDown(): void + { + remote('db:wipe')->mustRun(); + + parent::tearDown(); + } + + protected function defineEnvironment($app) + { + $connection = $app['config']->get('database.default'); + + if ($app['config']->get("database.connections.$connection.driver") !== 'sqlite') { + $this->markTestSkipped('Test requires a Sqlite connection.'); + } + } + #[RequiresOperatingSystem('Linux|Darwin')] public function testSchemaDumpOnSqlite() { - if ($this->driver !== 'sqlite') { - $this->markTestSkipped('Test requires a SQLite connection.'); + if ($this->usesSqliteInMemoryDatabaseConnection()) { + $this->markTestSkipped('Test cannot be run using :in-memory: database connection'); } - UserFactory::new()->create(); - $connection = DB::connection(); - $connection->statement('PRAGMA optimize;'); + $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('INSERT INTO users (email, name) VALUES ("taylor@laravel.com", "Taylor Otwell");'); + + $this->assertTrue($connection->table('sqlite_sequence')->exists()); $this->app['files']->ensureDirectoryExists(database_path('schema')); $connection->getSchemaState()->dump($connection, database_path('schema/sqlite-schema.sql')); - $this->assertFileDoesNotContains([ + $this->assertFileContains([ + 'CREATE TABLE users', + ], 'database/schema/sqlite-schema.sql'); + $this->assertFileNotContains([ 'sqlite_sequence', - 'sqlite_stat1', - 'sqlite_stat4', ], 'database/schema/sqlite-schema.sql'); } }