Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/FoxxMD/multi-scrobbler. Scrobble plays from multiple sources to multiple clients docs.multi-scrobbler.app
deezer docker jellyfin koito lastfm listenbrainz maloja mopidy mpris music music-assistant plex scrobble self-hosted spotify subsonic tautulli youtube-music
Something went wrong. Try again.
2.6 kB · 68 lines
TypeScript
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768import type { SqliteDatabase, Migration } from 'sqlite-up';import type {MigrateBaseContext} from '../appMigrator.ts';import { plays as drizzlePlays } from '../drizzle/schema/schema.ts';import clone from 'clone';import { eq } from 'drizzle-orm';import type {PlayLifecycle, PlayObject} from '../../../../core/Atomic.ts';
export const up: Migration<MigrateBaseContext>['up'] = async (db: SqliteDatabase, ctx: MigrateBaseContext | undefined): Promise<void> => { if(ctx === undefined) { throw new Error('Context must be defined'); } ctx.logger.info('Migrating Play lifecycle data to top-level location. This may take some time...');
let more = true; let offset = 0, processed = 0, updated = 0; while (more) { const playRows = await ctx.db.select().from(drizzlePlays).limit(100).offset(offset); for (const row of playRows) { const play = row.play as PlayObject<{lifecycle?: PlayLifecycle}>; try { const { meta: { lifecycle, lifecycle: { steps = [], scrobble } = {} } = {} } = play; if (lifecycle === undefined) { // already migrated or unneeded processed++; continue; } if (steps.length > 0) { row.play.lifecycle = clone(steps); } if (scrobble !== undefined) { row.play.scrobble = clone(scrobble); } // @ts-expect-error its fine delete row.play.meta.lifecycle; await ctx.db.update(drizzlePlays).set({ play: row.play }).where(eq(drizzlePlays.id, row.id)); updated++; processed++; } catch (e) { ctx.logger.warn(new Error(`Failed to migrate Play ${row.id} (${row.uid})`, { cause: e })); } } offset += 100; ctx.logger.verbose(`Migration Progress: Processed ${processed} | Updated ${updated}`); if(playRows.length < 100) { more = false; } }
// Migration code here // context is passed as ctx};
export const down: Migration<MigrateBaseContext>['down'] = async (db: SqliteDatabase, ctx: MigrateBaseContext | undefined): Promise<void> => { // Rollback code here // context is passed as ctx};