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

[12.x] Support for Attaching Databases as Schemas in SQLite #54380

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

hafezdivandari
Copy link
Contributor

@hafezdivandari hafezdivandari commented Jan 27, 2025

Continuing the work on #54274 to enhance multi-schema support, this PR introduces a new schemas config property that adds support for attaching databases as schemas to SQLite connections. The ATTACH DATABASE query is not persistent between SQLite connections, which is why we need to attach schemas every time the connection is established.

'connections' => [

    'sqlite' => [
        'driver' => 'sqlite',
        'database' => env('DB_DATABASE', database_path('database.sqlite')),
+       'schemas' => [
+           'secondary' => env('DB_SECONDARY_DATABASE', database_path('secondary.sqlite')),
+           'tertiary' => env('DB_TERTIARY_DATABASE', ':memory:'),
+       ],
        // ...
    ],

],

Note: When attaching databases to a SQLite connection using this new schemas config property, the following commands, testing traits and methods will drop tables/views in the attached databases (schemas) as well (This behavior is the same for PostgreSQL and SQL Server schemas):

  • db:wipe and migrate:fresh commands.
  • \Illuminate\Foundation\Testing\RefreshDatabase and \Illuminate\Foundation\Testing\DatabaseTruncation traits.
  • Schema::dropAllTables() and Schema::dropAllViews() methods.

This PR also moves the connection configurations logic from the SQLiteConnection class to the SQLiteConnector class, following the pattern of other database drivers. SQLite was the only DB driver that configured its connection in the Connection class instead of the Connector class. As part of this change, redundant Builder and Grammar methods have been removed. There are many pragma key/value pairs, so it doesn't make sense to have a method in both classes for each pragma key/value (added recently on #52052).

Summary

  • New schemas config property for the sqlite connection.
  • New Schema::connection('sqlite')->pragma($key, $value = null) method to get/set a pragma value.
    • This method also accepts schema-qualified keys, for example: Schema::pragma('schema_name.journal_mode', 'wal')
  • Moved SQLite connection configuration logic from the SQLiteConnection class to the SQLiteConnector class to be consistent with other DB drivers.
  • Removed redundant SQLiteGrammar::compileSetBusyTimeout(), compileSetJournalMode(), compileSetSynchronous(), compileEnableWriteableSchema(), and compileDisableWriteableSchema() methods.
  • Removed redundant SQLiteBuilder::setBusyTimeout(), setJournalMode(), and setSynchronous() methods:
    • Schema::setBusyTimeout(12345) -> Schema::pragma('busy_timeout', 12345)
    • Schema::setJournalMode('wal') -> Schema::pragma('journal_mode', 'wal')
    • Schema::setSynchronous('normal') -> Schema::pragma('synchronous', 'normal')

Copy link

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@hafezdivandari hafezdivandari marked this pull request as ready for review January 27, 2025 22:06
@hafezdivandari hafezdivandari marked this pull request as draft January 28, 2025 12:20
@hafezdivandari hafezdivandari marked this pull request as ready for review January 28, 2025 12:53
@Sophist-UK
Copy link

Given how much SQLite is starting to be used for scalable web services, this PR seems to me to be an excellent direction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants