-
Notifications
You must be signed in to change notification settings - Fork 407
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
Switch to Knex migrations #1154
Conversation
transaction still fails because knex can't find the knex_migrations table, which is quite odd this seems to be related to the sql batch query, because it works fine with a normal knex function (like createTableIfNotExists)
…n, so the test doesn't have to be run multiple times
…reserve ordering for complex foreign key dependencies
…th switching from previous src/migrations/index.js setup
…te DB connection from knex+migrations etc
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 want us to outline a step by step gameplan for the order of operations with rolling this out re:
Before merging into main, we probably want to cut a new release (3.0?) with some documentation on how to migrate from an older version that still has migrations between current main and when this PR is merged.
Here are a few guiding questions:
- This is not backwards compatible, so what are the exact steps that folks would need to take after they git pull to get fully onto this new system
- Will this influence anyone's current dev envs if they pull assuming they aren't doing a migration right now?
- How does
migrations/20190207220000_init_db.js
fit into this transition?
Awesome work @schuyler1d and @harpojaeger I'm really excited about this, and want to make sure we merge it as seamlessly as we can!
__test__/server/db/utils.js
Outdated
'message', | ||
'user_cell', | ||
'job_request', | ||
// 'migrations' // Now automatically managed by Knex |
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.
Wondering if we should just delete this line? Cause a few months after this PR this becomes a reference to a past state of the code that's no longer relevant? Could be useful though
knexfile.env.js
Outdated
@@ -0,0 +1,10 @@ | |||
// Update with your config settings. |
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.
Is this a leftover comment from knex's docs? Or is the implication here that anyone who pulls this change needs to make some personal change to this file?
@@ -0,0 +1,355 @@ | |||
const initialize = async (knex, Promise) => { |
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.
Is this file an initial migration that folks need to run to get everything set up on the new system? It seems like the knex CLI should be covering us for future migrations. If it is an initial migration that needs to be run, can we label it with a comment block and document it?
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.
it is an initial migration, but does not need to be run manually, because of this:
https://github.com/MoveOnOrg/Spoke/pull/1154/files#diff-6e0d62f54b853b53af874f5965d7adf2R36
i.e. on startup, by default migrations or initial db creation happen automatically.
So knex CLI is mostly useful for creating new migrations or manipulating them for db admin or doing it manually, e.g. in a production setting, where you want to run manually instead of let it 'autorun'
|
||
bluebird.promisifyAll(redis.RedisClient.prototype) | ||
bluebird.promisifyAll(redis.Multi.prototype) | ||
|
||
// // This was how to connect to rethinkdb: |
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.
Has all of this been lifted verbatim into knex.js
as a refactor? Or is there a bigger difference that I'm missing?
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.
Should be a verbatim copy
src/server/knex.js
Outdated
@@ -0,0 +1,73 @@ | |||
// Define a Knex connection. Currently, this is used only to instantiate the |
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.
Wondering if we should should rename this to knex-config.js
for clarity. From the name I wouldn't have guessed that the whole file is a config
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.
maybe knex-connect.js? the config is in environment variables, but this establishes the connection
If they had a database created before March (the time the last dev-affecting schema change was made), then they should clear/delete the database and let it autocreate again -- or do the instructions in (1)
It seems knexmigrations ignores the init migration if it is already connected to a database. So the important thing here is that it reflects the current state of prod (which it does at the moment) -- first installs/runs will auto create it with the init_db file. |
yarn.lock
Outdated
@@ -2793,13 +2793,6 @@ bl@~1.1.2: | |||
dependencies: | |||
readable-stream "~2.0.5" | |||
|
|||
block-stream@*: |
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.
Is this file accidentally included? Or included on purpose?
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.
AIUI, yarn.lock is updated occasionally based on upstream package dependencies.
I can remove the yarn.lock changes in this PR, if desired, but we should definitely update yarn.lock regularly -- and if we don't merge the changes here, then every time people do a yarn install
the same changes will enter through a different PR.
This builds on @harpojaeger's excellent work in #692 and should get us out of the hell of race conditions when creating/destroying the db -- especially during testing runs.
Using knex's native migrations support also should improve usability and predictability (esp. for anyone that is already familiar with knex).
One thing that we'll need to be careful of:
Before merging into main, we probably want to cut a new release (3.0?) with some documentation on how to migrate from an older version that still has migrations between current main and when this PR is merged. In that case, they should update their code to the latest release BEFORE this one, run migrations, and only then, checkout this version (or any future versions). I can add documentation for that, but since it will be release/version-specific, I thought I'd hold off for guidance on placement. If we want, we could skip deletion of the old migrations file, and then have some
yarn old-migrations
command that you would run. However, that might create confusion going forward if new devs find a src/migrations/index.js and incorrectly update that instead.From Harpo's changes, this does the following:
yarn knex ....
for easy migrationdotenv
dependency loading in server/index.js to preserve independence from .env contexts vs. e.g. other testing.cc: @ibrand and @lperson (since he is most likely to create schema change PRs)