Skip to content

Commit

Permalink
pass logger to migrations, include version numbers in migration name
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Dec 15, 2023
1 parent 5680dd7 commit eefe9fe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion gui/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export async function renderApp(config: Config, params: ServerParams, logger: Lo
...createProfileSlice(...slice),
}), {
migrate(persistedState, version) {
return applyStateMigrations(params, persistedState as UnknownState, version);
return applyStateMigrations(params, persistedState as UnknownState, version, logger);
},
name: STATE_KEY,
partialize(s) {
Expand Down
35 changes: 23 additions & 12 deletions gui/src/state/migration/default.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable camelcase */
import { Logger } from 'browser-bunyan';
import { ServerParams } from '../../config.js';
import { BaseImgParams } from '../../types/params.js';
import { OnnxState, STATE_VERSION } from '../full.js';
Expand All @@ -7,12 +8,8 @@ import { InpaintSlice } from '../inpaint.js';
import { Txt2ImgSlice } from '../txt2img.js';
import { UpscaleSlice } from '../upscale.js';

export const REMOVE_KEYS = ['tile', 'overlap'] as const;

export type RemovedKeys = typeof REMOVE_KEYS[number];

// TODO: can the compiler calculate this?
export type AddedKeysV11 = 'unet_tile' | 'unet_overlap' | 'vae_tile' | 'vae_overlap';
// #region V7
export const V7 = 7;

export type BaseImgParamsV7<T extends BaseImgParams> = Omit<T, AddedKeysV11> & {
overlap: number;
Expand All @@ -25,23 +22,37 @@ export type OnnxStateV7 = Omit<OnnxState, 'img2img' | 'txt2img'> & {
txt2img: BaseImgParamsV7<Txt2ImgSlice['txt2img']>;
upscale: BaseImgParamsV7<UpscaleSlice['upscale']>;
};
// #endregion

// #region V11
export const REMOVED_KEYS_V11 = ['tile', 'overlap'] as const;

export type RemovedKeysV11 = typeof REMOVED_KEYS_V11[number];

// TODO: can the compiler calculate this?
export type AddedKeysV11 = 'unet_tile' | 'unet_overlap' | 'vae_tile' | 'vae_overlap';
// #endregion

// add versions to this list as they are replaced
export type PreviousState = OnnxStateV7;

// always the latest version
export type CurrentState = OnnxState;

// any version of state
export type UnknownState = PreviousState | CurrentState;

export function applyStateMigrations(params: ServerParams, previousState: UnknownState, version: number): OnnxState {
// eslint-disable-next-line no-console
console.log('applying migrations from %s to %s', version, STATE_VERSION);
export function applyStateMigrations(params: ServerParams, previousState: UnknownState, version: number, logger: Logger): OnnxState {
logger.info('applying state migrations from version %s to version %s', version, STATE_VERSION);

if (version < STATE_VERSION) {
return migrateDefaults(params, previousState as PreviousState);
if (version <= V7) {
return migrateV7ToV11(params, previousState as PreviousState);
}

return previousState as CurrentState;
}

export function migrateDefaults(params: ServerParams, previousState: PreviousState): CurrentState {
export function migrateV7ToV11(params: ServerParams, previousState: PreviousState): CurrentState {
// add any missing keys
const result: CurrentState = {
...params,
Expand Down

0 comments on commit eefe9fe

Please sign in to comment.