1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import { getTableChangeQueries } from '../../dist/core/cli/migration-queries.js';
import { tablesSchema } from '../../dist/core/schemas.js';
import { column, defineTable } from '../../dist/runtime/virtual.js';
const BaseUser = defineTable({
columns: {
id: column.number({ primaryKey: true }),
name: column.text(),
age: column.number(),
email: column.text({ unique: true }),
mi: column.text({ optional: true }),
},
});
const BaseSentBox = defineTable({
columns: {
to: column.number(),
toName: column.text(),
subject: column.text(),
body: column.text(),
},
});
/**
* @typedef {import('../../dist/core/types.js').DBTable} DBTable
* @param {{ User: DBTable, SentBox: DBTable }} params
* @returns
*/
function resolveReferences(
{ User = BaseUser, SentBox = BaseSentBox } = {
User: BaseUser,
SentBox: BaseSentBox,
}
) {
return tablesSchema.parse({ User, SentBox });
}
function userChangeQueries(oldTable, newTable) {
return getTableChangeQueries({
tableName: 'User',
oldTable,
newTable,
});
}
describe('reference queries', () => {
it('adds references with lossless table recreate', async () => {
const { SentBox: Initial } = resolveReferences();
const { SentBox: Final } = resolveReferences({
SentBox: defineTable({
columns: {
...BaseSentBox.columns,
to: column.number({ references: () => BaseUser.columns.id }),
},
}),
});
const { queries } = await userChangeQueries(Initial, Final);
assert.equal(queries[0] !== undefined, true);
const tempTableName = getTempTableName(queries[0]);
assert.notEqual(typeof tempTableName, 'undefined');
assert.deepEqual(queries, [
`CREATE TABLE \"${tempTableName}\" (_id INTEGER PRIMARY KEY, \"to\" integer NOT NULL REFERENCES \"User\" (\"id\"), \"toName\" text NOT NULL, \"subject\" text NOT NULL, \"body\" text NOT NULL)`,
`INSERT INTO \"${tempTableName}\" (\"_id\", \"to\", \"toName\", \"subject\", \"body\") SELECT \"_id\", \"to\", \"toName\", \"subject\", \"body\" FROM \"User\"`,
'DROP TABLE "User"',
`ALTER TABLE \"${tempTableName}\" RENAME TO \"User\"`,
]);
});
it('removes references with lossless table recreate', async () => {
const { SentBox: Initial } = resolveReferences({
SentBox: defineTable({
columns: {
...BaseSentBox.columns,
to: column.number({ references: () => BaseUser.columns.id }),
},
}),
});
const { SentBox: Final } = resolveReferences();
const { queries } = await userChangeQueries(Initial, Final);
assert.equal(queries[0] !== undefined, true);
const tempTableName = getTempTableName(queries[0]);
assert.notEqual(typeof tempTableName, 'undefined');
assert.deepEqual(queries, [
`CREATE TABLE \"${tempTableName}\" (_id INTEGER PRIMARY KEY, \"to\" integer NOT NULL, \"toName\" text NOT NULL, \"subject\" text NOT NULL, \"body\" text NOT NULL)`,
`INSERT INTO \"${tempTableName}\" (\"_id\", \"to\", \"toName\", \"subject\", \"body\") SELECT \"_id\", \"to\", \"toName\", \"subject\", \"body\" FROM \"User\"`,
'DROP TABLE "User"',
`ALTER TABLE \"${tempTableName}\" RENAME TO \"User\"`,
]);
});
it('does not use ADD COLUMN when adding optional column with reference', async () => {
const { SentBox: Initial } = resolveReferences();
const { SentBox: Final } = resolveReferences({
SentBox: defineTable({
columns: {
...BaseSentBox.columns,
from: column.number({ references: () => BaseUser.columns.id, optional: true }),
},
}),
});
const { queries } = await userChangeQueries(Initial, Final);
assert.equal(queries[0] !== undefined, true);
const tempTableName = getTempTableName(queries[0]);
assert.deepEqual(queries, [
`CREATE TABLE \"${tempTableName}\" (_id INTEGER PRIMARY KEY, \"to\" integer NOT NULL, \"toName\" text NOT NULL, \"subject\" text NOT NULL, \"body\" text NOT NULL, \"from\" integer REFERENCES \"User\" (\"id\"))`,
`INSERT INTO \"${tempTableName}\" (\"_id\", \"to\", \"toName\", \"subject\", \"body\") SELECT \"_id\", \"to\", \"toName\", \"subject\", \"body\" FROM \"User\"`,
'DROP TABLE "User"',
`ALTER TABLE \"${tempTableName}\" RENAME TO \"User\"`,
]);
});
it('adds and updates foreign key with lossless table recreate', async () => {
const { SentBox: InitialWithoutFK } = resolveReferences();
const { SentBox: InitialWithDifferentFK } = resolveReferences({
SentBox: defineTable({
...BaseSentBox,
foreignKeys: [{ columns: ['to'], references: () => [BaseUser.columns.id] }],
}),
});
const { SentBox: Final } = resolveReferences({
SentBox: defineTable({
...BaseSentBox,
foreignKeys: [
{
columns: ['to', 'toName'],
references: () => [BaseUser.columns.id, BaseUser.columns.name],
},
],
}),
});
const expected = (tempTableName) => [
`CREATE TABLE \"${tempTableName}\" (_id INTEGER PRIMARY KEY, \"to\" integer NOT NULL, \"toName\" text NOT NULL, \"subject\" text NOT NULL, \"body\" text NOT NULL, FOREIGN KEY (\"to\", \"toName\") REFERENCES \"User\"(\"id\", \"name\"))`,
`INSERT INTO \"${tempTableName}\" (\"_id\", \"to\", \"toName\", \"subject\", \"body\") SELECT \"_id\", \"to\", \"toName\", \"subject\", \"body\" FROM \"User\"`,
'DROP TABLE "User"',
`ALTER TABLE \"${tempTableName}\" RENAME TO \"User\"`,
];
const addedForeignKey = await userChangeQueries(InitialWithoutFK, Final);
const updatedForeignKey = await userChangeQueries(InitialWithDifferentFK, Final);
assert.notEqual(typeof addedForeignKey.queries[0], 'undefined');
assert.notEqual(typeof updatedForeignKey.queries[0], 'undefined');
assert.deepEqual(
addedForeignKey.queries,
expected(getTempTableName(addedForeignKey.queries[0]))
);
assert.deepEqual(
updatedForeignKey.queries,
expected(getTempTableName(updatedForeignKey.queries[0]))
);
});
});
/** @param {string | undefined} query */
function getTempTableName(query) {
// eslint-disable-next-line regexp/no-unused-capturing-group
return query.match(/User_([a-z\d]+)/)?.[0];
}
|