Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Oct 9, 2023
1 parent ed6929b commit f94d973
Show file tree
Hide file tree
Showing 7 changed files with 1,087 additions and 51 deletions.
14 changes: 14 additions & 0 deletions meteor/lib/api/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export interface NewMigrationAPI {
forceMigration(chunks: Array<MigrationChunk>): Promise<void>
resetDatabaseVersions(): Promise<void>

/**
* Run `fixupConfig` on the blueprint for a Studio
* @param studioId Id of the Studio
*/
fixupConfigForStudio(studioId: StudioId): Promise<void>

/**
* Run `validateConfig` on the blueprint for a Studio
* @param studioId Id of the Studio
Expand All @@ -26,6 +32,12 @@ export interface NewMigrationAPI {
*/
runUpgradeForStudio(studioId: StudioId): Promise<void>

/**
* Run `fixupConfig` on the blueprint for a ShowStyleBase
* @param showStyleBaseId Id of the ShowStyleBase
*/
fixupConfigForShowStyleBase(showStyleBaseId: ShowStyleBaseId): Promise<void>

/**
* Run `validateConfig` on the blueprint for a ShowStyleBase
* @param showStyleBaseId Id of the ShowStyleBase
Expand All @@ -47,8 +59,10 @@ export enum MigrationAPIMethods {
'resetDatabaseVersions' = 'migration.resetDatabaseVersions',

'getUpgradeStatus' = 'migration.getUpgradeStatus',
'fixupConfigForStudio' = 'migration.fixupConfigForStudio',
'validateConfigForStudio' = 'migration.validateConfigForStudio',
'runUpgradeForStudio' = 'migration.runUpgradeForStudio',
'fixupConfigForShowStyleBase' = 'migration.fixupConfigForShowStyleBase',
'validateConfigForShowStyleBase' = 'migration.validateConfigForShowStyleBase',
'runUpgradeForShowStyleBase' = 'migration.runUpgradeForShowStyleBase',
}
Expand Down
18 changes: 18 additions & 0 deletions meteor/server/migration/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { MigrationStepInputResult } from '@sofie-automation/blueprints-integrati
import { MethodContextAPI } from '../../lib/api/methods'
import { SystemWriteAccess } from '../security/system'
import {
fixupConfigForShowStyleBase,
fixupConfigForStudio,
runUpgradeForShowStyleBase,
runUpgradeForStudio,
validateConfigForShowStyleBase,
Expand Down Expand Up @@ -49,6 +51,14 @@ class ServerMigrationAPI extends MethodContextAPI implements NewMigrationAPI {
return Migrations.resetDatabaseVersions()
}

async fixupConfigForStudio(studioId: StudioId): Promise<void> {
check(studioId, String)

await SystemWriteAccess.migrations(this)

return fixupConfigForStudio(studioId)
}

async validateConfigForStudio(studioId: StudioId): Promise<BlueprintValidateConfigForStudioResult> {
check(studioId, String)

Expand All @@ -65,6 +75,14 @@ class ServerMigrationAPI extends MethodContextAPI implements NewMigrationAPI {
return runUpgradeForStudio(studioId)
}

async fixupConfigForShowStyleBase(showStyleBaseId: ShowStyleBaseId): Promise<void> {
check(showStyleBaseId, String)

await SystemWriteAccess.migrations(this)

return fixupConfigForShowStyleBase(showStyleBaseId)
}

async validateConfigForShowStyleBase(
showStyleBaseId: ShowStyleBaseId
): Promise<BlueprintValidateConfigForStudioResult> {
Expand Down
155 changes: 105 additions & 50 deletions meteor/server/migration/upgrades/showStyleBase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { BlueprintManifestType, ShowStyleBlueprintManifest } from '@sofie-automation/blueprints-integration'
import {
BlueprintManifestType,
JSONBlobParse,
ShowStyleBlueprintManifest,
} from '@sofie-automation/blueprints-integration'
import { ShowStyleBaseId, TriggeredActionId } from '@sofie-automation/corelib/dist/dataModel/Ids'
import { normalizeArray, normalizeArrayToMap, getRandomId, literal, Complete } from '@sofie-automation/corelib/dist/lib'
import {
Expand All @@ -15,39 +19,58 @@ import { evalBlueprint } from '../../api/blueprints/cache'
import { logger } from '../../logging'
import { CommonContext } from './context'
import type { AnyBulkWriteOperation } from 'mongodb'
import { FixUpBlueprintConfigContext } from '@sofie-automation/corelib/dist/fixUpBlueprintConfig/context'
import { Blueprint } from '@sofie-automation/corelib/dist/dataModel/Blueprint'

export async function fixupConfigForShowStyleBase(showStyleBaseId: ShowStyleBaseId): Promise<void> {
const { showStyleBase, blueprint, blueprintManifest } = await loadShowStyleAndBlueprint(showStyleBaseId)

if (typeof blueprintManifest.fixUpConfig !== 'function') {
if (showStyleBase.lastBlueprintFixupHash) {
// Cleanup property to avoid getting stuck
await ShowStyleBases.updateAsync(showStyleBaseId, {
$unset: {
lastBlueprintFixupHash: 1,
},
})
}
throw new Meteor.Error(500, 'Blueprint does not support this config flow')
}

export async function validateConfigForShowStyleBase(
showStyleBaseId: ShowStyleBaseId
): Promise<BlueprintValidateConfigForStudioResult> {
const showStyleBase = (await ShowStyleBases.findOneAsync(showStyleBaseId, {
fields: {
_id: 1,
blueprintId: 1,
blueprintConfigPresetId: 1,
blueprintConfigWithOverrides: 1,
},
})) as
| Pick<DBShowStyleBase, '_id' | 'blueprintId' | 'blueprintConfigPresetId' | 'blueprintConfigWithOverrides'>
| undefined
if (!showStyleBase) throw new Meteor.Error(404, `ShowStyleBase "${showStyleBaseId}" not found!`)
const commonContext = new CommonContext(
'fixupConfig',
`showStyleBase:${showStyleBaseId},blueprint:${blueprint._id}`
)
const blueprintContext = new FixUpBlueprintConfigContext(
commonContext,
JSONBlobParse(blueprintManifest.showStyleConfigSchema),
showStyleBase.blueprintConfigWithOverrides
)

if (!showStyleBase.blueprintConfigPresetId) throw new Meteor.Error(500, 'ShowStyleBase is missing config preset')
blueprintManifest.fixUpConfig(blueprintContext)

const blueprint = showStyleBase.blueprintId
? await Blueprints.findOneAsync({
_id: showStyleBase.blueprintId,
blueprintType: BlueprintManifestType.SHOWSTYLE,
})
: undefined
if (!blueprint) throw new Meteor.Error(404, `Blueprint "${showStyleBase.blueprintId}" not found!`)
// TODO - track warnings

if (!blueprint.blueprintHash) throw new Meteor.Error(500, 'Blueprint is not valid')
// Save the 'fixed' config
await ShowStyleBases.updateAsync(showStyleBaseId, {
$set: {
lastBlueprintFixupHash: blueprint.blueprintHash,
blueprintConfigWithOverrides: showStyleBase.blueprintConfigWithOverrides,
},
})
}

const blueprintManifest = evalBlueprint(blueprint) as ShowStyleBlueprintManifest
export async function validateConfigForShowStyleBase(
showStyleBaseId: ShowStyleBaseId
): Promise<BlueprintValidateConfigForStudioResult> {
const { showStyleBase, blueprint, blueprintManifest } = await loadShowStyleAndBlueprint(showStyleBaseId)

if (typeof blueprintManifest.validateConfig !== 'function')
throw new Meteor.Error(500, 'Blueprint does not support this config flow')

// TODO - run fixUpConfig automatically?
throwIfNeedsFixupConfigRunning(showStyleBase, blueprint, blueprintManifest)

const blueprintContext = new CommonContext(
'applyConfig',
`showStyleBase:${showStyleBaseId},blueprint:${blueprint._id}`
Expand All @@ -67,35 +90,14 @@ export async function validateConfigForShowStyleBase(
export async function runUpgradeForShowStyleBase(showStyleBaseId: ShowStyleBaseId): Promise<void> {
logger.info(`Running upgrade for ShowStyleBase "${showStyleBaseId}"`)

const showStyleBase = (await ShowStyleBases.findOneAsync(showStyleBaseId, {
fields: {
_id: 1,
blueprintId: 1,
blueprintConfigPresetId: 1,
blueprintConfigWithOverrides: 1,
},
})) as
| Pick<DBShowStyleBase, '_id' | 'blueprintId' | 'blueprintConfigPresetId' | 'blueprintConfigWithOverrides'>
| undefined
if (!showStyleBase) throw new Meteor.Error(404, `ShowStyleBase "${showStyleBaseId}" not found!`)

if (!showStyleBase.blueprintConfigPresetId) throw new Meteor.Error(500, 'ShowStyleBase is missing config preset')

const blueprint = showStyleBase.blueprintId
? await Blueprints.findOneAsync({
_id: showStyleBase.blueprintId,
blueprintType: BlueprintManifestType.SHOWSTYLE,
})
: undefined
if (!blueprint) throw new Meteor.Error(404, `Blueprint "${showStyleBase.blueprintId}" not found!`)

if (!blueprint.blueprintHash) throw new Meteor.Error(500, 'Blueprint is not valid')

const blueprintManifest = evalBlueprint(blueprint) as ShowStyleBlueprintManifest
const { showStyleBase, blueprint, blueprintManifest } = await loadShowStyleAndBlueprint(showStyleBaseId)

if (typeof blueprintManifest.applyConfig !== 'function')
throw new Meteor.Error(500, 'Blueprint does not support this config flow')

// TODO - run fixUpConfig automatically?
throwIfNeedsFixupConfigRunning(showStyleBase, blueprint, blueprintManifest)

const blueprintContext = new CommonContext(
'applyConfig',
`showStyleBase:${showStyleBaseId},blueprint:${blueprint.blueprintId}`
Expand Down Expand Up @@ -180,3 +182,56 @@ export async function runUpgradeForShowStyleBase(showStyleBaseId: ShowStyleBaseI

await TriggeredActions.bulkWriteAsync(bulkOps)
}

async function loadShowStyleAndBlueprint(showStyleBaseId: ShowStyleBaseId) {
const showStyleBase = (await ShowStyleBases.findOneAsync(showStyleBaseId, {
fields: {
_id: 1,
blueprintId: 1,
blueprintConfigPresetId: 1,
blueprintConfigWithOverrides: 1,
lastBlueprintFixupHash: 1,
},
})) as
| Pick<
DBShowStyleBase,
| '_id'
| 'blueprintId'
| 'blueprintConfigPresetId'
| 'blueprintConfigWithOverrides'
| 'lastBlueprintFixupHash'
>
| undefined
if (!showStyleBase) throw new Meteor.Error(404, `ShowStyleBase "${showStyleBaseId}" not found!`)

if (!showStyleBase.blueprintConfigPresetId) throw new Meteor.Error(500, 'ShowStyleBase is missing config preset')

const blueprint = showStyleBase.blueprintId
? await Blueprints.findOneAsync({
_id: showStyleBase.blueprintId,
blueprintType: BlueprintManifestType.SHOWSTYLE,
})
: undefined
if (!blueprint) throw new Meteor.Error(404, `Blueprint "${showStyleBase.blueprintId}" not found!`)

if (!blueprint.blueprintHash) throw new Meteor.Error(500, 'Blueprint is not valid')

const blueprintManifest = evalBlueprint(blueprint) as ShowStyleBlueprintManifest

return {
showStyleBase,
blueprint,
blueprintManifest,
}
}

function throwIfNeedsFixupConfigRunning(
showStyleBase: Pick<DBShowStyleBase, 'lastBlueprintFixupHash'>,
blueprint: Blueprint,
blueprintManifest: ShowStyleBlueprintManifest
): void {
if (typeof blueprintManifest.fixUpConfig !== 'function') return

if (blueprint.blueprintHash !== showStyleBase.lastBlueprintFixupHash)
throw new Meteor.Error(500, `fixupConfigForShowStyleBase must be called first`)
}
4 changes: 4 additions & 0 deletions meteor/server/migration/upgrades/studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { Studios } from '../../collections'
import { logger } from '../../logging'
import { QueueStudioJob } from '../../worker/worker'

export async function fixupConfigForStudio(studioId: StudioId): Promise<void> {
// nocommit
}

export async function validateConfigForStudio(studioId: StudioId): Promise<BlueprintValidateConfigForStudioResult> {
const studio = (await Studios.findOneAsync(studioId, {
fields: {
Expand Down
Loading

0 comments on commit f94d973

Please sign in to comment.