refactor(classes): preserve student membership history
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { getDataSourceToken } from '@nestjs/typeorm';
|
||||
import { DatabaseMigrationsService } from './database-migrations.service';
|
||||
|
||||
function createRunner(tableExists: boolean, columns: string[] = []) {
|
||||
return {
|
||||
connect: jest.fn(),
|
||||
release: jest.fn(),
|
||||
getTables: jest.fn().mockResolvedValue(tableExists ? [{ name: 'class_student' }] : []),
|
||||
getTable: jest.fn().mockResolvedValue({
|
||||
name: 'class_student',
|
||||
columns: columns.map((name) => ({ name })),
|
||||
}),
|
||||
dropColumn: jest.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
}
|
||||
|
||||
async function createService(runner: ReturnType<typeof createRunner>) {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
DatabaseMigrationsService,
|
||||
{
|
||||
provide: getDataSourceToken(),
|
||||
useValue: {
|
||||
options: { type: 'better-sqlite3' },
|
||||
createQueryRunner: jest.fn().mockReturnValue(runner),
|
||||
},
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
return module.get(DatabaseMigrationsService) as DatabaseMigrationsService & {
|
||||
removeUnusedClassStudentColumns(): Promise<void>;
|
||||
};
|
||||
}
|
||||
|
||||
describe('DatabaseMigrationsService — class student cleanup', () => {
|
||||
it('drops the unused enrollment_id column', async () => {
|
||||
const runner = createRunner(true, ['id', 'enrollment_id']);
|
||||
const service = await createService(runner);
|
||||
|
||||
await service.removeUnusedClassStudentColumns();
|
||||
|
||||
expect(runner.dropColumn).toHaveBeenCalledWith('class_student', 'enrollment_id');
|
||||
expect(runner.release).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does nothing when the table is absent', async () => {
|
||||
const runner = createRunner(false);
|
||||
const service = await createService(runner);
|
||||
|
||||
await service.removeUnusedClassStudentColumns();
|
||||
|
||||
expect(runner.dropColumn).not.toHaveBeenCalled();
|
||||
expect(runner.release).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -16,6 +16,7 @@ export class DatabaseMigrationsService implements OnApplicationBootstrap {
|
||||
await this.protectAttendanceHistory();
|
||||
await this.removeUnusedClassroomColumns();
|
||||
await this.cleanupDepositRefundColumns();
|
||||
await this.removeUnusedClassStudentColumns();
|
||||
}
|
||||
|
||||
private async removeUnusedClassroomColumns(): Promise<void> {
|
||||
@@ -75,6 +76,22 @@ export class DatabaseMigrationsService implements OnApplicationBootstrap {
|
||||
}
|
||||
}
|
||||
|
||||
private async removeUnusedClassStudentColumns(): Promise<void> {
|
||||
const runner = this.dataSource.createQueryRunner();
|
||||
await runner.connect();
|
||||
try {
|
||||
const tables = await runner.getTables(['class_student']);
|
||||
if (tables.length === 0) return;
|
||||
|
||||
const table = await runner.getTable('class_student');
|
||||
if (table?.columns.some((column) => column.name === 'enrollment_id')) {
|
||||
await runner.dropColumn('class_student', 'enrollment_id');
|
||||
}
|
||||
} finally {
|
||||
await runner.release();
|
||||
}
|
||||
}
|
||||
|
||||
private async ensureAiConfigTable(): Promise<void> {
|
||||
const runner = this.dataSource.createQueryRunner();
|
||||
await runner.connect();
|
||||
|
||||
Reference in New Issue
Block a user