Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 10 additions & 22 deletions commands/migrate.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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);
}
}
Expand Down
18 changes: 18 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
36 changes: 33 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -40,5 +69,6 @@ module.exports = {
getClient,
createPool,
createClient,
createPromiseClient
createPromiseClient,
migrate: _runMigration
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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": {
Expand Down
5 changes: 5 additions & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ignoredBuiltDependencies:
- pre-commit

onlyBuiltDependencies:
- spawn-sync
9 changes: 3 additions & 6 deletions src/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
}

Expand Down Expand Up @@ -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;
Expand Down