Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat/Tech] Migration system #4255

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
70 changes: 70 additions & 0 deletions src/backend/components/migration/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { TypeCheckedStoreBackend } from '../../electron_store'
import { logError, logInfo } from '../../logger/logger'

import { LegendaryGlobalConfigFolderMigration } from './migrations/legendary'

import type { TypeCheckedStore } from 'common/types/electron_store'

export interface Migration {
identifier: string
run(): Promise<boolean>
}

export default class MigrationComponent {
constructor() {
this.migrationsStore = new TypeCheckedStoreBackend('migrationsStore', {
cwd: 'store',
name: 'migrations'
})
}

async applyMigrations() {
const appliedMigrations = this.migrationsStore.get('appliedMigrations', [])

// NOTE: This intentionally runs migrations sequentially to avoid race conditions
for (const migration of this.getAllMigrations()) {
if (appliedMigrations.includes(migration.identifier)) continue

const wasApplied = await this.applyMigration(migration)
if (wasApplied) appliedMigrations.push(migration.identifier)
}

this.migrationsStore.set('appliedMigrations', appliedMigrations)
}

private async applyMigration(migration: Migration): Promise<boolean> {
logInfo(['Applying migration', `"${migration.identifier}"`])
const result = await migration.run().catch((e: Error) => e)

if (!result) {
// The idea here is that the migration failed, but did so gracefully.
// It thus (hopefully) printed out some of its own logging on why it
// failed
logError([
'Migration',
`"${migration.identifier}"`,
'failed. More details will be available above this message'
])
return false
}

if (result instanceof Error) {
logError([
'Migration',
`"${migration.identifier}"`,
'encountered error while applying:',
result
])
return false
}

logInfo(['Migration', `"${migration.identifier}"`, 'successfully applied'])
return true
}

private getAllMigrations(): Migration[] {
return [new LegendaryGlobalConfigFolderMigration()]
}

private readonly migrationsStore: TypeCheckedStore<'migrationsStore'>
}
35 changes: 35 additions & 0 deletions src/backend/components/migration/migrations/legendary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { app } from 'electron'
import { type PathLike } from 'fs'
import { access, cp, mkdir } from 'fs/promises'
import { join } from 'path'

import { isLinux, legendaryConfigPath, userHome } from 'backend/constants'

import type { Migration } from '..'

const exists = async (path: PathLike) =>
access(path).then(
() => true,
() => false
)

export class LegendaryGlobalConfigFolderMigration implements Migration {
identifier = 'legendary-move-global-config-folder'
async run(): Promise<boolean> {
const hasHeroicSpecificConfig = await exists(legendaryConfigPath)
// Don't overwrite existing configuration
if (hasHeroicSpecificConfig) return true

const globalLegendaryConfig = isLinux
? join(app.getPath('appData'), 'legendary')
: join(userHome, '.config', 'legendary')

const hasGlobalConfig = await exists(globalLegendaryConfig)
// Nothing to migrate
if (!hasGlobalConfig) return true

await mkdir(legendaryConfigPath, { recursive: true })
await cp(globalLegendaryConfig, legendaryConfigPath, { recursive: true })
return true
}
}
16 changes: 7 additions & 9 deletions src/backend/electron_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import {
} from 'common/types/electron_store'
import { Get } from 'type-fest'

export class TypeCheckedStoreBackend<
Name extends ValidStoreName,
Structure extends StoreStructure[Name]
> implements TypeCheckedStore<Name, Structure>
export class TypeCheckedStoreBackend<Name extends ValidStoreName>
implements TypeCheckedStore<Name>
{
private store: Store

constructor(name: Name, options: Store.Options<Structure>) {
constructor(name: Name, options: Store.Options<StoreStructure[Name]>) {
// @ts-expect-error This looks like a bug in electron-store's type definitions
this.store = new Store(options)
}
Expand All @@ -26,22 +24,22 @@ export class TypeCheckedStoreBackend<

public get<KeyType extends string>(
key: KeyType,
defaultValue: NonNullable<UnknownGuard<Get<Structure, KeyType>>>
defaultValue: NonNullable<UnknownGuard<Get<StoreStructure[Name], KeyType>>>
) {
return this.store.get(key, defaultValue) as NonNullable<
UnknownGuard<Get<Structure, KeyType>>
UnknownGuard<Get<StoreStructure[Name], KeyType>>
>
}

public get_nodefault<KeyType extends string>(key: KeyType) {
return this.store.get(key) as UnknownGuard<
Get<Structure, KeyType> | undefined
Get<StoreStructure[Name], KeyType> | undefined
>
}

public set<KeyType extends string>(
key: KeyType,
value: UnknownGuard<Get<Structure, KeyType>>
value: UnknownGuard<Get<StoreStructure[Name], KeyType>>
) {
this.store.set(key, value)
}
Expand Down
4 changes: 4 additions & 0 deletions src/backend/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ import {
getGameSdl
} from 'backend/storeManagers/legendary/library'
import { storeMap } from 'common/utils'
import MigrationComponent from './components/migration'

app.commandLine?.appendSwitch('ozone-platform-hint', 'auto')

Expand Down Expand Up @@ -301,6 +302,9 @@ if (!gotTheLock) {
handleProtocol(argv)
})
app.whenReady().then(async () => {
const migrationComponent = new MigrationComponent()
await migrationComponent.applyMigrations()

Comment on lines +305 to +307
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little awkward, and is a result of porting this to "non-components-system" Heroic. If components would be in main right now, you'd not see any of this here (the component would instead be initialized automatically)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some questions about the components, but there's no PR for that right? to not ask the questions here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no PR yet, no. I do have a WIP branch, but it's a little outdated
The reason I haven't made a PR yet is that to properly show what components can do (and to get them some actual use, instead of just being an unused system), I need some preliminary steps (which this PR is one of)

initLogger()
initOnlineMonitor()
initStoreManagers()
Expand Down
13 changes: 0 additions & 13 deletions src/backend/storeManagers/legendary/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ import {
legendaryConfigPath,
legendaryLogFile,
legendaryMetadata,
isLinux,
userHome,
isWindows
} from '../../constants'
import {
Expand All @@ -50,8 +48,6 @@ import { callRunner } from '../../launcher'
import { dirname, join } from 'path'
import { isOnline } from 'backend/online_monitor'
import { update } from './games'
import { app } from 'electron'
import { copySync } from 'fs-extra'
import { LegendaryCommand } from './commands'
import { LegendaryAppName, LegendaryPlatform } from './commands/base'
import { Path } from 'backend/schemas'
Expand All @@ -65,15 +61,6 @@ let installedGames: Map<string, InstalledJsonMetadata> = new Map()
const library: Map<string, GameInfo> = new Map()

export async function initLegendaryLibraryManager() {
// Migrate user data from global Legendary config if necessary
const globalLegendaryConfig = isLinux
? join(app.getPath('appData'), 'legendary')
: join(userHome, '.config', 'legendary')
if (!existsSync(legendaryConfigPath) && existsSync(globalLegendaryConfig)) {
mkdirSync(legendaryConfigPath, { recursive: true })
copySync(globalLegendaryConfig, legendaryConfigPath)
}

loadGamesInAccount()
refreshInstalled()
}
Expand Down
16 changes: 8 additions & 8 deletions src/common/types/electron_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ export interface StoreStructure {
[title: string]: WikiInfo
}
uploadedLogs: Record<string, UploadedLogData>
migrationsStore: {
appliedMigrations: string[]
}
}

export type StoreOptions<T extends Record<string, unknown>> = Store.Options<T>
Expand All @@ -112,24 +115,21 @@ export type UnknownGuard<T> = unknown extends T
: never
: T

export abstract class TypeCheckedStore<
Name extends ValidStoreName,
Structure extends StoreStructure[Name]
> {
export abstract class TypeCheckedStore<Name extends ValidStoreName> {
abstract has(key: string): boolean

abstract get<KeyType extends string>(
key: KeyType,
defaultValue: NonNullable<UnknownGuard<Get<Structure, KeyType>>>
): NonNullable<UnknownGuard<Get<Structure, KeyType>>>
defaultValue: NonNullable<UnknownGuard<Get<StoreStructure[Name], KeyType>>>
): NonNullable<UnknownGuard<Get<StoreStructure[Name], KeyType>>>

abstract get_nodefault<KeyType extends string>(
key: KeyType
): UnknownGuard<Get<Structure, KeyType> | undefined>
): UnknownGuard<Get<StoreStructure[Name], KeyType> | undefined>

abstract set<KeyType extends string>(
key: KeyType,
value: UnknownGuard<Get<Structure, KeyType>>
value: UnknownGuard<Get<StoreStructure[Name], KeyType>>
): void

// FIXME: This is currently not type-checked properly
Expand Down
16 changes: 7 additions & 9 deletions src/frontend/helpers/electronStores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import {
} from 'common/types/electron_store'
import { GameInfo } from 'common/types'

export class TypeCheckedStoreFrontend<
Name extends ValidStoreName,
Structure extends StoreStructure[Name]
> implements TypeCheckedStore<Name, Structure>
export class TypeCheckedStoreFrontend<Name extends ValidStoreName>
implements TypeCheckedStore<Name>
{
private storeName: ValidStoreName

constructor(name: Name, options: StoreOptions<Structure>) {
constructor(name: Name, options: StoreOptions<StoreStructure[Name]>) {
this.storeName = name
// @ts-expect-error This looks like a bug in electron-store's type definitions
window.api.storeNew(name, options)
Expand All @@ -28,24 +26,24 @@ export class TypeCheckedStoreFrontend<

public get<KeyType extends string>(
key: KeyType,
defaultValue: NonNullable<UnknownGuard<Get<Structure, KeyType>>>
defaultValue: NonNullable<UnknownGuard<Get<StoreStructure[Name], KeyType>>>
) {
return window.api.storeGet(
this.storeName,
key,
defaultValue
) as NonNullable<UnknownGuard<Get<Structure, KeyType>>>
) as NonNullable<UnknownGuard<Get<StoreStructure[Name], KeyType>>>
}

public get_nodefault<KeyType extends string>(key: KeyType) {
return window.api.storeGet(this.storeName, key) as UnknownGuard<
Get<Structure, KeyType> | undefined
Get<StoreStructure[Name], KeyType> | undefined
>
}

public set<KeyType extends string>(
key: KeyType,
value: UnknownGuard<Get<Structure, KeyType>>
value: UnknownGuard<Get<StoreStructure[Name], KeyType>>
) {
window.api.storeSet(this.storeName, key, value)
}
Expand Down
Loading