I’m using Sequelize as an ORM for a Postgres database. I started learning & using Sequelize migrations and seeding features. So for I understood that the primary purpose of migrations is to reliably make changes to the database schema without affecting the data inside. I created the first migration to create tables based on the current schemas. I also created the first seed to populate the tables with some data. Now I want to understand: How do I apply this migration (and future ones) to spin up a database in production (the first time) and in development? Ideally, since these migrations keep track of the schema changes, I can just run them in any environment (production, staging, development) and it will create/update the tables in that environment.
Here's a rough overview of how migration works.
If you did a migration(npx sequelize-cli db:migrate
), then your table must have a table named SequelizeMeta
or something similar to that under the database.
// SequelizeMeta
name
xxxx-create-user-table
xxxx-update-user-table
...
You can check which migrations are applied or not by matching the names there.
So it's a git for a database schema.
Now let's answer the questions.
Q: How do I apply this migration (and future ones) to spin up a database in production (the first time) and in development?
A: access your server instance (it can be ec2 or your local machine) and run migration there (npx sequelize-cli db:migrate
).
Typically you'll use different databases for different environments, for example, use local MySQL database for development and use AWS RDS
for production, etc.
So you should do migration after deployment if you have new undone migrations. (access server instance and run migration)