-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setup database migration for staging and prod #601
Conversation
* Add migration in docker-entrypoint.sh, when DATABASE_SETUP !=1 * Add migration file creation script, with required permission * Create initial migration file * Add documentation for making migration files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've left a few comments, but I think this looks great.
I have one question: the Prisma docs talk about running this via CI. We're currently doing it differently, by running it on startup for the container. This means both instances will run it. I think this means we should adjust our Docker deploy settings to only bring up 1 instance at a time vs. doing both at once. We also need to shut down the existing ones first (i.e., we can't have zero-downtime deploys, which is OK). My reading of the docs indicates that the second instance will skip doing most of the work, since the db will already be in sync (i.e., it should be safe to do this in each instance). Do you agree?
For the docker changes, see https://docs.docker.com/compose/compose-file/deploy/#update_config. Specifically, I think you need to update our docker compose files: parallelism: 1
delay: 20s
failure_action: rollback
order: stop-first This is my reading of it, but confirm what you think. We'll have to play with this on staging to know for sure. |
* Adjust docker deployment for update * Add comments to database_migration and database_setup
I agree with what you said. I detected an issue with using our old I also added a bit more explanation in |
Thinking about it, I added extra warning in |
* Add more explanation for database_setup vs database_migration
28e49cb
to
6751b07
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking good. Made a few corrections/suggestions.
I'd also like to get @dadolhay's blessing on this approach.
I'm excited to try this on staging. Before we land it, let's co-ordinate so that I can change the service on staging (it currently runs DATABASE_SETUP=1
always).
OK. Thank you for catching those and making suggestions. I made the changes and added a bit along the way, like stating the database in docker needs to run locally for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome.
Just remember, ping me before you are going to merge this.
We would want to use If nothing goes wrong from there, we can switch it off for future updates. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see comment above
95048f2
to
396f31a
Compare
I changed all mentions of |
docker-entrypoint.sh
Outdated
# See if we need to do database wipe and reinitialize before starting. | ||
if [[ $DANGER_DATABASE_WIPE_REINITIALIZE == "1" ]]; then | ||
# Run database wipe and reinitialize | ||
DANGER_DATABASE_WIPE_REINITIALIZE |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is the fn call on line 10, should this not be lower case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right.
@dadolhay can you review this again please? |
Part of #291
Explanation
Helpfully these explanations make it easier to understand what I'm doing and why, in this PR.
This PR should allow staging and production to use Prisma migration by default, through deploying migration files.
Migration should keep data in the database. Only when
DATABASE_SETUP =1
is an environment variable, we would go back to force pushing and cleaning data like before.For there to be anything to deploy, I made the first migration file too.
A script is setup to make creating migration files locally with docker easier. When Prisma creates a migration file, it requires permission to create a temporary shadow database to do this, which means the user of MySQL database needs
CREATE
,ALTER
,DROP
, andREFERENCES
privilege. Therefore, I useroot
user to do this in the script, against the local docker database container. The script doesn't seed database or apply migration, just creating a migration file and also generating Prisma client (default behavior) in case a dev forgets.Changes
docker-entrypoint.sh
, when DATABASE_SETUP !=1package.json
prisma/migrations
DEPLOY.md
Test
1.You can test the script
npm run db:migration
to create migration files using our local docker database container.2.You can test deployment of the migration file via
npx prisma migrate deploy
, to an empty docker database container, or one that already has this migration applied to. It should either work or tell you no migration is needed.Note: this command doesn't check for database drift.
Having this run on staging would be the real test.