@coffer-org/server 1.13.0 → 1.14.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/dist/migrations.js +49 -2
- package/dist/plugin-hooks.d.ts +2 -1
- package/package.json +2 -2
package/dist/migrations.js
CHANGED
|
@@ -33,7 +33,9 @@ export async function introspectTable(em, table) {
|
|
|
33
33
|
return {
|
|
34
34
|
exists: !!t,
|
|
35
35
|
columns: new Set(t ? t.getColumns().map((c) => c.name) : []),
|
|
36
|
-
indexes: t
|
|
36
|
+
indexes: t
|
|
37
|
+
? t.getIndexes().map((i) => ({ columnNames: i.columnNames ?? [], unique: !!i.unique, primary: !!i.primary }))
|
|
38
|
+
: [],
|
|
37
39
|
};
|
|
38
40
|
}
|
|
39
41
|
export function makeTable(em, table) {
|
|
@@ -87,7 +89,9 @@ export function makeTable(em, table) {
|
|
|
87
89
|
const info = (await conn.execute(`PRAGMA index_info(${q(idx.name)})`));
|
|
88
90
|
if (!info.some((c) => c.name === column))
|
|
89
91
|
continue;
|
|
90
|
-
const meta = (await conn.execute(`SELECT sql FROM sqlite_master WHERE type='index' AND name=?`, [
|
|
92
|
+
const meta = (await conn.execute(`SELECT sql FROM sqlite_master WHERE type='index' AND name=?`, [
|
|
93
|
+
idx.name,
|
|
94
|
+
]));
|
|
91
95
|
const sql = meta[0]?.sql;
|
|
92
96
|
if (sql == null)
|
|
93
97
|
continue;
|
|
@@ -132,6 +136,49 @@ export function makeTable(em, table) {
|
|
|
132
136
|
throw e;
|
|
133
137
|
}
|
|
134
138
|
},
|
|
139
|
+
async convert(c) {
|
|
140
|
+
const info = await introspectTable(em, table);
|
|
141
|
+
if (!info.exists)
|
|
142
|
+
return;
|
|
143
|
+
if (!c.from.some((f) => info.columns.has(f)))
|
|
144
|
+
return;
|
|
145
|
+
const sources = new Set(c.from);
|
|
146
|
+
for (const t of c.to) {
|
|
147
|
+
if (info.columns.has(t.name) && !sources.has(t.name)) {
|
|
148
|
+
throw new Error(`[migrations] ${table}.${t.name}: цільова колонка convert вже існує і не є джерелом — використай changeType/renameColumn`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const sqlite = dialectOf(em) === 'sqlite';
|
|
152
|
+
const idCol = sqlite ? 'rowid' : 'id';
|
|
153
|
+
const colType = sqlite ? sqlType : pgType;
|
|
154
|
+
const present = c.from.filter((f) => info.columns.has(f));
|
|
155
|
+
await conn.execute(`BEGIN`);
|
|
156
|
+
try {
|
|
157
|
+
for (const t of c.to) {
|
|
158
|
+
if (!info.columns.has(t.name))
|
|
159
|
+
await conn.execute(`ALTER TABLE ${T} ADD COLUMN ${q(t.name)} ${colType(t.type)}`);
|
|
160
|
+
}
|
|
161
|
+
const select = present.map((f) => q(f)).join(', ');
|
|
162
|
+
const rows = (await conn.execute(`SELECT ${idCol} AS __rid, ${select} FROM ${T}`));
|
|
163
|
+
const setSql = c.to.map((t) => `${q(t.name)} = ?`).join(', ');
|
|
164
|
+
for (const r of rows) {
|
|
165
|
+
const src = {};
|
|
166
|
+
for (const f of c.from)
|
|
167
|
+
src[f] = r[f] ?? null;
|
|
168
|
+
const dst = c.map(src);
|
|
169
|
+
const params = c.to.map((t) => {
|
|
170
|
+
const v = dst[t.name];
|
|
171
|
+
return v === undefined || (typeof v === 'number' && Number.isNaN(v)) ? null : v;
|
|
172
|
+
});
|
|
173
|
+
await conn.execute(`UPDATE ${T} SET ${setSql} WHERE ${idCol} = ?`, [...params, r['__rid']]);
|
|
174
|
+
}
|
|
175
|
+
await conn.execute(`COMMIT`);
|
|
176
|
+
}
|
|
177
|
+
catch (e) {
|
|
178
|
+
await conn.execute(`ROLLBACK`);
|
|
179
|
+
throw e;
|
|
180
|
+
}
|
|
181
|
+
},
|
|
135
182
|
};
|
|
136
183
|
}
|
|
137
184
|
export function validateMigrations(pluginId, list) {
|
package/dist/plugin-hooks.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { EntityManager } from '@mikro-orm/core';
|
|
2
2
|
import type { z } from 'zod';
|
|
3
|
-
import type { ColumnType } from '@coffer-org/sdk/fields';
|
|
3
|
+
import type { ColumnType, ColumnConversion } from '@coffer-org/sdk/fields';
|
|
4
4
|
import { type Logger } from '@coffer-org/sdk/logger';
|
|
5
5
|
export type { BackgroundTask } from './background-scheduler.ts';
|
|
6
6
|
export interface TableOps {
|
|
@@ -12,6 +12,7 @@ export interface TableOps {
|
|
|
12
12
|
default?: string | number | boolean | null;
|
|
13
13
|
transform?: (old: unknown) => unknown;
|
|
14
14
|
}): Promise<void>;
|
|
15
|
+
convert(c: ColumnConversion): Promise<void>;
|
|
15
16
|
}
|
|
16
17
|
export interface MigrationCtx {
|
|
17
18
|
table(name: string): TableOps;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@coffer-org/server",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.14.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=24"
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@coffer-org/core": "^1.4.0",
|
|
28
|
-
"@coffer-org/sdk": "^1.
|
|
28
|
+
"@coffer-org/sdk": "^1.8.0",
|
|
29
29
|
"@extractus/oembed-extractor": "^4.1.0",
|
|
30
30
|
"@fastify/cors": "^11.2.0",
|
|
31
31
|
"@fastify/multipart": "^10.0.0",
|