rake-db 1.3.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env +3 -0
- package/.env.local +1 -0
- package/README.md +1 -545
- package/db.ts +16 -0
- package/dist/index.d.ts +94 -0
- package/dist/index.esm.js +190 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +201 -0
- package/dist/index.js.map +1 -0
- package/jest-setup.ts +3 -0
- package/migrations/20221009210157_first.ts +8 -0
- package/migrations/20221009210200_second.ts +5 -0
- package/package.json +55 -41
- package/rollup.config.js +3 -0
- package/src/commands/createOrDrop.test.ts +145 -0
- package/src/commands/createOrDrop.ts +107 -0
- package/src/commands/generate.test.ts +133 -0
- package/src/commands/generate.ts +85 -0
- package/src/commands/migrateOrRollback.test.ts +118 -0
- package/src/commands/migrateOrRollback.ts +108 -0
- package/src/common.test.ts +281 -0
- package/src/common.ts +224 -0
- package/src/index.ts +2 -0
- package/src/migration/change.ts +20 -0
- package/src/migration/changeTable.test.ts +417 -0
- package/src/migration/changeTable.ts +375 -0
- package/src/migration/createTable.test.ts +269 -0
- package/src/migration/createTable.ts +169 -0
- package/src/migration/migration.test.ts +341 -0
- package/src/migration/migration.ts +296 -0
- package/src/migration/migrationUtils.ts +281 -0
- package/src/rakeDb.ts +29 -0
- package/src/test-utils.ts +45 -0
- package/tsconfig.json +12 -0
- package/dist/lib/createAndDrop.d.ts +0 -2
- package/dist/lib/createAndDrop.js +0 -63
- package/dist/lib/defaults.d.ts +0 -2
- package/dist/lib/defaults.js +0 -5
- package/dist/lib/errorCodes.d.ts +0 -4
- package/dist/lib/errorCodes.js +0 -7
- package/dist/lib/generate.d.ts +0 -1
- package/dist/lib/generate.js +0 -99
- package/dist/lib/help.d.ts +0 -2
- package/dist/lib/help.js +0 -24
- package/dist/lib/init.d.ts +0 -2
- package/dist/lib/init.js +0 -276
- package/dist/lib/migrate.d.ts +0 -4
- package/dist/lib/migrate.js +0 -189
- package/dist/lib/migration.d.ts +0 -37
- package/dist/lib/migration.js +0 -159
- package/dist/lib/schema/changeTable.d.ts +0 -23
- package/dist/lib/schema/changeTable.js +0 -109
- package/dist/lib/schema/column.d.ts +0 -31
- package/dist/lib/schema/column.js +0 -201
- package/dist/lib/schema/createTable.d.ts +0 -10
- package/dist/lib/schema/createTable.js +0 -53
- package/dist/lib/schema/foreignKey.d.ts +0 -11
- package/dist/lib/schema/foreignKey.js +0 -53
- package/dist/lib/schema/index.d.ts +0 -3
- package/dist/lib/schema/index.js +0 -54
- package/dist/lib/schema/primaryKey.d.ts +0 -9
- package/dist/lib/schema/primaryKey.js +0 -24
- package/dist/lib/schema/table.d.ts +0 -43
- package/dist/lib/schema/table.js +0 -110
- package/dist/lib/schema/timestamps.d.ts +0 -3
- package/dist/lib/schema/timestamps.js +0 -9
- package/dist/lib/utils.d.ts +0 -26
- package/dist/lib/utils.js +0 -114
- package/dist/rake-db.d.ts +0 -2
- package/dist/rake-db.js +0 -34
- package/dist/types.d.ts +0 -94
- package/dist/types.js +0 -40
package/dist/lib/migrate.js
DELETED
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
-
}) : (function(o, m, k, k2) {
|
|
6
|
-
if (k2 === undefined) k2 = k;
|
|
7
|
-
o[k2] = m[k];
|
|
8
|
-
}));
|
|
9
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
-
}) : function(o, v) {
|
|
12
|
-
o["default"] = v;
|
|
13
|
-
});
|
|
14
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
-
if (mod && mod.__esModule) return mod;
|
|
16
|
-
var result = {};
|
|
17
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
-
__setModuleDefault(result, mod);
|
|
19
|
-
return result;
|
|
20
|
-
};
|
|
21
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
22
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
23
|
-
};
|
|
24
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
-
exports.rollback = exports.migrate = exports.run = void 0;
|
|
26
|
-
const fs = __importStar(require("fs"));
|
|
27
|
-
const path = __importStar(require("path"));
|
|
28
|
-
const utils_1 = require("./utils");
|
|
29
|
-
const migration_1 = __importDefault(require("./migration"));
|
|
30
|
-
const errorCodes_1 = require("./errorCodes");
|
|
31
|
-
const ts_node_1 = require("ts-node");
|
|
32
|
-
ts_node_1.register({ compilerOptions: { module: 'CommonJS' } });
|
|
33
|
-
const getMigratedVersionsQuery = (db) => db.value(`SELECT COALESCE(json_agg("schemaMigrations".version ORDER BY version), '[]')` +
|
|
34
|
-
`FROM "schemaMigrations"`);
|
|
35
|
-
const getMigratedVersions = async (db) => {
|
|
36
|
-
try {
|
|
37
|
-
return await getMigratedVersionsQuery(db);
|
|
38
|
-
}
|
|
39
|
-
catch (err) {
|
|
40
|
-
if (errorCodes_1.errorCodes[err.code] === 'undefined_table') {
|
|
41
|
-
await utils_1.createSchemaMigrations(db);
|
|
42
|
-
return await getMigratedVersionsQuery(db);
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
throw err;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
const getFiles = (migrationsPath, rollback) => new Promise((resolve, reject) => {
|
|
50
|
-
fs.readdir(migrationsPath, (err, allFiles) => {
|
|
51
|
-
if (err)
|
|
52
|
-
return reject(err);
|
|
53
|
-
if (rollback)
|
|
54
|
-
allFiles.sort((a, b) => (a < b ? 1 : -1));
|
|
55
|
-
else
|
|
56
|
-
allFiles.sort();
|
|
57
|
-
const files = [];
|
|
58
|
-
allFiles.forEach((file) => {
|
|
59
|
-
const arr = file.split('_');
|
|
60
|
-
const match = file.match(/\..+$/);
|
|
61
|
-
if (!match)
|
|
62
|
-
return;
|
|
63
|
-
const ext = match[0];
|
|
64
|
-
if (ext !== '.ts')
|
|
65
|
-
return;
|
|
66
|
-
if (arr.length === 1)
|
|
67
|
-
return;
|
|
68
|
-
const version = arr[0];
|
|
69
|
-
if (version.length !== 14)
|
|
70
|
-
return;
|
|
71
|
-
files.push({ version, path: file });
|
|
72
|
-
});
|
|
73
|
-
resolve(files);
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
const run = async (db, fn, version) => {
|
|
77
|
-
await db.wrapperTransaction(db, async (t) => {
|
|
78
|
-
if (fn.toString().startsWith('async')) {
|
|
79
|
-
await fn(t, !db.reverse);
|
|
80
|
-
}
|
|
81
|
-
else {
|
|
82
|
-
if (!db.reverse) {
|
|
83
|
-
fn(t, true);
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
const originalExec = t.exec;
|
|
87
|
-
const argsList = []; // eslint-disable-line
|
|
88
|
-
t.exec = (...args) => {
|
|
89
|
-
argsList.push(args);
|
|
90
|
-
return Promise.resolve();
|
|
91
|
-
};
|
|
92
|
-
await fn(t, false);
|
|
93
|
-
t.exec = originalExec;
|
|
94
|
-
for (const [sql, value] of argsList.reverse()) {
|
|
95
|
-
await t.exec(sql, value);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
await t.sync();
|
|
100
|
-
if (t.failed)
|
|
101
|
-
return;
|
|
102
|
-
const sql = db.reverse
|
|
103
|
-
? `DELETE FROM "schemaMigrations" WHERE "version" = '${version}'`
|
|
104
|
-
: `INSERT INTO "schemaMigrations"
|
|
105
|
-
VALUES ('${version}')`;
|
|
106
|
-
await t.exec(sql).catch(utils_1.noop);
|
|
107
|
-
});
|
|
108
|
-
};
|
|
109
|
-
exports.run = run;
|
|
110
|
-
const migrateFile = async (db, migrationsPath, version, file) => {
|
|
111
|
-
const filePath = path.resolve(migrationsPath, file);
|
|
112
|
-
const migration = require(filePath);
|
|
113
|
-
if (!db.reverse && !migration.up && !migration.change)
|
|
114
|
-
throw new Error(`Migration ${file} does not contain up or change exports`);
|
|
115
|
-
else if (!migration.down && !migration.change)
|
|
116
|
-
throw new Error(`Migration ${file} does not contain down or change exports`);
|
|
117
|
-
if (migration.before) {
|
|
118
|
-
await migration.before(db, !db.reverse);
|
|
119
|
-
}
|
|
120
|
-
for (const key in migration) {
|
|
121
|
-
if (key === (db.reverse ? 'down' : 'up') || key === 'change') {
|
|
122
|
-
await exports.run(db, migration[key], version);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
if (migration.after) {
|
|
126
|
-
await migration.after(db, !db.reverse);
|
|
127
|
-
}
|
|
128
|
-
console.info(`${filePath} ${db.reverse ? 'rolled back' : 'migrated'}`);
|
|
129
|
-
};
|
|
130
|
-
const migrateDb = async (db, migrationsPath, files) => {
|
|
131
|
-
for (const { path, version } of files) {
|
|
132
|
-
try {
|
|
133
|
-
await migrateFile(db, migrationsPath, version, path);
|
|
134
|
-
}
|
|
135
|
-
catch (err) {
|
|
136
|
-
console.error(err);
|
|
137
|
-
break;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
const migrateOrRollbackDatabase = async (rollback, db, migrationsPath) => {
|
|
142
|
-
await db.connect();
|
|
143
|
-
let [files, versions] = (await Promise.all([
|
|
144
|
-
getFiles(migrationsPath, rollback),
|
|
145
|
-
getMigratedVersions(db),
|
|
146
|
-
]));
|
|
147
|
-
versions = JSON.parse(versions);
|
|
148
|
-
if (rollback) {
|
|
149
|
-
const lastVersion = versions[versions.length - 1];
|
|
150
|
-
if (!lastVersion) {
|
|
151
|
-
files = [];
|
|
152
|
-
}
|
|
153
|
-
else {
|
|
154
|
-
const lastFile = files.find(({ version }) => version === lastVersion);
|
|
155
|
-
files = [lastFile];
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
else {
|
|
159
|
-
files = files.filter((file) => !versions.includes(file.version));
|
|
160
|
-
}
|
|
161
|
-
if (files.length)
|
|
162
|
-
await migrateDb(db, migrationsPath, files);
|
|
163
|
-
};
|
|
164
|
-
const migrateOrRollback = async (rollback) => {
|
|
165
|
-
const { migrationsPath, configs } = await utils_1.getConfig();
|
|
166
|
-
for (const config of configs) {
|
|
167
|
-
const db = new migration_1.default({ ...config, reverse: rollback });
|
|
168
|
-
try {
|
|
169
|
-
await migrateOrRollbackDatabase(rollback, db, migrationsPath);
|
|
170
|
-
}
|
|
171
|
-
catch (err) {
|
|
172
|
-
if (errorCodes_1.errorCodes[err.code] ===
|
|
173
|
-
'invalid_catalog_name') {
|
|
174
|
-
console.error(err.message);
|
|
175
|
-
}
|
|
176
|
-
else {
|
|
177
|
-
console.error(err);
|
|
178
|
-
}
|
|
179
|
-
break;
|
|
180
|
-
}
|
|
181
|
-
finally {
|
|
182
|
-
db.close();
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
};
|
|
186
|
-
const migrate = () => migrateOrRollback(false);
|
|
187
|
-
exports.migrate = migrate;
|
|
188
|
-
const rollback = () => migrateOrRollback(true);
|
|
189
|
-
exports.rollback = rollback;
|
package/dist/lib/migration.d.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { Adapter, AdapterProps } from 'pg-adapter';
|
|
2
|
-
import { ChangeTableCallback } from './schema/changeTable';
|
|
3
|
-
import { TableOptions, JoinTableOptions, ColumnOptions, TableCallback, ForeignKeyOptions, IndexOptions } from '../types';
|
|
4
|
-
export default class Migration extends Adapter {
|
|
5
|
-
reverse: boolean;
|
|
6
|
-
constructor({ reverse, ...params }: AdapterProps & {
|
|
7
|
-
reverse: boolean;
|
|
8
|
-
});
|
|
9
|
-
createTable(name: string, options?: TableOptions | TableCallback, fn?: TableCallback): void | Promise<unknown>;
|
|
10
|
-
changeTable(name: string, options?: TableOptions | ChangeTableCallback, fn?: ChangeTableCallback): void;
|
|
11
|
-
dropTable(name: string, options?: TableOptions | TableCallback, fn?: TableCallback): void | Promise<unknown>;
|
|
12
|
-
renameTable(from: string, to: string): void;
|
|
13
|
-
addColumn(table: string, name: string, type: string, options?: ColumnOptions): void;
|
|
14
|
-
addForeignKey(table: string, params: ForeignKeyOptions): void;
|
|
15
|
-
dropForeignKey(table: string, params: ForeignKeyOptions): void;
|
|
16
|
-
addPrimaryKey(table: string, columns: string[], name?: string): void;
|
|
17
|
-
dropPrimaryKey(table: string, columns: string[], name?: string): void;
|
|
18
|
-
addIndex(table: string, name: string, options?: IndexOptions): void;
|
|
19
|
-
dropIndex(table: string, name: string, options?: IndexOptions): void;
|
|
20
|
-
addTimestamps(table: string, options?: ColumnOptions): void;
|
|
21
|
-
dropTimestamps(table: string, options?: ColumnOptions): void;
|
|
22
|
-
changeColumn(table: string, name: string, options: ColumnOptions): void;
|
|
23
|
-
changeColumnComment(table: string, column: string, comment: string): void;
|
|
24
|
-
changeColumnDefault(table: string, column: string, value: unknown): void;
|
|
25
|
-
changeColumnNull(table: string, column: string, value: boolean): void;
|
|
26
|
-
changeTableComment(table: string, comment: string): void;
|
|
27
|
-
renameColumn(table: string, from: string, to: string): void;
|
|
28
|
-
dropColumn(table: string, name: string, type: string, options?: ColumnOptions): void;
|
|
29
|
-
columnExists(table: string, column: string): false | Promise<unknown>;
|
|
30
|
-
createJoinTable(tableOne: string, tableTwo: string, options?: JoinTableOptions | TableCallback, cb?: TableCallback): void;
|
|
31
|
-
dropJoinTable(tableOne: string, tableTwo: string, options?: JoinTableOptions | TableCallback, cb?: TableCallback): void;
|
|
32
|
-
foreignKeyExists(fromTable: string, options: string | {
|
|
33
|
-
name?: string;
|
|
34
|
-
column: string;
|
|
35
|
-
}): false | Promise<unknown>;
|
|
36
|
-
tableExists(table: string): false | Promise<unknown>;
|
|
37
|
-
}
|
package/dist/lib/migration.js
DELETED
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const pg_adapter_1 = require("pg-adapter");
|
|
4
|
-
const createTable_1 = require("./schema/createTable");
|
|
5
|
-
const changeTable_1 = require("./schema/changeTable");
|
|
6
|
-
const utils_1 = require("./utils");
|
|
7
|
-
const createTable = (db, name, fn, options) => new createTable_1.CreateTable(name, db.reverse, options).__commit(db, fn);
|
|
8
|
-
const dropTable = (db, name) => db.exec(`DROP TABLE "${name}" CASCADE`).catch(utils_1.noop);
|
|
9
|
-
const renameTable = (db, from, to) => db.exec(`ALTER TABLE "${from}" RENAME TO "${to}"`);
|
|
10
|
-
const createJoinTable = (db, tableOne, tableTwo, options, cb) => {
|
|
11
|
-
const { tableName, columnOptions, references = true, ...tableOptions } = (typeof options === 'object' ? options : {});
|
|
12
|
-
if (tableOptions.id === undefined)
|
|
13
|
-
tableOptions.id = false;
|
|
14
|
-
if (tableOptions.unique === undefined)
|
|
15
|
-
tableOptions.unique = true;
|
|
16
|
-
const name = tableName || utils_1.join(...[tableOne, tableTwo].sort());
|
|
17
|
-
const col = { type: 'integer', null: false, ...columnOptions };
|
|
18
|
-
const firstColumnName = utils_1.join(tableOne, 'id');
|
|
19
|
-
const secondColumnName = utils_1.join(tableTwo, 'id');
|
|
20
|
-
const fn = (t) => {
|
|
21
|
-
let column = t.column(firstColumnName, col.type, col);
|
|
22
|
-
if (references)
|
|
23
|
-
column.references(tableOne, 'id');
|
|
24
|
-
column = t.column(secondColumnName, col.type, col);
|
|
25
|
-
if (references)
|
|
26
|
-
column.references(tableTwo, 'id');
|
|
27
|
-
if (tableOptions.unique)
|
|
28
|
-
t.index([firstColumnName, secondColumnName], {
|
|
29
|
-
name: `${name}_unique_index`,
|
|
30
|
-
unique: true,
|
|
31
|
-
});
|
|
32
|
-
if (cb)
|
|
33
|
-
cb(t);
|
|
34
|
-
};
|
|
35
|
-
return createTable(db, name, fn, tableOptions);
|
|
36
|
-
};
|
|
37
|
-
const dropJoinTable = (db, tableOne, tableTwo, options) => {
|
|
38
|
-
const tableName = typeof options === 'object' ? options.tableName : undefined;
|
|
39
|
-
dropTable(db, tableName || utils_1.join(...[tableOne, tableTwo].sort()));
|
|
40
|
-
};
|
|
41
|
-
class Migration extends pg_adapter_1.Adapter {
|
|
42
|
-
constructor({ reverse, ...params }) {
|
|
43
|
-
super({ ...params, pool: 1 });
|
|
44
|
-
this.reverse = reverse;
|
|
45
|
-
}
|
|
46
|
-
createTable(name, options, fn) {
|
|
47
|
-
if (this.reverse)
|
|
48
|
-
return dropTable(this, name);
|
|
49
|
-
if (typeof options === 'function') {
|
|
50
|
-
fn = options;
|
|
51
|
-
options = {};
|
|
52
|
-
}
|
|
53
|
-
return createTable(this, name, fn, options);
|
|
54
|
-
}
|
|
55
|
-
changeTable(name, options, fn) {
|
|
56
|
-
if (typeof options === 'function') {
|
|
57
|
-
fn = options;
|
|
58
|
-
options = {};
|
|
59
|
-
}
|
|
60
|
-
return new changeTable_1.ChangeTable(name, this.reverse, options).__commit(this, fn);
|
|
61
|
-
}
|
|
62
|
-
dropTable(name, options, fn) {
|
|
63
|
-
if (this.reverse) {
|
|
64
|
-
if (typeof options === 'function')
|
|
65
|
-
return new createTable_1.CreateTable(name, this.reverse).__commit(this, options);
|
|
66
|
-
else
|
|
67
|
-
return new createTable_1.CreateTable(name, this.reverse, options).__commit(this, fn);
|
|
68
|
-
}
|
|
69
|
-
return dropTable(this, name);
|
|
70
|
-
}
|
|
71
|
-
renameTable(from, to) {
|
|
72
|
-
if (this.reverse)
|
|
73
|
-
renameTable(this, to, from);
|
|
74
|
-
else
|
|
75
|
-
renameTable(this, from, to);
|
|
76
|
-
}
|
|
77
|
-
addColumn(table, name, type, options) {
|
|
78
|
-
this.changeTable(table, (t) => t.column(name, type, options));
|
|
79
|
-
}
|
|
80
|
-
addForeignKey(table, params) {
|
|
81
|
-
this.changeTable(table, (t) => t.foreignKey(params));
|
|
82
|
-
}
|
|
83
|
-
dropForeignKey(table, params) {
|
|
84
|
-
this.changeTable(table, (t) => t.dropForeignKey(params));
|
|
85
|
-
}
|
|
86
|
-
addPrimaryKey(table, columns, name) {
|
|
87
|
-
this.changeTable(table, (t) => t.primaryKey(columns, name));
|
|
88
|
-
}
|
|
89
|
-
dropPrimaryKey(table, columns, name) {
|
|
90
|
-
this.changeTable(table, (t) => t.dropPrimaryKey(columns, name));
|
|
91
|
-
}
|
|
92
|
-
addIndex(table, name, options) {
|
|
93
|
-
this.changeTable(table, (t) => t.index(name, options));
|
|
94
|
-
}
|
|
95
|
-
dropIndex(table, name, options) {
|
|
96
|
-
this.changeTable(table, (t) => t.dropIndex(name, options));
|
|
97
|
-
}
|
|
98
|
-
addTimestamps(table, options) {
|
|
99
|
-
this.changeTable(table, (t) => t.timestamps(options));
|
|
100
|
-
}
|
|
101
|
-
dropTimestamps(table, options) {
|
|
102
|
-
this.changeTable(table, (t) => t.dropTimestamps(options));
|
|
103
|
-
}
|
|
104
|
-
changeColumn(table, name, options) {
|
|
105
|
-
this.changeTable(table, (t) => t.change(name, options));
|
|
106
|
-
}
|
|
107
|
-
changeColumnComment(table, column, comment) {
|
|
108
|
-
this.changeTable(table, (t) => t.comment(column, comment));
|
|
109
|
-
}
|
|
110
|
-
changeColumnDefault(table, column, value) {
|
|
111
|
-
this.changeTable(table, (t) => t.default(column, value));
|
|
112
|
-
}
|
|
113
|
-
changeColumnNull(table, column, value) {
|
|
114
|
-
this.changeTable(table, (t) => t.null(column, value));
|
|
115
|
-
}
|
|
116
|
-
changeTableComment(table, comment) {
|
|
117
|
-
this.changeTable(table, { comment });
|
|
118
|
-
}
|
|
119
|
-
renameColumn(table, from, to) {
|
|
120
|
-
this.changeTable(table, (t) => t.rename(from, to));
|
|
121
|
-
}
|
|
122
|
-
dropColumn(table, name, type, options) {
|
|
123
|
-
this.changeTable(table, (t) => t.drop(name, type, options));
|
|
124
|
-
}
|
|
125
|
-
columnExists(table, column) {
|
|
126
|
-
const value = this.value('SELECT 1 FROM "information_schema"."columns" ' +
|
|
127
|
-
`WHERE "table_name" = '${table}' AND "column_name" = '${column}'`);
|
|
128
|
-
return this.reverse ? !value : value;
|
|
129
|
-
}
|
|
130
|
-
createJoinTable(tableOne, tableTwo, options, cb) {
|
|
131
|
-
if (this.reverse)
|
|
132
|
-
return dropJoinTable(this, tableOne, tableTwo, options);
|
|
133
|
-
createJoinTable(this, tableOne, tableTwo, options, cb);
|
|
134
|
-
}
|
|
135
|
-
dropJoinTable(tableOne, tableTwo, options, cb) {
|
|
136
|
-
if (this.reverse) {
|
|
137
|
-
this.reverse = false;
|
|
138
|
-
createJoinTable(this, tableOne, tableTwo, options, cb);
|
|
139
|
-
this.reverse = true;
|
|
140
|
-
}
|
|
141
|
-
dropJoinTable(this, tableOne, tableTwo, options);
|
|
142
|
-
}
|
|
143
|
-
foreignKeyExists(fromTable, options) {
|
|
144
|
-
let name;
|
|
145
|
-
if (typeof options === 'string')
|
|
146
|
-
name = utils_1.join(fromTable, options, 'id', 'fkey');
|
|
147
|
-
else
|
|
148
|
-
name = options.name || utils_1.join(fromTable, options.column, 'fkey');
|
|
149
|
-
const value = this.value('SELECT 1 FROM "information_schema"."table_constraints" ' +
|
|
150
|
-
`WHERE "constraint_name" = '${name}'`);
|
|
151
|
-
return this.reverse ? !value : value;
|
|
152
|
-
}
|
|
153
|
-
tableExists(table) {
|
|
154
|
-
const value = this.value('SELECT FROM "information_schema"."tables" ' +
|
|
155
|
-
`WHERE "table_name" = '${table}'`);
|
|
156
|
-
return this.reverse ? !value : value;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
exports.default = Migration;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import Table from './table';
|
|
2
|
-
import { Column } from './column';
|
|
3
|
-
import { Migration, ColumnOptions, IndexOptions, ForeignKeyFunction, ColumnFunction } from '../../types';
|
|
4
|
-
import { PrimaryKey } from './primaryKey';
|
|
5
|
-
export declare type ChangeTableCallback = (t: ChangeTable) => void;
|
|
6
|
-
export declare class ChangeTable extends Table {
|
|
7
|
-
constraint: (name: string, sql?: string | undefined) => void;
|
|
8
|
-
removeConstraint: (name: string, sql?: string | undefined) => void;
|
|
9
|
-
dropTimestamps: (options?: ColumnOptions | undefined) => void;
|
|
10
|
-
column: ColumnFunction;
|
|
11
|
-
change: (name: string, options?: ColumnOptions | undefined) => Column;
|
|
12
|
-
rename: (from: string, to: string) => void;
|
|
13
|
-
comment: (column: string, message: string) => void;
|
|
14
|
-
default: (column: string, value: unknown) => void;
|
|
15
|
-
null: (column: string, value: boolean) => void;
|
|
16
|
-
drop: (name: string, type: string, options?: ColumnOptions | undefined) => Column;
|
|
17
|
-
dropIndex: (name: string | string[], options?: true | IndexOptions) => number;
|
|
18
|
-
foreignKey: ForeignKeyFunction;
|
|
19
|
-
dropForeignKey: ForeignKeyFunction;
|
|
20
|
-
primaryKey: (columns: string[], name?: string | undefined) => PrimaryKey;
|
|
21
|
-
dropPrimaryKey: (columns: string[], name?: string | undefined) => PrimaryKey;
|
|
22
|
-
__commit: (db: Migration, fn?: ChangeTableCallback | undefined) => void;
|
|
23
|
-
}
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.ChangeTable = void 0;
|
|
7
|
-
const table_1 = __importDefault(require("./table"));
|
|
8
|
-
const column_1 = require("./column");
|
|
9
|
-
const index_1 = require("./index");
|
|
10
|
-
const utils_1 = require("../utils");
|
|
11
|
-
const foreignKey_1 = require("./foreignKey");
|
|
12
|
-
const primaryKey_1 = require("./primaryKey");
|
|
13
|
-
const addConstraint = (name, sql) => `ADD CONSTRAINT ${sql ? `"${name}" ${sql}` : name}`;
|
|
14
|
-
const removeConstraint = (name) => `DROP CONSTRAINT "${name}"`;
|
|
15
|
-
class ChangeTable extends table_1.default {
|
|
16
|
-
constructor() {
|
|
17
|
-
super(...arguments);
|
|
18
|
-
this.constraint = (name, sql) => {
|
|
19
|
-
this.execute(this.reverse ? removeConstraint(name) : addConstraint(name, sql));
|
|
20
|
-
};
|
|
21
|
-
this.removeConstraint = (name, sql) => this.execute(this.reverse ? addConstraint(name, sql) : removeConstraint(name));
|
|
22
|
-
this.dropTimestamps = (options) => {
|
|
23
|
-
this.reverse = !this.reverse;
|
|
24
|
-
this.timestamps(options);
|
|
25
|
-
this.reverse = !this.reverse;
|
|
26
|
-
};
|
|
27
|
-
this.column = (name, type, options = {}) => {
|
|
28
|
-
if (this.reverse) {
|
|
29
|
-
this.removedColumns.push(name);
|
|
30
|
-
const column = new column_1.Column('drop', name, options, type);
|
|
31
|
-
this.lines.push(column);
|
|
32
|
-
return column;
|
|
33
|
-
}
|
|
34
|
-
const column = new column_1.Column('add', name, options, type);
|
|
35
|
-
this.lines.push(column);
|
|
36
|
-
return column;
|
|
37
|
-
};
|
|
38
|
-
this.change = (name, options) => {
|
|
39
|
-
const column = new column_1.Column('alter', name, options);
|
|
40
|
-
this.lines.push(column);
|
|
41
|
-
return column;
|
|
42
|
-
};
|
|
43
|
-
this.rename = (from, to) => {
|
|
44
|
-
const f = this.reverse ? to : from;
|
|
45
|
-
const t = this.reverse ? from : to;
|
|
46
|
-
this.execute(`RENAME COLUMN "${f}" TO "${t}"`);
|
|
47
|
-
};
|
|
48
|
-
this.comment = (column, message) => {
|
|
49
|
-
this.comments.push([column, message]);
|
|
50
|
-
};
|
|
51
|
-
this.default = (column, value) => {
|
|
52
|
-
this.lines.push(new column_1.Column('alter', column, { default: value }));
|
|
53
|
-
};
|
|
54
|
-
this.null = (column, value) => {
|
|
55
|
-
this.lines.push(new column_1.Column('alter', column, { null: value }));
|
|
56
|
-
};
|
|
57
|
-
this.drop = (name, type, options) => {
|
|
58
|
-
const column = new column_1.Column(this.reverse ? 'add' : 'drop', name, options, type);
|
|
59
|
-
this.lines.push(column);
|
|
60
|
-
return column;
|
|
61
|
-
};
|
|
62
|
-
this.dropIndex = (name, options = {}) => this.indices.push([this.reverse, name, options]);
|
|
63
|
-
this.foreignKey = (options) => {
|
|
64
|
-
const fkey = new foreignKey_1.ForeignKey('changeTable', this, this.reverse, options);
|
|
65
|
-
this.lines.push(fkey);
|
|
66
|
-
return fkey;
|
|
67
|
-
};
|
|
68
|
-
this.dropForeignKey = (options) => {
|
|
69
|
-
const fkey = new foreignKey_1.ForeignKey('changeTable', this, !this.reverse, options);
|
|
70
|
-
this.lines.push(fkey);
|
|
71
|
-
return fkey;
|
|
72
|
-
};
|
|
73
|
-
this.primaryKey = (columns, name) => {
|
|
74
|
-
const pkey = new primaryKey_1.PrimaryKey('add', columns, name);
|
|
75
|
-
this.lines.push(pkey);
|
|
76
|
-
return pkey;
|
|
77
|
-
};
|
|
78
|
-
this.dropPrimaryKey = (columns, name) => {
|
|
79
|
-
const pkey = new primaryKey_1.PrimaryKey('drop', columns, name);
|
|
80
|
-
this.lines.push(pkey);
|
|
81
|
-
return pkey;
|
|
82
|
-
};
|
|
83
|
-
this.__commit = (db, fn) => {
|
|
84
|
-
this.reverse = db.reverse;
|
|
85
|
-
if (fn)
|
|
86
|
-
fn(this);
|
|
87
|
-
if (this.lines.length) {
|
|
88
|
-
const lines = [];
|
|
89
|
-
this.lines.forEach((line) => {
|
|
90
|
-
const item = typeof line === 'string' ? line : line.toSql(this);
|
|
91
|
-
if (!item)
|
|
92
|
-
return;
|
|
93
|
-
typeof item === 'string' ? lines.push(item) : lines.push(...item);
|
|
94
|
-
});
|
|
95
|
-
const sql = `ALTER TABLE "${this.tableName}"\n` + lines.join(',\n');
|
|
96
|
-
db.exec(sql).catch(utils_1.noop);
|
|
97
|
-
}
|
|
98
|
-
for (const args of this.indices) {
|
|
99
|
-
const [create, name, options] = args;
|
|
100
|
-
if (create)
|
|
101
|
-
db.exec(index_1.addIndex(this.tableName, name, options)).catch(utils_1.noop);
|
|
102
|
-
else
|
|
103
|
-
db.exec(index_1.dropIndex(this.tableName, name, options)).catch(utils_1.noop);
|
|
104
|
-
}
|
|
105
|
-
this.addComments(db);
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
exports.ChangeTable = ChangeTable;
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { ColumnOptions, IndexOptions, OnUpdateOrDeleteAction } from '../../types';
|
|
2
|
-
import { CreateTable } from './createTable';
|
|
3
|
-
import { ChangeTable } from './changeTable';
|
|
4
|
-
export declare class Column {
|
|
5
|
-
action: 'create' | 'add' | 'alter' | 'drop';
|
|
6
|
-
name: string;
|
|
7
|
-
options: ColumnOptions;
|
|
8
|
-
_type?: string | undefined;
|
|
9
|
-
constructor(action: 'create' | 'add' | 'alter' | 'drop', name: string, options?: ColumnOptions, _type?: string | undefined);
|
|
10
|
-
primaryKey(): this;
|
|
11
|
-
type(type: string): this;
|
|
12
|
-
default(value: any): this;
|
|
13
|
-
required(): this;
|
|
14
|
-
optional(): this;
|
|
15
|
-
index(options?: boolean | IndexOptions): this;
|
|
16
|
-
comment(comment: string): this;
|
|
17
|
-
mode(mode: string): this;
|
|
18
|
-
unique(): this;
|
|
19
|
-
length(length: number | string): this;
|
|
20
|
-
precision(precision: number | string): this;
|
|
21
|
-
scale(scale: number | string): this;
|
|
22
|
-
collate(collate: string): this;
|
|
23
|
-
using(using: string): this;
|
|
24
|
-
references(table: string, column?: string): this;
|
|
25
|
-
column(column: string): this;
|
|
26
|
-
onUpdate(action: OnUpdateOrDeleteAction): this;
|
|
27
|
-
onDelete(action: OnUpdateOrDeleteAction): this;
|
|
28
|
-
toSql(table: CreateTable | ChangeTable): string | string[];
|
|
29
|
-
private alterColumn;
|
|
30
|
-
private getTypeSql;
|
|
31
|
-
}
|