@fragno-dev/db 0.0.1 → 0.1.0

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.
Files changed (200) hide show
  1. package/.turbo/turbo-build.log +137 -13
  2. package/.turbo/turbo-test.log +36 -0
  3. package/CHANGELOG.md +7 -0
  4. package/dist/adapters/adapters.d.ts +18 -0
  5. package/dist/adapters/adapters.d.ts.map +1 -0
  6. package/dist/adapters/drizzle/drizzle-adapter.d.ts +21 -0
  7. package/dist/adapters/drizzle/drizzle-adapter.d.ts.map +1 -0
  8. package/dist/adapters/drizzle/drizzle-adapter.js +62 -0
  9. package/dist/adapters/drizzle/drizzle-adapter.js.map +1 -0
  10. package/dist/adapters/drizzle/drizzle-query.d.ts +17 -0
  11. package/dist/adapters/drizzle/drizzle-query.d.ts.map +1 -0
  12. package/dist/adapters/drizzle/drizzle-query.js +139 -0
  13. package/dist/adapters/drizzle/drizzle-query.js.map +1 -0
  14. package/dist/adapters/drizzle/drizzle-uow-compiler.d.ts +9 -0
  15. package/dist/adapters/drizzle/drizzle-uow-compiler.d.ts.map +1 -0
  16. package/dist/adapters/drizzle/drizzle-uow-compiler.js +300 -0
  17. package/dist/adapters/drizzle/drizzle-uow-compiler.js.map +1 -0
  18. package/dist/adapters/drizzle/drizzle-uow-decoder.js +82 -0
  19. package/dist/adapters/drizzle/drizzle-uow-decoder.js.map +1 -0
  20. package/dist/adapters/drizzle/drizzle-uow-executor.js +125 -0
  21. package/dist/adapters/drizzle/drizzle-uow-executor.js.map +1 -0
  22. package/dist/adapters/drizzle/generate.js +273 -0
  23. package/dist/adapters/drizzle/generate.js.map +1 -0
  24. package/dist/adapters/drizzle/join-column-utils.js +28 -0
  25. package/dist/adapters/drizzle/join-column-utils.js.map +1 -0
  26. package/dist/adapters/drizzle/shared.js +11 -0
  27. package/dist/adapters/drizzle/shared.js.map +1 -0
  28. package/dist/adapters/kysely/kysely-adapter.d.ts +23 -0
  29. package/dist/adapters/kysely/kysely-adapter.d.ts.map +1 -0
  30. package/dist/adapters/kysely/kysely-adapter.js +119 -0
  31. package/dist/adapters/kysely/kysely-adapter.js.map +1 -0
  32. package/dist/adapters/kysely/kysely-query-builder.js +306 -0
  33. package/dist/adapters/kysely/kysely-query-builder.js.map +1 -0
  34. package/dist/adapters/kysely/kysely-query-compiler.js +67 -0
  35. package/dist/adapters/kysely/kysely-query-compiler.js.map +1 -0
  36. package/dist/adapters/kysely/kysely-query.js +158 -0
  37. package/dist/adapters/kysely/kysely-query.js.map +1 -0
  38. package/dist/adapters/kysely/kysely-uow-compiler.js +139 -0
  39. package/dist/adapters/kysely/kysely-uow-compiler.js.map +1 -0
  40. package/dist/adapters/kysely/kysely-uow-executor.js +89 -0
  41. package/dist/adapters/kysely/kysely-uow-executor.js.map +1 -0
  42. package/dist/adapters/kysely/migration/execute.js +176 -0
  43. package/dist/adapters/kysely/migration/execute.js.map +1 -0
  44. package/dist/fragment.d.ts +54 -0
  45. package/dist/fragment.d.ts.map +1 -0
  46. package/dist/fragment.js +92 -0
  47. package/dist/fragment.js.map +1 -0
  48. package/dist/id.d.ts +2 -0
  49. package/dist/migration-engine/auto-from-schema.js +116 -0
  50. package/dist/migration-engine/auto-from-schema.js.map +1 -0
  51. package/dist/migration-engine/create.d.ts +41 -0
  52. package/dist/migration-engine/create.d.ts.map +1 -0
  53. package/dist/migration-engine/create.js +58 -0
  54. package/dist/migration-engine/create.js.map +1 -0
  55. package/dist/migration-engine/shared.d.ts +90 -0
  56. package/dist/migration-engine/shared.d.ts.map +1 -0
  57. package/dist/migration-engine/shared.js +8 -0
  58. package/dist/migration-engine/shared.js.map +1 -0
  59. package/dist/mod.d.ts +55 -2
  60. package/dist/mod.d.ts.map +1 -1
  61. package/dist/mod.js +111 -2
  62. package/dist/mod.js.map +1 -1
  63. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/column-builder.js +108 -0
  64. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/column-builder.js.map +1 -0
  65. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/column.js +55 -0
  66. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/column.js.map +1 -0
  67. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/entity.js +18 -0
  68. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/entity.js.map +1 -0
  69. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/pg-core/columns/common.js +183 -0
  70. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/pg-core/columns/common.js.map +1 -0
  71. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/pg-core/columns/enum.js +58 -0
  72. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/pg-core/columns/enum.js.map +1 -0
  73. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/pg-core/foreign-keys.js +68 -0
  74. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/pg-core/foreign-keys.js.map +1 -0
  75. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/pg-core/unique-constraint.js +56 -0
  76. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/pg-core/unique-constraint.js.map +1 -0
  77. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/pg-core/utils/array.js +65 -0
  78. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/pg-core/utils/array.js.map +1 -0
  79. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/sql/expressions/conditions.js +81 -0
  80. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/sql/expressions/conditions.js.map +1 -0
  81. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/sql/expressions/select.js +13 -0
  82. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/sql/expressions/select.js.map +1 -0
  83. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/sql/functions/aggregate.js +10 -0
  84. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/sql/functions/aggregate.js.map +1 -0
  85. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/sql/sql.js +372 -0
  86. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/sql/sql.js.map +1 -0
  87. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/subquery.js +23 -0
  88. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/subquery.js.map +1 -0
  89. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/table.js +62 -0
  90. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/table.js.map +1 -0
  91. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/table.utils.js +6 -0
  92. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/table.utils.js.map +1 -0
  93. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/tracing-utils.js +8 -0
  94. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/tracing-utils.js.map +1 -0
  95. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/tracing.js +8 -0
  96. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/tracing.js.map +1 -0
  97. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/view-common.js +6 -0
  98. package/dist/node_modules/.bun/drizzle-orm@0.44.6_4fae081eecb963e2/node_modules/drizzle-orm/view-common.js.map +1 -0
  99. package/dist/query/condition-builder.d.ts +41 -0
  100. package/dist/query/condition-builder.d.ts.map +1 -0
  101. package/dist/query/condition-builder.js +93 -0
  102. package/dist/query/condition-builder.js.map +1 -0
  103. package/dist/query/cursor.d.ts +88 -0
  104. package/dist/query/cursor.d.ts.map +1 -0
  105. package/dist/query/cursor.js +103 -0
  106. package/dist/query/cursor.js.map +1 -0
  107. package/dist/query/orm/orm.d.ts +18 -0
  108. package/dist/query/orm/orm.d.ts.map +1 -0
  109. package/dist/query/orm/orm.js +48 -0
  110. package/dist/query/orm/orm.js.map +1 -0
  111. package/dist/query/query.d.ts +79 -0
  112. package/dist/query/query.d.ts.map +1 -0
  113. package/dist/query/query.js +1 -0
  114. package/dist/query/result-transform.js +155 -0
  115. package/dist/query/result-transform.js.map +1 -0
  116. package/dist/query/unit-of-work.d.ts +435 -0
  117. package/dist/query/unit-of-work.d.ts.map +1 -0
  118. package/dist/query/unit-of-work.js +549 -0
  119. package/dist/query/unit-of-work.js.map +1 -0
  120. package/dist/schema/create.d.ts +273 -116
  121. package/dist/schema/create.d.ts.map +1 -1
  122. package/dist/schema/create.js +410 -222
  123. package/dist/schema/create.js.map +1 -1
  124. package/dist/schema/serialize.js +101 -0
  125. package/dist/schema/serialize.js.map +1 -0
  126. package/dist/schema-generator/schema-generator.d.ts +15 -0
  127. package/dist/schema-generator/schema-generator.d.ts.map +1 -0
  128. package/dist/shared/providers.d.ts +6 -0
  129. package/dist/shared/providers.d.ts.map +1 -0
  130. package/dist/util/import-generator.js +26 -0
  131. package/dist/util/import-generator.js.map +1 -0
  132. package/dist/util/parse.js +15 -0
  133. package/dist/util/parse.js.map +1 -0
  134. package/dist/util/types.d.ts +8 -0
  135. package/dist/util/types.d.ts.map +1 -0
  136. package/package.json +63 -2
  137. package/src/adapters/adapters.ts +22 -0
  138. package/src/adapters/drizzle/drizzle-adapter-pglite.test.ts +433 -0
  139. package/src/adapters/drizzle/drizzle-adapter.test.ts +122 -0
  140. package/src/adapters/drizzle/drizzle-adapter.ts +118 -0
  141. package/src/adapters/drizzle/drizzle-query.ts +234 -0
  142. package/src/adapters/drizzle/drizzle-uow-compiler.test.ts +1084 -0
  143. package/src/adapters/drizzle/drizzle-uow-compiler.ts +546 -0
  144. package/src/adapters/drizzle/drizzle-uow-decoder.ts +165 -0
  145. package/src/adapters/drizzle/drizzle-uow-executor.ts +213 -0
  146. package/src/adapters/drizzle/generate.test.ts +643 -0
  147. package/src/adapters/drizzle/generate.ts +481 -0
  148. package/src/adapters/drizzle/join-column-utils.test.ts +79 -0
  149. package/src/adapters/drizzle/join-column-utils.ts +39 -0
  150. package/src/adapters/drizzle/migrate-drizzle.test.ts +226 -0
  151. package/src/adapters/drizzle/shared.ts +22 -0
  152. package/src/adapters/drizzle/test-utils.ts +56 -0
  153. package/src/adapters/kysely/kysely-adapter-pglite.test.ts +789 -0
  154. package/src/adapters/kysely/kysely-adapter.ts +196 -0
  155. package/src/adapters/kysely/kysely-query-builder.test.ts +1344 -0
  156. package/src/adapters/kysely/kysely-query-builder.ts +611 -0
  157. package/src/adapters/kysely/kysely-query-compiler.ts +124 -0
  158. package/src/adapters/kysely/kysely-query.ts +254 -0
  159. package/src/adapters/kysely/kysely-uow-compiler.test.ts +916 -0
  160. package/src/adapters/kysely/kysely-uow-compiler.ts +271 -0
  161. package/src/adapters/kysely/kysely-uow-executor.ts +149 -0
  162. package/src/adapters/kysely/kysely-uow-joins.test.ts +811 -0
  163. package/src/adapters/kysely/migration/execute-mysql.test.ts +1173 -0
  164. package/src/adapters/kysely/migration/execute-postgres.test.ts +2657 -0
  165. package/src/adapters/kysely/migration/execute.ts +382 -0
  166. package/src/adapters/kysely/migration/kysely-migrator.test.ts +197 -0
  167. package/src/fragment.test.ts +287 -0
  168. package/src/fragment.ts +198 -0
  169. package/src/migration-engine/auto-from-schema.test.ts +118 -58
  170. package/src/migration-engine/auto-from-schema.ts +103 -32
  171. package/src/migration-engine/create.test.ts +34 -46
  172. package/src/migration-engine/create.ts +41 -26
  173. package/src/migration-engine/shared.ts +26 -6
  174. package/src/mod.ts +197 -1
  175. package/src/query/condition-builder.test.ts +379 -0
  176. package/src/query/condition-builder.ts +294 -0
  177. package/src/query/cursor.test.ts +296 -0
  178. package/src/query/cursor.ts +147 -0
  179. package/src/query/orm/orm.ts +92 -0
  180. package/src/query/query-type.test.ts +429 -0
  181. package/src/query/query.ts +200 -0
  182. package/src/query/result-transform.test.ts +795 -0
  183. package/src/query/result-transform.ts +247 -0
  184. package/src/query/unit-of-work-types.test.ts +192 -0
  185. package/src/query/unit-of-work.test.ts +947 -0
  186. package/src/query/unit-of-work.ts +1199 -0
  187. package/src/schema/create.test.ts +653 -110
  188. package/src/schema/create.ts +708 -337
  189. package/src/schema/serialize.test.ts +559 -0
  190. package/src/schema/serialize.ts +359 -0
  191. package/src/schema-generator/schema-generator.ts +12 -0
  192. package/src/shared/config.ts +0 -8
  193. package/src/util/import-generator.ts +28 -0
  194. package/src/util/parse.ts +16 -0
  195. package/src/util/types.ts +4 -0
  196. package/tsconfig.json +1 -1
  197. package/tsdown.config.ts +11 -1
  198. package/vitest.config.ts +3 -0
  199. /package/dist/{cuid.js → id.js} +0 -0
  200. /package/src/{cuid.ts → id.ts} +0 -0
@@ -0,0 +1,1173 @@
1
+ import { Kysely, MysqlDialect } from "kysely";
2
+ import { describe, expect, it, beforeAll } from "vitest";
3
+ import { execute } from "./execute";
4
+ import type { MigrationOperation } from "../../../migration-engine/shared";
5
+ import type { KyselyConfig } from "../kysely-adapter";
6
+
7
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
+ type KyselyAny = Kysely<any>;
9
+
10
+ function assertSingleResult<T>(result: T | T[]): asserts result is T {
11
+ if (Array.isArray(result)) {
12
+ throw new Error("Expected single result, got array");
13
+ }
14
+ }
15
+
16
+ describe("execute() - MySQL", () => {
17
+ let db: KyselyAny;
18
+ let config: KyselyConfig;
19
+
20
+ beforeAll(async () => {
21
+ // Create a Kysely instance with a MysqlDialect, but not actually connected to a database
22
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
23
+ db = new Kysely({ dialect: new MysqlDialect({} as any) });
24
+ config = { db, provider: "mysql" };
25
+ });
26
+
27
+ describe("create-table", () => {
28
+ it("should generate SQL for simple table with columns", () => {
29
+ const operation: MigrationOperation = {
30
+ type: "create-table",
31
+ name: "users",
32
+ columns: [
33
+ {
34
+ name: "id",
35
+ type: "integer",
36
+ isNullable: false,
37
+ role: "external-id",
38
+ },
39
+ {
40
+ name: "name",
41
+ type: "string",
42
+ isNullable: false,
43
+ role: "regular",
44
+ },
45
+ {
46
+ name: "email",
47
+ type: "string",
48
+ isNullable: false,
49
+ role: "regular",
50
+ },
51
+ ],
52
+ };
53
+
54
+ const result = execute(operation, config, () => {
55
+ throw new Error("No custom operations");
56
+ });
57
+
58
+ assertSingleResult(result);
59
+
60
+ const compiled = result.compile();
61
+ expect(compiled.sql).toMatchInlineSnapshot(
62
+ `"create table \`users\` (\`id\` integer not null unique, \`name\` text not null, \`email\` text not null)"`,
63
+ );
64
+ });
65
+
66
+ it("should generate SQL for table with various column types", () => {
67
+ const operation: MigrationOperation = {
68
+ type: "create-table",
69
+ name: "test_types",
70
+ columns: [
71
+ { name: "col_int", type: "integer", isNullable: false, role: "external-id" },
72
+ { name: "col_bigint", type: "bigint", isNullable: false, role: "regular" },
73
+ { name: "col_decimal", type: "decimal", isNullable: false, role: "regular" },
74
+ { name: "col_bool", type: "bool", isNullable: false, role: "regular" },
75
+ { name: "col_date", type: "date", isNullable: false, role: "regular" },
76
+ { name: "col_timestamp", type: "timestamp", isNullable: false, role: "regular" },
77
+ { name: "col_json", type: "json", isNullable: false, role: "regular" },
78
+ { name: "col_binary", type: "binary", isNullable: false, role: "regular" },
79
+ { name: "col_varchar", type: "varchar(255)", isNullable: false, role: "regular" },
80
+ ],
81
+ };
82
+
83
+ const result = execute(operation, config, () => {
84
+ throw new Error("No custom operations");
85
+ });
86
+
87
+ assertSingleResult(result);
88
+
89
+ const compiled = result.compile();
90
+ expect(compiled.sql).toMatchInlineSnapshot(
91
+ `"create table \`test_types\` (\`col_int\` integer not null unique, \`col_bigint\` bigint not null, \`col_decimal\` decimal not null, \`col_bool\` boolean not null, \`col_date\` date not null, \`col_timestamp\` timestamp not null, \`col_json\` json not null, \`col_binary\` longblob not null, \`col_varchar\` varchar(255) not null)"`,
92
+ );
93
+ });
94
+
95
+ it("should generate SQL for table with nullable columns", () => {
96
+ const operation: MigrationOperation = {
97
+ type: "create-table",
98
+ name: "nullable_test",
99
+ columns: [
100
+ { name: "id", type: "integer", isNullable: false, role: "external-id" },
101
+ { name: "optional_name", type: "string", isNullable: true, role: "regular" },
102
+ { name: "optional_age", type: "integer", isNullable: true, role: "regular" },
103
+ ],
104
+ };
105
+
106
+ const result = execute(operation, config, () => {
107
+ throw new Error("No custom operations");
108
+ });
109
+
110
+ assertSingleResult(result);
111
+
112
+ const compiled = result.compile();
113
+ expect(compiled.sql).toMatchInlineSnapshot(
114
+ `"create table \`nullable_test\` (\`id\` integer not null unique, \`optional_name\` text, \`optional_age\` integer)"`,
115
+ );
116
+ });
117
+
118
+ it("should generate SQL for table with default values", () => {
119
+ const operation: MigrationOperation = {
120
+ type: "create-table",
121
+ name: "defaults_test",
122
+ columns: [
123
+ { name: "id", type: "integer", isNullable: false, role: "external-id" },
124
+ {
125
+ name: "status",
126
+ type: "string",
127
+ isNullable: false,
128
+ role: "regular",
129
+ default: { value: "pending" },
130
+ },
131
+ {
132
+ name: "count",
133
+ type: "integer",
134
+ isNullable: false,
135
+ role: "regular",
136
+ default: { value: 0 },
137
+ },
138
+ {
139
+ name: "is_active",
140
+ type: "bool",
141
+ isNullable: false,
142
+ role: "regular",
143
+ default: { value: true },
144
+ },
145
+ ],
146
+ };
147
+
148
+ const result = execute(operation, config, () => {
149
+ throw new Error("No custom operations");
150
+ });
151
+
152
+ assertSingleResult(result);
153
+
154
+ const compiled = result.compile();
155
+ expect(compiled.sql).toMatchInlineSnapshot(
156
+ `"create table \`defaults_test\` (\`id\` integer not null unique, \`status\` text not null, \`count\` integer default 0 not null, \`is_active\` boolean default true not null)"`,
157
+ );
158
+ });
159
+
160
+ it("should generate SQL for table with timestamp default to CURRENT_TIMESTAMP", () => {
161
+ const operation: MigrationOperation = {
162
+ type: "create-table",
163
+ name: "timestamps_test",
164
+ columns: [
165
+ { name: "id", type: "integer", isNullable: false, role: "external-id" },
166
+ {
167
+ name: "created_at",
168
+ type: "timestamp",
169
+ isNullable: false,
170
+ role: "regular",
171
+ default: { runtime: "now" },
172
+ },
173
+ ],
174
+ };
175
+
176
+ const result = execute(operation, config, () => {
177
+ throw new Error("No custom operations");
178
+ });
179
+
180
+ assertSingleResult(result);
181
+
182
+ const compiled = result.compile();
183
+ expect(compiled.sql).toMatchInlineSnapshot(
184
+ `"create table \`timestamps_test\` (\`id\` integer not null unique, \`created_at\` timestamp default CURRENT_TIMESTAMP not null)"`,
185
+ );
186
+ });
187
+
188
+ it("should generate SQL for table with reference column", () => {
189
+ const operation: MigrationOperation = {
190
+ type: "create-table",
191
+ name: "posts",
192
+ columns: [
193
+ { name: "id", type: "integer", isNullable: false, role: "external-id" },
194
+ { name: "user_id", type: "integer", isNullable: false, role: "reference" },
195
+ { name: "title", type: "string", isNullable: false, role: "regular" },
196
+ ],
197
+ };
198
+
199
+ const result = execute(operation, config, () => {
200
+ throw new Error("No custom operations");
201
+ });
202
+
203
+ assertSingleResult(result);
204
+
205
+ const compiled = result.compile();
206
+ expect(compiled.sql).toMatchInlineSnapshot(
207
+ `"create table \`posts\` (\`id\` integer not null unique, \`user_id\` integer not null, \`title\` text not null)"`,
208
+ );
209
+ });
210
+ });
211
+
212
+ describe("rename-table", () => {
213
+ it("should generate SQL for MySQL rename", () => {
214
+ const operation: MigrationOperation = {
215
+ type: "rename-table",
216
+ from: "old_name",
217
+ to: "new_name",
218
+ };
219
+
220
+ const result = execute(operation, config, () => {
221
+ throw new Error("No custom operations");
222
+ });
223
+
224
+ assertSingleResult(result);
225
+
226
+ const compiled = result.compile();
227
+ expect(compiled.sql).toMatchInlineSnapshot(
228
+ `"alter table \`old_name\` rename to \`new_name\`"`,
229
+ );
230
+ });
231
+ });
232
+
233
+ describe("drop-table", () => {
234
+ it("should generate SQL to drop table", () => {
235
+ const operation: MigrationOperation = {
236
+ type: "drop-table",
237
+ name: "to_drop",
238
+ };
239
+
240
+ const result = execute(operation, config, () => {
241
+ throw new Error("No custom operations");
242
+ });
243
+
244
+ assertSingleResult(result);
245
+
246
+ const compiled = result.compile();
247
+ expect(compiled.sql).toMatchInlineSnapshot(`"drop table \`to_drop\`"`);
248
+ });
249
+ });
250
+
251
+ describe("alter-table - create-column", () => {
252
+ it("should generate SQL to add a new column", () => {
253
+ const operation: MigrationOperation = {
254
+ type: "alter-table",
255
+ name: "test_table",
256
+ value: [
257
+ {
258
+ type: "create-column",
259
+ value: {
260
+ name: "new_column",
261
+ type: "string",
262
+ isNullable: true,
263
+ role: "regular",
264
+ },
265
+ },
266
+ ],
267
+ };
268
+
269
+ const results = execute(operation, config, () => {
270
+ throw new Error("No custom operations");
271
+ });
272
+
273
+ if (!Array.isArray(results)) {
274
+ throw new Error("Expected array of results");
275
+ }
276
+
277
+ expect(results).toHaveLength(1);
278
+ const compiled = results[0].compile();
279
+ expect(compiled.sql).toMatchInlineSnapshot(
280
+ `"alter table \`test_table\` add column \`new_column\` text"`,
281
+ );
282
+ });
283
+
284
+ it("should generate SQL for multiple columns", () => {
285
+ const operation: MigrationOperation = {
286
+ type: "alter-table",
287
+ name: "test_table",
288
+ value: [
289
+ {
290
+ type: "create-column",
291
+ value: {
292
+ name: "col1",
293
+ type: "string",
294
+ isNullable: true,
295
+ role: "regular",
296
+ },
297
+ },
298
+ {
299
+ type: "create-column",
300
+ value: {
301
+ name: "col2",
302
+ type: "integer",
303
+ isNullable: false,
304
+ role: "regular",
305
+ default: { value: 0 },
306
+ },
307
+ },
308
+ ],
309
+ };
310
+
311
+ const results = execute(operation, config, () => {
312
+ throw new Error("No custom operations");
313
+ });
314
+
315
+ if (!Array.isArray(results)) {
316
+ throw new Error("Expected array of results");
317
+ }
318
+
319
+ expect(results).toHaveLength(2);
320
+ expect(results[0].compile().sql).toMatchInlineSnapshot(
321
+ `"alter table \`test_table\` add column \`col1\` text"`,
322
+ );
323
+ expect(results[1].compile().sql).toMatchInlineSnapshot(
324
+ `"alter table \`test_table\` add column \`col2\` integer default 0 not null"`,
325
+ );
326
+ });
327
+ });
328
+
329
+ describe("alter-table - rename-column", () => {
330
+ it("should generate SQL to rename a column", () => {
331
+ const operation: MigrationOperation = {
332
+ type: "alter-table",
333
+ name: "test_table",
334
+ value: [
335
+ {
336
+ type: "rename-column",
337
+ from: "old_name",
338
+ to: "new_name",
339
+ },
340
+ ],
341
+ };
342
+
343
+ const results = execute(operation, config, () => {
344
+ throw new Error("No custom operations");
345
+ });
346
+
347
+ if (!Array.isArray(results)) {
348
+ throw new Error("Expected array of results");
349
+ }
350
+
351
+ expect(results).toHaveLength(1);
352
+ const compiled = results[0].compile();
353
+ expect(compiled.sql).toMatchInlineSnapshot(
354
+ `"alter table \`test_table\` rename column \`old_name\` to \`new_name\`"`,
355
+ );
356
+ });
357
+ });
358
+
359
+ describe("alter-table - drop-column", () => {
360
+ it("should generate SQL to drop a column", () => {
361
+ const operation: MigrationOperation = {
362
+ type: "alter-table",
363
+ name: "test_table",
364
+ value: [
365
+ {
366
+ type: "drop-column",
367
+ name: "to_drop",
368
+ },
369
+ ],
370
+ };
371
+
372
+ const results = execute(operation, config, () => {
373
+ throw new Error("No custom operations");
374
+ });
375
+
376
+ if (!Array.isArray(results)) {
377
+ throw new Error("Expected array of results");
378
+ }
379
+
380
+ expect(results).toHaveLength(1);
381
+ const compiled = results[0].compile();
382
+ expect(compiled.sql).toMatchInlineSnapshot(
383
+ `"alter table \`test_table\` drop column \`to_drop\`"`,
384
+ );
385
+ });
386
+ });
387
+
388
+ describe("alter-table - update-column", () => {
389
+ it("should generate SQL to update column data type (MySQL doesn't use USING clause)", () => {
390
+ const operation: MigrationOperation = {
391
+ type: "alter-table",
392
+ name: "test_table",
393
+ value: [
394
+ {
395
+ type: "update-column",
396
+ name: "test_col",
397
+ value: {
398
+ name: "test_col",
399
+ type: "integer",
400
+ isNullable: true,
401
+ role: "regular",
402
+ },
403
+ updateDataType: true,
404
+ updateNullable: false,
405
+ updateDefault: false,
406
+ },
407
+ ],
408
+ };
409
+
410
+ const results = execute(operation, config, () => {
411
+ throw new Error("No custom operations");
412
+ });
413
+
414
+ if (!Array.isArray(results)) {
415
+ throw new Error("Expected array of results");
416
+ }
417
+
418
+ expect(results).toHaveLength(1);
419
+ const compiled = results[0].compile();
420
+ expect(compiled.sql).toMatchInlineSnapshot(
421
+ `"alter table \`test_table\` modify column \`test_col\` integer"`,
422
+ );
423
+ });
424
+
425
+ it("should generate SQL to set column NOT NULL", () => {
426
+ const operation: MigrationOperation = {
427
+ type: "alter-table",
428
+ name: "test_table",
429
+ value: [
430
+ {
431
+ type: "update-column",
432
+ name: "test_col",
433
+ value: {
434
+ name: "test_col",
435
+ type: "string",
436
+ isNullable: false,
437
+ role: "regular",
438
+ },
439
+ updateDataType: false,
440
+ updateNullable: true,
441
+ updateDefault: false,
442
+ },
443
+ ],
444
+ };
445
+
446
+ const results = execute(operation, config, () => {
447
+ throw new Error("No custom operations");
448
+ });
449
+
450
+ if (!Array.isArray(results)) {
451
+ throw new Error("Expected array of results");
452
+ }
453
+
454
+ expect(results).toHaveLength(1);
455
+ const compiled = results[0].compile();
456
+ expect(compiled.sql).toMatchInlineSnapshot(
457
+ `"alter table \`test_table\` modify column \`test_col\` text not null"`,
458
+ );
459
+ });
460
+
461
+ it("should generate SQL to drop NOT NULL constraint", () => {
462
+ const operation: MigrationOperation = {
463
+ type: "alter-table",
464
+ name: "test_table",
465
+ value: [
466
+ {
467
+ type: "update-column",
468
+ name: "test_col",
469
+ value: {
470
+ name: "test_col",
471
+ type: "string",
472
+ isNullable: true,
473
+ role: "regular",
474
+ },
475
+ updateDataType: false,
476
+ updateNullable: true,
477
+ updateDefault: false,
478
+ },
479
+ ],
480
+ };
481
+
482
+ const results = execute(operation, config, () => {
483
+ throw new Error("No custom operations");
484
+ });
485
+
486
+ if (!Array.isArray(results)) {
487
+ throw new Error("Expected array of results");
488
+ }
489
+
490
+ expect(results).toHaveLength(1);
491
+ const compiled = results[0].compile();
492
+ expect(compiled.sql).toMatchInlineSnapshot(
493
+ `"alter table \`test_table\` modify column \`test_col\` text"`,
494
+ );
495
+ });
496
+
497
+ it("should generate SQL to set default value", () => {
498
+ const operation: MigrationOperation = {
499
+ type: "alter-table",
500
+ name: "test_table",
501
+ value: [
502
+ {
503
+ type: "update-column",
504
+ name: "test_col",
505
+ value: {
506
+ name: "test_col",
507
+ type: "string",
508
+ isNullable: false,
509
+ role: "regular",
510
+ default: { value: "default_value" },
511
+ },
512
+ updateDataType: false,
513
+ updateNullable: false,
514
+ updateDefault: true,
515
+ },
516
+ ],
517
+ };
518
+
519
+ const results = execute(operation, config, () => {
520
+ throw new Error("No custom operations");
521
+ });
522
+
523
+ if (!Array.isArray(results)) {
524
+ throw new Error("Expected array of results");
525
+ }
526
+
527
+ expect(results).toHaveLength(1);
528
+ const compiled = results[0].compile();
529
+ expect(compiled.sql).toMatchInlineSnapshot(
530
+ `"alter table \`test_table\` modify column \`test_col\` text not null"`,
531
+ );
532
+ });
533
+
534
+ it("should generate SQL to drop default value", () => {
535
+ const operation: MigrationOperation = {
536
+ type: "alter-table",
537
+ name: "test_table",
538
+ value: [
539
+ {
540
+ type: "update-column",
541
+ name: "test_col",
542
+ value: {
543
+ name: "test_col",
544
+ type: "string",
545
+ isNullable: true,
546
+ role: "regular",
547
+ },
548
+ updateDataType: false,
549
+ updateNullable: false,
550
+ updateDefault: true,
551
+ },
552
+ ],
553
+ };
554
+
555
+ const results = execute(operation, config, () => {
556
+ throw new Error("No custom operations");
557
+ });
558
+
559
+ if (!Array.isArray(results)) {
560
+ throw new Error("Expected array of results");
561
+ }
562
+
563
+ expect(results).toHaveLength(1);
564
+ const compiled = results[0].compile();
565
+ expect(compiled.sql).toMatchInlineSnapshot(
566
+ `"alter table \`test_table\` modify column \`test_col\` text"`,
567
+ );
568
+ });
569
+
570
+ it("should generate SQL to update multiple properties at once", () => {
571
+ const operation: MigrationOperation = {
572
+ type: "alter-table",
573
+ name: "test_table",
574
+ value: [
575
+ {
576
+ type: "update-column",
577
+ name: "test_col",
578
+ value: {
579
+ name: "test_col",
580
+ type: "integer",
581
+ isNullable: false,
582
+ role: "regular",
583
+ default: { value: 0 },
584
+ },
585
+ updateDataType: true,
586
+ updateNullable: true,
587
+ updateDefault: true,
588
+ },
589
+ ],
590
+ };
591
+
592
+ const results = execute(operation, config, () => {
593
+ throw new Error("No custom operations");
594
+ });
595
+
596
+ if (!Array.isArray(results)) {
597
+ throw new Error("Expected array of results");
598
+ }
599
+
600
+ expect(results).toHaveLength(1);
601
+ expect(results[0].compile().sql).toMatchInlineSnapshot(
602
+ `"alter table \`test_table\` modify column \`test_col\` integer default 0 not null"`,
603
+ );
604
+ });
605
+
606
+ it("should throw error when trying to update ID column", () => {
607
+ const operation: MigrationOperation = {
608
+ type: "alter-table",
609
+ name: "test_table",
610
+ value: [
611
+ {
612
+ type: "update-column",
613
+ name: "id",
614
+ value: {
615
+ name: "id",
616
+ type: "bigint",
617
+ isNullable: false,
618
+ role: "external-id",
619
+ },
620
+ updateDataType: true,
621
+ updateNullable: false,
622
+ updateDefault: false,
623
+ },
624
+ ],
625
+ };
626
+
627
+ expect(() => {
628
+ execute(operation, config, () => {
629
+ throw new Error("No custom operations");
630
+ });
631
+ }).toThrow(
632
+ "ID columns cannot be updated. Not every database supports updating primary keys and often requires workarounds.",
633
+ );
634
+ });
635
+
636
+ it("should handle no-op update-column (no flags set)", () => {
637
+ const operation: MigrationOperation = {
638
+ type: "alter-table",
639
+ name: "test_table",
640
+ value: [
641
+ {
642
+ type: "update-column",
643
+ name: "test_col",
644
+ value: {
645
+ name: "test_col",
646
+ type: "string",
647
+ isNullable: true,
648
+ role: "regular",
649
+ },
650
+ updateDataType: false,
651
+ updateNullable: false,
652
+ updateDefault: false,
653
+ },
654
+ ],
655
+ };
656
+
657
+ const results = execute(operation, config, () => {
658
+ throw new Error("No custom operations");
659
+ });
660
+
661
+ if (!Array.isArray(results)) {
662
+ throw new Error("Expected array of results");
663
+ }
664
+
665
+ // No-op should return empty array
666
+ expect(results).toHaveLength(0);
667
+ });
668
+
669
+ it("should generate SQL for timestamp default", () => {
670
+ const operation: MigrationOperation = {
671
+ type: "alter-table",
672
+ name: "test_table",
673
+ value: [
674
+ {
675
+ type: "update-column",
676
+ name: "updated_at",
677
+ value: {
678
+ name: "updated_at",
679
+ type: "timestamp",
680
+ isNullable: false,
681
+ role: "regular",
682
+ default: { runtime: "now" },
683
+ },
684
+ updateDataType: false,
685
+ updateNullable: false,
686
+ updateDefault: true,
687
+ },
688
+ ],
689
+ };
690
+
691
+ const results = execute(operation, config, () => {
692
+ throw new Error("No custom operations");
693
+ });
694
+
695
+ if (!Array.isArray(results)) {
696
+ throw new Error("Expected array of results");
697
+ }
698
+
699
+ expect(results).toHaveLength(1);
700
+ const compiled = results[0].compile();
701
+ expect(compiled.sql).toMatchInlineSnapshot(
702
+ `"alter table \`test_table\` modify column \`updated_at\` timestamp default CURRENT_TIMESTAMP not null"`,
703
+ );
704
+ });
705
+ });
706
+
707
+ describe("add-foreign-key", () => {
708
+ it("should generate SQL for foreign key constraint", () => {
709
+ const operation: MigrationOperation = {
710
+ type: "add-foreign-key",
711
+ table: "posts",
712
+ value: {
713
+ name: "posts_user_id_fk",
714
+ columns: ["user_id"],
715
+ referencedTable: "users",
716
+ referencedColumns: ["id"],
717
+ },
718
+ };
719
+
720
+ const result = execute(operation, config, () => {
721
+ throw new Error("No custom operations");
722
+ });
723
+
724
+ assertSingleResult(result);
725
+
726
+ const compiled = result.compile();
727
+ expect(compiled.sql).toMatchInlineSnapshot(
728
+ `"alter table \`posts\` add constraint \`posts_user_id_fk\` foreign key (\`user_id\`) references \`users\` (\`id\`) on delete restrict on update restrict"`,
729
+ );
730
+ });
731
+
732
+ it("should generate SQL for composite foreign key", () => {
733
+ const operation: MigrationOperation = {
734
+ type: "add-foreign-key",
735
+ table: "posts",
736
+ value: {
737
+ name: "posts_user_fk",
738
+ columns: ["org_id", "user_id"],
739
+ referencedTable: "users",
740
+ referencedColumns: ["org_id", "user_id"],
741
+ },
742
+ };
743
+
744
+ const result = execute(operation, config, () => {
745
+ throw new Error("No custom operations");
746
+ });
747
+
748
+ assertSingleResult(result);
749
+
750
+ const compiled = result.compile();
751
+ expect(compiled.sql).toMatchInlineSnapshot(
752
+ `"alter table \`posts\` add constraint \`posts_user_fk\` foreign key (\`org_id\`, \`user_id\`) references \`users\` (\`org_id\`, \`user_id\`) on delete restrict on update restrict"`,
753
+ );
754
+ });
755
+ });
756
+
757
+ describe("drop-foreign-key", () => {
758
+ it("should generate SQL to drop foreign key constraint", () => {
759
+ const operation: MigrationOperation = {
760
+ type: "drop-foreign-key",
761
+ table: "posts",
762
+ name: "posts_user_id_fk",
763
+ };
764
+
765
+ const result = execute(operation, config, () => {
766
+ throw new Error("No custom operations");
767
+ });
768
+
769
+ assertSingleResult(result);
770
+
771
+ const compiled = result.compile();
772
+ expect(compiled.sql).toMatchInlineSnapshot(
773
+ `"alter table \`posts\` drop constraint \`posts_user_id_fk\`"`,
774
+ );
775
+ });
776
+ });
777
+
778
+ describe("add-index", () => {
779
+ it("should generate SQL for regular index", () => {
780
+ const operation: MigrationOperation = {
781
+ type: "add-index",
782
+ table: "test_table",
783
+ columns: ["email"],
784
+ name: "idx_email",
785
+ unique: false,
786
+ };
787
+
788
+ const result = execute(operation, config, () => {
789
+ throw new Error("No custom operations");
790
+ });
791
+
792
+ assertSingleResult(result);
793
+
794
+ const compiled = result.compile();
795
+ expect(compiled.sql).toMatchInlineSnapshot(
796
+ `"create index \`idx_email\` on \`test_table\` (\`email\`)"`,
797
+ );
798
+ });
799
+
800
+ it("should generate SQL for unique index", () => {
801
+ const operation: MigrationOperation = {
802
+ type: "add-index",
803
+ table: "test_table",
804
+ columns: ["email"],
805
+ name: "idx_unique_email",
806
+ unique: true,
807
+ };
808
+
809
+ const result = execute(operation, config, () => {
810
+ throw new Error("No custom operations");
811
+ });
812
+
813
+ assertSingleResult(result);
814
+
815
+ const compiled = result.compile();
816
+ expect(compiled.sql).toMatchInlineSnapshot(
817
+ `"create unique index \`idx_unique_email\` on \`test_table\` (\`email\`)"`,
818
+ );
819
+ });
820
+
821
+ it("should generate SQL for composite index", () => {
822
+ const operation: MigrationOperation = {
823
+ type: "add-index",
824
+ table: "test_table",
825
+ columns: ["email", "name"],
826
+ name: "idx_email_name",
827
+ unique: false,
828
+ };
829
+
830
+ const result = execute(operation, config, () => {
831
+ throw new Error("No custom operations");
832
+ });
833
+
834
+ assertSingleResult(result);
835
+
836
+ const compiled = result.compile();
837
+ expect(compiled.sql).toMatchInlineSnapshot(
838
+ `"create index \`idx_email_name\` on \`test_table\` (\`email\`, \`name\`)"`,
839
+ );
840
+ });
841
+
842
+ it("should generate SQL for unique composite index", () => {
843
+ const operation: MigrationOperation = {
844
+ type: "add-index",
845
+ table: "test_table",
846
+ columns: ["email", "name"],
847
+ name: "idx_unique_email_name",
848
+ unique: true,
849
+ };
850
+
851
+ const result = execute(operation, config, () => {
852
+ throw new Error("No custom operations");
853
+ });
854
+
855
+ assertSingleResult(result);
856
+
857
+ const compiled = result.compile();
858
+ expect(compiled.sql).toMatchInlineSnapshot(
859
+ `"create unique index \`idx_unique_email_name\` on \`test_table\` (\`email\`, \`name\`)"`,
860
+ );
861
+ });
862
+ });
863
+
864
+ describe("drop-index", () => {
865
+ it("should generate SQL to drop index", () => {
866
+ const operation: MigrationOperation = {
867
+ type: "drop-index",
868
+ table: "test_table",
869
+ name: "idx_email",
870
+ };
871
+
872
+ const result = execute(operation, config, () => {
873
+ throw new Error("No custom operations");
874
+ });
875
+
876
+ assertSingleResult(result);
877
+
878
+ const compiled = result.compile();
879
+ expect(compiled.sql).toMatchInlineSnapshot(`"drop index if exists \`idx_email\`"`);
880
+ });
881
+ });
882
+
883
+ describe("custom operations", () => {
884
+ it("should handle custom operations via callback", () => {
885
+ const operation: MigrationOperation = {
886
+ type: "custom",
887
+ customType: "test-operation",
888
+ data: "test-data",
889
+ };
890
+
891
+ let customCallbackCalled = false;
892
+
893
+ const result = execute(operation, config, (op) => {
894
+ customCallbackCalled = true;
895
+ expect(op).toEqual(operation);
896
+
897
+ // Return a kysely query
898
+ return db.schema.createTable("custom_table").addColumn("id", "integer");
899
+ });
900
+
901
+ expect(customCallbackCalled).toBe(true);
902
+
903
+ assertSingleResult(result);
904
+
905
+ const compiled = result.compile();
906
+ expect(compiled.sql).toContain("create table");
907
+ expect(compiled.sql).toContain("custom_table");
908
+ });
909
+
910
+ it("should support custom operations returning array of nodes", () => {
911
+ const operation: MigrationOperation = {
912
+ type: "custom",
913
+ customType: "multi-operation",
914
+ };
915
+
916
+ const results = execute(operation, config, () => {
917
+ return [
918
+ db.schema.createTable("table1").addColumn("id", "integer"),
919
+ db.schema.createTable("table2").addColumn("id", "integer"),
920
+ ];
921
+ });
922
+
923
+ if (!Array.isArray(results)) {
924
+ throw new Error("Expected array of results");
925
+ }
926
+
927
+ expect(results).toHaveLength(2);
928
+ expect(results[0].compile().sql).toContain("table1");
929
+ expect(results[1].compile().sql).toContain("table2");
930
+ });
931
+ });
932
+
933
+ describe("complex migration scenarios", () => {
934
+ it("should generate correct SQL for full schema migration", () => {
935
+ // 1. Create users table
936
+ const createUsers = execute(
937
+ {
938
+ type: "create-table",
939
+ name: "users",
940
+ columns: [
941
+ { name: "id", type: "integer", isNullable: false, role: "external-id" },
942
+ { name: "email", type: "string", isNullable: false, role: "regular" },
943
+ { name: "name", type: "string", isNullable: false, role: "regular" },
944
+ ],
945
+ },
946
+ config,
947
+ () => {
948
+ throw new Error("No custom operations");
949
+ },
950
+ );
951
+
952
+ assertSingleResult(createUsers);
953
+
954
+ expect(createUsers.compile().sql).toMatchInlineSnapshot(
955
+ `"create table \`users\` (\`id\` integer not null unique, \`email\` text not null, \`name\` text not null)"`,
956
+ );
957
+
958
+ // 2. Add unique index on email
959
+ const addIndex = execute(
960
+ {
961
+ type: "add-index",
962
+ table: "users",
963
+ columns: ["email"],
964
+ name: "idx_unique_email",
965
+ unique: true,
966
+ },
967
+ config,
968
+ () => {
969
+ throw new Error("No custom operations");
970
+ },
971
+ );
972
+
973
+ assertSingleResult(addIndex);
974
+
975
+ expect(addIndex.compile().sql).toMatchInlineSnapshot(
976
+ `"create unique index \`idx_unique_email\` on \`users\` (\`email\`)"`,
977
+ );
978
+
979
+ // 3. Create posts table
980
+ const createPosts = execute(
981
+ {
982
+ type: "create-table",
983
+ name: "posts",
984
+ columns: [
985
+ { name: "id", type: "integer", isNullable: false, role: "external-id" },
986
+ { name: "user_id", type: "integer", isNullable: false, role: "reference" },
987
+ { name: "title", type: "string", isNullable: false, role: "regular" },
988
+ { name: "content", type: "string", isNullable: false, role: "regular" },
989
+ ],
990
+ },
991
+ config,
992
+ () => {
993
+ throw new Error("No custom operations");
994
+ },
995
+ );
996
+
997
+ assertSingleResult(createPosts);
998
+
999
+ expect(createPosts.compile().sql).toMatchInlineSnapshot(
1000
+ `"create table \`posts\` (\`id\` integer not null unique, \`user_id\` integer not null, \`title\` text not null, \`content\` text not null)"`,
1001
+ );
1002
+
1003
+ // 4. Add foreign key
1004
+ const addFk = execute(
1005
+ {
1006
+ type: "add-foreign-key",
1007
+ table: "posts",
1008
+ value: {
1009
+ name: "posts_user_id_fk",
1010
+ columns: ["user_id"],
1011
+ referencedTable: "users",
1012
+ referencedColumns: ["id"],
1013
+ },
1014
+ },
1015
+ config,
1016
+ () => {
1017
+ throw new Error("No custom operations");
1018
+ },
1019
+ );
1020
+
1021
+ assertSingleResult(addFk);
1022
+
1023
+ expect(addFk.compile().sql).toMatchInlineSnapshot(
1024
+ `"alter table \`posts\` add constraint \`posts_user_id_fk\` foreign key (\`user_id\`) references \`users\` (\`id\`) on delete restrict on update restrict"`,
1025
+ );
1026
+
1027
+ // 5. Alter posts table to add a new column
1028
+ const alterResults = execute(
1029
+ {
1030
+ type: "alter-table",
1031
+ name: "posts",
1032
+ value: [
1033
+ {
1034
+ type: "create-column",
1035
+ value: {
1036
+ name: "published",
1037
+ type: "bool",
1038
+ isNullable: false,
1039
+ role: "regular",
1040
+ default: { value: false },
1041
+ },
1042
+ },
1043
+ ],
1044
+ },
1045
+ config,
1046
+ () => {
1047
+ throw new Error("No custom operations");
1048
+ },
1049
+ );
1050
+
1051
+ if (!Array.isArray(alterResults)) {
1052
+ throw new Error("Expected array of results");
1053
+ }
1054
+
1055
+ expect(alterResults[0].compile().sql).toMatchInlineSnapshot(
1056
+ `"alter table \`posts\` add column \`published\` boolean default false not null"`,
1057
+ );
1058
+ });
1059
+
1060
+ it("should handle multiple alter-table operations", () => {
1061
+ const operation: MigrationOperation = {
1062
+ type: "alter-table",
1063
+ name: "users",
1064
+ value: [
1065
+ {
1066
+ type: "create-column",
1067
+ value: {
1068
+ name: "age",
1069
+ type: "integer",
1070
+ isNullable: true,
1071
+ role: "regular",
1072
+ },
1073
+ },
1074
+ {
1075
+ type: "rename-column",
1076
+ from: "name",
1077
+ to: "full_name",
1078
+ },
1079
+ {
1080
+ type: "drop-column",
1081
+ name: "old_field",
1082
+ },
1083
+ ],
1084
+ };
1085
+
1086
+ const results = execute(operation, config, () => {
1087
+ throw new Error("No custom operations");
1088
+ });
1089
+
1090
+ if (!Array.isArray(results)) {
1091
+ throw new Error("Expected array of results");
1092
+ }
1093
+
1094
+ expect(results).toHaveLength(3);
1095
+ expect(results[0].compile().sql).toMatchInlineSnapshot(
1096
+ `"alter table \`users\` add column \`age\` integer"`,
1097
+ );
1098
+ expect(results[1].compile().sql).toMatchInlineSnapshot(
1099
+ `"alter table \`users\` rename column \`name\` to \`full_name\`"`,
1100
+ );
1101
+ expect(results[2].compile().sql).toMatchInlineSnapshot(
1102
+ `"alter table \`users\` drop column \`old_field\`"`,
1103
+ );
1104
+ });
1105
+ });
1106
+
1107
+ describe("edge cases", () => {
1108
+ it("should handle table names with special characters", () => {
1109
+ const operation: MigrationOperation = {
1110
+ type: "create-table",
1111
+ name: "user-profiles",
1112
+ columns: [{ name: "id", type: "integer", isNullable: false, role: "external-id" }],
1113
+ };
1114
+
1115
+ const result = execute(operation, config, () => {
1116
+ throw new Error("No custom operations");
1117
+ });
1118
+
1119
+ assertSingleResult(result);
1120
+
1121
+ const compiled = result.compile();
1122
+ expect(compiled.sql).toContain("`user-profiles`");
1123
+ });
1124
+
1125
+ it("should handle column names with special characters", () => {
1126
+ const operation: MigrationOperation = {
1127
+ type: "create-table",
1128
+ name: "test",
1129
+ columns: [
1130
+ { name: "id", type: "integer", isNullable: false, role: "external-id" },
1131
+ { name: "user-name", type: "string", isNullable: false, role: "regular" },
1132
+ ],
1133
+ };
1134
+
1135
+ const result = execute(operation, config, () => {
1136
+ throw new Error("No custom operations");
1137
+ });
1138
+
1139
+ assertSingleResult(result);
1140
+
1141
+ const compiled = result.compile();
1142
+ expect(compiled.sql).toContain("`user-name`");
1143
+ });
1144
+
1145
+ it("should properly escape string default values", () => {
1146
+ const operation: MigrationOperation = {
1147
+ type: "create-table",
1148
+ name: "test",
1149
+ columns: [
1150
+ { name: "id", type: "integer", isNullable: false, role: "external-id" },
1151
+ {
1152
+ name: "status",
1153
+ type: "string",
1154
+ isNullable: false,
1155
+ role: "regular",
1156
+ default: { value: "it's pending" },
1157
+ },
1158
+ ],
1159
+ };
1160
+
1161
+ const result = execute(operation, config, () => {
1162
+ throw new Error("No custom operations");
1163
+ });
1164
+
1165
+ assertSingleResult(result);
1166
+
1167
+ const compiled = result.compile();
1168
+ expect(compiled.sql).toMatchInlineSnapshot(
1169
+ `"create table \`test\` (\`id\` integer not null unique, \`status\` text not null)"`,
1170
+ );
1171
+ });
1172
+ });
1173
+ });