Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/scripts/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"0.7.410"
"0.7.419"
1 change: 1 addition & 0 deletions foundations/core/packages/core/src/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@ export interface Version extends Doc {
export interface MigrationState extends Doc {
plugin: string
state: string
durationMs?: number
}

/**
Expand Down
48 changes: 29 additions & 19 deletions foundations/core/packages/model/src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,22 +176,28 @@ export async function tryMigrate (
const states = client.migrateState.get(plugin) ?? new Set()
for (const migration of migrations) {
if (states.has(migration.state)) continue
if (migration.mode == null || migration.mode === mode) {
try {
client.logger.log('running migration', { plugin, state: migration.state })
await migration.func(client, mode)
} catch (err: any) {
client.logger.error('Failed to run migration', { plugin, state: migration.state, err })
Analytics.handleError(err)
continue
}
// Do not persist MigrationState when this migration is not applicable to the
// current mode — otherwise it would be skipped forever on the correct mode.
if (migration.mode != null && migration.mode !== mode) {
continue
}
const startedAt = Date.now()
try {
client.logger.log('running migration', { plugin, state: migration.state })
await migration.func(client, mode)
} catch (err: any) {
client.logger.error('Failed to run migration', { plugin, state: migration.state, err })
Analytics.handleError(err)
continue
}
const finishedAt = Date.now()
const st: MigrationState = {
plugin,
state: migration.state,
durationMs: finishedAt - startedAt,
space: core.space.Configuration,
modifiedBy: core.account.System,
modifiedOn: Date.now(),
modifiedOn: finishedAt,
_class: core.class.MigrationState,
_id: generateId()
}
Expand All @@ -212,19 +218,23 @@ export async function tryUpgrade (
const states = state.get(plugin) ?? new Set()
for (const upgrades of migrations) {
if (states.has(upgrades.state)) continue
if (upgrades.mode != null && upgrades.mode !== mode) {
continue
}
const _client = await client()
if (upgrades.mode == null || upgrades.mode === mode) {
try {
await upgrades.func(_client, mode)
} catch (err: any) {
console.error(err)
Analytics.handleError(err)
continue
}
const startedAt = Date.now()
try {
await upgrades.func(_client, mode)
} catch (err: any) {
console.error(err)
Analytics.handleError(err)
continue
}
const finishedAt = Date.now()
const st: Data<MigrationState> = {
plugin,
state: upgrades.state
state: upgrades.state,
durationMs: finishedAt - startedAt
}
const tx = new TxOperations(_client, core.account.System)
await tx.createDoc(core.class.MigrationState, core.space.Configuration, st)
Expand Down
1 change: 1 addition & 0 deletions models/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ export class TVersion extends TDoc implements Version {
export class TMigrationState extends TDoc implements MigrationState {
plugin!: string
state!: string
durationMs?: number
}

@Model(core.class.PluginConfiguration, core.class.Doc, DOMAIN_MODEL)
Expand Down
Loading