Skip to content

Commit

Permalink
fix: generate index name with column name (#230)
Browse files Browse the repository at this point in the history
<!--
Thank you for your pull request. Please review below requirements.
Bug fixes and new features should include tests and possibly benchmarks.
Contributors guide:
https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md

感谢您贡献代码。请确认下列 checklist 的完成情况。
Bug 修复和新功能必须包含测试,必要时请附上性能测试。
Contributors guide:
https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
-->

##### Checklist
<!-- Remove items that do not apply. For completed items, change [ ] to
[x]. -->

- [ ] `npm test` passes
- [ ] tests and/or benchmarks are included
- [ ] documentation is changed or added
- [ ] commit message follows commit guidelines

##### Affected core subsystem(s)
<!-- Provide affected core subsystem(s). -->


##### Description of change
<!-- Provide a description of the change below this comment. -->

<!--
- any feature?
- close https://github.com/eggjs/egg/ISSUE_URL
-->

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit


- **New Features**
- Enhanced index building functionality in the application by allowing
class type references during index creation.
- Introduced a new database table schema representation, improving the
integration with ORM frameworks.

- **Bug Fixes**
- Improved error messages for clearer identification of issues related
to missing columns in index creation.

- **Tests**
- Added a new test case for the SQL generator, ensuring accurate SQL
generation for the new database table model.
- Modified test logic to skip execution on macOS and Windows platforms,
enhancing robustness.

- **Chores**
- Updated MySQL installation and startup commands in GitHub Actions for
improved compatibility and simplicity.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
killagu authored Aug 9, 2024
1 parent 38c68db commit 82ec72d
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ jobs:
node-version: ${{ matrix.node-version }}

- name: Install Mysql
run: brew install mysql@5.7
run: brew install mysql

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

- name: Install Npm
run: npm i -g npm@9
Expand Down
8 changes: 4 additions & 4 deletions core/dal-decorator/src/model/IndexModel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IndexType } from '@eggjs/tegg-types';
import { EggProtoImplClass, IndexType } from '@eggjs/tegg-types';
import type { IndexParams, IndexStoreType } from '@eggjs/tegg-types';
import { ColumnModel } from './ColumnModel';

Expand Down Expand Up @@ -43,19 +43,19 @@ export class IndexModel {
return prefix + keys.join('_');
}

static build(params: IndexParams, columns: ColumnModel[]) {
static build(params: IndexParams, columns: ColumnModel[], clazz: EggProtoImplClass<unknown>) {
const type = params.type ?? IndexType.INDEX;
const name = params.name ?? IndexModel.buildIndexName(params.keys, type);
const keys: Array<IndexKey> = params.keys.map(t => {
const column = columns.find(c => c.propertyName === t);
if (!column) {
throw new Error(`Index "${name}" configuration error: has no property named "${t}"`);
throw new Error(`Table ${clazz.name} index configuration error: has no property named "${t}"`);
}
return {
propertyName: column!.propertyName,
columnName: column!.columnName,
};
});
const name = params.name ?? IndexModel.buildIndexName(keys.map(t => t.columnName), type);
return new IndexModel({
name,
keys,
Expand Down
2 changes: 1 addition & 1 deletion core/dal-decorator/src/model/TableModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class TableModel<T=object> {

const indexList = IndexInfoUtil.getIndexList(clazz as EggProtoImplClass);
for (const index of indexList) {
indices.push(IndexModel.build(index, columns));
indices.push(IndexModel.build(index, columns, clazz));
}

return new TableModel({
Expand Down
17 changes: 17 additions & 0 deletions core/dal-runtime/test/SqlGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TableModel } from '@eggjs/dal-decorator';
import { Foo } from './fixtures/modules/dal/Foo';
import { SqlGenerator } from '../src/SqlGenerator';
import { AutoUpdateTime } from './fixtures/modules/dal/AutoUpdateTime';
import { FooIndexName } from './fixtures/modules/dal/FooIndexName';

describe('test/SqlGenerator.test.ts', () => {
it('generator should work', () => {
Expand Down Expand Up @@ -67,4 +68,20 @@ describe('test/SqlGenerator.test.ts', () => {
' date_4 TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) NOT NULL UNIQUE KEY\n' +
') ;');
});

it('generator index name should work', () => {
const generator = new SqlGenerator();
const fooIndexNameTableModel = TableModel.build(FooIndexName);
const sql = generator.generate(fooIndexNameTableModel);
assert.equal(sql, 'CREATE TABLE IF NOT EXISTS egg_foo (\n' +
' id INT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT \'the primary key\',\n' +
' name VARCHAR(100) NOT NULL UNIQUE KEY,\n' +
' col1 VARCHAR(100) NOT NULL,\n' +
' bit_column BIT(10) NOT NULL,\n' +
' bool_column BOOL NOT NULL,\n' +
' FULLTEXT KEY idx_col1_bool_column (col1,bool_column) COMMENT \'index comment\\n\',\n' +
' UNIQUE KEY uk_name_col1_bit_column (name,col1,bit_column) USING BTREE COMMENT \'index comment\\n\'\n' +
') DEFAULT CHARACTER SET utf8mb4, DEFAULT COLLATE utf8mb4_unicode_ci, COMMENT=\'foo table\';',
);
});
});
63 changes: 63 additions & 0 deletions core/dal-runtime/test/fixtures/modules/dal/FooIndexName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {
Column,
ColumnType,
Index,
IndexStoreType,
IndexType,
Table,
} from '@eggjs/dal-decorator';

@Table({
name: 'egg_foo',
comment: 'foo table',
characterSet: 'utf8mb4',
collate: 'utf8mb4_unicode_ci',
})
@Index({
keys: [ 'name', 'col1', 'bitColumn' ],
type: IndexType.UNIQUE,
storeType: IndexStoreType.BTREE,
comment: 'index comment\n',
})
@Index({
keys: [ 'col1', 'boolColumn' ],
type: IndexType.FULLTEXT,
comment: 'index comment\n',
})
export class FooIndexName {
@Column({
type: ColumnType.INT,
}, {
primaryKey: true,
autoIncrement: true,
comment: 'the primary key',
})
id: number;

@Column({
type: ColumnType.VARCHAR,
length: 100,
}, {
uniqueKey: true,
})
name: string;

@Column({
type: ColumnType.VARCHAR,
length: 100,
}, {
name: 'col1',
})
col1: string;

@Column({
type: ColumnType.BIT,
length: 10,
})
bitColumn: Buffer;

@Column({
type: ColumnType.BOOL,
})
boolColumn: 0 | 1;
}
2 changes: 1 addition & 1 deletion plugin/orm/test/fixtures/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async function init() {
(async () => {
try {
// TODO win32 ci not support mysql
if (os.platform() === 'win32') {
if ([ 'darwin', 'win32' ].includes(os.platform())) {
return;
}
connect();
Expand Down
2 changes: 1 addition & 1 deletion plugin/orm/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { EggContext } from '@eggjs/tegg';

describe('plugin/orm/test/orm.test.ts', () => {
// TODO win32 ci not support mysql
if (os.platform() === 'win32') {
if ([ 'darwin', 'win32' ].includes(os.platform())) {
return;
}

Expand Down

0 comments on commit 82ec72d

Please sign in to comment.