-
-
Notifications
You must be signed in to change notification settings - Fork 518
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(schema): add ability to ignore specific column changes (#3503)
```ts @Property({ columnType: 'timestamp', extra: 'VIRTUAL GENERATED', ignoreSchemaChanges: ['type', 'extra'], }) changingField!: Date; ``` This is useful for situations such as #1904, where `knex` is unable to properly diff the column. Closes #1904
- Loading branch information
1 parent
0af0d58
commit 05fb1ce
Showing
8 changed files
with
212 additions
and
2 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
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
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,114 @@ | ||
import { Entity, MikroORM, PrimaryKey, Property } from '@mikro-orm/core'; | ||
import type { MySqlDriver } from '@mikro-orm/mysql'; | ||
|
||
@Entity({ tableName: 'book' }) | ||
export class Book1 { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property({ columnType: 'int' }) | ||
changingField!: number; | ||
|
||
} | ||
|
||
@Entity({ tableName: 'book' }) | ||
export class Book2 { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property({ columnType: 'timestamp', ignoreSchemaChanges: ['type'] }) | ||
changingField!: Date; | ||
|
||
} | ||
|
||
@Entity({ tableName: 'book' }) | ||
export class Book3 { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property({ | ||
columnType: 'int', | ||
extra: 'VIRTUAL GENERATED', | ||
ignoreSchemaChanges: ['extra'], | ||
}) | ||
changingField!: number; | ||
|
||
} | ||
|
||
@Entity({ tableName: 'book' }) | ||
export class Book4 { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property({ | ||
columnType: 'timestamp', | ||
extra: 'VIRTUAL GENERATED', | ||
ignoreSchemaChanges: ['extra', 'type'], | ||
}) | ||
changingField!: Date; | ||
|
||
} | ||
|
||
@Entity({ tableName: 'book' }) | ||
export class Book5 { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property({ columnType: 'timestamp' }) | ||
changingField!: Date; | ||
|
||
} | ||
|
||
describe('ignore specific schema changes (GH 1904)', () => { | ||
let orm: MikroORM<MySqlDriver>; | ||
|
||
beforeEach(async () => { | ||
orm = await MikroORM.init({ | ||
entities: [Book1], | ||
dbName: `mikro_orm_test_gh_1904`, | ||
type: 'mysql', | ||
port: 3308, | ||
}); | ||
await orm.schema.refreshDatabase(); | ||
}); | ||
|
||
afterEach(() => orm.close(true)); | ||
|
||
test('schema generator respects ignoreSchemaChanges for `type`', async () => { | ||
const diff0 = await orm.schema.getUpdateSchemaSQL({ wrap: false }); | ||
expect(diff0).toBe(''); | ||
await orm.discoverEntity(Book2); | ||
orm.getMetadata().reset('Book1'); | ||
const diff1 = await orm.schema.getUpdateSchemaSQL({ wrap: false }); | ||
expect(diff1).toBe(''); | ||
|
||
// Once we remove ignoreSchemaChanges, we should see a diff again. | ||
await orm.discoverEntity(Book5); | ||
orm.getMetadata().reset('Book2'); | ||
const diff2 = await orm.schema.getUpdateSchemaSQL({ wrap: false }); | ||
expect(diff2).toBe('alter table `book` modify `changing_field` timestamp not null;\n\n'); | ||
}); | ||
|
||
test('schema generator respects ignoreSchemaChanges for `extra`', async () => { | ||
const diff0 = await orm.schema.getUpdateSchemaSQL({ wrap: false }); | ||
expect(diff0).toBe(''); | ||
await orm.discoverEntity(Book3); | ||
orm.getMetadata().reset('Book1'); | ||
const diff1 = await orm.schema.getUpdateSchemaSQL({ wrap: false }); | ||
expect(diff1).toBe(''); | ||
}); | ||
|
||
test('schema generator respects ignoreSchemaChanges for `extra` and `type`', async () => { | ||
const diff0 = await orm.schema.getUpdateSchemaSQL({ wrap: false }); | ||
expect(diff0).toBe(''); | ||
await orm.discoverEntity(Book4); | ||
orm.getMetadata().reset('Book1'); | ||
const diff1 = await orm.schema.getUpdateSchemaSQL({ wrap: false }); | ||
expect(diff1).toBe(''); | ||
}); | ||
}); |