rake-db 2.0.1 → 2.0.2
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 +1 -3
- package/.env.local +1 -1
- package/dist/index.d.ts +26 -4
- package/dist/index.esm.js +201 -196
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +202 -194
- package/dist/index.js.map +1 -1
- package/migrations/20221017181504_createUser.ts +14 -0
- package/migrations/20221017200111_createProfile.ts +10 -0
- package/migrations/20221017200252_createChat.ts +9 -0
- package/migrations/20221017200326_createChatUser.ts +7 -0
- package/migrations/20221017200900_createMessage.ts +12 -0
- package/migrations/20221017201235_createGeoSchema.ts +5 -0
- package/migrations/{20221009210157_first.ts → 20221017210011_createCountry.ts} +2 -2
- package/migrations/20221017210133_createCity.ts +9 -0
- package/package.json +1 -1
- package/src/commands/generate.test.ts +6 -6
- package/src/commands/generate.ts +3 -3
- package/src/commands/migrateOrRollback.test.ts +20 -9
- package/src/commands/migrateOrRollback.ts +18 -6
- package/src/common.test.ts +13 -0
- package/src/common.ts +16 -3
- package/src/index.ts +2 -1
- package/src/migration/changeTable.ts +4 -2
- package/src/migration/createTable.ts +21 -16
- package/src/migration/migration.test.ts +93 -0
- package/src/migration/migration.ts +137 -2
- package/src/migration/migrationUtils.ts +17 -10
- package/src/rakeDb.ts +2 -2
- package/src/test-utils.ts +3 -1
- package/tsconfig.json +1 -1
- package/migrations/20221009210200_second.ts +0 -5
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import {
|
|
2
|
-
Adapter,
|
|
3
2
|
ColumnType,
|
|
4
3
|
ForeignKeyModel,
|
|
5
4
|
ForeignKeyOptions,
|
|
@@ -10,7 +9,7 @@ import {
|
|
|
10
9
|
toArray,
|
|
11
10
|
} from 'pqb';
|
|
12
11
|
import { ColumnComment, ColumnIndex, Migration } from './migration';
|
|
13
|
-
import { joinColumns, joinWords } from '../common';
|
|
12
|
+
import { joinColumns, joinWords, quoteTable } from '../common';
|
|
14
13
|
|
|
15
14
|
export const columnToSql = (
|
|
16
15
|
key: string,
|
|
@@ -121,7 +120,9 @@ export const referencesToSql = (
|
|
|
121
120
|
columns: string[],
|
|
122
121
|
foreignKey: Pick<ForeignKeyOptions, 'match' | 'onDelete' | 'onUpdate'>,
|
|
123
122
|
) => {
|
|
124
|
-
const sql: string[] = [
|
|
123
|
+
const sql: string[] = [
|
|
124
|
+
`REFERENCES ${quoteTable(table)}(${joinColumns(columns)})`,
|
|
125
|
+
];
|
|
125
126
|
|
|
126
127
|
if (foreignKey.match) {
|
|
127
128
|
sql.push(`MATCH ${foreignKey.match.toUpperCase()}`);
|
|
@@ -176,7 +177,7 @@ export const migrateIndex = (
|
|
|
176
177
|
sql.push('UNIQUE');
|
|
177
178
|
}
|
|
178
179
|
|
|
179
|
-
sql.push(`INDEX "${indexName}" ON
|
|
180
|
+
sql.push(`INDEX "${indexName}" ON ${quoteTable(state.tableName)}`);
|
|
180
181
|
|
|
181
182
|
if (options.using) {
|
|
182
183
|
sql.push(`USING ${options.using}`);
|
|
@@ -243,7 +244,9 @@ export const migrateComments = async (
|
|
|
243
244
|
) => {
|
|
244
245
|
for (const { column, comment } of comments) {
|
|
245
246
|
await state.migration.query(
|
|
246
|
-
`COMMENT ON COLUMN
|
|
247
|
+
`COMMENT ON COLUMN ${quoteTable(state.tableName)}."${column}" IS ${quote(
|
|
248
|
+
comment,
|
|
249
|
+
)}`,
|
|
247
250
|
);
|
|
248
251
|
}
|
|
249
252
|
};
|
|
@@ -258,11 +261,12 @@ export const primaryKeyToSql = (
|
|
|
258
261
|
};
|
|
259
262
|
|
|
260
263
|
export const getPrimaryKeysOfTable = async (
|
|
261
|
-
db:
|
|
264
|
+
db: Migration,
|
|
262
265
|
tableName: string,
|
|
263
266
|
): Promise<{ name: string; type: string }[]> => {
|
|
264
|
-
const { rows } = await db.query<{ name: string; type: string }>(
|
|
265
|
-
|
|
267
|
+
const { rows } = await db.query<{ name: string; type: string }>(
|
|
268
|
+
{
|
|
269
|
+
text: `SELECT
|
|
266
270
|
pg_attribute.attname AS name,
|
|
267
271
|
format_type(pg_attribute.atttypid, pg_attribute.atttypmod) AS type
|
|
268
272
|
FROM pg_index, pg_class, pg_attribute, pg_namespace
|
|
@@ -274,8 +278,11 @@ WHERE
|
|
|
274
278
|
pg_attribute.attrelid = pg_class.oid AND
|
|
275
279
|
pg_attribute.attnum = any(pg_index.indkey) AND
|
|
276
280
|
indisprimary`,
|
|
277
|
-
|
|
278
|
-
|
|
281
|
+
values: [tableName],
|
|
282
|
+
},
|
|
283
|
+
db.types,
|
|
284
|
+
undefined,
|
|
285
|
+
);
|
|
279
286
|
|
|
280
287
|
return rows;
|
|
281
288
|
};
|
package/src/rakeDb.ts
CHANGED
|
@@ -18,9 +18,9 @@ export const rakeDb = async (
|
|
|
18
18
|
} else if (command === 'drop') {
|
|
19
19
|
await dropDb(options);
|
|
20
20
|
} else if (command === 'migrate') {
|
|
21
|
-
await migrate(options, config);
|
|
21
|
+
await migrate(options, config, args.slice(1));
|
|
22
22
|
} else if (command === 'rollback') {
|
|
23
|
-
await rollback(options, config);
|
|
23
|
+
await rollback(options, config, args.slice(1));
|
|
24
24
|
} else if (command === 'g' || command === 'generate') {
|
|
25
25
|
await generate(config, args.slice(1));
|
|
26
26
|
} else {
|
package/src/test-utils.ts
CHANGED
|
@@ -6,7 +6,9 @@ let db: Migration | undefined;
|
|
|
6
6
|
export const getDb = () => {
|
|
7
7
|
if (db) return db;
|
|
8
8
|
|
|
9
|
-
db = new Migration({} as unknown as TransactionAdapter, true
|
|
9
|
+
db = new Migration({} as unknown as TransactionAdapter, true, {
|
|
10
|
+
log: false,
|
|
11
|
+
});
|
|
10
12
|
db.query = queryMock;
|
|
11
13
|
return db;
|
|
12
14
|
};
|
package/tsconfig.json
CHANGED