sourcefuse / loopback4-starter

Loopback 4 starter application. Multi-tenant architecture supported. Authentication, Authorization, Soft deletes, environment vars, Audit logs, included.
MIT License
158 stars 59 forks source link

Re run db: migrate #6

Closed vikramkh closed 5 years ago

vikramkh commented 5 years ago

How do I re run db: migrate? I changed the sql files and wanted to re run migrate but it just says nothing to migrate. I emptied my database and it still wont migrate, it just says No migrations to run. image

vikramkh commented 5 years ago

I realized now I should have used reset, but now that the table is gone, how do I re run migrate, because if I try reset now, it will tell me that the schema does not exist, but migrate will say that no migrations to run

vikramkh commented 5 years ago

Ok not sure how but I figured it out. However, is lbstarter.dbs being used? I tried to add my own code to the sql files but was getting errors and I wasn't sure if I needed to modify somewhere else to fit the new tables that I was adding in?

samarpan-b commented 5 years ago

@vikramkh Few things to note here -

  1. I need to improve documentation further describing how to manage db migrations.
  2. The best way to ensure db migrations work perfectly fine is - never update existing migrations which are already run. For any changes to DB tables or data, just create a new migration file using db-migrate cli. This will create 3 files - one js in /migrations and 2 sql files inside /migrations/sqls. You can write your own alter scripts here for any modification you need. As the project progresses, you may need to update your DB again, you should repeat the same step - create new migration everytime.
  3. If there is still utmost need to change existing migrations, say there was a small error in some script, there is a simple workaround. Just go to public schema in database. There will be a migrations table. Check all the entries in this table. db-migrate reads this table to know which all migrations have been run. Refer docs here. So, whichever script you changed, just delete that file entry from this table. Run npm run db:migrate again. It will work.
  4. If you want to just reset the database to empty state, then run npm run db:migrate:reset. It will run all your down scripts in reverse order and will empty its own table 'migrations' in DB.
  5. If you want to just go down one version only, then run npm run db:migrate:down. It will run the last down script and remove it from migrations table.

Please have a read at the documentation of db-migrate to learn about how useful this tool is for handling db migrations and versioning.

samarpan-b commented 5 years ago

Ok not sure how but I figured it out. However, is lbstarter.dbs being used? I tried to add my own code to the sql files but was getting errors and I wasn't sure if I needed to modify somewhere else to fit the new tables that I was adding in?

lbstarter.dbs is the project file for DbSchema tool that we have used to create Entity-relationship diagram for the database and execute it into database directly. It helps in keeping the DB schema intact, up to date and easily shareable. It can also be used to extract DB scripts out of it for migrations sqls.

Install DbSchema and open this file there. You will see the entire ERD.

Hope this helps.

vikramkh commented 5 years ago

Thanks so much for the help!

argupta23 commented 5 years ago

Ok not sure how but I figured it out. However, is lbstarter.dbs being used? I tried to add my own code to the sql files but was getting errors and I wasn't sure if I needed to modify somewhere else to fit the new tables that I was adding in?

lbstarter.dbs is the project file for DbSchema tool that we have used to create Entity-relationship diagram for the database and execute it into database directly. It helps in keeping the DB schema intact, up to date and easily shareable. It can also be used to extract DB scripts out of it for migrations sqls.

Install DbSchema and open this file there. You will see the entire ERD.

Hope this helps.

@samarpan-b Is there a mongodb version of the schema available that you could share or steps on how to convert the postgres version to mongodb are also welcome?

Thanks

samarpan-b commented 5 years ago

@argupta23 I don't have a mongodb version available right now. I'll need some time to provide you for that. I'll try next week. Please raise a separate feature request for this.

If you are able to do it before that, feel free to contribute with a PR.