Skip to content

Commit

Permalink
feat: impl Date/timestamp on update (#203)
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**
- Introduced an `autoUpdate` property for `DateTime` and `Timestamp`
columns to enable automatic updating of timestamps.
- **Documentation**
- Updated documentation to include details about the new `autoUpdate`
property for time-related columns.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
killagu authored Apr 2, 2024
1 parent 9fd585d commit e5c7b8d
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 1 deletion.
2 changes: 2 additions & 0 deletions core/dal-decorator/src/decorator/Column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ export interface DateParams extends IColumnTypeParams {
export interface DateTimeParams extends IColumnTypeParams {
type: ColumnType.DATETIME;
precision?: number;
autoUpdate?: boolean;
}

export interface TimestampParams extends IColumnTypeParams {
type: ColumnType.TIMESTAMP;
precision?: number;
autoUpdate?: boolean;
}

export interface TimeParams extends IColumnTypeParams {
Expand Down
16 changes: 15 additions & 1 deletion core/dal-runtime/src/SqlGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,21 @@ export class SqlGenerator {
break;
}
case ColumnType.DATETIME:
case ColumnType.TIMESTAMP:
case ColumnType.TIMESTAMP: {
if (columnType.precision) {
sqls.push(`${columnType.type}(${columnType.precision})`);
} else {
sqls.push(columnType.type);
}
if (columnType.autoUpdate) {
if (columnType.precision) {
sqls.push(`ON UPDATE CURRENT_TIMESTAMP(${columnType.precision})`);
} else {
sqls.push('ON UPDATE CURRENT_TIMESTAMP');
}
}
break;
}
case ColumnType.TIME: {
if (columnType.precision) {
sqls.push(`${columnType.type}(${columnType.precision})`);
Expand Down
14 changes: 14 additions & 0 deletions core/dal-runtime/test/SqlGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assert from 'node:assert';
import { Foo } from './fixtures/modules/dal/Foo';
import { SqlGenerator } from '../src/SqlGenerator';
import { TableModel } from '@eggjs/dal-decorator';
import { AutoUpdateTime } from './fixtures/modules/dal/AutoUpdateTime';

describe('test/SqlGenerator.test.ts', () => {
it('generator should work', () => {
Expand Down Expand Up @@ -53,4 +54,17 @@ describe('test/SqlGenerator.test.ts', () => {
' UNIQUE KEY uk_name_col1 (name,col1) USING BTREE COMMENT \'index comment\\n\'\n' +
') DEFAULT CHARACTER SET utf8mb4, DEFAULT COLLATE utf8mb4_unicode_ci, COMMENT=\'foo table\';');
});

it('generator auto update should work', () => {
const generator = new SqlGenerator();
const autoUpdateTimeTableModel = TableModel.build(AutoUpdateTime);
const sql = generator.generate(autoUpdateTimeTableModel);
assert.equal(sql, 'CREATE TABLE IF NOT EXISTS auto_update_times (\n' +
' id INT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT \'the primary key\',\n' +
' date DATETIME ON UPDATE CURRENT_TIMESTAMP NOT NULL UNIQUE KEY,\n' +
' date_2 DATETIME(3) ON UPDATE CURRENT_TIMESTAMP(3) NOT NULL UNIQUE KEY,\n' +
' date_3 TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL UNIQUE KEY,\n' +
' date_4 TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) NOT NULL UNIQUE KEY\n' +
') ;');
});
});
51 changes: 51 additions & 0 deletions core/dal-runtime/test/fixtures/modules/dal/AutoUpdateTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
Column,
ColumnType,
Table,
} from '@eggjs/dal-decorator';

@Table()
export class AutoUpdateTime {
@Column({
type: ColumnType.INT,
}, {
primaryKey: true,
autoIncrement: true,
comment: 'the primary key',
})
id: number;

@Column({
type: ColumnType.DATETIME,
autoUpdate: true,
}, {
uniqueKey: true,
})
date: Date;

@Column({
type: ColumnType.DATETIME,
precision: 3,
autoUpdate: true,
}, {
uniqueKey: true,
})
date2: Date;

@Column({
type: ColumnType.TIMESTAMP,
autoUpdate: true,
}, {
uniqueKey: true,
})
date3: Date;

@Column({
type: ColumnType.TIMESTAMP,
precision: 3,
autoUpdate: true,
}, {
uniqueKey: true,
})
date4: Date;
}
6 changes: 6 additions & 0 deletions plugin/dal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,18 @@ export interface DateParams {
export interface DateTimeParams {
type: ColumnType.DATETIME;
precision?: number;
// 自动添加 ON UPDATE CURRENT_TIMESTAMP
// 如果有精度则为 ON UPDATE CURRENT_TIMESTAMP(precision)
autoUpdate?: boolean;
}
// Timestamp 类型,对应 js 中的 Date
export interface TimestampParams {
type: ColumnType.TIMESTAMP;
precision?: number;
// 自动添加 ON UPDATE CURRENT_TIMESTAMP
// 如果有精度则为 ON UPDATE CURRENT_TIMESTAMP(precision)
autoUpdate?: boolean;
}
// Times 类型,对应 js 中的 string
Expand Down

0 comments on commit e5c7b8d

Please sign in to comment.