restart93's avatar

how do you do testing with migrations (drop foreign key, dropcolumns, add new)?

Hello! I have 54 migrations. I was removing, adding columns, dropping indexes, adding indexes, etc.

Now tests (phpunit) don't run . I have bunch o sqlite problem like

BadMethodCallException: SQLite doesn't support dropping foreign keys (you would need to re-create the table).

and porbably many more.

Is there any good way so test will run fast (in memory?) and without errors?

0 likes
7 replies
kevinbui's avatar

Working with foreign keys in migrations with sqlite can be annoying.

@tykus solution probably works. Alternatively, in the config/database.php, sqlite section, you can disable foreign_key_constraints.

connections => [
    'sqlite' => [
        'driver' => 'sqlite',
        'url' => env('DATABASE_URL'),
        'database' => env('DB_DATABASE', database_path('database.sqlite')),
        'prefix' => '',
        'foreign_key_constraints' => env('DB_FOREIGN_KEYS', false),
    ],
]
restart93's avatar

the sqlite memory database doesn't want to use dumped sql file.

Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 near "AUTO_INCREMENT": syntax error (Connection: sqlite, SQL: /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;

I'm using mysql now for testing. This feels wrong

Tray2's avatar

@restart93 I don't agree. It's the correct move to make, SQLite is really good for starting your application, but as the complexity in the database grows, you will and should sooner or later move to a RDBMS, and when you do so, make sure it is the same as in production. That way you will not get any nasty surprises on deploy day.

1 like
martinbean's avatar

@restart93 Because the dump file will be a MySQL dump; not a SQLite dump.

Squashing migrations is not the answer. Using the same database engine in your tests is.

Please or to participate in this conversation.