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!
|
||||
*/
|
||||
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`
|
||||
@@ -125,10 +125,13 @@ export class ODVersionMigration {
|
||||
version: api.ODVersion
|
||||
/**The migration function */
|
||||
#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.#func = func
|
||||
this.#afterInitFunc = afterInitFunc
|
||||
}
|
||||
/**Run this version migration as a plugin. Returns `false` when someting goes wrong. */
|
||||
async migrate(): Promise<boolean> {
|
||||
@@ -139,6 +142,15 @@ export class ODVersionMigration {
|
||||
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 = {
|
||||
|
||||
@@ -29,6 +29,8 @@ export const loadVersionMigrationSystem = async () => {
|
||||
|
||||
//LEAVE MIGRATION CONTEXT
|
||||
await unloadMigrationContext()
|
||||
|
||||
return lastVersion
|
||||
}
|
||||
|
||||
const preloadMigrationContext = async () => {
|
||||
@@ -71,7 +73,27 @@ const loadAllVersionMigrations = async (lastVersion:api.ODVersion) => {
|
||||
if (migration.version.compare(lastVersion) == "higher"){
|
||||
const success = await migration.migrate()
|
||||
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 = [
|
||||
//MIGRATE TO v4.0.0
|
||||
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 :)
|
||||
})
|
||||
]
|
||||
Reference in New Issue
Block a user