-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
executable file
·131 lines (108 loc) · 3.74 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env node
require('dotenv').config();
const fs = require('fs');
const path = require('path');
const { docopt } = require('docopt');
const { parse } = require('pg-connection-string');
const { promisify } = require('util');
const PgMigrate = require('./pg-migrate');
const packagejson = require('./package.json');
const doc = `
Urbica PostgreSQL Migrate.
Usage:
pg-migrate [options] new <name>
pg-migrate [options] migrate
pg-migrate [options] rollback <N>
pg-migrate [options] reset
pg-migrate --help
pg-migrate --version
Examples:
pg-migrate new create-users
pg-migrate migrate
pg-migrate rollback 1
pg-migrate reset
Options:
--help Show this screen
--version Show version
--verbose Show verbose output
-m --migrations-dir=DIR The directory containing your migration files [default: ./migrations]
-t --migrations-table=TABLE Set the name of the migrations table [default: migrations]
-s --migrations-schema=SCHEMA Set the name of the migrations table scheme [default: public]
Connection options:
-c --connection=DATABASE_URL database connection string in libpq format
-d --db=PGDATABASE database name to connect to
-h --host=PGHOST database server host or socket directory [default: localhost]
-p --port=PGPORT database server port [default: 5432]
-U --user=PGUSER database user name
-W --password=PGPASSWORD database user name password
-S --ssl=PGSSL database connection using ssl [default: false]
`;
const opt = docopt(doc, { version: packagejson.version });
const connectionString = opt['--connection'] || process.env.DATABASE_URL;
const connection = (connectionString && parse(connectionString)) || {
database: opt['--db'] || process.env.PGDATABASE || process.env.POSTGRES_DB,
host: opt['--host'] || process.env.PGHOST || process.env.POSTGRES_HOST,
port: opt['--port'] || process.env.PGPORT || process.env.POSTGRES_PORT,
user: opt['--user'] || process.env.PGUSER || process.env.POSTGRES_USER,
password:
opt['--password'] ||
process.env.PGPASSWORD ||
process.env.POSTGRES_PASSWORD,
ssl:
(opt['--ssl'] || process.env.PGSSL || process.env.POSTGRES_SSL) === 'true'
};
const options = {
...connection,
migrationsSchema: opt['--migrations-schema'],
migrationsTable: opt['--migrations-table'],
migrationsDir: opt['--migrations-dir'],
verbose: opt['--verbose'] || true
};
const { root } = path.parse(options.migrationsDir);
if (root !== '/') {
options.migrationsDir = path.join(process.cwd(), options.migrationsDir);
}
async function main() {
/* eslint-disable no-console */
if (opt.new) {
const writeFileAsync = promisify(fs.writeFile);
const content = '-- replace with your sql';
const ts = Math.floor(new Date() / 1000);
const baseName = `${ts}-${opt['<name>']}`;
const up = path.format({
dir: options.migrationsDir,
name: baseName,
ext: '.up.sql'
});
const down = path.format({
dir: options.migrationsDir,
name: baseName,
ext: '.down.sql'
});
await Promise.all([
writeFileAsync(up, content),
writeFileAsync(down, content)
]);
console.log(up);
console.log(down);
return;
}
try {
const pgMigrate = new PgMigrate(options);
await pgMigrate.connect();
if (opt.migrate) {
await pgMigrate.migrate();
}
if (opt.rollback) {
await pgMigrate.rollback(opt['<N>']);
}
if (opt.reset) {
await pgMigrate.reset();
}
await pgMigrate.end();
} catch (error) {
console.error(error.message);
process.exit(-1);
}
}
main();