3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

LumenでsqliteのURI Filenamesを使いたい

Last updated at Posted at 2018-06-29

訂正

そもそもPDOのsqliteドライバはfile:をサポートしていないことがわかりました。(#74184 Sqlite PDO doesn't support URI filenames) mysqlと比べると速くなりましたが、in-memoryになっていたわけではないようです。すいません。

なんとなくlsしたらfile::memory:?cache=sharedなる名前のファイルが生成されていて気がつきました。

ということで以下は無視してください。

はじめに

Lumenでテストが増えてきたのでsqliteにしてin memory databaseにすると少しは速くなるという話だったのですが一筋縄ではいかなかったのでメモ。

:memory:だとうまくいかない

まず単純に以下の設定で試してみました。

DB_CONNECTION=sqlite
DB_DATABASE=:memory:

しかし、なぜかテスト時にテーブルが存在しないというエラーが出ます。よくよく調べるとmigrationの時とテストのときでそれぞれデータベースへのconnectを行っていました。:memory:は接続のたびに新しいdbをメモリ内に作る(=内容は引き継がれない)ということなのでこれではだめそう。(DatabaseMigrationsでなければ多分問題ないんだろうけど)

file::memory:?cache=sharedを使う

sqliteのマニュアルによればfile::memory:?cache=sharedと指定すれば複数の接続から同じin-memoryデータベースを開くことができるとありました。これを使えば良さそうです。

しかし、

DB_CONNECTION=sqlite
DB_DATABASE=file::memory:?cache=shared

とやってみると、

Illuminate\Database\QueryException : Database (file::memory:?cache=shared) does not exist. (SQL: select * from sqlite_master where type = 'table' and name = migrations)

というエラーになってしまいました。メモリ内にこれから作られるはずのものが存在しないというのも変な話なのでこのエラーを出しているところを調べると以下のようになっていました。

Illuminate\Database\Connectors\SQLiteConnectorクラス:

    public function connect(array $config)
    {
        $options = $this->getOptions($config);

        // SQLite supports "in-memory" databases that only last as long as the owning
        // connection does. These are useful for tests or for short lifetime store
        // querying. In-memory databases may only have a single open connection.
        if ($config['database'] == ':memory:') {
            return $this->createConnection('sqlite::memory:', $config, $options);
        }

        $path = realpath($config['database']);

        // Here we'll verify that the SQLite database exists before going any further
        // as the developer probably wants to know if the database exists and this
        // SQLite driver will not throw any exception if it does not by default.
        if ($path === false) {
            throw new InvalidArgumentException("Database (${config['database']}) does not exist.");
        }

        return $this->createConnection("sqlite:{$path}", $config, $options);
    }

つまり、:memory:という文字列だけは特別扱いするけどそれ以外はrealpathで存在を確認してなければエラーになります。

file:の考慮もないしこれ余計なお世話なんじゃないの...?

SQLiteConnectorを置き換える

DB_CONNECTIONに対応するconnectorのクラスはdb.connector.<DB_CONNECTIONの名前>のコンテナがあればそれを使うようになっているので、入れ替えることが出来ます。(Illuminate\Database\Connectors\ConnectionFactorycreateConnectorメソッド)

そこで余計なチェックを一切しない以下のようなApp\Database\SqliteConnectorクラスを作成し、

namespace App\Database;


use Illuminate\Database\Connectors\Connector;
use Illuminate\Database\Connectors\ConnectorInterface;

class SqliteConnector extends Connector implements ConnectorInterface
{
    /**
     * Establish a database connection.
     *
     * @param  array  $config
     * @return \PDO
     */
    public function connect(array $config)
    {
        $options = $this->getOptions($config);
        return $this->createConnection('sqlite:'.$config['database'], $config, $options);
    }
}

AppServiceProviderregisterメソッドの中で以下のように登録しました。

    $this->app->bind('db.connector.sqlite', SqliteConnector::class);

試したところ問題なく動作するようです。

おわりに

これでmysqlで1分46秒かかっていたテストが28秒になりました。(docker for mac上)

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?