-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[db] Move migrations from *.sql to TS
- Loading branch information
Showing
10 changed files
with
83 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
components/gitpod-db/migrate-migrations/0_2_0_down_procedure.sql
This file was deleted.
Oops, something went wrong.
27 changes: 0 additions & 27 deletions
27
components/gitpod-db/migrate-migrations/0_2_0_up_procedure.sql
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
components/gitpod-db/src/typeorm/migrate-migrations-0_2_0.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;`); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters