Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 2x | import type { IBaseDBUpdate } from '../lib';
import CoreDBCon from './CoreDBCon';
import CoreDBPrefab from './CoreDBPrefab';
/**
* Represents a migration step that updates the database schema or data from one
* version to another.
* The update logic is encapsulated in the {@link performe} method, which must
* be implemented by subclasses. Updates can be chained by setting a
* successor via {@link setNext}. The {@link update} method runs the current
* step, persists the new version and then recursively triggers the next step.
*
* @template D - The database instance type. Must be either {@link CoreDBCon} or
* {@link CoreDBPrefab}.
* @implements {IBaseDBUpdate}
*/
export default abstract class CoreDBUpdate<
D extends CoreDBCon<any, any> | CoreDBPrefab<any>,
> implements IBaseDBUpdate
{
srcVersion: string;
tarVersion: string;
private readonly db: D;
private nextUpdate: CoreDBUpdate<D> | null;
protected constructor(srcVersion: string, tarVersion: string, db: D) {
this.srcVersion = srcVersion;
this.tarVersion = tarVersion;
this.db = db;
this.nextUpdate = null;
}
async update(): Promise<boolean> {
const perf = await this.perform();
if (perf) {
this.db.info(
`Update DB from ${this.srcVersion} to ${this.tarVersion} successful`,
);
} else E{
throw this.db.lError(
`Updating DB from ${this.srcVersion} to ${this.tarVersion} failed`,
);
}
await this.db.setConfig('dbversion', this.tarVersion);
const next = await this.updateNext();
return perf && next;
}
abstract perform(): Promise<boolean>;
async updateNext(): Promise<boolean> {
if (this.nextUpdate !== null) {
return this.nextUpdate.update();
}
return true;
}
setNext(db: CoreDBUpdate<D>): void {
this.nextUpdate = db;
}
find(version: string): CoreDBUpdate<D> | null {
Eif (this.srcVersion === version) {
return this;
}
if (this.nextUpdate !== null) {
return this.nextUpdate.find(version);
}
return null;
}
getDb(): D {
return this.db;
}
getSource(): string {
return this.srcVersion;
}
}
|