-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
89 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env node | ||
|
||
require('../dist/cli.js') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import {Client} from "@elastic/elasticsearch"; | ||
import {join} from "path"; | ||
import Storage from "./storage"; | ||
import Runner from "./runner"; | ||
import {Migration} from "./migration"; | ||
|
||
export interface Configuration { | ||
root: string, | ||
migrations: string[] | ||
elasticsearch: string | ||
index: string | ||
} | ||
|
||
export function getClient(argv: Configuration) { | ||
if(typeof argv.elasticsearch === 'string') { | ||
if(argv.elasticsearch.match(/http/)) { | ||
return new Client({node: argv.elasticsearch}); | ||
} | ||
else { | ||
return require(join(argv.root, argv.elasticsearch)) | ||
} | ||
} | ||
return new Client(argv.elasticsearch); | ||
} | ||
|
||
export function getRunner(argv: Configuration) { | ||
const client = getClient(argv); | ||
const storage = new Storage(client, argv.index); | ||
const runner = new Runner(client, storage); | ||
runner.on('up', (migration: Migration) => { | ||
process.stdout.write(`\u2713 ${migration.title}\n`) | ||
}); | ||
runner.on('down', (migration: Migration) => { | ||
process.stdout.write(`\u2713 ${migration.title}\n`) | ||
}) | ||
|
||
return runner; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,15 @@ | ||
import {Client} from "@elastic/elasticsearch"; | ||
import Runner from './runner'; | ||
import Storage from './storage'; | ||
import Loader from "./loader"; | ||
import {Migration} from "./migration"; | ||
import {join} from 'path' | ||
|
||
import yargs from 'yargs' | ||
|
||
interface Configuration { | ||
root: string, | ||
migrations: string[] | ||
elasticsearch: string | ||
index: string | ||
} | ||
|
||
yargs.options({ | ||
r: {alias: 'root', type: 'string', default: process.cwd()}, | ||
m: {alias: 'migrations', type: 'array', default: ['./migrations/*.js']}, | ||
e: {alias: 'elasticsearch', type: 'string', default: 'http://localhost:9200'}, | ||
i: {alias: 'index', type: 'string', default: 'migrations'}, | ||
}); | ||
yargs.pkgConf('elasticsearch-migrate'); | ||
|
||
yargs.command({ | ||
command: 'list', | ||
describe: 'List migrations', | ||
handler: async function(argv: Configuration) { | ||
const loader = new Loader(argv.root, argv.migrations); | ||
const client = getClient(argv); | ||
const storage = new Storage(client); | ||
const migrations = await loader.load(); | ||
const last = await storage.getLastRevision(); | ||
process.stdout.write('Migrations:\n-----------\n') | ||
migrations.forEach(function (migration) { | ||
process.stdout.write(`${migration.title === last ? '->' : ' '} ${migration.title}\n`) | ||
}) | ||
await client.close(); | ||
}, | ||
}) | ||
|
||
yargs.command({ | ||
command: 'up', | ||
describe: 'Execute migrations in order', | ||
handler: async function(argv: Configuration) { | ||
const loader = new Loader(argv.root, argv.migrations); | ||
const runner = getRunner(argv); | ||
process.stdout.write('Running migrations...\n---------------------\n'); | ||
await runner.up(await loader.load()); | ||
await runner.client.close(); | ||
} | ||
}) | ||
|
||
yargs.command({ | ||
command: 'down', | ||
describe: 'Rollback migrations', | ||
handler: async function(argv: Configuration) { | ||
const loader = new Loader(argv.root, argv.migrations); | ||
const runner = getRunner(argv); | ||
process.stdout.write('Running downward migrations...\n------------------------------\n'); | ||
await runner.down(await loader.load()); | ||
await runner.client.close(); | ||
} | ||
}) | ||
|
||
yargs.help() | ||
yargs.pkgConf('elasticsearch-migrate') | ||
.commandDir(join(__dirname, 'commands')) | ||
.help() | ||
.demandCommand() | ||
.argv | ||
|
||
function getClient(argv: Configuration) { | ||
if(typeof argv.elasticsearch === 'string') { | ||
if(argv.elasticsearch.match(/http/)) { | ||
return new Client({node: argv.elasticsearch}); | ||
} | ||
else { | ||
return require(join(argv.root, argv.elasticsearch)) | ||
} | ||
} | ||
return new Client(argv.elasticsearch); | ||
} | ||
|
||
function getRunner(argv: Configuration) { | ||
const client = getClient(argv); | ||
const storage = new Storage(client, argv.index); | ||
const runner = new Runner(client, storage); | ||
runner.on('up', (migration: Migration) => { | ||
process.stdout.write(`\u2713 ${migration.title}\n`) | ||
}); | ||
runner.on('down', (migration: Migration) => { | ||
process.stdout.write(`\u2713 ${migration.title}\n`) | ||
}) | ||
|
||
return runner; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import Loader from "../loader"; | ||
import {Configuration, getRunner} from "../cli.helper"; | ||
|
||
export const command = 'down'; | ||
export const describe = 'Rollback migrations'; | ||
export const handler = async function(argv: Configuration) { | ||
const loader = new Loader(argv.root, argv.migrations); | ||
const runner = getRunner(argv); | ||
process.stdout.write('Running downward migrations...\n------------------------------\n'); | ||
await runner.down(await loader.load()); | ||
await runner.client.close(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {Configuration, getClient} from "../cli.helper"; | ||
import Loader from "../loader"; | ||
import Storage from "../storage"; | ||
|
||
export const command = 'list' | ||
export const describe = 'List migrations' | ||
export const handler = async function(argv: Configuration) { | ||
const loader = new Loader(argv.root, argv.migrations); | ||
const client = getClient(argv); | ||
const storage = new Storage(client); | ||
const migrations = await loader.load(); | ||
const last = await storage.getLastRevision(); | ||
process.stdout.write('Migrations:\n-----------\n') | ||
migrations.forEach(function (migration) { | ||
process.stdout.write(`${migration.title === last ? '->' : ' '} ${migration.title}\n`) | ||
}) | ||
await client.close(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import Loader from "../loader"; | ||
import {Configuration, getRunner} from "../cli.helper"; | ||
|
||
export const command = 'up'; | ||
export const describe = 'Execute migrations in order'; | ||
export const handler = async function(argv: Configuration) { | ||
const loader = new Loader(argv.root, argv.migrations); | ||
const runner = getRunner(argv); | ||
process.stdout.write('Running migrations...\n---------------------\n'); | ||
await runner.up(await loader.load()); | ||
await runner.client.close(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters