Skip to content

Commit

Permalink
refactor(settingsmigrator): settings migration handler and the migrat…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
fallenbagel committed May 31, 2024
1 parent 130bb29 commit ce04315
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 56 deletions.
22 changes: 6 additions & 16 deletions server/lib/settings/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { MediaServerType } from '@server/constants/server';
import { Permission } from '@server/lib/permissions';
import { SettingsMigrator } from '@server/lib/settings/settingsMigrator';
import logger from '@server/logger';
import { runMigrations } from '@server/lib/settings/migrator';
import { randomUUID } from 'crypto';
import fs from 'fs';
import { merge } from 'lodash';
Expand Down Expand Up @@ -295,7 +294,7 @@ export interface AllSettings {

const SETTINGS_PATH = process.env.CONFIG_DIRECTORY
? `${process.env.CONFIG_DIRECTORY}/settings.json`
: path.join(__dirname, '../../config/settings.json');
: path.join(__dirname, '../../../config/settings.json');

class Settings {
private data: AllSettings;
Expand Down Expand Up @@ -643,20 +642,11 @@ class Settings {

if (data) {
const parsedJson = JSON.parse(data);
this.data = runMigrations(parsedJson);

SettingsMigrator.migrateSettings(parsedJson)
.then((migrated) => {
this.data = Object.assign(this.data, migrated);
this.data = merge(this.data, migrated);

this.save();
})
.catch((error) => {
logger.error('Error migrating settings', {
label: 'Settings',
error: error,
});
});
this.data = merge(this.data, parsedJson);

this.save();
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import type { AllSettings } from '@server/lib/settings';

export default function removeHostnameMigration(settings: any): AllSettings {
const migrateHostname = (settings: any): AllSettings => {
const oldJellyfinSettings = settings.jellyfin;

if (oldJellyfinSettings && oldJellyfinSettings.hostname) {
const { hostname } = oldJellyfinSettings;
const protocolMatch = hostname.match(/^(https?):\/\//i);
const useSsl = protocolMatch && protocolMatch[1].toLowerCase() === 'https';

const remainingUrl = hostname.replace(/^(https?):\/\//i, '');
const urlMatch = remainingUrl.match(/^([^:]+)(:([0-9]+))?(\/.*)?$/);

delete oldJellyfinSettings.hostname;

if (urlMatch) {
const [, ip, , port, urlBase] = urlMatch;
settings.jellyfin = {
Expand All @@ -24,10 +21,10 @@ export default function removeHostnameMigration(settings: any): AllSettings {
};
}
}

if (settings.jellyfin && settings.jellyfin.hostname) {
delete settings.jellyfin.hostname;
}

return settings;
}
};

export default migrateHostname;
21 changes: 21 additions & 0 deletions server/lib/settings/migrator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { AllSettings } from '@server/lib/settings';
import fs from 'fs';
import path from 'path';

const migrationsDir = path.join(__dirname, 'migrations');

export const runMigrations = (settings: AllSettings): AllSettings => {
const migrations = fs
.readdirSync(migrationsDir)
.filter((file) => file.endsWith('.js') || file.endsWith('.ts'))
// eslint-disable-next-line @typescript-eslint/no-var-requires
.map((file) => require(path.join(migrationsDir, file)).default);

let migrated = settings;

for (const migration of migrations) {
migrated = migration(migrated);
}

return migrated;
};
33 changes: 0 additions & 33 deletions server/lib/settings/settingsMigrator.ts

This file was deleted.

0 comments on commit ce04315

Please sign in to comment.