mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-05 05:29:26 +08:00
fix(database): fix datetimeNoTz field (#6588)
* fix(database): fix datetimeNoTz field * fix(database): fix test case
This commit is contained in:
parent
4c3255d455
commit
164772afb8
@ -29,13 +29,12 @@ export class DatetimeNoTzField extends Field {
|
||||
return DatetimeNoTzTypeMySQL;
|
||||
}
|
||||
|
||||
return DataTypes.STRING;
|
||||
return DataTypes.DATE;
|
||||
}
|
||||
|
||||
init() {
|
||||
beforeSave = async (instance, options) => {
|
||||
const { name, defaultToCurrentTime, onUpdateToCurrentTime } = this.options;
|
||||
|
||||
this.beforeSave = async (instance, options) => {
|
||||
const value = instance.get(name);
|
||||
|
||||
if (!value && instance.isNewRecord && defaultToCurrentTime) {
|
||||
@ -48,7 +47,6 @@ export class DatetimeNoTzField extends Field {
|
||||
return;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
additionalSequelizeOptions(): {} {
|
||||
const { name } = this.options;
|
||||
@ -57,17 +55,14 @@ export class DatetimeNoTzField extends Field {
|
||||
const timezone = this.database.options.rawTimezone || '+00:00';
|
||||
|
||||
const isPg = this.database.inDialect('postgres');
|
||||
const isMySQLCompatibleDialect = this.database.isMySQLCompatibleDialect();
|
||||
|
||||
return {
|
||||
get() {
|
||||
const val = this.getDataValue(name);
|
||||
|
||||
if (val instanceof Date) {
|
||||
if (isPg) {
|
||||
return moment(val).format('YYYY-MM-DD HH:mm:ss');
|
||||
}
|
||||
// format to YYYY-MM-DD HH:mm:ss
|
||||
const momentVal = moment(val).utcOffset(timezone);
|
||||
const momentVal = moment(val);
|
||||
return momentVal.format('YYYY-MM-DD HH:mm:ss');
|
||||
}
|
||||
|
||||
@ -75,18 +70,24 @@ export class DatetimeNoTzField extends Field {
|
||||
},
|
||||
|
||||
set(val) {
|
||||
if (typeof val === 'string' && isIso8601(val)) {
|
||||
const momentVal = moment(val).utcOffset(timezone);
|
||||
val = momentVal.format('YYYY-MM-DD HH:mm:ss');
|
||||
if (val == null) {
|
||||
return this.setDataValue(name, null);
|
||||
}
|
||||
|
||||
if (val && val instanceof Date) {
|
||||
// format to YYYY-MM-DD HH:mm:ss
|
||||
const momentVal = moment(val).utcOffset(timezone);
|
||||
val = momentVal.format('YYYY-MM-DD HH:mm:ss');
|
||||
const dateOffset = new Date().getTimezoneOffset();
|
||||
const momentVal = moment(val);
|
||||
if ((typeof val === 'string' && isIso8601(val)) || val instanceof Date) {
|
||||
momentVal.utcOffset(timezone);
|
||||
momentVal.utcOffset(-dateOffset, true);
|
||||
}
|
||||
|
||||
return this.setDataValue(name, val);
|
||||
if (isMySQLCompatibleDialect) {
|
||||
momentVal.millisecond(0);
|
||||
}
|
||||
|
||||
const date = momentVal.toDate();
|
||||
|
||||
return this.setDataValue(name, date);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
@ -359,6 +359,50 @@ describe('workflow > triggers > collection', () => {
|
||||
const executions = await workflow.getExecutions();
|
||||
expect(executions.length).toBe(1);
|
||||
});
|
||||
|
||||
it('datetime field not changed', async () => {
|
||||
const workflow = await WorkflowModel.create({
|
||||
enabled: true,
|
||||
sync: true,
|
||||
type: 'collection',
|
||||
config: {
|
||||
mode: 2,
|
||||
collection: 'posts',
|
||||
changed: ['createdAt'],
|
||||
},
|
||||
});
|
||||
|
||||
const post = await PostRepo.create({ values: { title: 't1' } });
|
||||
await PostRepo.update({ filterByTk: post.id, values: { ...post.get(), title: 't2' } });
|
||||
|
||||
const executions = await workflow.getExecutions();
|
||||
expect(executions.length).toBe(0);
|
||||
});
|
||||
|
||||
it('datetimeNoTz field not changed', async () => {
|
||||
db.getCollection('posts').addField('dateOnly', {
|
||||
type: 'datetimeNoTz',
|
||||
});
|
||||
|
||||
await db.sync();
|
||||
|
||||
const workflow = await WorkflowModel.create({
|
||||
enabled: true,
|
||||
sync: true,
|
||||
type: 'collection',
|
||||
config: {
|
||||
mode: 2,
|
||||
collection: 'posts',
|
||||
changed: ['dateOnly'],
|
||||
},
|
||||
});
|
||||
|
||||
const post = await PostRepo.create({ values: { title: 't1', dateOnly: '2020-01-01 00:00:00' } });
|
||||
await PostRepo.update({ filterByTk: post.id, values: { ...post.get(), title: 't2' } });
|
||||
|
||||
const executions = await workflow.getExecutions();
|
||||
expect(executions.length).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('config.condition', () => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user