Skip to content

Commit 70f9c93

Browse files
committed
feat: add ley status command
1 parent c97dc45 commit 70f9c93

File tree

5 files changed

+55
-5
lines changed

5 files changed

+55
-5
lines changed

bin.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ sade('ley')
3333
.option('-a, --all', 'Run all "down" migrations')
3434
.action(wrap('down'))
3535

36+
.command('status')
37+
.describe('Check for migration status. Counts unapplied migrations.')
38+
.action(wrap('status'))
39+
3640
.command('new <filename>')
3741
.describe('Create a new migration file.')
3842
.option('-t, --timestamp', 'Prefix the filename with a timestamp')
@@ -42,5 +46,4 @@ sade('ley')
4246
return wrap('new')(opts);
4347
})
4448

45-
4649
.parse(process.argv);

index.js

+12
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ exports.down = async function (opts={}) {
6767
}
6868
}
6969

70+
exports.status = async function (opts={}) {
71+
let client, { driver, migrations } = await parse(opts);
72+
73+
try {
74+
client = await driver.connect(opts.config);
75+
const exists = await driver.setup(client);
76+
return $.diff(exists, migrations).map(x => x.name);
77+
} finally {
78+
if (client) await driver.end(client);
79+
}
80+
}
81+
7082
exports.new = async function (opts={}) {
7183
let { migrations } = await parse(opts);
7284

ley.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ declare class MigrationError extends Error {
3131

3232
export function up(opts?: Options.Up): Promise<string[]>;
3333
export function down(opts?: Options.Down): Promise<string[]>;
34+
export function status(opts?: Options.Base): Promise<string[]>;
3435

3536
declare function n(opts?: Options.New): Promise<string>;
3637
export { n as new };

lib/log.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { cyan, green, underline, red, dim } = require('kleur');
1+
const { cyan, green, yellow, underline, red, dim } = require('kleur');
22
const { MigrationError } = require('./util');
33

44
exports.info = msg => {
@@ -12,9 +12,12 @@ exports.done = (type, arr) => {
1212
msg += 'Created ' + green('1') + ' file:';
1313
msg += green().dim('\n ⌁ ') + underline().grey(arr);
1414
} else if (arr.length > 0) {
15-
let i=0, arrow = type === 'up' ? '↑' : '↓';
16-
msg += ('Migrated ' + green(arr.length) + (arr.length > 1 ? ' files:' : ' file:'));
17-
for (; i < arr.length; i++) msg += (green().dim(`\n ${arrow} `) + underline().grey(arr[i]));
15+
let i=0, len=arr.length;
16+
let arrow = type === 'down' ? '↓' : '↑';
17+
let cc = type === 'status' ? yellow : green;
18+
msg += (type === 'status' ? 'Awaiting ' : 'Migrated ');
19+
msg += cc(len) + (len > 1 ? ' files:' : ' file:');
20+
for (; i < len; i++) msg += (cc().dim(`\n ${arrow} `) + underline().grey(arr[i]));
1821
} else {
1922
msg += 'All caught up! 🎉';
2023
}

readme.md

+31
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,37 @@ Enable to apply **all** migration files' `down` task.<br>
237237
By default, only the most recently-applied migration file is invoked.
238238
239239
240+
### ley.status(opts?)
241+
Returns: `Promise<string[]>`
242+
243+
Returns a list of the _relative filenames_ (eg, `000-users.js`) that have not yet been applied.
244+
245+
#### opts.cwd
246+
Type: `string`<br>
247+
Default: `.`
248+
249+
A target location to treat as the current working directory.
250+
251+
> **Note:** This value is `path.resolve()`d from the current `process.cwd()` location.
252+
253+
#### opts.dir
254+
Type: `string`<br>
255+
Default: `migrations`
256+
257+
The directory (relative to `opts.cwd`) to find migration files.
258+
259+
#### opts.client
260+
Type: `string`<br>
261+
Default: `undefined`
262+
263+
The **name** of your desired client driver; for example, `pg`.<br>
264+
When unspecified, `ley` searches for all supported client drivers in this order:
265+
266+
```js
267+
['postgres', 'pg']; // TODO: more
268+
```
269+
270+
240271
### ley.new(opts?)
241272
Returns: `Promise<string>`
242273

0 commit comments

Comments
 (0)