Skip to content

Instantly share code, notes, and snippets.

@michaelbromley
Last active July 20, 2020 10:56
Show Gist options
  • Save michaelbromley/7b4c2acaa51ac540e26023736d08fe6c to your computer and use it in GitHub Desktop.
Save michaelbromley/7b4c2acaa51ac540e26023736d08fe6c to your computer and use it in GitHub Desktop.
A TypeORM migration script for updating to Vendure v0.14.0
import { MigrationInterface, QueryRunner } from 'typeorm';
export class v01401594033611805 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<any> {
await queryRunner.query(
'CREATE TABLE `authentication_method` (`createdAt` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), `updatedAt` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), `identifier` varchar(255) NULL, `passwordHash` varchar(255) NULL, `verificationToken` varchar(255) NULL, `passwordResetToken` varchar(255) NULL, `identifierChangeToken` varchar(255) NULL, `pendingIdentifier` varchar(255) NULL, `strategy` varchar(255) NULL, `externalIdentifier` varchar(255) NULL, `metadata` text NULL, `id` int NOT NULL AUTO_INCREMENT, `type` varchar(255) NOT NULL, `userId` int NULL, INDEX `IDX_a23445b2c942d8dfcae15b8de2` (`type`), PRIMARY KEY (`id`)) ENGINE=InnoDB',
undefined,
);
await queryRunner.query(
`INSERT INTO authentication_method (identifier, passwordHash, verificationToken, passwordResetToken, identifierChangeToken, pendingIdentifier, type, userId)
SELECT identifier, passwordHash, verificationToken, passwordResetToken, identifierChangeToken, pendingIdentifier, 'NativeAuthenticationMethod', id FROM user`,
undefined,
);
await queryRunner.query('ALTER TABLE `user` DROP COLUMN `passwordHash`', undefined);
await queryRunner.query('ALTER TABLE `user` DROP COLUMN `verificationToken`', undefined);
await queryRunner.query('ALTER TABLE `user` DROP COLUMN `passwordResetToken`', undefined);
await queryRunner.query('ALTER TABLE `user` DROP COLUMN `identifierChangeToken`', undefined);
await queryRunner.query('ALTER TABLE `user` DROP COLUMN `pendingIdentifier`', undefined);
await queryRunner.query('ALTER TABLE `administrator` ADD `deletedAt` datetime NULL', undefined);
await queryRunner.query(
'ALTER TABLE `session` ADD `authenticationStrategy` varchar(255) NULL',
undefined,
);
// Other TypeORM-generated migration queries not related to the `user`, `session`, `administrator` or `authentication_method` tables
// should go here
}
public async down(queryRunner: QueryRunner): Promise<any> {
await queryRunner.query('ALTER TABLE `session` DROP COLUMN `authenticationStrategy`', undefined);
await queryRunner.query('ALTER TABLE `administrator` DROP COLUMN `deletedAt`', undefined);
await queryRunner.query(
'ALTER TABLE `user` ADD `pendingIdentifier` varchar(255) NULL DEFAULT \'NULL\'',
undefined,
);
await queryRunner.query(
'ALTER TABLE `user` ADD `identifierChangeToken` varchar(255) NULL DEFAULT \'NULL\'',
undefined,
);
await queryRunner.query(
'ALTER TABLE `user` ADD `passwordResetToken` varchar(255) NULL DEFAULT \'NULL\'',
undefined,
);
await queryRunner.query(
'ALTER TABLE `user` ADD `verificationToken` varchar(255) NULL DEFAULT \'NULL\'',
undefined,
);
await queryRunner.query('ALTER TABLE `user` ADD `passwordHash` varchar(255) NOT NULL', undefined);
await queryRunner.query(
`INSERT INTO user (passwordHash, verificationToken, passwordResetToken, identifierChangeToken, pendingIdentifier)
SELECT passwordHash, verificationToken, passwordResetToken, identifierChangeToken, pendingIdentifier FROM authentication_method
WHERE user.id = authentication_method.userId`,
undefined,
);
await queryRunner.query('DROP TABLE `authentication_method`', undefined);
// Other TypeORM-generated migration queries not related to the `user`, `session`, `administrator` or `authentication_method` tables
// should go here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment