rake-db 2.1.3 → 2.1.5
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.local +1 -1
- package/CHANGELOG.md +16 -0
- package/db.ts +10 -6
- package/dist/index.d.ts +5 -2
- package/dist/index.esm.js +30 -2
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +30 -2
- package/dist/index.js.map +1 -1
- package/migrations/20221105202843_createUniqueTable.ts +1 -0
- package/package.json +11 -10
- package/src/common.ts +7 -1
- package/src/migration/createTable.test.ts +55 -11
- package/src/migration/createTable.ts +30 -2
- package/src/migration/migration.ts +8 -4
- package/src/test-utils.ts +3 -0
package/.env.local
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
DATABASE_URL=postgres://postgres:@localhost:5432/porm
|
|
2
|
-
DATABASE_URL_TEST=postgres://postgres:@localhost:5432/porm_test
|
|
2
|
+
#DATABASE_URL_TEST=postgres://postgres:@localhost:5432/porm_test
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# rake-db
|
|
2
2
|
|
|
3
|
+
## 2.1.5
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [9b8b3d5]
|
|
8
|
+
- pqb@0.7.2
|
|
9
|
+
|
|
10
|
+
## 2.1.4
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- Handle tables without primary key
|
|
15
|
+
- ecd7521: Support copy
|
|
16
|
+
- Updated dependencies
|
|
17
|
+
- pqb@0.7.1
|
|
18
|
+
|
|
3
19
|
## 2.1.3
|
|
4
20
|
|
|
5
21
|
### Patch Changes
|
package/db.ts
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
1
1
|
import { config } from 'dotenv';
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import { rakeDb } from './src/rakeDb';
|
|
4
|
+
import { AdapterOptions } from 'pqb';
|
|
4
5
|
|
|
5
6
|
config({ path: path.resolve(process.cwd(), '.env.local') });
|
|
6
7
|
config();
|
|
7
8
|
|
|
9
|
+
const options: AdapterOptions[] = [];
|
|
10
|
+
|
|
8
11
|
const connectionString = process.env.DATABASE_URL;
|
|
9
12
|
if (!connectionString) {
|
|
10
13
|
throw new Error('DATABASE_URL is missing in .env');
|
|
11
14
|
}
|
|
12
15
|
|
|
16
|
+
options.push({ connectionString });
|
|
17
|
+
|
|
13
18
|
const connectionStringTest = process.env.DATABASE_URL_TEST;
|
|
14
|
-
if (
|
|
15
|
-
|
|
19
|
+
if (connectionStringTest) {
|
|
20
|
+
options.push({ connectionString: connectionStringTest });
|
|
16
21
|
}
|
|
17
22
|
|
|
18
|
-
rakeDb(
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
);
|
|
23
|
+
rakeDb(options, {
|
|
24
|
+
migrationsPath: path.resolve(process.cwd(), 'migrations'),
|
|
25
|
+
});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import * as pqb from 'pqb';
|
|
2
|
-
import { QueryLogOptions, MaybeArray, AdapterOptions, ColumnTypes, EmptyObject, RawExpression, ColumnType, ForeignKey, SingleColumnIndexOptions, raw, ColumnsShape, IndexColumnOptions, IndexOptions, TransactionAdapter, QueryLogObject, QueryResultRow, QueryInput, TypeParsers, QueryResult, QueryArraysResult, ForeignKeyOptions } from 'pqb';
|
|
2
|
+
import { NoPrimaryKeyOption, QueryLogOptions, MaybeArray, AdapterOptions, ColumnTypes, EmptyObject, RawExpression, ColumnType, ForeignKey, SingleColumnIndexOptions, raw, ColumnsShape, IndexColumnOptions, IndexOptions, TransactionAdapter, QueryLogObject, QueryResultRow, QueryInput, TypeParsers, QueryResult, QueryArraysResult, ForeignKeyOptions } from 'pqb';
|
|
3
3
|
|
|
4
4
|
declare type MigrationConfig = {
|
|
5
5
|
migrationsPath: string;
|
|
6
6
|
migrationsTable: string;
|
|
7
7
|
requireTs(path: string): void;
|
|
8
|
+
noPrimaryKey?: NoPrimaryKeyOption;
|
|
8
9
|
} & QueryLogOptions;
|
|
9
10
|
|
|
10
11
|
declare const createDb: (arg: MaybeArray<AdapterOptions>, config: MigrationConfig) => Promise<void>;
|
|
@@ -54,6 +55,7 @@ declare type DropMode = 'CASCADE' | 'RESTRICT';
|
|
|
54
55
|
declare type TableOptions = {
|
|
55
56
|
dropMode?: DropMode;
|
|
56
57
|
comment?: string;
|
|
58
|
+
noPrimaryKey?: boolean;
|
|
57
59
|
};
|
|
58
60
|
declare type ColumnsShapeCallback = (t: ColumnTypes & {
|
|
59
61
|
raw: typeof raw;
|
|
@@ -82,8 +84,9 @@ declare type ExtensionOptions = {
|
|
|
82
84
|
};
|
|
83
85
|
declare class Migration extends TransactionAdapter {
|
|
84
86
|
up: boolean;
|
|
87
|
+
options: MigrationConfig;
|
|
85
88
|
log?: QueryLogObject;
|
|
86
|
-
constructor(tx: TransactionAdapter, up: boolean, options:
|
|
89
|
+
constructor(tx: TransactionAdapter, up: boolean, options: MigrationConfig);
|
|
87
90
|
query<T extends QueryResultRow = any>(query: QueryInput, types?: TypeParsers, log?: QueryLogObject | undefined): Promise<QueryResult<T>>;
|
|
88
91
|
arrays<R extends any[] = any[]>(query: QueryInput, types?: TypeParsers, log?: QueryLogObject | undefined): Promise<QueryArraysResult<R>>;
|
|
89
92
|
createTable(tableName: string, options: TableOptions, fn: ColumnsShapeCallback): Promise<void>;
|
package/dist/index.esm.js
CHANGED
|
@@ -423,7 +423,13 @@ class UnknownColumn extends ColumnType {
|
|
|
423
423
|
const createJoinTable = async (migration, up, tables, options, fn) => {
|
|
424
424
|
const tableName = options.tableName || joinWords(...tables);
|
|
425
425
|
if (!up) {
|
|
426
|
-
return createTable(
|
|
426
|
+
return createTable(
|
|
427
|
+
migration,
|
|
428
|
+
up,
|
|
429
|
+
tableName,
|
|
430
|
+
__spreadProps$1(__spreadValues$2({}, options), { noPrimaryKey: true }),
|
|
431
|
+
() => ({})
|
|
432
|
+
);
|
|
427
433
|
}
|
|
428
434
|
const tablesWithPrimaryKeys = await Promise.all(
|
|
429
435
|
tables.map(async (table) => {
|
|
@@ -473,7 +479,29 @@ const types = Object.assign(Object.create(columnTypes), {
|
|
|
473
479
|
raw
|
|
474
480
|
});
|
|
475
481
|
const createTable = async (migration, up, tableName, options, fn) => {
|
|
482
|
+
var _a, _b;
|
|
476
483
|
const shape = getColumnTypes(types, fn);
|
|
484
|
+
const tableData = getTableData();
|
|
485
|
+
const { noPrimaryKey } = migration.options;
|
|
486
|
+
if (!options.noPrimaryKey && (!noPrimaryKey || noPrimaryKey !== "ignore")) {
|
|
487
|
+
let hasPrimaryKey = !!((_b = (_a = tableData.primaryKey) == null ? void 0 : _a.columns) == null ? void 0 : _b.length);
|
|
488
|
+
if (!hasPrimaryKey) {
|
|
489
|
+
for (const key in shape) {
|
|
490
|
+
if (shape[key].isPrimaryKey) {
|
|
491
|
+
hasPrimaryKey = true;
|
|
492
|
+
break;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
if (!hasPrimaryKey) {
|
|
497
|
+
const message = `Table ${tableName} has no primary key`;
|
|
498
|
+
if (!noPrimaryKey || noPrimaryKey === "error") {
|
|
499
|
+
throw new Error(message);
|
|
500
|
+
} else {
|
|
501
|
+
console.warn(message);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
}
|
|
477
505
|
if (!up) {
|
|
478
506
|
const { dropMode } = options;
|
|
479
507
|
await migration.query(
|
|
@@ -496,7 +524,6 @@ const createTable = async (migration, up, tableName, options, fn) => {
|
|
|
496
524
|
lines.push(`
|
|
497
525
|
${columnToSql(key, item, state)}`);
|
|
498
526
|
}
|
|
499
|
-
const tableData = getTableData();
|
|
500
527
|
if (tableData.primaryKey) {
|
|
501
528
|
lines.push(`
|
|
502
529
|
${primaryKeyToSql(tableData.primaryKey)}`);
|
|
@@ -852,6 +879,7 @@ class Migration extends TransactionAdapter {
|
|
|
852
879
|
constructor(tx, up, options) {
|
|
853
880
|
super(tx.pool, tx.client, tx.types);
|
|
854
881
|
this.up = up;
|
|
882
|
+
this.options = options;
|
|
855
883
|
this.log = logParamToLogObject(options.logger || console, options.log);
|
|
856
884
|
}
|
|
857
885
|
async query(query, types = this.types, log = this.log) {
|