-
-
Notifications
You must be signed in to change notification settings - Fork 16
Clinwiki DB Migration Process
There are 2 key things to track when making db changes. Our db schema changes of course but so does our hasura meta. In order to track both appropriately please follow these steps.
The core of the migration process is the node package db-migrate https://db-migrate.readthedocs.io/en/latest/ . In order to simplify things regarding environment variables and paths, some new commands were added to package.json.
cd /api
run the command: npm run db-migration-new [migration-name]
This will create a new file in the /migrations directory that is a stub. You will be editing that stub in order to create your migration. Note that you will only be implementing the up() function. Use of the “down” functionality is avoided. If something needs to be reverted, create a new migration to change it.
Examples To create a new table:
exports.up = function(db,callback) {
db.createTable('BOB', {
id: { type: 'int', primaryKey: true, autoIncrement: true},
name: 'string'
},callback);
};
To add a new column:
exports.up = function(db,callback) {
db.addColumn(‘BOB’, ‘STUFF’,{ type: ‘string’},callback);
};
Once the migration file is finished, it can be applied with: npm run db-migration-up
This will apply any missing migrations to the database defined in the .env in the root clinwiki project.
After applying migrations you will want to update your meta
There is a file with most current meta in db/hasuraMeta. Please export your meta after any db schema changes or permission changes.
This can be done from Hasura console under Settings > Metadata Actions > Export Metadata
Once downloaded scroll down to bottom of file there is a configuration
section, replace your database url with "CHANGE ME".
Save file as hasura_metadata_current and replace whats currently in db/hasuraMeta.
You will want to open the hasura_metadata_current
file in db/hasuraMetadata, scroll down to bottom of file there is a configuration
section, replace "CHANGE ME" with connection url string.