rake-db 2.3.3 → 2.3.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.
@@ -1,107 +0,0 @@
1
- import { ColumnType, Operators } from 'pqb';
2
- import { ColumnsShapeCallback, JoinTableOptions, Migration } from './migration';
3
- import {
4
- getSchemaAndTableFromName,
5
- joinWords,
6
- quoteWithSchema,
7
- } from '../common';
8
- import { getPrimaryKeysOfTable } from './migrationUtils';
9
- import { singular } from 'pluralize';
10
- import { createTable } from './createTable';
11
-
12
- class UnknownColumn extends ColumnType {
13
- operators = Operators.any;
14
-
15
- constructor(public dataType: string) {
16
- super();
17
- }
18
-
19
- toCode() {
20
- return 'unknown';
21
- }
22
- }
23
-
24
- export const createJoinTable = async (
25
- migration: Migration,
26
- up: boolean,
27
- tables: string[],
28
- options: JoinTableOptions,
29
- fn?: ColumnsShapeCallback,
30
- ) => {
31
- const tableName = options.tableName || joinWords(...tables);
32
-
33
- if (!up) {
34
- return createTable(
35
- migration,
36
- up,
37
- tableName,
38
- { ...options, noPrimaryKey: true },
39
- () => ({}),
40
- );
41
- }
42
-
43
- const tablesWithPrimaryKeys = await Promise.all(
44
- tables.map(async (table) => {
45
- const primaryKeys = await getPrimaryKeysOfTable(migration, table).then(
46
- (items) =>
47
- items.map((item) => ({
48
- ...item,
49
- joinedName: joinWords(singular(table), item.name),
50
- })),
51
- );
52
-
53
- const [schema, name] = getSchemaAndTableFromName(table);
54
- if (!primaryKeys.length) {
55
- throw new Error(
56
- `Primary key for table ${quoteWithSchema({
57
- schema,
58
- name,
59
- })} is not defined`,
60
- );
61
- }
62
-
63
- return [schema, table, primaryKeys] as const;
64
- }),
65
- );
66
-
67
- return createTable(migration, up, tableName, options, (t) => {
68
- const result: Record<string, ColumnType> = {};
69
-
70
- tablesWithPrimaryKeys.forEach(([schema, table, primaryKeys]) => {
71
- if (primaryKeys.length === 1) {
72
- const [{ type, joinedName, name }] = primaryKeys;
73
-
74
- const column = new UnknownColumn(type);
75
-
76
- result[joinedName] = column.foreignKey(
77
- schema ? `${schema}.${table}` : table,
78
- name,
79
- );
80
-
81
- return;
82
- }
83
-
84
- primaryKeys.forEach(({ joinedName, type }) => {
85
- result[joinedName] = new UnknownColumn(type);
86
- });
87
-
88
- t.foreignKey(
89
- primaryKeys.map((key) => key.joinedName) as [string, ...string[]],
90
- table,
91
- primaryKeys.map((key) => key.name) as [string, ...string[]],
92
- );
93
- });
94
-
95
- if (fn) {
96
- Object.assign(result, fn(t));
97
- }
98
-
99
- t.primaryKey(
100
- tablesWithPrimaryKeys.flatMap(([, , primaryKeys]) =>
101
- primaryKeys.map((item) => item.joinedName),
102
- ),
103
- );
104
-
105
- return result;
106
- });
107
- };