2025-06-04 12:02:07 -04:00
import { db } from "../../db/sqlite" ;
2025-04-06 11:38:00 -04:00
import { APP_PATH , configFilePath1 , configFilePath2 } from "@server/lib/consts" ;
2025-04-04 22:58:01 -04:00
import { sql } from "drizzle-orm" ;
2025-04-05 22:28:47 -04:00
import fs from "fs" ;
import yaml from "js-yaml" ;
2025-04-06 11:38:00 -04:00
import path from "path" ;
import { z } from "zod" ;
import { fromZodError } from "zod-validation-error" ;
2025-04-04 22:58:01 -04:00
const version = "1.2.0" ;
export default async function migration() {
console . log ( ` Running setup script ${ version } ... ` ) ;
try {
db . transaction ( ( trx ) = > {
trx . run (
sql ` ALTER TABLE 'resources' ADD 'enabled' integer DEFAULT true NOT NULL; `
) ;
} ) ;
console . log ( ` Migrated database schema ` ) ;
} catch ( e ) {
console . log ( "Unable to migrate database schema" ) ;
throw e ;
}
2025-04-05 22:28:47 -04:00
try {
// Determine which config file exists
const filePaths = [ configFilePath1 , configFilePath2 ] ;
let filePath = "" ;
for ( const path of filePaths ) {
if ( fs . existsSync ( path ) ) {
filePath = path ;
break ;
}
}
if ( ! filePath ) {
throw new Error (
` No config file found (expected config.yml or config.yaml). `
) ;
}
// Read and parse the YAML file
let rawConfig : any ;
const fileContents = fs . readFileSync ( filePath , "utf8" ) ;
rawConfig = yaml . load ( fileContents ) ;
if ( ! rawConfig . flags ) {
rawConfig . flags = { } ;
}
rawConfig . server . resource_access_token_headers = {
2025-04-06 11:38:00 -04:00
id : "P-Access-Token-Id" ,
2025-04-05 22:28:47 -04:00
token : "P-Access-Token"
} ;
// Write the updated YAML back to the file
const updatedYaml = yaml . dump ( rawConfig ) ;
fs . writeFileSync ( filePath , updatedYaml , "utf8" ) ;
console . log ( ` Added new config option: resource_access_token_headers ` ) ;
} catch ( e ) {
console . log (
` Unable to add new config option: resource_access_token_headers. Please add it manually. https://docs.fossorial.io/Pangolin/Configuration/config `
) ;
console . error ( e ) ;
}
2025-04-06 11:38:00 -04:00
try {
const traefikPath = path . join (
APP_PATH ,
"traefik" ,
"traefik_config.yml"
) ;
const schema = z . object ( {
experimental : z.object ( {
plugins : z.object ( {
badger : z.object ( {
moduleName : z.string ( ) ,
version : z.string ( )
} )
} )
} )
} ) ;
const traefikFileContents = fs . readFileSync ( traefikPath , "utf8" ) ;
const traefikConfig = yaml . load ( traefikFileContents ) as any ;
const parsedConfig = schema . safeParse ( traefikConfig ) ;
if ( ! parsedConfig . success ) {
throw new Error ( fromZodError ( parsedConfig . error ) . toString ( ) ) ;
}
traefikConfig . experimental . plugins . badger . version = "v1.1.0" ;
const updatedTraefikYaml = yaml . dump ( traefikConfig ) ;
fs . writeFileSync ( traefikPath , updatedTraefikYaml , "utf8" ) ;
console . log (
"Updated the version of Badger in your Traefik configuration to v1.1.0"
) ;
} catch ( e ) {
console . log (
"We were unable to update the version of Badger in your Traefik configuration. Please update it manually. Check the release notes for this version for more information."
) ;
console . error ( e ) ;
}
2025-04-04 22:58:01 -04:00
console . log ( ` ${ version } migration complete ` ) ;
}