Skip to content

Commit c2e23e7

Browse files
authored
Merge pull request #274 from constructive-io/devin/1767763974-schema-rename-map
refactor: rename renameMap to schemaRenameMap for clarity
2 parents a1f8a49 + 0627460 commit c2e23e7

File tree

2 files changed

+47
-47
lines changed

2 files changed

+47
-47
lines changed

packages/plpgsql-deparser/__tests__/__snapshots__/schema-rename-mapped.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

3-
exports[`schema rename mapped should transform schema names and snapshot rename map and output: rename-map 1`] = `
3+
exports[`schema rename mapped should transform schema names and snapshot schema rename map and output: schema-rename-map 1`] = `
44
{
55
"app_internal": {
66
"newSchema": "myapp_internal_v2",
@@ -187,7 +187,7 @@ exports[`schema rename mapped should transform schema names and snapshot rename
187187
}
188188
`;
189189

190-
exports[`schema rename mapped should transform schema names and snapshot rename map and output: transformed-sql 1`] = `
190+
exports[`schema rename mapped should transform schema names and snapshot schema rename map and output: transformed-sql 1`] = `
191191
"CREATE FUNCTION myapp_v2.get_user_stats(
192192
p_user_id int
193193
) RETURNS int LANGUAGE plpgsql AS $$DECLARE

packages/plpgsql-deparser/__tests__/schema-rename-mapped.test.ts

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ interface SchemaReference {
2323
location: string;
2424
}
2525

26-
interface RenameMap {
26+
interface SchemaRenameMap {
2727
[oldSchema: string]: {
2828
newSchema: string;
2929
references: SchemaReference[];
@@ -43,7 +43,7 @@ describe('schema rename mapped', () => {
4343
*/
4444
function collectAndTransformSqlAst(
4545
node: any,
46-
renameMap: RenameMap,
46+
schemaRenameMap: SchemaRenameMap,
4747
location: string
4848
): void {
4949
if (node === null || node === undefined || typeof node !== 'object') {
@@ -52,38 +52,38 @@ describe('schema rename mapped', () => {
5252

5353
if (Array.isArray(node)) {
5454
for (let i = 0; i < node.length; i++) {
55-
collectAndTransformSqlAst(node[i], renameMap, `${location}[${i}]`);
55+
collectAndTransformSqlAst(node[i], schemaRenameMap, `${location}[${i}]`);
5656
}
5757
return;
5858
}
5959

6060
// Handle RangeVar nodes (table references like app_public.users)
6161
if ('RangeVar' in node) {
6262
const rangeVar = node.RangeVar;
63-
if (rangeVar.schemaname && renameMap[rangeVar.schemaname]) {
63+
if (rangeVar.schemaname && schemaRenameMap[rangeVar.schemaname]) {
6464
const ref: SchemaReference = {
6565
type: 'table_ref',
6666
schema: rangeVar.schemaname,
6767
name: rangeVar.relname || 'unknown',
6868
location: `${location}.RangeVar`,
6969
};
70-
renameMap[rangeVar.schemaname].references.push(ref);
71-
rangeVar.schemaname = renameMap[rangeVar.schemaname].newSchema;
70+
schemaRenameMap[rangeVar.schemaname].references.push(ref);
71+
rangeVar.schemaname = schemaRenameMap[rangeVar.schemaname].newSchema;
7272
}
7373
}
7474

7575
// Handle direct relation references (INSERT/UPDATE/DELETE statements)
7676
if ('relation' in node && node.relation && typeof node.relation === 'object') {
7777
const relation = node.relation;
78-
if (relation.schemaname && renameMap[relation.schemaname]) {
78+
if (relation.schemaname && schemaRenameMap[relation.schemaname]) {
7979
const ref: SchemaReference = {
8080
type: 'relation',
8181
schema: relation.schemaname,
8282
name: relation.relname || 'unknown',
8383
location: `${location}.relation`,
8484
};
85-
renameMap[relation.schemaname].references.push(ref);
86-
relation.schemaname = renameMap[relation.schemaname].newSchema;
85+
schemaRenameMap[relation.schemaname].references.push(ref);
86+
relation.schemaname = schemaRenameMap[relation.schemaname].newSchema;
8787
}
8888
}
8989

@@ -93,16 +93,16 @@ describe('schema rename mapped', () => {
9393
if (Array.isArray(typeName.names) && typeName.names.length >= 2) {
9494
const firstNameNode = typeName.names[0];
9595
const schemaName = firstNameNode?.String?.sval;
96-
if (schemaName && renameMap[schemaName]) {
96+
if (schemaName && schemaRenameMap[schemaName]) {
9797
const secondNameNode = typeName.names[1];
9898
const ref: SchemaReference = {
9999
type: 'type_name',
100100
schema: schemaName,
101101
name: secondNameNode?.String?.sval || 'unknown',
102102
location: `${location}.TypeName`,
103103
};
104-
renameMap[schemaName].references.push(ref);
105-
firstNameNode.String.sval = renameMap[schemaName].newSchema;
104+
schemaRenameMap[schemaName].references.push(ref);
105+
firstNameNode.String.sval = schemaRenameMap[schemaName].newSchema;
106106
}
107107
}
108108
}
@@ -113,16 +113,16 @@ describe('schema rename mapped', () => {
113113
if (Array.isArray(funcCall.funcname) && funcCall.funcname.length >= 2) {
114114
const firstNameNode = funcCall.funcname[0];
115115
const schemaName = firstNameNode?.String?.sval;
116-
if (schemaName && renameMap[schemaName]) {
116+
if (schemaName && schemaRenameMap[schemaName]) {
117117
const secondNameNode = funcCall.funcname[1];
118118
const ref: SchemaReference = {
119119
type: 'func_call',
120120
schema: schemaName,
121121
name: secondNameNode?.String?.sval || 'unknown',
122122
location: `${location}.FuncCall`,
123123
};
124-
renameMap[schemaName].references.push(ref);
125-
firstNameNode.String.sval = renameMap[schemaName].newSchema;
124+
schemaRenameMap[schemaName].references.push(ref);
125+
firstNameNode.String.sval = schemaRenameMap[schemaName].newSchema;
126126
}
127127
}
128128
}
@@ -133,16 +133,16 @@ describe('schema rename mapped', () => {
133133
if (Array.isArray(createFunc.funcname) && createFunc.funcname.length >= 2) {
134134
const firstNameNode = createFunc.funcname[0];
135135
const schemaName = firstNameNode?.String?.sval;
136-
if (schemaName && renameMap[schemaName]) {
136+
if (schemaName && schemaRenameMap[schemaName]) {
137137
const secondNameNode = createFunc.funcname[1];
138138
const ref: SchemaReference = {
139139
type: 'function_name',
140140
schema: schemaName,
141141
name: secondNameNode?.String?.sval || 'unknown',
142142
location: `${location}.CreateFunctionStmt.funcname`,
143143
};
144-
renameMap[schemaName].references.push(ref);
145-
firstNameNode.String.sval = renameMap[schemaName].newSchema;
144+
schemaRenameMap[schemaName].references.push(ref);
145+
firstNameNode.String.sval = schemaRenameMap[schemaName].newSchema;
146146
}
147147
}
148148
}
@@ -151,22 +151,22 @@ describe('schema rename mapped', () => {
151151
if ('names' in node && 'typemod' in node && Array.isArray(node.names) && node.names.length >= 2) {
152152
const firstNameNode = node.names[0];
153153
const schemaName = firstNameNode?.String?.sval;
154-
if (schemaName && renameMap[schemaName]) {
154+
if (schemaName && schemaRenameMap[schemaName]) {
155155
const secondNameNode = node.names[1];
156156
const ref: SchemaReference = {
157157
type: 'return_type',
158158
schema: schemaName,
159159
name: secondNameNode?.String?.sval || 'unknown',
160160
location: `${location}.returnType`,
161161
};
162-
renameMap[schemaName].references.push(ref);
163-
firstNameNode.String.sval = renameMap[schemaName].newSchema;
162+
schemaRenameMap[schemaName].references.push(ref);
163+
firstNameNode.String.sval = schemaRenameMap[schemaName].newSchema;
164164
}
165165
}
166166

167167
// Recurse into all object properties
168168
for (const [key, value] of Object.entries(node)) {
169-
collectAndTransformSqlAst(value, renameMap, `${location}.${key}`);
169+
collectAndTransformSqlAst(value, schemaRenameMap, `${location}.${key}`);
170170
}
171171
}
172172

@@ -175,7 +175,7 @@ describe('schema rename mapped', () => {
175175
*/
176176
function collectAndTransformPlpgsqlAst(
177177
node: any,
178-
renameMap: RenameMap,
178+
schemaRenameMap: SchemaRenameMap,
179179
location: string
180180
): void {
181181
if (node === null || node === undefined || typeof node !== 'object') {
@@ -184,7 +184,7 @@ describe('schema rename mapped', () => {
184184

185185
if (Array.isArray(node)) {
186186
for (let i = 0; i < node.length; i++) {
187-
collectAndTransformPlpgsqlAst(node[i], renameMap, `${location}[${i}]`);
187+
collectAndTransformPlpgsqlAst(node[i], schemaRenameMap, `${location}[${i}]`);
188188
}
189189
return;
190190
}
@@ -196,17 +196,17 @@ describe('schema rename mapped', () => {
196196

197197
if (query && typeof query === 'object' && 'kind' in query) {
198198
if (query.kind === 'sql-stmt' && query.parseResult) {
199-
collectAndTransformSqlAst(query.parseResult, renameMap, `${location}.PLpgSQL_expr.query.parseResult`);
199+
collectAndTransformSqlAst(query.parseResult, schemaRenameMap, `${location}.PLpgSQL_expr.query.parseResult`);
200200
}
201201
if (query.kind === 'sql-expr' && query.expr) {
202-
collectAndTransformSqlAst(query.expr, renameMap, `${location}.PLpgSQL_expr.query.expr`);
202+
collectAndTransformSqlAst(query.expr, schemaRenameMap, `${location}.PLpgSQL_expr.query.expr`);
203203
}
204204
if (query.kind === 'assign') {
205205
if (query.targetExpr) {
206-
collectAndTransformSqlAst(query.targetExpr, renameMap, `${location}.PLpgSQL_expr.query.targetExpr`);
206+
collectAndTransformSqlAst(query.targetExpr, schemaRenameMap, `${location}.PLpgSQL_expr.query.targetExpr`);
207207
}
208208
if (query.valueExpr) {
209-
collectAndTransformSqlAst(query.valueExpr, renameMap, `${location}.PLpgSQL_expr.query.valueExpr`);
209+
collectAndTransformSqlAst(query.valueExpr, schemaRenameMap, `${location}.PLpgSQL_expr.query.valueExpr`);
210210
}
211211
}
212212
}
@@ -216,7 +216,7 @@ describe('schema rename mapped', () => {
216216
if ('PLpgSQL_type' in node) {
217217
const plType = node.PLpgSQL_type;
218218
if (plType.typname) {
219-
for (const oldSchema of Object.keys(renameMap)) {
219+
for (const oldSchema of Object.keys(schemaRenameMap)) {
220220
if (plType.typname.startsWith(oldSchema + '.')) {
221221
const typeName = plType.typname.substring(oldSchema.length + 1);
222222
const ref: SchemaReference = {
@@ -225,8 +225,8 @@ describe('schema rename mapped', () => {
225225
name: typeName,
226226
location: `${location}.PLpgSQL_type.typname`,
227227
};
228-
renameMap[oldSchema].references.push(ref);
229-
plType.typname = renameMap[oldSchema].newSchema + '.' + typeName;
228+
schemaRenameMap[oldSchema].references.push(ref);
229+
plType.typname = schemaRenameMap[oldSchema].newSchema + '.' + typeName;
230230
break;
231231
}
232232
}
@@ -235,7 +235,7 @@ describe('schema rename mapped', () => {
235235

236236
// Recurse into all object properties
237237
for (const [key, value] of Object.entries(node)) {
238-
collectAndTransformPlpgsqlAst(value, renameMap, `${location}.${key}`);
238+
collectAndTransformPlpgsqlAst(value, schemaRenameMap, `${location}.${key}`);
239239
}
240240
}
241241

@@ -244,7 +244,7 @@ describe('schema rename mapped', () => {
244244
*/
245245
function transformStatement(
246246
sql: string,
247-
renameMap: RenameMap,
247+
schemaRenameMap: SchemaRenameMap,
248248
stmtIndex: number
249249
): string {
250250
const sqlParsed = parseSync(sql) as any;
@@ -257,15 +257,15 @@ describe('schema rename mapped', () => {
257257
);
258258

259259
// Transform outer SQL AST
260-
collectAndTransformSqlAst(sqlParsed, renameMap, `stmt[${stmtIndex}]`);
260+
collectAndTransformSqlAst(sqlParsed, schemaRenameMap, `stmt[${stmtIndex}]`);
261261

262262
if (isPlpgsql) {
263263
try {
264264
const plpgsqlParsed = parsePlPgSQLSync(sql) as unknown as PLpgSQLParseResult;
265265
const { ast: hydratedAst } = hydratePlpgsqlAst(plpgsqlParsed);
266266

267267
// Transform PL/pgSQL AST
268-
collectAndTransformPlpgsqlAst(hydratedAst, renameMap, `stmt[${stmtIndex}].plpgsql`);
268+
collectAndTransformPlpgsqlAst(hydratedAst, schemaRenameMap, `stmt[${stmtIndex}].plpgsql`);
269269

270270
// Dehydrate and deparse
271271
const dehydratedAst = dehydratePlpgsqlAst(hydratedAst);
@@ -368,9 +368,9 @@ describe('schema rename mapped', () => {
368368
return statements;
369369
}
370370

371-
it('should transform schema names and snapshot rename map and output', () => {
372-
// Define the rename map with schemas to transform
373-
const renameMap: RenameMap = {
371+
it('should transform schema names and snapshot schema rename map and output', () => {
372+
// Define the schema rename map with schemas to transform
373+
const schemaRenameMap: SchemaRenameMap = {
374374
'app_public': {
375375
newSchema: 'myapp_v2',
376376
references: [],
@@ -399,7 +399,7 @@ describe('schema rename mapped', () => {
399399
continue;
400400
}
401401
try {
402-
const transformed = transformStatement(stmt, renameMap, i);
402+
const transformed = transformStatement(stmt, schemaRenameMap, i);
403403
transformedStatements.push(transformed);
404404
} catch (err) {
405405
// Log the error for debugging
@@ -409,18 +409,18 @@ describe('schema rename mapped', () => {
409409
}
410410
}
411411

412-
// Create a summary of the rename map (without location details for cleaner snapshot)
413-
const renameMapSummary: Record<string, { newSchema: string; referenceCount: number; references: Array<{ type: string; name: string }> }> = {};
414-
for (const [oldSchema, data] of Object.entries(renameMap)) {
415-
renameMapSummary[oldSchema] = {
412+
// Create a summary of the schema rename map (without location details for cleaner snapshot)
413+
const schemaRenameMapSummary: Record<string, { newSchema: string; referenceCount: number; references: Array<{ type: string; name: string }> }> = {};
414+
for (const [oldSchema, data] of Object.entries(schemaRenameMap)) {
415+
schemaRenameMapSummary[oldSchema] = {
416416
newSchema: data.newSchema,
417417
referenceCount: data.references.length,
418418
references: data.references.map(r => ({ type: r.type, name: r.name })),
419419
};
420420
}
421421

422-
// Snapshot the rename map
423-
expect(renameMapSummary).toMatchSnapshot('rename-map');
422+
// Snapshot the schema rename map
423+
expect(schemaRenameMapSummary).toMatchSnapshot('schema-rename-map');
424424

425425
// Snapshot the transformed SQL
426426
const finalSQL = transformedStatements.join(';\n\n') + ';';

0 commit comments

Comments
 (0)