ahmadsalah's avatar

Handle differences between SQLite and MySQL in test environment

I like to use in-memory SQLite for testing but I always get stuck on the differences between the two

for example, regex and full-text search indices are diffrent so how you cope with that?

0 likes
1 reply
Tray2's avatar

Depends a little.

If I'm going to be using a MySQL/MariaDB I'd switch to it when SQLite no longer is an option without writing different SQL for test and production.

However I try to stay away from using raw SQL and If I need to I usually push it down into the database as a view.

Here is one example of that since MySQL and SQLite has different syntaxes when concatination.

 public function up()
    {
        if (config('database.default') == 'mysql') {
            DB::statement("CREATE VIEW book_collections AS SELECT a.id,
            CONCAT(a.last_name, ', ', a.first_name) author_name,
            b.title,
            b.genre_id,
            g.genre,
            b.format_id,
            f.format,
            bu.user_id,
            DATE_FORMAT(bu.created_at, '%Y-%m-%d') created_at
            FROM authors a,
                 book_users bu,
                 books b,
                 genres g,
                 formats f,
                 author_books ab
            WHERE a.id = ab.author_id
            AND b.id = ab.book_id
            AND bu.book_id = b.id
            AND b.genre_id = g.id
            AND b.format_id = f.id");
        } else {
            DB::statement("CREATE VIEW book_collections AS SELECT a.id,
            a.last_name || ', ' || a.first_name author_name,
            b.title,
            b.genre_id,
            g.genre,
            b.format_id,
            f.format,
            bu.user_id,
            bu.created_at
            FROM authors a,
                 book_users bu,
                 books b,
                 genres g,
                 formats f,
                 author_books ab
            WHERE a.id = ab.author_id
            AND b.id = ab.book_id
            AND bu.book_id = b.id
            AND b.genre_id = g.id
            AND b.format_id = f.id");
        }
    }
1 like

Please or to participate in this conversation.