dbdock 1.1.16 → 1.1.19
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/README.md +236 -11
- package/dist/cli/commands/analyze.d.ts +1 -0
- package/dist/cli/commands/analyze.js +135 -0
- package/dist/cli/commands/analyze.js.map +1 -0
- package/dist/cli/commands/cross-migrate.d.ts +11 -0
- package/dist/cli/commands/cross-migrate.js +307 -0
- package/dist/cli/commands/cross-migrate.js.map +1 -0
- package/dist/cli/index.js +20 -0
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/utils/config.js +125 -5
- package/dist/cli/utils/config.js.map +1 -1
- package/dist/config/config.service.js +21 -3
- package/dist/config/config.service.js.map +1 -1
- package/dist/config/env-url.helper.d.ts +11 -0
- package/dist/config/env-url.helper.js +56 -0
- package/dist/config/env-url.helper.js.map +1 -0
- package/dist/config/secrets.validator.js +5 -1
- package/dist/config/secrets.validator.js.map +1 -1
- package/dist/migration/analyzers/mongodb.analyzer.d.ts +3 -0
- package/dist/migration/analyzers/mongodb.analyzer.js +208 -0
- package/dist/migration/analyzers/mongodb.analyzer.js.map +1 -0
- package/dist/migration/analyzers/postgres.analyzer.d.ts +3 -0
- package/dist/migration/analyzers/postgres.analyzer.js +176 -0
- package/dist/migration/analyzers/postgres.analyzer.js.map +1 -0
- package/dist/migration/config.manager.d.ts +3 -0
- package/dist/migration/config.manager.js +80 -0
- package/dist/migration/config.manager.js.map +1 -0
- package/dist/migration/engines/migration.engine.d.ts +6 -0
- package/dist/migration/engines/migration.engine.js +62 -0
- package/dist/migration/engines/migration.engine.js.map +1 -0
- package/dist/migration/engines/mongo-to-postgres.engine.d.ts +2 -0
- package/dist/migration/engines/mongo-to-postgres.engine.js +409 -0
- package/dist/migration/engines/mongo-to-postgres.engine.js.map +1 -0
- package/dist/migration/engines/postgres-to-mongo.engine.d.ts +2 -0
- package/dist/migration/engines/postgres-to-mongo.engine.js +192 -0
- package/dist/migration/engines/postgres-to-mongo.engine.js.map +1 -0
- package/dist/migration/mappers/mongo-to-postgres.mapper.d.ts +2 -0
- package/dist/migration/mappers/mongo-to-postgres.mapper.js +263 -0
- package/dist/migration/mappers/mongo-to-postgres.mapper.js.map +1 -0
- package/dist/migration/mappers/postgres-to-mongo.mapper.d.ts +2 -0
- package/dist/migration/mappers/postgres-to-mongo.mapper.js +174 -0
- package/dist/migration/mappers/postgres-to-mongo.mapper.js.map +1 -0
- package/dist/migration/reference.detector.d.ts +2 -0
- package/dist/migration/reference.detector.js +57 -0
- package/dist/migration/reference.detector.js.map +1 -0
- package/dist/migration/type.mapper.d.ts +12 -0
- package/dist/migration/type.mapper.js +197 -0
- package/dist/migration/type.mapper.js.map +1 -0
- package/dist/migration/types.d.ts +183 -0
- package/dist/migration/types.js +11 -0
- package/dist/migration/types.js.map +1 -0
- package/package.json +11 -2
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.executePostgresToMongo = executePostgresToMongo;
|
|
4
|
+
const mongodb_1 = require("mongodb");
|
|
5
|
+
const pg_1 = require("pg");
|
|
6
|
+
async function executePostgresToMongo(plan, onProgress) {
|
|
7
|
+
const startTime = Date.now();
|
|
8
|
+
const errors = [];
|
|
9
|
+
const results = [];
|
|
10
|
+
const pgPool = new pg_1.Pool({ connectionString: plan.source.url });
|
|
11
|
+
const mongoClient = new mongodb_1.MongoClient(plan.target.url);
|
|
12
|
+
try {
|
|
13
|
+
await mongoClient.connect();
|
|
14
|
+
const db = mongoClient.db(plan.target.database);
|
|
15
|
+
const errorsCollection = plan.options.createErrorsTable
|
|
16
|
+
? db.collection('_migration_errors')
|
|
17
|
+
: null;
|
|
18
|
+
for (const mapping of plan.documentMappings || []) {
|
|
19
|
+
const result = await migrateTable(pgPool, db, mapping, plan, errors, onProgress);
|
|
20
|
+
results.push(result);
|
|
21
|
+
}
|
|
22
|
+
if (errorsCollection && errors.length > 0) {
|
|
23
|
+
const errorDocs = errors.map((e) => ({
|
|
24
|
+
collection: e.collection,
|
|
25
|
+
sourceId: e.sourceId,
|
|
26
|
+
error: e.error,
|
|
27
|
+
sourceData: e.data,
|
|
28
|
+
createdAt: new Date(),
|
|
29
|
+
}));
|
|
30
|
+
await errorsCollection.insertMany(errorDocs);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
finally {
|
|
34
|
+
await mongoClient.close();
|
|
35
|
+
await pgPool.end();
|
|
36
|
+
}
|
|
37
|
+
const duration = Date.now() - startTime;
|
|
38
|
+
const allSuccess = results.every((r) => r.status === 'success');
|
|
39
|
+
return {
|
|
40
|
+
success: allSuccess && errors.length === 0,
|
|
41
|
+
tables: results,
|
|
42
|
+
totalErrors: errors.length,
|
|
43
|
+
duration,
|
|
44
|
+
dryRun: plan.options.dryRun,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
async function migrateTable(pgPool, db, mapping, plan, errors, onProgress) {
|
|
48
|
+
const batchSize = plan.options.batchSize;
|
|
49
|
+
const collection = db.collection(mapping.targetCollection);
|
|
50
|
+
if (!plan.options.incremental) {
|
|
51
|
+
await collection.deleteMany({});
|
|
52
|
+
}
|
|
53
|
+
let whereClause = '';
|
|
54
|
+
if (plan.options.incremental && plan.options.since) {
|
|
55
|
+
whereClause = buildIncrementalWhere(plan.options.since);
|
|
56
|
+
}
|
|
57
|
+
const countResult = await pgPool.query(`SELECT count(*)::integer AS count FROM "${mapping.primaryTable}" ${whereClause}`);
|
|
58
|
+
const totalCount = parseInt(countResult.rows[0]?.count || '0');
|
|
59
|
+
let processed = 0;
|
|
60
|
+
let failedCount = 0;
|
|
61
|
+
let offset = 0;
|
|
62
|
+
while (offset < totalCount) {
|
|
63
|
+
const rows = await pgPool.query(`SELECT * FROM "${mapping.primaryTable}" ${whereClause} ORDER BY 1 LIMIT $1 OFFSET $2`, [batchSize, offset]);
|
|
64
|
+
const documents = [];
|
|
65
|
+
for (const row of rows.rows) {
|
|
66
|
+
try {
|
|
67
|
+
const doc = await buildDocument(pgPool, mapping, row);
|
|
68
|
+
documents.push(doc);
|
|
69
|
+
}
|
|
70
|
+
catch (err) {
|
|
71
|
+
failedCount++;
|
|
72
|
+
const pkColumn = Object.keys(row)[0];
|
|
73
|
+
errors.push({
|
|
74
|
+
collection: mapping.targetCollection,
|
|
75
|
+
sourceId: String(row[pkColumn]),
|
|
76
|
+
error: err instanceof Error ? err.message : String(err),
|
|
77
|
+
data: JSON.stringify(row),
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (documents.length > 0) {
|
|
82
|
+
try {
|
|
83
|
+
if (plan.options.incremental) {
|
|
84
|
+
for (const doc of documents) {
|
|
85
|
+
await collection.replaceOne({ _id: doc._id }, doc, { upsert: true });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
await collection.insertMany(documents, { ordered: false });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
catch (err) {
|
|
93
|
+
failedCount += documents.length;
|
|
94
|
+
errors.push({
|
|
95
|
+
collection: mapping.targetCollection,
|
|
96
|
+
sourceId: 'batch',
|
|
97
|
+
error: err instanceof Error ? err.message : String(err),
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
processed += rows.rows.length;
|
|
102
|
+
offset += batchSize;
|
|
103
|
+
onProgress?.(mapping.targetCollection, processed, totalCount);
|
|
104
|
+
}
|
|
105
|
+
const targetCount = processed - failedCount;
|
|
106
|
+
return {
|
|
107
|
+
name: mapping.targetCollection,
|
|
108
|
+
sourceCount: totalCount,
|
|
109
|
+
targetCount,
|
|
110
|
+
failedCount,
|
|
111
|
+
status: failedCount === 0
|
|
112
|
+
? 'success'
|
|
113
|
+
: targetCount > 0
|
|
114
|
+
? 'partial'
|
|
115
|
+
: 'failed',
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
async function buildDocument(pgPool, mapping, row) {
|
|
119
|
+
const doc = {};
|
|
120
|
+
for (const [pgCol, mongoField] of Object.entries(mapping.fieldMappings)) {
|
|
121
|
+
if (pgCol.startsWith('_'))
|
|
122
|
+
continue;
|
|
123
|
+
const value = row[pgCol];
|
|
124
|
+
if (value === undefined)
|
|
125
|
+
continue;
|
|
126
|
+
if (mongoField === '_id') {
|
|
127
|
+
doc._id = convertToMongoId(value);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
doc[mongoField] = convertPgValueToMongo(value);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
for (const embed of mapping.embeddings) {
|
|
134
|
+
const childRows = await pgPool.query(`SELECT * FROM "${embed.sourceTable}" WHERE "${embed.foreignKey}" = $1`, [getPrimaryKeyValue(row)]);
|
|
135
|
+
if (embed.isArray) {
|
|
136
|
+
doc[embed.embedAs] = childRows.rows.map((r) => convertRowToEmbeddedDoc(r, embed.foreignKey));
|
|
137
|
+
}
|
|
138
|
+
else if (childRows.rows.length > 0) {
|
|
139
|
+
doc[embed.embedAs] = convertRowToEmbeddedDoc(childRows.rows[0], embed.foreignKey);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
for (const [pgCol, mongoField] of Object.entries(mapping.fieldMappings)) {
|
|
143
|
+
if (!pgCol.startsWith('_'))
|
|
144
|
+
continue;
|
|
145
|
+
const otherTable = pgCol.slice(1);
|
|
146
|
+
const relatedRows = await pgPool.query(`SELECT * FROM "${otherTable}" ORDER BY 1`);
|
|
147
|
+
doc[mongoField] = relatedRows.rows.map((r) => {
|
|
148
|
+
const pk = Object.keys(r)[0];
|
|
149
|
+
return convertPgValueToMongo(r[Object.keys(r).find((k) => k !== pk) || pk]);
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
return doc;
|
|
153
|
+
}
|
|
154
|
+
function convertToMongoId(value) {
|
|
155
|
+
if (typeof value === 'string' && /^[a-f0-9]{24}$/.test(value)) {
|
|
156
|
+
return new mongodb_1.ObjectId(value);
|
|
157
|
+
}
|
|
158
|
+
return value;
|
|
159
|
+
}
|
|
160
|
+
function convertPgValueToMongo(value) {
|
|
161
|
+
if (value === null || value === undefined)
|
|
162
|
+
return null;
|
|
163
|
+
if (value instanceof Date)
|
|
164
|
+
return value;
|
|
165
|
+
if (Buffer.isBuffer(value))
|
|
166
|
+
return value;
|
|
167
|
+
if (typeof value === 'object' && !Array.isArray(value))
|
|
168
|
+
return value;
|
|
169
|
+
if (Array.isArray(value))
|
|
170
|
+
return value.map(convertPgValueToMongo);
|
|
171
|
+
return value;
|
|
172
|
+
}
|
|
173
|
+
function convertRowToEmbeddedDoc(row, excludeFk) {
|
|
174
|
+
const doc = {};
|
|
175
|
+
for (const [key, value] of Object.entries(row)) {
|
|
176
|
+
if (key === excludeFk)
|
|
177
|
+
continue;
|
|
178
|
+
if (key === 'id')
|
|
179
|
+
continue;
|
|
180
|
+
const camelKey = key.replace(/_([a-z])/g, (_, l) => l.toUpperCase());
|
|
181
|
+
doc[camelKey] = convertPgValueToMongo(value);
|
|
182
|
+
}
|
|
183
|
+
return doc;
|
|
184
|
+
}
|
|
185
|
+
function getPrimaryKeyValue(row) {
|
|
186
|
+
return row.id || row[Object.keys(row)[0]];
|
|
187
|
+
}
|
|
188
|
+
function buildIncrementalWhere(since) {
|
|
189
|
+
const sinceDate = new Date(since).toISOString();
|
|
190
|
+
return `WHERE COALESCE(created_at, updated_at, '1970-01-01'::timestamptz) >= '${sinceDate}'`;
|
|
191
|
+
}
|
|
192
|
+
//# sourceMappingURL=postgres-to-mongo.engine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"postgres-to-mongo.engine.js","sourceRoot":"","sources":["../../../src/migration/engines/postgres-to-mongo.engine.ts"],"names":[],"mappings":";;AAgBA,wDAuDC;AAvED,qCAAoD;AACpD,2BAA0B;AAenB,KAAK,UAAU,sBAAsB,CAC1C,IAAmB,EACnB,UAA2E;IAE3E,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CA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ateMongoToPostgresPlan = generateMongoToPostgresPlan;
|
|
4
|
+
const types_1 = require("../types");
|
|
5
|
+
const type_mapper_1 = require("../type.mapper");
|
|
6
|
+
const reference_detector_1 = require("../reference.detector");
|
|
7
|
+
function generateMongoToPostgresPlan(analysis, sourceUrl, targetUrl, options = {}) {
|
|
8
|
+
const mergedOptions = { ...types_1.DEFAULT_MIGRATION_OPTIONS, ...options };
|
|
9
|
+
const conflicts = [];
|
|
10
|
+
const referenceMap = (0, reference_detector_1.detectReferences)(analysis.collections);
|
|
11
|
+
const tableMappings = [];
|
|
12
|
+
for (const collection of analysis.collections) {
|
|
13
|
+
const refs = referenceMap.get(collection.name) || [];
|
|
14
|
+
const mapping = mapCollection(collection, refs, conflicts, mergedOptions.maxNestingDepth);
|
|
15
|
+
tableMappings.push(mapping);
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
version: '1.0.0',
|
|
19
|
+
direction: 'mongo_to_postgres',
|
|
20
|
+
source: { type: 'mongodb', url: sourceUrl, database: analysis.database },
|
|
21
|
+
target: {
|
|
22
|
+
type: 'postgresql',
|
|
23
|
+
url: targetUrl,
|
|
24
|
+
database: extractDbName(targetUrl),
|
|
25
|
+
},
|
|
26
|
+
tableMappings,
|
|
27
|
+
conflicts,
|
|
28
|
+
options: mergedOptions,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function extractDbName(url) {
|
|
32
|
+
try {
|
|
33
|
+
const parsed = new URL(url);
|
|
34
|
+
return parsed.pathname.replace(/^\//, '') || 'postgres';
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return 'postgres';
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function mapCollection(collection, detectedRefs, conflicts, maxDepth) {
|
|
41
|
+
const tableName = (0, type_mapper_1.toSnakeCase)(collection.name);
|
|
42
|
+
const fields = [];
|
|
43
|
+
const nestedMappings = [];
|
|
44
|
+
const arrayMappings = [];
|
|
45
|
+
for (const field of collection.fields) {
|
|
46
|
+
processField(field, tableName, collection.name, fields, nestedMappings, arrayMappings, conflicts, maxDepth, collection.documentCount);
|
|
47
|
+
}
|
|
48
|
+
const hasIdField = fields.some((f) => f.sourceField === '_id');
|
|
49
|
+
if (!hasIdField) {
|
|
50
|
+
fields.unshift({
|
|
51
|
+
sourceField: '_id',
|
|
52
|
+
targetColumn: 'id',
|
|
53
|
+
targetType: 'uuid',
|
|
54
|
+
transform: 'uuid_from_objectid',
|
|
55
|
+
nullable: false,
|
|
56
|
+
isUnique: true,
|
|
57
|
+
isPrimaryKey: true,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
const usedColumns = new Set();
|
|
61
|
+
for (const field of fields) {
|
|
62
|
+
if (usedColumns.has(field.targetColumn)) {
|
|
63
|
+
const original = field.targetColumn;
|
|
64
|
+
field.targetColumn = field.isPrimaryKey
|
|
65
|
+
? original
|
|
66
|
+
: `${original}_original`;
|
|
67
|
+
}
|
|
68
|
+
usedColumns.add(field.targetColumn);
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
sourceCollection: collection.name,
|
|
72
|
+
targetTable: tableName,
|
|
73
|
+
fields,
|
|
74
|
+
nestedMappings,
|
|
75
|
+
arrayMappings,
|
|
76
|
+
detectedReferences: detectedRefs,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function processField(field, parentTable, collectionName, fields, nestedMappings, arrayMappings, conflicts, maxDepth, docCount) {
|
|
80
|
+
if (field.name === '_id') {
|
|
81
|
+
fields.push({
|
|
82
|
+
sourceField: '_id',
|
|
83
|
+
targetColumn: 'id',
|
|
84
|
+
targetType: 'uuid',
|
|
85
|
+
transform: 'uuid_from_objectid',
|
|
86
|
+
nullable: false,
|
|
87
|
+
isUnique: true,
|
|
88
|
+
isPrimaryKey: true,
|
|
89
|
+
});
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const nonNullTypes = Object.entries(field.types).filter(([t]) => t !== 'null' && t !== 'undefined');
|
|
93
|
+
if (nonNullTypes.length > 1) {
|
|
94
|
+
const sorted = nonNullTypes.sort((a, b) => b[1] - a[1]);
|
|
95
|
+
const majorType = sorted[0][0];
|
|
96
|
+
const total = sorted.reduce((sum, [, c]) => sum + c, 0);
|
|
97
|
+
conflicts.push({
|
|
98
|
+
type: 'type_mismatch',
|
|
99
|
+
location: `${collectionName}.${field.path}`,
|
|
100
|
+
field: field.name,
|
|
101
|
+
details: sorted
|
|
102
|
+
.map(([t, c]) => `${t} in ${c} docs`)
|
|
103
|
+
.join(', '),
|
|
104
|
+
suggestion: `cast to ${(0, type_mapper_1.mongoTypeToPgType)(majorType)}, log failures`,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
if (field.frequency < 100) {
|
|
108
|
+
conflicts.push({
|
|
109
|
+
type: 'missing_field',
|
|
110
|
+
location: `${collectionName}.${field.path}`,
|
|
111
|
+
field: field.name,
|
|
112
|
+
details: `missing in ${(100 - field.frequency).toFixed(1)}% of documents`,
|
|
113
|
+
suggestion: 'nullable column',
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
if (field.isNestedObject && !field.isArray) {
|
|
117
|
+
handleNestedObject(field, parentTable, collectionName, fields, nestedMappings, conflicts, maxDepth, docCount);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (field.isArray) {
|
|
121
|
+
handleArray(field, parentTable, collectionName, arrayMappings, conflicts, maxDepth, docCount);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const majorType = (0, type_mapper_1.resolveMajorityType)(field.types);
|
|
125
|
+
const pgType = field.isObjectId ? 'uuid' : (0, type_mapper_1.mongoTypeToPgType)(majorType);
|
|
126
|
+
const hasNullValues = (field.types['null'] || 0) + (field.types['undefined'] || 0) > 0;
|
|
127
|
+
const isNullable = field.frequency < 100 || hasNullValues;
|
|
128
|
+
const columnName = (0, type_mapper_1.toSnakeCase)(field.name);
|
|
129
|
+
const isUniqueFromIndex = false;
|
|
130
|
+
fields.push({
|
|
131
|
+
sourceField: field.path,
|
|
132
|
+
targetColumn: columnName,
|
|
133
|
+
targetType: pgType,
|
|
134
|
+
transform: field.isObjectId ? 'uuid_from_objectid' : undefined,
|
|
135
|
+
nullable: isNullable,
|
|
136
|
+
isUnique: isUniqueFromIndex,
|
|
137
|
+
isPrimaryKey: false,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
function handleNestedObject(field, parentTable, collectionName, fields, nestedMappings, conflicts, maxDepth, docCount) {
|
|
141
|
+
const nestedFields = field.nestedFields || [];
|
|
142
|
+
const isConsistent = nestedFields.length > 0 &&
|
|
143
|
+
nestedFields.length <= 20 &&
|
|
144
|
+
nestedFields.every((f) => f.frequency > 50);
|
|
145
|
+
if (isConsistent && field.depth < maxDepth) {
|
|
146
|
+
const childTable = `${(0, type_mapper_1.singularize)(parentTable)}_${(0, type_mapper_1.toSnakeCase)(field.name)}`;
|
|
147
|
+
const childFields = [
|
|
148
|
+
{
|
|
149
|
+
sourceField: 'id',
|
|
150
|
+
targetColumn: 'id',
|
|
151
|
+
targetType: 'uuid',
|
|
152
|
+
nullable: false,
|
|
153
|
+
isUnique: true,
|
|
154
|
+
isPrimaryKey: true,
|
|
155
|
+
defaultValue: 'gen_random_uuid()',
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
sourceField: `${parentTable}_id`,
|
|
159
|
+
targetColumn: `${(0, type_mapper_1.singularize)(parentTable)}_id`,
|
|
160
|
+
targetType: 'uuid',
|
|
161
|
+
nullable: false,
|
|
162
|
+
isUnique: false,
|
|
163
|
+
isPrimaryKey: false,
|
|
164
|
+
},
|
|
165
|
+
];
|
|
166
|
+
for (const nested of nestedFields) {
|
|
167
|
+
const majorType = (0, type_mapper_1.resolveMajorityType)(nested.types);
|
|
168
|
+
childFields.push({
|
|
169
|
+
sourceField: nested.path,
|
|
170
|
+
targetColumn: (0, type_mapper_1.toSnakeCase)(nested.name),
|
|
171
|
+
targetType: (0, type_mapper_1.mongoTypeToPgType)(majorType),
|
|
172
|
+
nullable: nested.frequency < 100,
|
|
173
|
+
isUnique: false,
|
|
174
|
+
isPrimaryKey: false,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
nestedMappings.push({
|
|
178
|
+
sourceField: field.path,
|
|
179
|
+
targetTable: childTable,
|
|
180
|
+
strategy: 'table',
|
|
181
|
+
parentForeignKey: `${(0, type_mapper_1.singularize)(parentTable)}_id`,
|
|
182
|
+
fields: childFields,
|
|
183
|
+
relationType: '1:1',
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
fields.push({
|
|
188
|
+
sourceField: field.path,
|
|
189
|
+
targetColumn: (0, type_mapper_1.toSnakeCase)(field.name),
|
|
190
|
+
targetType: 'jsonb',
|
|
191
|
+
transform: 'jsonb',
|
|
192
|
+
nullable: field.frequency < 100,
|
|
193
|
+
isUnique: false,
|
|
194
|
+
isPrimaryKey: false,
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function handleArray(field, parentTable, collectionName, arrayMappings, conflicts, maxDepth, docCount) {
|
|
199
|
+
const elementTypes = field.arrayElementType?.split(' | ') || [];
|
|
200
|
+
const hasObjects = elementTypes.includes('object');
|
|
201
|
+
const hasPrimitives = elementTypes.some((t) => t !== 'object' && t !== 'null' && t !== 'undefined');
|
|
202
|
+
if (hasObjects && field.nestedFields && field.nestedFields.length > 0) {
|
|
203
|
+
const childTable = `${(0, type_mapper_1.singularize)(parentTable)}_${(0, type_mapper_1.toSnakeCase)(field.name)}`;
|
|
204
|
+
const childFields = [
|
|
205
|
+
{
|
|
206
|
+
sourceField: 'id',
|
|
207
|
+
targetColumn: 'id',
|
|
208
|
+
targetType: 'uuid',
|
|
209
|
+
nullable: false,
|
|
210
|
+
isUnique: true,
|
|
211
|
+
isPrimaryKey: true,
|
|
212
|
+
defaultValue: 'gen_random_uuid()',
|
|
213
|
+
},
|
|
214
|
+
{
|
|
215
|
+
sourceField: `${parentTable}_id`,
|
|
216
|
+
targetColumn: `${(0, type_mapper_1.singularize)(parentTable)}_id`,
|
|
217
|
+
targetType: 'uuid',
|
|
218
|
+
nullable: false,
|
|
219
|
+
isUnique: false,
|
|
220
|
+
isPrimaryKey: false,
|
|
221
|
+
},
|
|
222
|
+
];
|
|
223
|
+
for (const nested of field.nestedFields) {
|
|
224
|
+
const majorType = (0, type_mapper_1.resolveMajorityType)(nested.types);
|
|
225
|
+
childFields.push({
|
|
226
|
+
sourceField: nested.path,
|
|
227
|
+
targetColumn: (0, type_mapper_1.toSnakeCase)(nested.name),
|
|
228
|
+
targetType: (0, type_mapper_1.mongoTypeToPgType)(majorType),
|
|
229
|
+
nullable: nested.frequency < 100,
|
|
230
|
+
isUnique: false,
|
|
231
|
+
isPrimaryKey: false,
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
arrayMappings.push({
|
|
235
|
+
sourceField: field.path,
|
|
236
|
+
targetTable: childTable,
|
|
237
|
+
strategy: 'child_table',
|
|
238
|
+
parentForeignKey: `${(0, type_mapper_1.singularize)(parentTable)}_id`,
|
|
239
|
+
fields: childFields,
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
else if (hasPrimitives && !hasObjects) {
|
|
243
|
+
const majorType = elementTypes.find((t) => t !== 'null' && t !== 'undefined') || 'string';
|
|
244
|
+
const pgElementType = (0, type_mapper_1.mongoTypeToPgType)(majorType);
|
|
245
|
+
arrayMappings.push({
|
|
246
|
+
sourceField: field.path,
|
|
247
|
+
targetTable: `${(0, type_mapper_1.singularize)(parentTable)}_${(0, type_mapper_1.toSnakeCase)(field.name)}`,
|
|
248
|
+
strategy: 'array_column',
|
|
249
|
+
parentForeignKey: `${(0, type_mapper_1.singularize)(parentTable)}_id`,
|
|
250
|
+
elementType: pgElementType,
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
arrayMappings.push({
|
|
255
|
+
sourceField: field.path,
|
|
256
|
+
targetTable: `${(0, type_mapper_1.singularize)(parentTable)}_${(0, type_mapper_1.toSnakeCase)(field.name)}`,
|
|
257
|
+
strategy: 'junction',
|
|
258
|
+
parentForeignKey: `${(0, type_mapper_1.singularize)(parentTable)}_id`,
|
|
259
|
+
elementType: 'jsonb',
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
//# sourceMappingURL=mongo-to-postgres.mapper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mongo-to-postgres.mapper.js","sourceRoot":"","sources":["../../../src/migration/mappers/mongo-to-postgres.mapper.ts"],"names":[],"mappings":";;AAqBA,kEAmCC;AAxDD,oCAYkB;AAClB,gDAKwB;AACxB,8DAAyD;AAEzD,SAAgB,2BAA2B,CACzC,QAA6B,EAC7B,SAAiB,EACjB,SAAiB,EACjB,UAAqC,EAAE;IAEvC,MAAM,aAAa,GAAG,EAAE,GAAG,iCAAyB,EAAE,GAAG,OAAO,EAAE,CAAC;IACnE,MAAM,SAAS,GAAwB,EAAE,CAAC;IAC1C,MAAM,YAAY,GAAG,IAAA,qCAAgB,EAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;IAC5D,MAAM,aAAa,GAAmB,EAAE,CAAC;IAEzC,KAAK,MAAM,UAAU,IAAI,QAAQ,CAAC,WAAW,EAAE,CAAC;QAC9C,MAAM,IAAI,GAAG,YAAY,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;QACrD,MAAM,OAAO,GAAG,aAAa,CAC3B,UAAU,EACV,IAAI,EACJ,SAAS,EACT,aAAa,CAAC,eAAe,CAC9B,CAAC;QACF,aAAa,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC9B,CAAC;IAED,OAAO;QACL,OAAO,EAAE,OAAO;QAChB,SAAS,EAAE,mBAAmB;QAC9B,MAAM,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,GAAG,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,CAAC,QAAQ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,UAAU;YACvB,QAAQ,EAAE,aAAa;YACvB,gBAAgB,EAAE,GAAG,IAAA,yBAAW,EAAC,WAAW,CAAC,KAAK;YAClD,MAAM,EAAE,WAAW;SACpB,CAAC,CAAC;IACL,CAAC;SAAM,IAAI,aAAa,IAAI,CAAC,UAAU,EAAE,CAAC;QACxC,MAAM,SAAS,GAAG,YAAY,CAAC,IAAI,CACjC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,MAAM,IAAI,CAAC,KAAK,WAAW,CACzC,IAAI,QAAQ,CAAC;QACd,MAAM,aAAa,GAAG,IAAA,+BAAiB,EAAC,SAAS,CAAC,CAAC;QAEnD,aAAa,CAAC,IAAI,CAAC;YACjB,WAAW,EAAE,KAAK,CAAC,IAAI;YACvB,WAAW,EAAE,GAAG,IAAA,yBAAW,EAAC,WAAW,CAAC,IAAI,IAAA,yBAAW,EAAC,KAAK,CAAC,IAAI,CAAC,EAAE;YACrE,QAAQ,EAAE,cAAc;YACxB,gBAAgB,EAAE,GAAG,IAAA,yBAAW,EAAC,WAAW,CAAC,KAAK;YAClD,WAAW,EAAE,aAAa;SAC3B,CAAC,CAAC;IACL,CAAC;SAAM,CAAC;QACN,aAAa,CAAC,IAAI,CAAC;YACjB,WAAW,EAAE,KAAK,CAAC,IAAI;YACvB,WAAW,EAAE,GAAG,IAAA,yBAAW,EAAC,WAAW,CAAC,IAAI,IAAA,yBAAW,EAAC,KAAK,CAAC,IAAI,CAAC,EAAE;YACrE,QAAQ,EAAE,UAAU;YACpB,gBAAgB,EAAE,GAAG,IAAA,yBAAW,EAAC,WAAW,CAAC,KAAK;YAClD,WAAW,EAAE,OAAO;SACrB,CAAC,CAAC;IACL,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generatePostgresToMongoPlan = generatePostgresToMongoPlan;
|
|
4
|
+
const types_1 = require("../types");
|
|
5
|
+
const type_mapper_1 = require("../type.mapper");
|
|
6
|
+
const EMBED_THRESHOLD = 1000;
|
|
7
|
+
function generatePostgresToMongoPlan(analysis, sourceUrl, targetUrl, options = {}) {
|
|
8
|
+
const mergedOptions = { ...types_1.DEFAULT_MIGRATION_OPTIONS, ...options };
|
|
9
|
+
const conflicts = [];
|
|
10
|
+
const relationships = detectRelationships(analysis);
|
|
11
|
+
const junctionTables = detectJunctionTables(analysis, relationships);
|
|
12
|
+
const childTables = new Set();
|
|
13
|
+
const documentMappings = [];
|
|
14
|
+
for (const rel of relationships) {
|
|
15
|
+
if (junctionTables.has(rel.fromTable))
|
|
16
|
+
continue;
|
|
17
|
+
if (rel.type === '1:1' || rel.type === '1:many') {
|
|
18
|
+
childTables.add(rel.fromTable);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
for (const table of analysis.tables) {
|
|
22
|
+
if (junctionTables.has(table.name))
|
|
23
|
+
continue;
|
|
24
|
+
if (childTables.has(table.name))
|
|
25
|
+
continue;
|
|
26
|
+
const mapping = mapTableToDocument(table, analysis, relationships, junctionTables, conflicts);
|
|
27
|
+
documentMappings.push(mapping);
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
version: '1.0.0',
|
|
31
|
+
direction: 'postgres_to_mongo',
|
|
32
|
+
source: {
|
|
33
|
+
type: 'postgresql',
|
|
34
|
+
url: sourceUrl,
|
|
35
|
+
database: analysis.database,
|
|
36
|
+
},
|
|
37
|
+
target: {
|
|
38
|
+
type: 'mongodb',
|
|
39
|
+
url: targetUrl,
|
|
40
|
+
database: extractDbName(targetUrl),
|
|
41
|
+
},
|
|
42
|
+
documentMappings,
|
|
43
|
+
conflicts,
|
|
44
|
+
options: mergedOptions,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function extractDbName(url) {
|
|
48
|
+
try {
|
|
49
|
+
const parsed = new URL(url);
|
|
50
|
+
return parsed.pathname.replace(/^\//, '') || 'test';
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return 'test';
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function detectRelationships(analysis) {
|
|
57
|
+
const relationships = [];
|
|
58
|
+
const tableRowCounts = new Map();
|
|
59
|
+
for (const table of analysis.tables) {
|
|
60
|
+
tableRowCounts.set(table.name, table.rowCount);
|
|
61
|
+
}
|
|
62
|
+
for (const table of analysis.tables) {
|
|
63
|
+
for (const fk of table.foreignKeys) {
|
|
64
|
+
const fromCount = tableRowCounts.get(table.name) || 0;
|
|
65
|
+
const toCount = tableRowCounts.get(fk.referencedTable) || 0;
|
|
66
|
+
const isUniqueOnFrom = table.columns.find((c) => c.name === fk.columnName)?.isUnique;
|
|
67
|
+
let type;
|
|
68
|
+
if (isUniqueOnFrom) {
|
|
69
|
+
type = '1:1';
|
|
70
|
+
}
|
|
71
|
+
else if (fromCount > toCount * 0.5) {
|
|
72
|
+
type = '1:many';
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
type = '1:many';
|
|
76
|
+
}
|
|
77
|
+
relationships.push({
|
|
78
|
+
fromTable: table.name,
|
|
79
|
+
toTable: fk.referencedTable,
|
|
80
|
+
fromColumn: fk.columnName,
|
|
81
|
+
toColumn: fk.referencedColumn,
|
|
82
|
+
type,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return relationships;
|
|
87
|
+
}
|
|
88
|
+
function detectJunctionTables(analysis, relationships) {
|
|
89
|
+
const junctions = new Set();
|
|
90
|
+
for (const table of analysis.tables) {
|
|
91
|
+
const fks = table.foreignKeys;
|
|
92
|
+
const nonFkColumns = table.columns.filter((c) => !fks.some((fk) => fk.columnName === c.name) && !c.isPrimaryKey);
|
|
93
|
+
if (fks.length === 2 && nonFkColumns.length <= 2) {
|
|
94
|
+
junctions.add(table.name);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return junctions;
|
|
98
|
+
}
|
|
99
|
+
function mapTableToDocument(table, analysis, relationships, junctionTables, conflicts) {
|
|
100
|
+
const collectionName = table.name;
|
|
101
|
+
const fieldMappings = {};
|
|
102
|
+
const embeddings = [];
|
|
103
|
+
const references = [];
|
|
104
|
+
for (const col of table.columns) {
|
|
105
|
+
if (col.isPrimaryKey) {
|
|
106
|
+
fieldMappings[col.name] = '_id';
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
fieldMappings[col.name] = toCamelCase(col.name);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const incomingRels = relationships.filter((r) => r.toTable === table.name);
|
|
113
|
+
for (const rel of incomingRels) {
|
|
114
|
+
if (junctionTables.has(rel.fromTable)) {
|
|
115
|
+
const junctionTable = analysis.tables.find((t) => t.name === rel.fromTable);
|
|
116
|
+
if (junctionTable) {
|
|
117
|
+
const otherFk = junctionTable.foreignKeys.find((fk) => fk.referencedTable !== table.name);
|
|
118
|
+
if (otherFk) {
|
|
119
|
+
handleManyToMany(table, junctionTable, otherFk, analysis, embeddings, fieldMappings);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
const childTable = analysis.tables.find((t) => t.name === rel.fromTable);
|
|
125
|
+
if (!childTable)
|
|
126
|
+
continue;
|
|
127
|
+
const shouldEmbed = rel.type === '1:1' ||
|
|
128
|
+
(rel.type === '1:many' && childTable.rowCount < EMBED_THRESHOLD);
|
|
129
|
+
if (shouldEmbed) {
|
|
130
|
+
embeddings.push({
|
|
131
|
+
sourceTable: rel.fromTable,
|
|
132
|
+
foreignKey: rel.fromColumn,
|
|
133
|
+
embedAs: rel.type === '1:1' ? toCamelCase(rel.fromTable) : toCamelCase(rel.fromTable),
|
|
134
|
+
isArray: rel.type !== '1:1',
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
references.push({
|
|
139
|
+
sourceTable: rel.fromTable,
|
|
140
|
+
foreignKey: rel.fromColumn,
|
|
141
|
+
refField: `${toCamelCase(table.name)}Id`,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
primaryTable: table.name,
|
|
147
|
+
targetCollection: collectionName,
|
|
148
|
+
fieldMappings,
|
|
149
|
+
embeddings,
|
|
150
|
+
references,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
function handleManyToMany(parentTable, junctionTable, otherFk, analysis, embeddings, fieldMappings) {
|
|
154
|
+
const otherTable = analysis.tables.find((t) => t.name === otherFk.referencedTable);
|
|
155
|
+
if (!otherTable)
|
|
156
|
+
return;
|
|
157
|
+
const hasExtraColumns = junctionTable.columns.filter((c) => !junctionTable.foreignKeys.some((fk) => fk.columnName === c.name) &&
|
|
158
|
+
!c.isPrimaryKey);
|
|
159
|
+
if (hasExtraColumns.length === 0 && otherTable.columns.length <= 3) {
|
|
160
|
+
fieldMappings[`_${otherTable.name}`] = toCamelCase((0, type_mapper_1.pluralize)(otherTable.name));
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
embeddings.push({
|
|
164
|
+
sourceTable: junctionTable.name,
|
|
165
|
+
foreignKey: junctionTable.foreignKeys.find((fk) => fk.referencedTable === parentTable.name)?.columnName || '',
|
|
166
|
+
embedAs: toCamelCase((0, type_mapper_1.pluralize)(otherTable.name)),
|
|
167
|
+
isArray: true,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
function toCamelCase(str) {
|
|
172
|
+
return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
173
|
+
}
|
|
174
|
+
//# sourceMappingURL=postgres-to-mongo.mapper.js.map
|