-
-
Notifications
You must be signed in to change notification settings - Fork 452
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
CommandMC
wants to merge
3
commits into
main
Choose a base branch
from
feat/migrations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)