W
Waspβ€’3w ago
mgp25

Missing migrations

- Wasp version: 0.13.x I have an instance were it seems it is missing the migrations files, so when running in prod, wasp db migrate-dev we face the following issue:
🐝 --- Starting database migration... ---------------------------------------------

Environment variables loaded from .env
Prisma schema loaded from ../db/schema.prisma
Datasource "db": PostgreSQL database "postgres", schema "public" at "127.0.0.1:5432"

Drift detected: Your database schema is not in sync with your migration history.

The following is a summary of the differences between the expected database schema given your migrations files, and the actual schema of the database.

It should be understood as the set of changes to get from the expected schema to the actual schema.

...
...
...

βœ” We need to reset the "public" schema at "127.0.0.1:5432"
Do you want to continue? All data will be lost. … no
🐝 --- Starting database migration... ---------------------------------------------

Environment variables loaded from .env
Prisma schema loaded from ../db/schema.prisma
Datasource "db": PostgreSQL database "postgres", schema "public" at "127.0.0.1:5432"

Drift detected: Your database schema is not in sync with your migration history.

The following is a summary of the differences between the expected database schema given your migrations files, and the actual schema of the database.

It should be understood as the set of changes to get from the expected schema to the actual schema.

...
...
...

βœ” We need to reset the "public" schema at "127.0.0.1:5432"
Do you want to continue? All data will be lost. … no
I have already tried wasp clean and running wasp db migrate-dev, but the same problems remains. I have also tried removing the migration from:
DELETE FROM _prisma_migrations WHERE migration_name = '20241128215415_';
DELETE FROM _prisma_migrations WHERE migration_name = '20241128215415_';
Same problem is happening. And yes, keeping data is a must. Any idea how to solve this? Thank you!
4 Replies
kapa.ai
kapa.aiβ€’3w ago
Hi @mgp25. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the information provided in the knowledge sources, I can offer some insights and suggestions for your issue with missing migrations in Wasp 0.13.x. First, it's important to note that the wasp db migrate-dev command is typically used for development purposes and not in production environments. In a deployed app, migrations are usually run automatically on every application start Discord support thread. However, given your situation where you're facing drift between your database schema and migration history, here are some steps you can try: 1. Check your migrations folder: Ensure that your Wasp project has a migrations/ directory with the necessary migration files. If this directory is missing or empty, it could explain why you're facing this issue Github Issue. 2. Verify your schema.prisma file: Make sure your schema.prisma file accurately reflects your current data model. Any discrepancies here could cause migration issues. 3. Consider using prisma migrate status: This command can help you check if your migrations are in sync with your database. It might provide more detailed information about the state of your migrations Github Issue. 4. Database backup: Before attempting any further actions, ensure you have a backup of your database, especially since you mentioned that keeping data is a must. 5. Recreate migrations: If possible, you might want to consider recreating your migrations. This would involve: - Backing up your database - Deleting the existing migrations - Running wasp db migrate-dev to create a new initial migration based on your current schema However, be cautious with this approach as it involves deleting existing migrations Wasp Docs. 6. Check for Prisma client initialization: Ensure that the Prisma client is properly initialized. Sometimes, running npx prisma generate --schema ../db/schema.prisma in your server directory can help resolve Prisma-related issues Github Issue. If none of these steps resolve your issue, you might need to seek more specialized help. Consider reaching out to the Wasp community on their Discord channel or opening an issue on their GitHub repository with detailed information about your setup and the steps you've already taken. Remember, working with database migrations in a production environment requires careful consideration and planning to avoid data loss or service disruptions. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @mgp25, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
mgp25
mgp25OPβ€’3w ago
@kapa.ai if I have working migration files in local, how can I recreate them in production
kapa.ai
kapa.aiβ€’3w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- To recreate your working local migration files in production, you need to be careful to avoid data loss. Based on the knowledge sources provided, here's a suggested approach: 1. First, ensure you have a backup of your production database. This is crucial in case anything goes wrong during the process. 2. Connect to your production database. If you're using Fly.io for deployment, you can use the Fly CLI to create a tunnel to your database. Here's how you can do it:
fly proxy 5432 -a your-app-name-db

fly proxy 5432 -a your-app-name-db

Leave this terminal tab running and open a new one for further commands Github Issue. 3. Set up your local environment to connect to the production database. You can do this by setting the DATABASE_URL environment variable:
export DATABASE_URL="your_production_database_url"

export DATABASE_URL="your_production_database_url"

Make sure to replace "your_production_database_url" with the actual URL of your production database Github Issue. 4. Once connected, you can apply your local migrations to the production database using:
wasp db migrate-dev

wasp db migrate-dev

This command will apply your local migrations to the production database Discord support thread. 5. After applying the migrations, verify that everything is working as expected. You can use wasp db studio to inspect the database structure and ensure it matches your local setup. 6. Once you've confirmed everything is correct, don't forget to clean up: - Remove the DATABASE_URL from your local .env.server file - Close the database tunnel you opened with Fly.io Remember, working with a production database is always risky. Make sure you have proper backups and ideally test this process on a staging environment before applying it to your actual production database. Also, note that for future deployments, Wasp typically runs migrations automatically on application start in production environments, so you shouldn't need to manually apply migrations for regular updates Discord support thread. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @mgp25, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
mgp25
mgp25OPβ€’3w ago
Well... I have ended up crafting the migration missing from the DB. For those who encounter the same issue as me, it is just a diff with your current schema. Once you have created the migration file, you can simply place it in .wasp/out/db/migrations if the folder does not exist, create it. Run the following command to check that you have everything correctly:
DATABASE_URL=<DB_URL> npx prisma migrate status
DATABASE_URL=<DB_URL> npx prisma migrate status
then:
DATABASE_URL=<DB_URL> npx prisma migrate deploy
DATABASE_URL=<DB_URL> npx prisma migrate deploy
If you did everything right, you will get the following message:
Prisma schema loaded from schema.prisma

1 migration found in prisma/migrations

Applying migration `20250211213836_`

The following migration have been applied:

migrations/
└─ 20250211213836_/
└─ migration.sql

All migrations have been successfully applied.
Prisma schema loaded from schema.prisma

1 migration found in prisma/migrations

Applying migration `20250211213836_`

The following migration have been applied:

migrations/
└─ 20250211213836_/
└─ migration.sql

All migrations have been successfully applied.
Hope this could be helpful

Did you find this page helpful?