Skip to content

Commit 33a5034

Browse files
committed
refactor: revert clean database logic
1 parent 5e07f6d commit 33a5034

1 file changed

Lines changed: 36 additions & 13 deletions

File tree

__tests__/setup.ts

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,43 @@ const cleanDatabase = async (): Promise<void> => {
7171
await remoteConfig.init();
7272

7373
const con = await createOrGetConnection();
74+
for (const entity of con.entityMetadatas) {
75+
const repository = con.getRepository(entity.name);
76+
if (repository.metadata.tableType === 'view') continue;
7477

75-
// Get all table names to truncate (excluding views and seed data tables)
76-
const tablesToTruncate = con.entityMetadatas
77-
.filter(
78-
(entity) =>
79-
entity.tableType !== 'view' && !SEED_DATA_TABLES.has(entity.tableName),
80-
)
81-
.map((entity) => `"${testSchema}"."${entity.tableName}"`);
78+
// Skip seed data tables - they're populated once and tests expect them to exist
79+
if (SEED_DATA_TABLES.has(entity.tableName)) continue;
8280

83-
if (tablesToTruncate.length > 0) {
84-
// Single TRUNCATE with CASCADE handles FK dependencies and RESTART IDENTITY resets sequences
85-
await con.query(
86-
`TRUNCATE ${tablesToTruncate.join(', ')} RESTART IDENTITY CASCADE`,
87-
);
81+
await repository.query(`DELETE FROM "${entity.tableName}";`);
82+
83+
for (const column of entity.primaryColumns) {
84+
if (column.generationStrategy === 'increment') {
85+
// Reset sequences/identity columns for auto-increment primary keys
86+
// Must use schema-qualified table name for schema isolation to work
87+
try {
88+
// First try pg_get_serial_sequence (works for SERIAL columns)
89+
// Schema-qualify the table name for proper resolution in worker schemas
90+
const schemaQualifiedTable = `${testSchema}.${entity.tableName}`;
91+
const seqResult = await repository.query(
92+
`SELECT pg_get_serial_sequence($1, $2) as seq_name`,
93+
[schemaQualifiedTable, column.databaseName],
94+
);
95+
if (seqResult[0]?.seq_name) {
96+
await repository.query(
97+
`ALTER SEQUENCE ${seqResult[0].seq_name} RESTART WITH 1`,
98+
);
99+
} else {
100+
// If no sequence found, try resetting IDENTITY column directly
101+
// This handles GENERATED AS IDENTITY columns
102+
await repository.query(
103+
`ALTER TABLE "${testSchema}"."${entity.tableName}" ALTER COLUMN "${column.databaseName}" RESTART WITH 1`,
104+
);
105+
}
106+
} catch {
107+
// Sequence/identity might not exist or not be resettable, ignore
108+
}
109+
}
110+
}
88111
}
89112
};
90113

@@ -103,4 +126,4 @@ beforeEach(async () => {
103126
loadAuthKeys();
104127

105128
await cleanDatabase();
106-
}, 30000); // 30 second timeout for database cleanup
129+
}, 60000);

0 commit comments

Comments
 (0)