Summer Sale! All accounts are 50% off this week.

kossa's avatar
Level 20

Load schema in testing

Hello,

I'm starting new Laravel app for an existing app, I dont want to create or copy old migration, so in old app I generated schema, I copied the schema to the new app.

When I run : php artisan migrate I have all old table, but in testing(using memory) it does not work.

Any solution ?

0 likes
8 replies
martinbean's avatar

@kossa I don’t really follow? If you use the RefreshDatabase trait in your tests, then this will migrate the database during a test.

kossa's avatar
Level 20

On old app I run : php artisan schema:dump --prune a "mysql-schema.dump" was generated, I copied it in new app under schema folder, when I run : php artisan migrate from command the migration works fine

But in testing I have an error : SQLSTATE[HY000]: General error: 1 no such table: users (SQL: insert into "users" ....

Seems like RefreshDatabase trait don't load "mysql-schema.dump"

pascual's avatar

Are you sure that your testing environment is using the same database connection as the application? Check your phpunit.xml config file for that.

martinbean's avatar

@kossa No, you can’t load a MySQL schema into SQLite. They’re two completely different types of databases.

kossa's avatar
Level 20

I'm using SQLite + memory because it's very fast than using mysql and run migration each test, there is a better way ? Taylor said : "Personally I recommend just using a file-based SQLite database during testing if you want to use schema dumps" but how ?

4 likes
pascual's avatar

I don't know your specific use case, but I you run into problems using data dumps from a production site when doing local testing that could be an issue in itself in several ways.

Keeping a separate set of dummy data for testing in development have a lot of advantages in my opinion, and can be quite easily done using seeders.

There was quite an interesting talk about this in the latest Laravel News Podcast.

Laravel News Podcast #141

Article: Why Dummy Data Matters

Another discussion on the topic in this forum

JonathanAspeling's avatar

Posting here because I couldn't find it anywhere else.

I had an issue where a test touches another schema in my db outside of the default public schema called reporting.

Kept getting hit with schema Invalid schema name: 7 ERROR: schema "reporting" does not exist

I added some lines in the migrations to create this schema CREATE SCHEMA reporting To no avail.

The end solution was to mount my Test DB in my IDE and manually create the schema after which all ran well.

I left a note at the offending migration lines that if you encounter that issue on your local or in CI pipeline then to follow the steps mentioned above.

Please or to participate in this conversation.