forge-sql-orm 2.0.9 → 2.0.10

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.
@@ -4,12 +4,28 @@ import { generateDropTableStatements as generateStatements } from "../utils/sqlU
4
4
  import { getHttpResponse, TriggerResponse } from "./index";
5
5
 
6
6
  /**
7
- * ⚠️ WARNING: This web trigger will permanently delete all data in the specified tables.
8
- * DO NOT use this in production environment as it will cause irreversible data loss.
7
+ * ⚠️ DEVELOPMENT ONLY WEB TRIGGER ⚠️
8
+ *
9
+ * This web trigger is designed for development environments only and will permanently delete all data in the specified tables.
10
+ * It generates and executes SQL statements to drop tables and their associated constraints.
11
+ *
12
+ * @warning This trigger should NEVER be used in production environments because:
13
+ * - It permanently deletes all data in the specified tables
14
+ * - The operation cannot be undone
15
+ * - It may affect application functionality
16
+ * - It could lead to data loss and system instability
9
17
  *
10
- * Generates SQL statements to drop tables and executes them
11
18
  * @param tables - Array of table schemas to drop
12
- * @returns Trigger response with execution status and list of dropped tables
19
+ * @returns {Promise<TriggerResponse<string>>} A response containing:
20
+ * - On success: 200 status with warning message about permanent deletion
21
+ * - On failure: 500 status with error message
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * // Example usage in development only
26
+ * await dropSchemaMigrations([users, orders]);
27
+ * // ⚠️ Warning: This will permanently delete all data in users and orders tables
28
+ * ```
13
29
  */
14
30
  export async function dropSchemaMigrations(
15
31
  tables: AnyMySqlTable[],
@@ -0,0 +1,96 @@
1
+ import { sql } from "@forge/sql";
2
+ import { getHttpResponse, TriggerResponse } from "./index";
3
+ import { forgeSystemTables } from "../core/SystemTables";
4
+ import { getTableName } from "drizzle-orm/table";
5
+
6
+ interface CreateTableRow {
7
+ Table: string;
8
+ "Create Table": string;
9
+ }
10
+
11
+ /**
12
+ * ⚠️ DEVELOPMENT ONLY WEB TRIGGER ⚠️
13
+ *
14
+ * This web trigger retrieves the current database schema from Atlassian Forge SQL.
15
+ * It generates SQL statements that can be used to recreate the database structure.
16
+ *
17
+ * @warning This trigger should ONLY be used in development environments. It:
18
+ * - Exposes your database structure
19
+ * - Disables foreign key checks temporarily
20
+ * - Generates SQL that could potentially be used maliciously
21
+ * - May expose sensitive table names and structures
22
+ *
23
+ * @returns {Promise<TriggerResponse<string>>} A response containing SQL statements to recreate the database schema
24
+ * - On success: Returns 200 status with SQL statements
25
+ * - On failure: Returns 500 status with error message
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * // The response will contain SQL statements like:
30
+ * // SET foreign_key_checks = 0;
31
+ * // CREATE TABLE IF NOT EXISTS users (...);
32
+ * // CREATE TABLE IF NOT EXISTS orders (...);
33
+ * // SET foreign_key_checks = 1;
34
+ * ```
35
+ */
36
+ export async function fetchSchemaWebTrigger(): Promise<TriggerResponse<string>> {
37
+ try {
38
+ const tables = await getTables();
39
+ const createTableStatements = await generateCreateTableStatements(tables);
40
+ const sqlStatements = wrapWithForeignKeyChecks(createTableStatements);
41
+
42
+ return getHttpResponse<string>(200, sqlStatements.join(";\n"));
43
+ } catch (error: unknown) {
44
+ console.error(JSON.stringify(error));
45
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
46
+ return getHttpResponse<string>(500, errorMessage);
47
+ }
48
+ }
49
+
50
+ /**
51
+ * Retrieves all tables from the database
52
+ */
53
+ async function getTables(): Promise<string[]> {
54
+ const tables = await sql.executeDDL<string>("SHOW TABLES");
55
+ return tables.rows.flatMap((tableInfo) => Object.values(tableInfo));
56
+ }
57
+
58
+ /**
59
+ * Generates CREATE TABLE statements for each table
60
+ */
61
+ async function generateCreateTableStatements(tables: string[]): Promise<string[]> {
62
+ const statements: string[] = [];
63
+
64
+ for (const table of tables) {
65
+ const createTableResult = await sql.executeDDL<CreateTableRow>(`SHOW CREATE TABLE ${table}`);
66
+
67
+ const createTableStatements = createTableResult.rows
68
+ .filter((row) => !isSystemTable(row.Table))
69
+ .map((row) => formatCreateTableStatement(row["Create Table"]));
70
+
71
+ statements.push(...createTableStatements);
72
+ }
73
+
74
+ return statements;
75
+ }
76
+
77
+ /**
78
+ * Checks if the table is a system table
79
+ */
80
+ function isSystemTable(tableName: string): boolean {
81
+ return forgeSystemTables.some((st) => getTableName(st) === tableName);
82
+ }
83
+
84
+ /**
85
+ * Formats the CREATE TABLE statement
86
+ */
87
+ function formatCreateTableStatement(statement: string): string {
88
+ return statement.replace(/"/g, "").replace("CREATE TABLE", "CREATE TABLE IF NOT EXISTS");
89
+ }
90
+
91
+ /**
92
+ * Wraps the SQL statements with foreign key check controls
93
+ */
94
+ function wrapWithForeignKeyChecks(statements: string[]): string[] {
95
+ return ["SET foreign_key_checks = 0", ...statements, "SET foreign_key_checks = 1"];
96
+ }
@@ -1,5 +1,6 @@
1
1
  export * from "./dropMigrationWebTrigger";
2
2
  export * from "./applyMigrationsWebTrigger";
3
+ export * from "./fetchSchemaWebTrigger";
3
4
 
4
5
  export interface TriggerResponse<BODY> {
5
6
  body?: BODY;