Skip to content

Commit 1b58861

Browse files
committed
feat(pfapi): prepare backwards migrations
1 parent 9a4f42e commit 1b58861

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

Diff for: src/app/pfapi/api/migration/migration.service.ts

+21-8
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,28 @@ export class MigrationService<MD extends ModelCfgs> {
6767
};
6868
}
6969
if (dataInCrossModelVersion > codeModelVersion) {
70-
throw new ImpossibleError('Saved model version is higher than current one');
70+
// TODO implement migrate down
71+
// if (cfg?.crossModelBackwardMigrations) {
72+
// // ...
73+
// }
74+
throw new ImpossibleError(
75+
'Saved model version is higher than current one and no backwards migrations available',
76+
);
7177
}
78+
79+
return this._migrateUp(codeModelVersion, dataInCrossModelVersion, dataIn);
80+
}
81+
82+
private async _migrateUp(
83+
codeModelVersion: number,
84+
dataInCrossModelVersion: number,
85+
dataIn: AllSyncModels<MD>,
86+
): Promise<{
87+
dataAfter: AllSyncModels<MD>;
88+
versionAfter: number;
89+
wasMigrated: boolean;
90+
}> {
91+
const cfg = this._pfapiMain.cfg;
7292
if (!cfg?.crossModelMigrations) {
7393
throw new ImpossibleError('No migration function provided');
7494
}
@@ -115,12 +135,5 @@ export class MigrationService<MD extends ModelCfgs> {
115135
pfLog(0, `Migration functions failed to execute`, { error });
116136
throw new ModelMigrationError('Error running migration functions', error);
117137
}
118-
119-
// TODO single model migration
120-
// const modelIds = Object.keys(this.m);
121-
// for (const modelId of modelIds) {
122-
// const modelCtrl = this.m[modelId];
123-
//
124-
// }
125138
}
126139
}

Diff for: src/app/pfapi/api/pfapi.model.ts

+6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ export interface CrossModelMigrations {
5656
[version: number]: CrossModelMigrateFn;
5757
}
5858

59+
export type CrossModelBackwardsMigrateFn = <R, F>(fullData: F) => R;
60+
export interface CrossModelBackwardsMigrations {
61+
[version: number]: CrossModelBackwardsMigrateFn;
62+
}
63+
5964
export interface PfapiBaseCfg<T extends ModelCfgs> {
6065
dbAdapter?: DatabaseAdapter;
6166
onDbError?: (err: any) => void;
@@ -66,6 +71,7 @@ export interface PfapiBaseCfg<T extends ModelCfgs> {
6671
isCreateBackups?: boolean;
6772
crossModelVersion?: number;
6873
crossModelMigrations?: CrossModelMigrations;
74+
crossModelBackwardMigrations?: CrossModelBackwardsMigrations;
6975
validate?: (data: AllModelData<T>) => IValidation<AllModelData<T>>;
7076
repair?: <R>(data: R | unknown) => AllModelData<T>;
7177

Diff for: src/app/pfapi/api/sync/sync.service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ export class SyncService<const MD extends ModelCfgs> {
111111
switch (status) {
112112
case SyncStatus.UpdateLocal:
113113
if (this.IS_DO_CROSS_MODEL_MIGRATIONS) {
114-
const mcr = modelVersionCheck({
114+
const mvcR = modelVersionCheck({
115115
// TODO check for problems
116116
clientVersion:
117117
this._pfapiMain.cfg?.crossModelVersion || localMeta.crossModelVersion,
118118
toImport: remoteMeta.crossModelVersion,
119119
});
120-
switch (mcr) {
120+
switch (mvcR) {
121121
case ModelVersionCheckResult.MinorUpdate:
122122
case ModelVersionCheckResult.MajorUpdate:
123123
alert('Downloading all since cross model version changed');

0 commit comments

Comments
 (0)