Made database migration compatible with plugins
This commit is contained in:
@@ -112,7 +112,7 @@ export interface ODUtilities {
|
|||||||
*
|
*
|
||||||
* It shouldn't be used by plugins because this is an internal API feature!
|
* It shouldn't be used by plugins because this is an internal API feature!
|
||||||
*/
|
*/
|
||||||
ODVersionMigration:new (version:api.ODVersion,func:() => void|Promise<void>) => ODVersionMigration
|
ODVersionMigration:new (version:api.ODVersion,func:() => void|Promise<void>,afterInitFunc:() => void|Promise<void>) => ODVersionMigration
|
||||||
}
|
}
|
||||||
|
|
||||||
/**## ODVersionMigration `utility class`
|
/**## ODVersionMigration `utility class`
|
||||||
@@ -125,10 +125,13 @@ export class ODVersionMigration {
|
|||||||
version: api.ODVersion
|
version: api.ODVersion
|
||||||
/**The migration function */
|
/**The migration function */
|
||||||
#func: () => void|Promise<void>
|
#func: () => void|Promise<void>
|
||||||
|
/**The migration function */
|
||||||
|
#afterInitFunc: () => void|Promise<void>
|
||||||
|
|
||||||
constructor(version:api.ODVersion,func:() => void|Promise<void>){
|
constructor(version:api.ODVersion,func:() => void|Promise<void>,afterInitFunc:() => void|Promise<void>){
|
||||||
this.version = version
|
this.version = version
|
||||||
this.#func = func
|
this.#func = func
|
||||||
|
this.#afterInitFunc = afterInitFunc
|
||||||
}
|
}
|
||||||
/**Run this version migration as a plugin. Returns `false` when someting goes wrong. */
|
/**Run this version migration as a plugin. Returns `false` when someting goes wrong. */
|
||||||
async migrate(): Promise<boolean> {
|
async migrate(): Promise<boolean> {
|
||||||
@@ -139,6 +142,15 @@ export class ODVersionMigration {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/**Run this version migration as a plugin. Returns `false` when someting goes wrong. */
|
||||||
|
async migrateAfterInit(): Promise<boolean> {
|
||||||
|
try{
|
||||||
|
await this.#afterInitFunc()
|
||||||
|
return true
|
||||||
|
}catch{
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const utilities: ODUtilities = {
|
export const utilities: ODUtilities = {
|
||||||
|
|||||||
@@ -29,6 +29,8 @@ export const loadVersionMigrationSystem = async () => {
|
|||||||
|
|
||||||
//LEAVE MIGRATION CONTEXT
|
//LEAVE MIGRATION CONTEXT
|
||||||
await unloadMigrationContext()
|
await unloadMigrationContext()
|
||||||
|
|
||||||
|
return lastVersion
|
||||||
}
|
}
|
||||||
|
|
||||||
const preloadMigrationContext = async () => {
|
const preloadMigrationContext = async () => {
|
||||||
@@ -71,7 +73,27 @@ const loadAllVersionMigrations = async (lastVersion:api.ODVersion) => {
|
|||||||
if (migration.version.compare(lastVersion) == "higher"){
|
if (migration.version.compare(lastVersion) == "higher"){
|
||||||
const success = await migration.migrate()
|
const success = await migration.migrate()
|
||||||
if (success) opendiscord.log("Migrated data to "+migration.version.toString()+"!","debug",[
|
if (success) opendiscord.log("Migrated data to "+migration.version.toString()+"!","debug",[
|
||||||
{key:"success",value:success ? "true" : "false"}
|
{key:"success",value:success ? "true" : "false"},
|
||||||
|
{key:"afterInit",value:"false"}
|
||||||
|
])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const loadAllAfterInitVersionMigrations = async (lastVersion:api.ODVersion) => {
|
||||||
|
const migrations = (await import("./migration.js")).migrations
|
||||||
|
migrations.sort((a,b) => {
|
||||||
|
const comparison = a.version.compare(b.version)
|
||||||
|
if (comparison == "equal") return 0
|
||||||
|
else if (comparison == "higher") return 1
|
||||||
|
else return -1
|
||||||
|
})
|
||||||
|
for (const migration of migrations){
|
||||||
|
if (migration.version.compare(lastVersion) == "higher"){
|
||||||
|
const success = await migration.migrateAfterInit()
|
||||||
|
if (success) opendiscord.log("Migrated data to "+migration.version.toString()+"!","debug",[
|
||||||
|
{key:"success",value:success ? "true" : "false"},
|
||||||
|
{key:"afterInit",value:"true"}
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,12 @@ import {opendiscord, api, utilities} from "../../index"
|
|||||||
export const migrations = [
|
export const migrations = [
|
||||||
//MIGRATE TO v4.0.0
|
//MIGRATE TO v4.0.0
|
||||||
new utilities.ODVersionMigration(api.ODVersion.fromString("opendiscord:version","v4.0.0"),async () => {
|
new utilities.ODVersionMigration(api.ODVersion.fromString("opendiscord:version","v4.0.0"),async () => {
|
||||||
|
//MIGRATE BEFORE STARTUP
|
||||||
|
|
||||||
|
//nothing needs to be transferred :)
|
||||||
|
},() => {
|
||||||
|
//MIGRATE AFTER INITIAL STARTUP (plugins, flags, config, database, language, ... => loaded)
|
||||||
|
|
||||||
//nothing needs to be transferred :)
|
//nothing needs to be transferred :)
|
||||||
})
|
})
|
||||||
]
|
]
|
||||||
+5
-2
@@ -76,8 +76,8 @@ const main = async () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
//handle data migration
|
//handle data migration (PART 1)
|
||||||
await (await import("./core/startup/manageMigration.js")).loadVersionMigrationSystem()
|
const lastVersion = await (await import("./core/startup/manageMigration.js")).loadVersionMigrationSystem()
|
||||||
|
|
||||||
//load plugins
|
//load plugins
|
||||||
if (opendiscord.defaults.getDefault("pluginLoading")){
|
if (opendiscord.defaults.getDefault("pluginLoading")){
|
||||||
@@ -222,6 +222,9 @@ const main = async () => {
|
|||||||
|
|
||||||
await opendiscord.events.get("afterLanguagesSelected").emit([opendiscord.languages.get(languageId),opendiscord.languages.get(backupLanguageId),opendiscord.languages])
|
await opendiscord.events.get("afterLanguagesSelected").emit([opendiscord.languages.get(languageId),opendiscord.languages.get(backupLanguageId),opendiscord.languages])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//handle data migration (PART 2)
|
||||||
|
if (lastVersion) await (await import("./core/startup/manageMigration.js")).loadAllAfterInitVersionMigrations(lastVersion)
|
||||||
|
|
||||||
//load config checker
|
//load config checker
|
||||||
opendiscord.log("Loading config checker...","system")
|
opendiscord.log("Loading config checker...","system")
|
||||||
|
|||||||
Reference in New Issue
Block a user