SQL migrations - metaspace2020/metaspace GitHub Wiki
The database schema is managed by the graphql project. Several approaches were evaluated and we found that graphql has the biggest dependence on SQL access, and TypeORM was the best available TypeScript-capable ORM available at the time.
Beware that TypeORM doesn't check that the database schema matches its expected structure. It's easy to put your database out-of-sync with the migrations on the filesystem by changing branch before reverting, generating migrations when there have been manual changes, changing migrations after they've run, etc. Be careful and follow the below processes when making migrations.
- Run
yarn exec typeorm migration:runto make sure you are on the latest migration. - Make the changes to the TypeORM entity models.
- If you are running graphql in any form that automatically reloads on code change (e.g. in the development docker containers), stop it so that it doesn't automatically apply the migration.
- Run
yarn exec typeorm -- migration:generate -n YourMigrationName. - Open the new file in
src/migrations. - Comment out the bugged
SET DEFAULTlines generated by TypeORM every migration:(BUG: https://github.com/typeorm/typeorm/issues/3076 )public async up(queryRunner: QueryRunner): Promise<any> { // Comment out these "SET DEFAULT" lines // await queryRunner.query(`ALTER TABLE "graphql"."project" ALTER COLUMN "created_dt" SET DEFAULT (now() at time zone 'utc')`); // await queryRunner.query(`ALTER TABLE "graphql"."coloc_job" ALTER COLUMN "start" SET DEFAULT (now() at time zone 'utc')`); // await queryRunner.query(`ALTER TABLE "graphql"."coloc_job" ALTER COLUMN "finish" SET DEFAULT (now() at time zone 'utc')`); } public async down(queryRunner: QueryRunner): Promise<any> { // Comment out these "SET DEFAULT" lines // await queryRunner.query(`ALTER TABLE "graphql"."coloc_job" ALTER COLUMN "finish" SET DEFAULT timezone('utc'`); // await queryRunner.query(`ALTER TABLE "graphql"."coloc_job" ALTER COLUMN "start" SET DEFAULT timezone('utc'`); // await queryRunner.query(`ALTER TABLE "graphql"."project" ALTER COLUMN "created_dt" SET DEFAULT timezone('utc'`); } - Run
yarn exec typeorm migration:runand check that it runs correctly. - If it runs without error, but the changes are incorrect, run
yarn exec typeorm migration:revertto revert the last migration BEFORE attempting to fix the migration file. - Make sure to commit the migration file. WebStorm doesn't automatically add it to git - you have to find it in the "Unversioned Files" section of the commit window.
- Run
yarn run gen-sql-schemato re-generate thedb_schema.sqlscript.
- Run
yarn exec typeorm migration:runto make sure you are on the latest migration. - Run
yarn exec typeorm migration:createto make a new empty migration file.
The following steps can help debugging, if you have made a mistake in a feature branch:
- Change to the
masterbranch. - Delete branch-specific tables/columns in the database.
- Use
yarn exec typeorm migration:showto show migrations from the current branch, and manually delete any other migrations from thegraphql.migrationstable. - Run
yarn exec typeorm migration:runto ensure you have all migrations frommasterapplied. - Run
yarn exec typeorm schema:syncto re-synchronize the schema.