Skip to content

Commit

Permalink
[db] Move migrations from *.sql to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
geropl authored and roboquat committed Nov 8, 2021
1 parent 54bbe8d commit 9038ae9
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 59 deletions.
6 changes: 5 additions & 1 deletion components/gitpod-db/BUILD.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ packages:
type: yarn
srcs:
- "src/typeorm/migration/**/*.ts"
- "src/typeorm/migrate-migrations-0_2_0.ts"
- "src/typeorm/entity/*.ts"
- "src/typeorm/ormconfig.ts"
- "src/typeorm/typeorm.ts"
Expand All @@ -25,6 +26,7 @@ packages:
- "src/typeorm/transformer.ts"
- "src/config.ts"
- "src/wait-for-db.ts"
- "src/migrate-migrations.ts"
- "src/user-db.ts"
- "package.json"
deps:
Expand Down Expand Up @@ -73,6 +75,9 @@ packages:
- ["sh", "-c", "find chart-config-db-init--init-scripts -name \"*.sql\" | sort | xargs cat | mysql -h \"$DB_HOST\" -P \"$DB_PORT\" -p$DB_PASSWORD -u $DB_USER"]
# Run DB migrations
- ["sh", "-c", "mkdir -p mig; cd mig; ../components-gitpod-db--migrations/install.sh"]
# migrate 'migrations'
- ["yarn", "--cwd", "mig/node_modules/@gitpod/gitpod-db", "run", "migrate-migrations"]
# Run actual migrations
- ["yarn", "--cwd", "mig/node_modules/@gitpod/gitpod-db", "typeorm", "migrations:run"]
- name: docker
type: docker
Expand All @@ -81,7 +86,6 @@ packages:
- migrate_gcp.sh
- typeorm.sh
- typeorm_gcp.sh
- migrate-migrations/**
deps:
- :migrations
argdeps:
Expand Down
1 change: 0 additions & 1 deletion components/gitpod-db/leeway.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ COPY migrate.sh /app/migrate.sh
COPY migrate_gcp.sh /app/migrate_gcp.sh
COPY typeorm.sh /app/typeorm.sh
COPY typeorm_gcp.sh /app/typeorm_gcp.sh
COPY migrate-migrations /app/migrate-migrations
RUN mkdir /home/jenkins && chown -R 10000 /home/jenkins
COPY --from=proxy /bin/cloud_sql_proxy /bin/cloud_sql_proxy
COPY --from=proxy /etc/ssl/certs/ /etc/ssl/certs/
Expand Down
21 changes: 0 additions & 21 deletions components/gitpod-db/migrate-migrations/0_2_0_down_procedure.sql

This file was deleted.

27 changes: 0 additions & 27 deletions components/gitpod-db/migrate-migrations/0_2_0_up_procedure.sql

This file was deleted.

6 changes: 2 additions & 4 deletions components/gitpod-db/migrate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

set -euo pipefail

# perform migration of 'migrations' table
/app/typeorm.sh query "$(sed '/^--/d' < /app/migrate-migrations/0_2_0_up_procedure.sql)"
/app/typeorm.sh query "CALL migrations_0_2_0_up();"
/app/typeorm.sh query "DROP PROCEDURE IF EXISTS migrations_0_2_0_up;"
# migrate 'migrations' table
yarn --cwd /app/node_modules/@gitpod/gitpod-db run migrate-migrations

/app/typeorm.sh migrations:run
6 changes: 2 additions & 4 deletions components/gitpod-db/migrate_gcp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@

set -euo pipefail

# perform migration of 'migrations' table
/app/typeorm.sh query "$(sed '/^--/d' < /app/migrate-migrations/0_2_0_up_procedure.sql)"
/app/typeorm.sh query "CALL migrations_0_2_0_up();"
/app/typeorm.sh query "DROP PROCEDURE IF EXISTS migrations_0_2_0_up;"
# migrate 'migrations' table
yarn --cwd /app/node_modules/@gitpod/gitpod-db run migrate-migrations

/app/typeorm_gcp.sh migrations:run
1 change: 1 addition & 0 deletions components/gitpod-db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"db-test-run": "mocha --opts mocha.opts '**/*.spec.db.ts' --exclude './node_modules/**'",
"wait-for-db": "node ./lib/wait-for-db.js",
"typeorm": "typeorm -f lib/typeorm/ormconfig",
"migrate-migrations": "node ./lib/migrate-migrations.js",
"clean": "yarn run rimraf lib",
"clean:node": "yarn run rimraf node_modules",
"purge": "yarn clean && yarn clean:node && yarn run rimraf yarn.lock"
Expand Down
29 changes: 29 additions & 0 deletions components/gitpod-db/src/migrate-migrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (c) 2021 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License-AGPL.txt in the project root for license information.
*/


import { TypeORM } from "./typeorm/typeorm";
import { Config } from "./config";
import { MigrateMigrations0_2_0 } from "./typeorm/migrate-migrations-0_2_0";


async function migrateMigrationsTable() {
const config = new Config();
const typeorm = new TypeORM(config, {});
const conn = await typeorm.getConnection();

const runner = conn.createQueryRunner();
const migration_0_2_0 = new MigrateMigrations0_2_0();
await migration_0_2_0.up(runner);

conn.close();
console.log("successfully migrated 'migrations' table.");
}

migrateMigrationsTable().catch((err) => {
console.error(err);
process.exit(1);
});
43 changes: 43 additions & 0 deletions components/gitpod-db/src/typeorm/migrate-migrations-0_2_0.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2021 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License-AGPL.txt in the project root for license information.
*/

import { MigrationInterface, QueryRunner } from "typeorm";
import { columnExists, tableExists } from "./migration/helper/helper";

/**
* This is a migration that is necessary due to our switch from typeorm 0.1.x to 0.2.x.
* As this affects the migration process itself, we cannot use it to fix itself. Instead, we add another
* entrypoint that triggers this "meta-migration", which re-uses the common migration interface.
*/
export class MigrateMigrations0_2_0 implements MigrationInterface {

public async up(queryRunner: QueryRunner): Promise<any> {
if (await tableExists(queryRunner, "migrations")) {
const idColumnExists = await columnExists(queryRunner, "migrations", "id");
if (!idColumnExists) {
await queryRunner.query(`ALTER TABLE migrations ADD COLUMN id int(11) NOT NULL;`);
await queryRunner.query(`SET @next_id := 0;`);
await queryRunner.query(`UPDATE migrations SET id = @next_id := @next_id + 1 ORDER BY timestamp ASC;`);
await queryRunner.query(`ALTER TABLE migrations DROP PRIMARY KEY;`);
await queryRunner.query(`ALTER TABLE migrations ADD PRIMARY KEY (id);`);
await queryRunner.query(`ALTER TABLE migrations MODIFY COLUMN id int(11) NOT NULL AUTO_INCREMENT;`);
}
}
}

public async down(queryRunner: QueryRunner): Promise<any> {
if (await tableExists(queryRunner, "migrations")) {
const idColumnExists = await columnExists(queryRunner, "migrations", "id");
if (idColumnExists) {
await queryRunner.query(`ALTER TABLE migrations MODIFY COLUMN id int(11);`);
await queryRunner.query(`ALTER TABLE migrations DROP PRIMARY KEY;`);
await queryRunner.query(`ALTER TABLE migrations ADD PRIMARY KEY (timestamp);`);
await queryRunner.query(`ALTER TABLE migrations DROP COLUMN id;`);
}
}
}

}
2 changes: 1 addition & 1 deletion components/gitpod-db/src/typeorm/typeorm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class TypeORM {
protected readonly _options: ConnectionOptions;

constructor(@inject(Config) protected readonly config: Config,
@inject(TypeORMOptions) @optional() protected readonly options: Partial<ConnectionOptions>) {
@inject(TypeORMOptions) @optional() protected readonly options?: Partial<ConnectionOptions>) {
options = options || {};
this._options = {
...TypeORM.defaultOptions(__dirname),
Expand Down

0 comments on commit 9038ae9

Please sign in to comment.