forked from wangziqi/gongxue-base
fix permissions and teacher attendance workflows
This commit is contained in:
41
apps/server/src/database/database-migrations.service.ts
Normal file
41
apps/server/src/database/database-migrations.service.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { Injectable, Logger, OnApplicationBootstrap } from '@nestjs/common';
|
||||
import { DataSource } from 'typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class DatabaseMigrationsService implements OnApplicationBootstrap {
|
||||
private readonly logger = new Logger(DatabaseMigrationsService.name);
|
||||
|
||||
constructor(private readonly dataSource: DataSource) {}
|
||||
|
||||
async onApplicationBootstrap(): Promise<void> {
|
||||
await this.normalizeClassDates();
|
||||
}
|
||||
|
||||
private async normalizeClassDates(): Promise<void> {
|
||||
const driver = this.dataSource.options.type;
|
||||
const dateExpression = (column: string) =>
|
||||
driver === 'mysql' ? `DATE(${column})` : `substr(${column}, 1, 10)`;
|
||||
|
||||
const lengthFunction = driver === 'mysql' ? 'CHAR_LENGTH' : 'length';
|
||||
const result = await this.dataSource.transaction((manager) =>
|
||||
manager.query(`
|
||||
UPDATE classes
|
||||
SET
|
||||
start_date = CASE
|
||||
WHEN start_date IS NULL OR start_date = '' THEN start_date
|
||||
ELSE ${dateExpression('start_date')}
|
||||
END,
|
||||
end_date = CASE
|
||||
WHEN end_date IS NULL OR end_date = '' THEN end_date
|
||||
ELSE ${dateExpression('end_date')}
|
||||
END
|
||||
WHERE
|
||||
(start_date IS NOT NULL AND ${lengthFunction}(start_date) > 10)
|
||||
OR (end_date IS NOT NULL AND ${lengthFunction}(end_date) > 10)
|
||||
`),
|
||||
);
|
||||
|
||||
const affected = typeof result?.changes === 'number' ? result.changes : result?.affectedRows;
|
||||
if (affected) this.logger.log(`已规范化 ${affected} 条班级日期数据`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user