Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Improve readability of SQLite schema dumps #52172

Merged
merged 10 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change here seems to have affected an application I have in which it used to return all the tables I had in my sqlite database file, but now only returns the migrations one. I'm not sure how to fix it to make it work with all of them though. I think this test is only testing the sqlite in memory and not the sqlite database file and so it got missed.


$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') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes allow the TestCase to be executed on persistent and in-memory SQLite

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

$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
50 changes: 38 additions & 12 deletions tests/Integration/Database/Sqlite/SchemaStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,66 @@

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;
use function Orchestra\Testbench\remote;

#[WithMigration]
class SchemaStateTest extends DatabaseTestCase
class SchemaStateTest extends TestCase
{
use InteractsWithPublishedFiles;

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

protected function setUp(): void
{
parent::setUp();

$this->artisan('migrate:install')->run();
}

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');
}
}