Skip to content

Commit

Permalink
[11.x] Improve readability of SQLite schema dumps (#52172)
Browse files Browse the repository at this point in the history
* Fix schema test to run against real database file

* Indent schema dump

* Wip

* Simplify test

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* Update SchemaStateTest.php

* Update SchemaStateTest.php

---------

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Co-authored-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
bakerkretzmar and crynobone authored Jul 18, 2024
1 parent 3e07f5a commit a128fc5
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 26 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/databases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 3 additions & 5 deletions src/Illuminate/Database/Schema/SqliteSchemaState.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}

Expand Down
6 changes: 4 additions & 2 deletions tests/Integration/Database/Sqlite/EscapeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}

Expand Down
52 changes: 39 additions & 13 deletions tests/Integration/Database/Sqlite/SchemaStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,67 @@
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;

protected $files = [
'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');
}
}

0 comments on commit a128fc5

Please sign in to comment.