Skip to content

Commit 335d338

Browse files
committed
fix: generate index name with column name
1 parent 38c68db commit 335d338

File tree

7 files changed

+89
-9
lines changed

7 files changed

+89
-9
lines changed

.github/workflows/nodejs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ jobs:
6262
node-version: ${{ matrix.node-version }}
6363

6464
- name: Install Mysql
65-
run: brew install mysql@5.7
65+
run: brew install mysql
6666

6767
- name: Start Mysql
6868
## arm64/x86 homebrew mysql path different
69-
run: /usr/local/opt/[email protected]/bin/mysql.server start || /opt/homebrew/Cellar/mysql@5.7/5.7.44_1/bin/mysql.server start
69+
run: brew services start mysql
7070

7171
- name: Install Npm
7272
run: npm i -g npm@9

core/dal-decorator/src/model/IndexModel.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IndexType } from '@eggjs/tegg-types';
1+
import { EggProtoImplClass, IndexType } from '@eggjs/tegg-types';
22
import type { IndexParams, IndexStoreType } from '@eggjs/tegg-types';
33
import { ColumnModel } from './ColumnModel';
44

@@ -43,19 +43,19 @@ export class IndexModel {
4343
return prefix + keys.join('_');
4444
}
4545

46-
static build(params: IndexParams, columns: ColumnModel[]) {
46+
static build(params: IndexParams, columns: ColumnModel[], clazz: EggProtoImplClass<unknown>) {
4747
const type = params.type ?? IndexType.INDEX;
48-
const name = params.name ?? IndexModel.buildIndexName(params.keys, type);
4948
const keys: Array<IndexKey> = params.keys.map(t => {
5049
const column = columns.find(c => c.propertyName === t);
5150
if (!column) {
52-
throw new Error(`Index "${name}" configuration error: has no property named "${t}"`);
51+
throw new Error(`Table ${clazz.name} index configuration error: has no property named "${t}"`);
5352
}
5453
return {
5554
propertyName: column!.propertyName,
5655
columnName: column!.columnName,
5756
};
5857
});
58+
const name = params.name ?? IndexModel.buildIndexName(keys.map(t => t.columnName), type);
5959
return new IndexModel({
6060
name,
6161
keys,

core/dal-decorator/src/model/TableModel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export class TableModel<T=object> {
113113

114114
const indexList = IndexInfoUtil.getIndexList(clazz as EggProtoImplClass);
115115
for (const index of indexList) {
116-
indices.push(IndexModel.build(index, columns));
116+
indices.push(IndexModel.build(index, columns, clazz));
117117
}
118118

119119
return new TableModel({

core/dal-runtime/test/SqlGenerator.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { TableModel } from '@eggjs/dal-decorator';
33
import { Foo } from './fixtures/modules/dal/Foo';
44
import { SqlGenerator } from '../src/SqlGenerator';
55
import { AutoUpdateTime } from './fixtures/modules/dal/AutoUpdateTime';
6+
import { FooIndexName } from './fixtures/modules/dal/FooIndexName';
67

78
describe('test/SqlGenerator.test.ts', () => {
89
it('generator should work', () => {
@@ -67,4 +68,20 @@ describe('test/SqlGenerator.test.ts', () => {
6768
' date_4 TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) NOT NULL UNIQUE KEY\n' +
6869
') ;');
6970
});
71+
72+
it('generator index name should work', () => {
73+
const generator = new SqlGenerator();
74+
const fooIndexNameTableModel = TableModel.build(FooIndexName);
75+
const sql = generator.generate(fooIndexNameTableModel);
76+
assert.equal(sql, 'CREATE TABLE IF NOT EXISTS egg_foo (\n' +
77+
' id INT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT \'the primary key\',\n' +
78+
' name VARCHAR(100) NOT NULL UNIQUE KEY,\n' +
79+
' col1 VARCHAR(100) NOT NULL,\n' +
80+
' bit_column BIT(10) NOT NULL,\n' +
81+
' bool_column BOOL NOT NULL,\n' +
82+
' FULLTEXT KEY idx_col1_bool_column (col1,bool_column) COMMENT \'index comment\\n\',\n' +
83+
' UNIQUE KEY uk_name_col1_bit_column (name,col1,bit_column) USING BTREE COMMENT \'index comment\\n\'\n' +
84+
') DEFAULT CHARACTER SET utf8mb4, DEFAULT COLLATE utf8mb4_unicode_ci, COMMENT=\'foo table\';',
85+
);
86+
});
7087
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import {
2+
Column,
3+
ColumnType,
4+
Index,
5+
IndexStoreType,
6+
IndexType,
7+
Table,
8+
} from '@eggjs/dal-decorator';
9+
10+
@Table({
11+
name: 'egg_foo',
12+
comment: 'foo table',
13+
characterSet: 'utf8mb4',
14+
collate: 'utf8mb4_unicode_ci',
15+
})
16+
@Index({
17+
keys: [ 'name', 'col1', 'bitColumn' ],
18+
type: IndexType.UNIQUE,
19+
storeType: IndexStoreType.BTREE,
20+
comment: 'index comment\n',
21+
})
22+
@Index({
23+
keys: [ 'col1', 'boolColumn' ],
24+
type: IndexType.FULLTEXT,
25+
comment: 'index comment\n',
26+
})
27+
export class FooIndexName {
28+
@Column({
29+
type: ColumnType.INT,
30+
}, {
31+
primaryKey: true,
32+
autoIncrement: true,
33+
comment: 'the primary key',
34+
})
35+
id: number;
36+
37+
@Column({
38+
type: ColumnType.VARCHAR,
39+
length: 100,
40+
}, {
41+
uniqueKey: true,
42+
})
43+
name: string;
44+
45+
@Column({
46+
type: ColumnType.VARCHAR,
47+
length: 100,
48+
}, {
49+
name: 'col1',
50+
})
51+
col1: string;
52+
53+
@Column({
54+
type: ColumnType.BIT,
55+
length: 10,
56+
})
57+
bitColumn: Buffer;
58+
59+
@Column({
60+
type: ColumnType.BOOL,
61+
})
62+
boolColumn: 0 | 1;
63+
}

plugin/orm/test/fixtures/prepare.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async function init() {
5555
(async () => {
5656
try {
5757
// TODO win32 ci not support mysql
58-
if (os.platform() === 'win32') {
58+
if ([ 'darwin', 'win32' ].includes(os.platform())) {
5959
return;
6060
}
6161
connect();

plugin/orm/test/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { EggContext } from '@eggjs/tegg';
1313

1414
describe('plugin/orm/test/orm.test.ts', () => {
1515
// TODO win32 ci not support mysql
16-
if (os.platform() === 'win32') {
16+
if ([ 'darwin', 'win32' ].includes(os.platform())) {
1717
return;
1818
}
1919

0 commit comments

Comments
 (0)