diff --git a/commands/migrate.js b/commands/migrate.js index b481194..fda9cea 100644 --- a/commands/migrate.js +++ b/commands/migrate.js @@ -1,8 +1,8 @@ 'use strict'; -const { Command, debug, Workflow } = require('@axiosleo/cli-tool'); +const { Command, debug } = require('@axiosleo/cli-tool'); +const { migrate } = require('../index'); const is = require('@axiosleo/cli-tool/src/helper/is'); -const migration = require('../src/migration'); class MigrateCommand extends Command { constructor() { @@ -26,31 +26,19 @@ class MigrateCommand extends Command { * @param {*} options */ async exec(args, options) { - const workflow = new Workflow(migration); try { - await workflow.start({ - task_key: 'migrate_logs', - action: args.action, - config: { - dir: args.dir - }, - connection: { - host: options.host, - port: is.number(options.port) ? - options.port : parseInt(options.port), - user: options.user, - password: options.pass, - database: options.db - }, + await migrate(args.action, args.dir, { + host: options.host, + port: is.string(options.port) ? + parseInt(options.port) : options.port || 3306, + user: options.user, + password: options.pass, + database: options.db, debug: options.debug }); process.exit(0); } catch (e) { - if (e.curr && e.curr.error) { - debug.error(e.curr.error); - } else { - debug.log(e); - } + debug.error(e); process.exit(1); } } diff --git a/index.d.ts b/index.d.ts index 5c7bd94..9d31983 100644 --- a/index.d.ts +++ b/index.d.ts @@ -661,3 +661,21 @@ export declare class MigrationInterface { raw(sql: string, values: any[]): void; } + +export type MigrateAction = 'up' | 'down' | 'UP' | 'DOWN'; +export type MigrateOptions = { + host?: string, + port?: number, + user?: string, + password?: string, + database?: string, + debug?: boolean +}; + +/** + * migrate database + * @param action + * @param dir + * @param options + */ +export function migrate(action: MigrateAction, dir: string, options?: MigrateOptions): Promise; diff --git a/index.js b/index.js index caaed0a..ee81d35 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,10 @@ 'use strict'; +const { Workflow } = require('@axiosleo/cli-tool'); +const Hook = require('./src/hook'); +const { Builder } = require('./src/builder'); +const migration = require('./src/migration'); + const { QueryHandler, QueryOperator, @@ -21,8 +26,32 @@ const { MySQLClient } = require('./src/client'); -const Hook = require('./src/hook'); -const { Builder } = require('./src/builder'); +const _runMigration = async (action, dir, options = {}) => { + const workflow = new Workflow(migration); + try { + await workflow.start({ + task_key: 'migrate_logs', + action: action.toLowerCase(), + config: { + dir: dir + }, + connection: { + host: options.host || 'localhost', + port: options.port || 3306, + user: options.user || 'root', + password: options.password || '', + database: options.database || '' + }, + debug: options.debug + }); + } catch (e) { + if (e.curr && e.curr.error) { + throw e.curr.error; + } else { + throw e; + } + } +}; module.exports = { Hook, @@ -40,5 +69,6 @@ module.exports = { getClient, createPool, createClient, - createPromiseClient + createPromiseClient, + migrate: _runMigration }; diff --git a/package.json b/package.json index 49f9251..96f9a18 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "test": "mocha --reporter spec --timeout 3000 tests/*.tests.js", "test-cov": "nyc -r=lcov -r=html -r=text -r=json mocha -t 10000 -R spec tests/*.tests.js", "test-one": "mocha --reporter spec --timeout 3000 ", + "build": "DEPLOY_ENV=prod bun build ./index.js --compile --outfile dist/orm-mysql", "feature-test": "node tests/transaction.ft.js", "setup-feature-db": "node tests/setup-feature-db.js", "feature-test:local": "docker compose up -d && sleep 10 && npm run setup-feature-db && npm run feature-test && docker compose down", @@ -48,7 +49,6 @@ "mocha-sinon": "^2.1.2", "nyc": "^15.1.0", "pre-commit": "^1.2.2", - "sinon": "^17.0.1", "typescript": "^5.3.3" }, "pre-commit": { diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml new file mode 100644 index 0000000..dacb4a5 --- /dev/null +++ b/pnpm-workspace.yaml @@ -0,0 +1,5 @@ +ignoredBuiltDependencies: + - pre-commit + +onlyBuiltDependencies: + - spawn-sync diff --git a/src/migration.js b/src/migration.js index 0635ec2..8f65925 100644 --- a/src/migration.js +++ b/src/migration.js @@ -97,8 +97,7 @@ async function init(context) { let res = await _execSQL(conn, builder.sql); conn.end(); if (res.serverStatus !== 2 && res.serverStatus !== 16386) { - printer.error('create migration table failed.'); - process.exit(1); + throw new Error('create migration table failed.'); } } @@ -319,16 +318,14 @@ async function run(context) { switch (context.action) { case 'up': { if (typeof script.up !== 'function') { - printer.error(`Migration file "${file}" must have a function named up.`); - process.exit(1); + throw new Error(`Migration file "${file}" must have a function named up.`); } await script.up(migration); break; } case 'down': { if (typeof script.down !== 'function') { - printer.error(`Migration file "${file}" must have a function named down.`); - process.exit(1); + throw new Error(`Migration file "${file}" must have a function named down.`); } await script.down(migration); break;