rake-db 2.2.6 → 2.3.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/CHANGELOG.md +13 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.esm.js +82 -64
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +82 -64
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/ast.ts +3 -3
- package/src/migration/changeTable.test.ts +10 -15
- package/src/migration/changeTable.ts +90 -77
- package/src/migration/createTable.test.ts +2 -3
- package/src/migration/migrationUtils.ts +28 -22
- package/src/pull/dbStructure.test.ts +14 -6
- package/src/pull/dbStructure.ts +167 -47
- package/src/pull/structureToAst.test.ts +203 -35
- package/src/pull/structureToAst.ts +69 -18
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
import { DbStructure } from './dbStructure';
|
|
2
2
|
import { RakeDbAst } from '../ast';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
columnsByType,
|
|
5
|
+
ColumnsShape,
|
|
6
|
+
ForeignKeyOptions,
|
|
7
|
+
instantiateColumn,
|
|
8
|
+
} from 'pqb';
|
|
9
|
+
|
|
10
|
+
const matchMap = {
|
|
11
|
+
s: undefined,
|
|
12
|
+
f: 'FULL',
|
|
13
|
+
p: 'PARTIAL',
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const fkeyActionMap = {
|
|
17
|
+
a: 'NO ACTION',
|
|
18
|
+
r: undefined, // default
|
|
19
|
+
c: 'CASCADE',
|
|
20
|
+
n: 'SET NULL',
|
|
21
|
+
d: 'SET DEFAULT',
|
|
22
|
+
};
|
|
4
23
|
|
|
5
24
|
export const structureToAst = async (db: DbStructure): Promise<RakeDbAst[]> => {
|
|
6
25
|
const ast: RakeDbAst[] = [];
|
|
@@ -9,7 +28,7 @@ export const structureToAst = async (db: DbStructure): Promise<RakeDbAst[]> => {
|
|
|
9
28
|
schemas,
|
|
10
29
|
tables,
|
|
11
30
|
allColumns,
|
|
12
|
-
|
|
31
|
+
allPrimaryKeys,
|
|
13
32
|
allIndexes,
|
|
14
33
|
allForeignKeys,
|
|
15
34
|
extensions,
|
|
@@ -17,7 +36,7 @@ export const structureToAst = async (db: DbStructure): Promise<RakeDbAst[]> => {
|
|
|
17
36
|
db.getSchemas(),
|
|
18
37
|
db.getTables(),
|
|
19
38
|
db.getColumns(),
|
|
20
|
-
db.
|
|
39
|
+
db.getPrimaryKeys(),
|
|
21
40
|
db.getIndexes(),
|
|
22
41
|
db.getForeignKeys(),
|
|
23
42
|
db.getExtensions(),
|
|
@@ -39,16 +58,16 @@ export const structureToAst = async (db: DbStructure): Promise<RakeDbAst[]> => {
|
|
|
39
58
|
const belongsToTable = makeBelongsToTable(schemaName, name);
|
|
40
59
|
|
|
41
60
|
const columns = allColumns.filter(belongsToTable);
|
|
42
|
-
const
|
|
43
|
-
const primaryKey = tableConstraints.find(
|
|
44
|
-
(item) => item.type === 'PRIMARY KEY',
|
|
45
|
-
);
|
|
61
|
+
const primaryKey = allPrimaryKeys.find(belongsToTable);
|
|
46
62
|
const tableIndexes = allIndexes.filter(belongsToTable);
|
|
47
63
|
const tableForeignKeys = allForeignKeys.filter(belongsToTable);
|
|
48
64
|
|
|
49
65
|
const shape: ColumnsShape = {};
|
|
50
66
|
for (const item of columns) {
|
|
51
67
|
const klass = columnsByType[item.type];
|
|
68
|
+
if (!klass) {
|
|
69
|
+
throw new Error(`Column type \`${item.type}\` is not supported`);
|
|
70
|
+
}
|
|
52
71
|
let column = instantiateColumn(klass, item);
|
|
53
72
|
|
|
54
73
|
if (
|
|
@@ -58,29 +77,41 @@ export const structureToAst = async (db: DbStructure): Promise<RakeDbAst[]> => {
|
|
|
58
77
|
column = column.primaryKey();
|
|
59
78
|
}
|
|
60
79
|
|
|
61
|
-
const
|
|
80
|
+
const indexes = tableIndexes.filter(
|
|
62
81
|
(it) =>
|
|
63
|
-
it.
|
|
64
|
-
it.
|
|
65
|
-
it.
|
|
82
|
+
it.columns.length === 1 &&
|
|
83
|
+
'column' in it.columns[0] &&
|
|
84
|
+
it.columns[0].column === item.name,
|
|
66
85
|
);
|
|
67
|
-
|
|
86
|
+
for (const index of indexes) {
|
|
87
|
+
const options = index.columns[0];
|
|
68
88
|
column = column.index({
|
|
89
|
+
collate: options.collate,
|
|
90
|
+
opclass: options.opclass,
|
|
91
|
+
order: options.order,
|
|
69
92
|
name: index.name,
|
|
93
|
+
using: index.using === 'btree' ? undefined : index.using,
|
|
70
94
|
unique: index.isUnique,
|
|
95
|
+
include: index.include,
|
|
96
|
+
with: index.with,
|
|
97
|
+
tablespace: index.tablespace,
|
|
98
|
+
where: index.where,
|
|
71
99
|
});
|
|
72
100
|
}
|
|
73
101
|
|
|
74
|
-
const
|
|
102
|
+
const foreignKeys = tableForeignKeys.filter(
|
|
75
103
|
(it) => it.columnNames.length === 1 && it.columnNames[0] === item.name,
|
|
76
104
|
);
|
|
77
|
-
|
|
105
|
+
for (const foreignKey of foreignKeys) {
|
|
78
106
|
column = column.foreignKey(
|
|
79
107
|
foreignKey.foreignTableName,
|
|
80
108
|
foreignKey.foreignColumnNames[0],
|
|
81
109
|
{
|
|
82
110
|
name: foreignKey.name,
|
|
83
|
-
|
|
111
|
+
match: matchMap[foreignKey.match],
|
|
112
|
+
onUpdate: fkeyActionMap[foreignKey.onUpdate],
|
|
113
|
+
onDelete: fkeyActionMap[foreignKey.onDelete],
|
|
114
|
+
} as ForeignKeyOptions,
|
|
84
115
|
);
|
|
85
116
|
}
|
|
86
117
|
|
|
@@ -91,6 +122,7 @@ export const structureToAst = async (db: DbStructure): Promise<RakeDbAst[]> => {
|
|
|
91
122
|
type: 'table',
|
|
92
123
|
action: 'create',
|
|
93
124
|
schema: schemaName === 'public' ? undefined : schemaName,
|
|
125
|
+
comment: table.comment,
|
|
94
126
|
name: name,
|
|
95
127
|
shape,
|
|
96
128
|
noPrimaryKey: primaryKey ? 'error' : 'ignore',
|
|
@@ -105,12 +137,28 @@ export const structureToAst = async (db: DbStructure): Promise<RakeDbAst[]> => {
|
|
|
105
137
|
}
|
|
106
138
|
: undefined,
|
|
107
139
|
indexes: tableIndexes
|
|
108
|
-
.filter(
|
|
140
|
+
.filter(
|
|
141
|
+
(index) =>
|
|
142
|
+
index.columns.length > 1 ||
|
|
143
|
+
index.columns.some((it) => 'expression' in it),
|
|
144
|
+
)
|
|
109
145
|
.map((index) => ({
|
|
110
|
-
columns: index.
|
|
146
|
+
columns: index.columns.map((it) => ({
|
|
147
|
+
...('column' in it
|
|
148
|
+
? { column: it.column }
|
|
149
|
+
: { expression: it.expression }),
|
|
150
|
+
collate: it.collate,
|
|
151
|
+
opclass: it.opclass,
|
|
152
|
+
order: it.order,
|
|
153
|
+
})),
|
|
111
154
|
options: {
|
|
112
155
|
name: index.name,
|
|
156
|
+
using: index.using === 'btree' ? undefined : index.using,
|
|
113
157
|
unique: index.isUnique,
|
|
158
|
+
include: index.include,
|
|
159
|
+
with: index.with,
|
|
160
|
+
tablespace: index.tablespace,
|
|
161
|
+
where: index.where,
|
|
114
162
|
},
|
|
115
163
|
})),
|
|
116
164
|
foreignKeys: tableForeignKeys
|
|
@@ -121,7 +169,10 @@ export const structureToAst = async (db: DbStructure): Promise<RakeDbAst[]> => {
|
|
|
121
169
|
foreignColumns: it.foreignColumnNames,
|
|
122
170
|
options: {
|
|
123
171
|
name: it.name,
|
|
124
|
-
|
|
172
|
+
match: matchMap[it.match],
|
|
173
|
+
onUpdate: fkeyActionMap[it.onUpdate],
|
|
174
|
+
onDelete: fkeyActionMap[it.onDelete],
|
|
175
|
+
} as ForeignKeyOptions,
|
|
125
176
|
})),
|
|
126
177
|
});
|
|
127
178
|
}
|