Koishi Database Migration Plugin
koishi-plugin-migrate-database is a utility plugin for the Koishi chatbot framework, designed to facilitate the migration of data between different database instances. As of version 0.2.1, it provides basic functionality for moving existing Koishi data, which is critical for developers managing evolving bot infrastructures or changing database backends. This plugin is typically released on an as-needed basis to support specific migration scenarios or Koishi core updates. Its primary differentiator is its integration directly into the Koishi ecosystem, allowing for migrations that are aware of Koishi's internal data structures, unlike generic database migration tools. Users should be aware that, as a 0.x.x version, its API and internal workings may still evolve, and thorough testing is recommended before production use.
Common errors
-
Error: Plugin 'koishi-plugin-migrate-database' not found.
cause The plugin package was not correctly installed or registered, or the import path is incorrect.fixEnsure the package is installed (`npm i koishi-plugin-migrate-database`) and correctly added to your Koishi configuration file (e.g., `koishi.config.ts`) using `ctx.plugin(apply, config)`. -
Peer dependency 'koishi@^4.17.5' not met.
cause Your installed Koishi version does not satisfy the requirements of the migration plugin.fixUpgrade or downgrade your Koishi installation to a version compatible with `^4.17.5` (e.g., `npm update koishi` or `npm install koishi@4.17.5`). -
TypeError: Cannot read properties of undefined (reading 'database')
cause The Koishi context or database service was not properly initialized or available when the plugin attempted to access it.fixVerify that your Koishi instance is correctly configured with a database adapter and that the plugin is registered after the core Koishi services are set up. -
Error: SQLITE_CANTOPEN: unable to open database file
cause The specified database file path is incorrect, or there are insufficient permissions to access or create the database file.fixCheck the `path` configuration for your database (e.g., SQLite) and ensure the Koishi process has read/write permissions for the directory containing the database file.
Warnings
- breaking This plugin directly manipulates database records. Improper use, misconfiguration, or unexpected schema differences can lead to irreversible data loss or corruption. Always perform a full database backup before attempting any migration.
- gotcha As a utility for database migration, this plugin's functionality is highly sensitive to the underlying database types (e.g., SQLite, MySQL, MongoDB) and their specific connection parameters. Generic settings may not work, and deep understanding of your database configuration is required.
- breaking The plugin has a peer dependency on `koishi: ^4.17.5`. Using it with significantly older or newer major versions of Koishi may lead to runtime errors due to API changes in the core framework or database adapters.
- gotcha This plugin is in its early `0.x.x` development phase. The API, configuration options, and internal implementation may undergo significant changes in minor or patch releases without following strict semantic versioning for non-breaking changes, potentially requiring code adjustments.
Install
-
npm install koishi-plugin-migrate-database -
yarn add koishi-plugin-migrate-database -
pnpm add koishi-plugin-migrate-database
Imports
- apply
import migrateDatabase from 'koishi-plugin-migrate-database'
import { apply } from 'koishi-plugin-migrate-database' - Config
import { Config } from 'koishi-plugin-migrate-database' - koishi-plugin-migrate-database
const migrateDatabase = require('koishi-plugin-migrate-database')ctx.plugin(apply, config)
Quickstart
import { Context, Schema } from 'koishi';
import { apply, Config } from 'koishi-plugin-migrate-database';
// Assuming you have a koishi.config.ts or similar configuration setup
// This demonstrates how to register the plugin within your Koishi application.
const ctx = new Context({
// Your Koishi configuration, e.g., database, adapters, etc.
database: {
// Example database configuration for a new database
// type: 'sqlite',
// path: 'data.db',
},
// ... other configurations
});
// Register the migration plugin. Be extremely cautious when using this in production.
// Ensure you have backups and understand the migration process.
ctx.plugin(apply, {
// Example configuration for the migration plugin (adjust as needed)
// sourceDatabase: 'sqlite',
// sourcePath: 'old_data.db',
// targetDatabase: 'mysql',
// targetUri: process.env.MYSQL_URI ?? '',
// dryRun: true, // It is highly recommended to use dryRun first
// enable: true, // Only enable when explicitly ready to migrate
});
ctx.start();
console.log('Koishi application started with migration plugin registered.');
console.log('Ensure to check plugin logs for migration status and potential errors.');