forge-sql-orm 2.0.26 → 2.0.28

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.
@@ -127,11 +127,20 @@ function getTableMetadata(table2) {
127
127
  ...builders
128
128
  };
129
129
  }
130
- function generateDropTableStatements(tables) {
130
+ function generateDropTableStatements(tables, options) {
131
131
  const dropStatements = [];
132
+ const validOptions = options ?? { sequence: true, table: true };
133
+ if (!validOptions.sequence && !validOptions.table) {
134
+ console.warn('No drop operations requested: both "table" and "sequence" options are false');
135
+ return [];
136
+ }
132
137
  tables.forEach((tableName) => {
133
- dropStatements.push(`DROP TABLE IF EXISTS \`${tableName}\`;`);
134
- dropStatements.push(`DROP SEQUENCE IF EXISTS \`${tableName}\`;`);
138
+ if (validOptions.table) {
139
+ dropStatements.push(`DROP TABLE IF EXISTS \`${tableName}\`;`);
140
+ }
141
+ if (validOptions.sequence) {
142
+ dropStatements.push(`DROP SEQUENCE IF EXISTS \`${tableName}\`;`);
143
+ }
135
144
  });
136
145
  return dropStatements;
137
146
  }
@@ -1246,7 +1255,7 @@ const forgeSystemTables = [migrations];
1246
1255
  async function dropSchemaMigrations() {
1247
1256
  try {
1248
1257
  const tables = await getTables();
1249
- const dropStatements = generateDropTableStatements(tables);
1258
+ const dropStatements = generateDropTableStatements(tables, { sequence: true, table: true });
1250
1259
  for (const statement of dropStatements) {
1251
1260
  console.warn(statement);
1252
1261
  await sql$1.sql.executeDDL(statement);
@@ -1273,7 +1282,7 @@ const applySchemaMigrations = async (migration) => {
1273
1282
  const successfulMigrations = await migrations2.run();
1274
1283
  console.info("Migrations applied:", successfulMigrations);
1275
1284
  const migrationList = await sql$1.migrationRunner.list();
1276
- const migrationHistory = Array.isArray(migrationList) && migrationList.length > 0 ? migrationList.map((y) => `${y.id}, ${y.name}, ${y.migratedAt.toUTCString()}`).join("\n") : "No migrations found";
1285
+ const migrationHistory = Array.isArray(migrationList) && migrationList.length > 0 ? migrationList.sort((a, b) => a.migratedAt.getTime() - b.migratedAt.getTime()).map((y) => `${y.id}, ${y.name}, ${y.migratedAt.toUTCString()}`).join("\n") : "No migrations found";
1277
1286
  console.info("Migrations history:\nid, name, migrated_at\n", migrationHistory);
1278
1287
  return {
1279
1288
  headers: { "Content-Type": ["application/json"] },
@@ -1282,7 +1291,12 @@ const applySchemaMigrations = async (migration) => {
1282
1291
  body: "Migrations successfully executed"
1283
1292
  };
1284
1293
  } catch (error) {
1285
- console.error("Error during migration:", error);
1294
+ try {
1295
+ console.error("Error during migration:", JSON.stringify(error));
1296
+ } catch (e) {
1297
+ console.trace("Error stringify:", e);
1298
+ console.error("Error during migration:", error);
1299
+ }
1286
1300
  return {
1287
1301
  headers: { "Content-Type": ["application/json"] },
1288
1302
  statusCode: 500,
@@ -1321,6 +1335,24 @@ function formatCreateTableStatement(statement) {
1321
1335
  function wrapWithForeignKeyChecks(statements) {
1322
1336
  return ["SET foreign_key_checks = 0", ...statements, "SET foreign_key_checks = 1"];
1323
1337
  }
1338
+ async function dropTableSchemaMigrations() {
1339
+ try {
1340
+ const tables = await getTables();
1341
+ const dropStatements = generateDropTableStatements(tables, { sequence: false, table: true });
1342
+ for (const statement of dropStatements) {
1343
+ console.warn(statement);
1344
+ await sql$1.sql.executeDDL(statement);
1345
+ }
1346
+ return getHttpResponse(
1347
+ 200,
1348
+ "⚠️ All data in these tables has been permanently deleted. This operation cannot be undone."
1349
+ );
1350
+ } catch (error) {
1351
+ console.error(error);
1352
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
1353
+ return getHttpResponse(500, errorMessage);
1354
+ }
1355
+ }
1324
1356
  const getHttpResponse = (statusCode, body) => {
1325
1357
  let statusText = "";
1326
1358
  if (statusCode === 200) {
@@ -1341,6 +1373,7 @@ exports.applyFromDriverTransform = applyFromDriverTransform;
1341
1373
  exports.applySchemaMigrations = applySchemaMigrations;
1342
1374
  exports.default = ForgeSQLORM;
1343
1375
  exports.dropSchemaMigrations = dropSchemaMigrations;
1376
+ exports.dropTableSchemaMigrations = dropTableSchemaMigrations;
1344
1377
  exports.fetchSchemaWebTrigger = fetchSchemaWebTrigger;
1345
1378
  exports.forgeDateString = forgeDateString;
1346
1379
  exports.forgeDateTimeString = forgeDateTimeString;