Services

Internal service classes for advanced usage and customization.

Table of contents

  1. Services
    1. MigrationService
      1. Methods
        1. findMigrationScripts()
        2. findBeforeMigrateScript()
    2. SchemaVersionService
      1. Methods
        1. init()
        2. save()
        3. getAllMigratedScripts()
        4. remove()
    3. BackupService
      1. Methods
        1. backup()
        2. restore()
        3. deleteBackup()

Services

MigrationService

Service for scanning and loading migration files.

import { MigrationService } from '@migration-script-runner/core';

const service = new MigrationService();
const scripts = await service.readMigrationScripts(config);

Methods

findMigrationScripts()

Scan directory and load all migration scripts.

async findMigrationScripts(config: Config): Promise<MigrationScript[]>

Parameters:

  • config: Configuration object

Returns: Array of MigrationScript objects sorted by timestamp

v0.4.0 API Change: Method renamed from readMigrationScripts() to findMigrationScripts() for consistency.

The beforeMigrate file (if it exists) is NOT included in the results. It’s handled separately via findBeforeMigrateScript().


findBeforeMigrateScript()

Check if a beforeMigrate setup script exists.

async findBeforeMigrateScript(config: Config): Promise<string | undefined>

Parameters:

  • config: Configuration object containing beforeMigrateName property

Returns: Path to beforeMigrate script if found, undefined otherwise

v0.4.0 API Change: Method renamed from getBeforeMigrateScript() to findBeforeMigrateScript() for consistency.

Behavior:

  • Returns undefined if config.beforeMigrateName is null (feature disabled)
  • Looks for files with configured name + .ts or .js extension
  • Only searches in root of migrations folder (not recursive)

Example:

const config = new Config();
config.beforeMigrateName = 'beforeMigrate';  // default
config.folder = './migrations';

const service = new MigrationService();
const path = await service.findBeforeMigrateScript(config);

if (path) {
  console.log(`Found beforeMigrate script: ${path}`);
  // migrations/beforeMigrate.ts
}

SchemaVersionService

Service for managing the schema version tracking table.

import { SchemaVersionService } from '@migration-script-runner/core';

const service = new SchemaVersionService(config, handler);
await service.init();

Methods

init()

Initialize the schema version table.

async init(): Promise<void>

save()

Save migration info to tracking table.

async save(info: IMigrationInfo): Promise<void>

getAllMigratedScripts()

Get all executed migrations.

async getAllMigratedScripts(): Promise<IMigrationInfo[]>

remove()

Remove a migration record from the schema version table.

async remove(timestamp: number): Promise<void>

Parameters:

  • timestamp: The timestamp of the migration to remove

Usage: Used internally by downTo() to remove migration records after successful rollback. Can also be used manually for maintenance operations.

Example:

const service = new SchemaVersionService(config, handler);
await service.remove(202501220100);

This method is called automatically by downTo() after successfully executing a migration’s down() method. Manual use is typically not needed unless performing database maintenance.


BackupService

Service for backup and restore operations.

import { BackupService } from '@migration-script-runner/core';

const service = new BackupService(config.backup, handler);
const backupPath = await service.backup();

Methods

backup()

Create a backup file.

async backup(): Promise<string>

Returns: Path to created backup file


restore()

Restore from a backup file.

async restore(backupPath?: string): Promise<void>

Parameters:

  • backupPath (optional): Path to specific backup file. If not provided, uses the most recent backup.

deleteBackup()

Delete the backup file from disk.

deleteBackup(): void

Behavior:

  • Only deletes if config.backup.deleteBackup is true
  • Safe to call multiple times