Skip to content

Commit

Permalink
fix(schema): fix renaming of multiple columns at the same time
Browse files Browse the repository at this point in the history
Closes #1262
  • Loading branch information
B4nan committed Jan 13, 2021
1 parent a0419a4 commit 677a2b7
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/knex/src/schema/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,10 @@ export class SchemaGenerator {
for (const prop of create) {
for (const fieldName of prop.fieldNames) {
const match = remove.find(column => {
if (renamed.some(item => item.from === column)) {
return false;
}

const copy = Utils.copy(column);
copy.name = fieldName;

Expand All @@ -519,6 +523,8 @@ export class SchemaGenerator {
remove.splice(remove.indexOf(prop.from), 1);
});

console.log('findRenamedColumns', renamed);

return renamed;
}

Expand Down
62 changes: 62 additions & 0 deletions tests/issues/GH1262.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import 'reflect-metadata';
import { Entity, MikroORM, PrimaryKey, Property } from '@mikro-orm/core';
import { remove } from 'fs-extra';
import { TEMP_DIR } from '../bootstrap';

@Entity({ tableName: 'user' })
class UserBefore {

@PrimaryKey()
id!: string;

@Property()
created: Date = new Date();

@Property()
updated: Date = new Date();

@Property()
deleted: Date = new Date();

}

@Entity({ tableName: 'user' })
class UserAfter {

@PrimaryKey()
id!: string;

@Property()
createdAt: Date = new Date();

@Property()
updatedAt: Date = new Date();

@Property()
deletedAt: Date = new Date();

}

describe('GH issue 1262', () => {

async function createAndRunMigration(entities: any[]) {
const db = await MikroORM.init({
type: 'sqlite',
entities,
dbName: TEMP_DIR + '/gh_1262.db',
});

await db.getSchemaGenerator().updateSchema();
await db.close();
}

test('renaming multiple columns at once', async () => {
await remove(TEMP_DIR + '/gh_1262.db');
await createAndRunMigration([UserBefore]);

// Simulates adding `profile` to the User entity
await createAndRunMigration([UserAfter]);
await remove(TEMP_DIR + '/gh_1262.db');
});

});

0 comments on commit 677a2b7

Please sign in to comment.