-
-
Notifications
You must be signed in to change notification settings - Fork 499
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(mysql): enforce 64 character limit for identifier names in SQL (#…
- Loading branch information
Showing
4 changed files
with
87 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Collection, Entity, Logger, ManyToOne, MikroORM, OneToMany, PrimaryKey } from '@mikro-orm/core'; | ||
import { AbstractSqlDriver, SchemaGenerator } from '@mikro-orm/knex'; | ||
|
||
|
||
@Entity({ tableName: 'very_long_table_name_64_chars_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' }) | ||
class ChildEntity { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@ManyToOne({ type: 'ParentEntity' }) | ||
parent!: any; | ||
|
||
} | ||
|
||
@Entity() | ||
class ParentEntity { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@OneToMany({ entity: () => ChildEntity, mappedBy: 'parent' }) | ||
children = new Collection<ChildEntity>(this); | ||
|
||
} | ||
|
||
describe('GH issue 1271', () => { | ||
let orm: MikroORM<AbstractSqlDriver>; | ||
const log = jest.fn(); | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
entities: [ParentEntity, ChildEntity], | ||
dbName: `mikro_orm_test_gh_1271`, | ||
port: 3307, | ||
type: 'mysql', | ||
cache: { enabled: false }, | ||
}); | ||
const logger = new Logger(log, ['query', 'query-params']); | ||
Object.assign(orm.config, { logger }); | ||
|
||
await new SchemaGenerator(orm.em).ensureDatabase(); | ||
await orm.getSchemaGenerator().dropSchema(); | ||
}); | ||
|
||
afterAll(() => orm.close(true)); | ||
|
||
test('Index and FK names should be a max of 64 chars in mysql', async () => { | ||
const sql = await orm.getSchemaGenerator().getCreateSchemaSQL(); | ||
expect(sql).toMatchSnapshot(); | ||
await orm.getSchemaGenerator().createSchema(); | ||
}); | ||
}); |
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,16 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`GH issue 1271 Index and FK names should be a max of 64 chars in mysql 1`] = ` | ||
"set names utf8mb4; | ||
set foreign_key_checks = 0; | ||
create table \`parent_entity\` (\`id\` int unsigned not null auto_increment primary key) default character set utf8mb4 engine = InnoDB; | ||
create table \`very_long_table_name_64_chars_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\` (\`id\` int unsigned not null auto_increment primary key, \`parent_id\` int(11) unsigned not null) default character set utf8mb4 engine = InnoDB; | ||
alter table \`very_long_table_name_64_chars_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\` add index \`very_long_table_name_64_chars_xxxxxxxxxxxxxxxxxxxxxx_d90a7_index\`(\`parent_id\`); | ||
alter table \`very_long_table_name_64_chars_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\` add constraint \`very_long_table_name_64_chars_xxxxxxxxxxxxxxxxxxxx_19cce_foreign\` foreign key (\`parent_id\`) references \`parent_entity\` (\`id\`) on update cascade; | ||
set foreign_key_checks = 1; | ||
" | ||
`; |