Skip to content

Commit

Permalink
fix(schema): skip changes of enum items on enum arrays
Browse files Browse the repository at this point in the history
They are not enforced on schema level, so it does not make sense to compare the items.

Related #476
  • Loading branch information
B4nan committed Oct 15, 2023
1 parent 5a28e9b commit 9accdf6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
17 changes: 15 additions & 2 deletions packages/knex/src/schema/SchemaComparator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { inspect } from 'util';
import { BooleanType, DateTimeType, JsonType, parseJsonSafe, Utils, type Dictionary, type EntityProperty } from '@mikro-orm/core';
import {
ArrayType,
BooleanType,
DateTimeType,
JsonType,
parseJsonSafe,
Utils,
type Dictionary,
type EntityProperty,
} from '@mikro-orm/core';
import type { Check, Column, ForeignKey, Index, SchemaDifference, TableDifference } from '../typings';
import type { DatabaseSchema } from './DatabaseSchema';
import type { DatabaseTable } from './DatabaseTable';
Expand Down Expand Up @@ -443,7 +452,11 @@ export class SchemaComparator {
changedProperties.add('comment');
}

if (this.diffEnumItems(column1.enumItems, column2.enumItems)) {
if (
!(column1.mappedType instanceof ArrayType) &&
!(column2.mappedType instanceof ArrayType) &&
this.diffEnumItems(column1.enumItems, column2.enumItems)
) {
log(`'enumItems' changed for column ${tableName}.${column1.name}`, { column1, column2 });
changedProperties.add('enumItems');
}
Expand Down
36 changes: 36 additions & 0 deletions tests/features/schema-generator/enum-array.postgres.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Entity, Enum, PrimaryKey } from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/postgresql';

enum AdminPermission {
ROOT = 'ROOT',
ACCESS = 'ACCESS',
}

@Entity()
class Admin {

@PrimaryKey()
id!: number;

@Enum({
items: () => AdminPermission,
array: true,
default: [],
})
permissions: AdminPermission[] = [];

}

test('enum array diffing', async () => {
const orm = await MikroORM.init({
entities: [Admin],
dbName: `mikro_orm_test_enum_array_diffing`,
});

await orm.schema.refreshDatabase();

const diff = await orm.schema.getUpdateSchemaSQL({ wrap: false });
expect(diff).toBe('');

await orm.close(true);
});

0 comments on commit 9accdf6

Please sign in to comment.