@supabase/pg-delta 1.0.0-alpha.3 → 1.0.0-alpha.4

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 (463) hide show
  1. package/README.md +22 -0
  2. package/dist/cli/bin/cli.js +0 -0
  3. package/dist/cli/commands/plan.js +21 -0
  4. package/dist/cli/utils.d.ts +2 -0
  5. package/dist/cli/utils.js +1 -1
  6. package/dist/core/objects/table/table.model.d.ts +4 -2
  7. package/dist/core/objects/table/table.model.js +3 -0
  8. package/dist/core/objects/trigger/changes/trigger.alter.js +23 -0
  9. package/dist/core/objects/trigger/changes/trigger.create.js +2 -1
  10. package/dist/core/objects/trigger/trigger.model.d.ts +1 -0
  11. package/dist/core/objects/trigger/trigger.model.js +3 -0
  12. package/dist/core/plan/apply.js +3 -3
  13. package/dist/core/plan/create.js +34 -15
  14. package/dist/core/plan/sql-format/constants.d.ts +2 -0
  15. package/dist/core/plan/sql-format/constants.js +11 -0
  16. package/dist/core/plan/sql-format/fixtures.d.ts +2 -0
  17. package/dist/core/plan/sql-format/fixtures.js +2449 -0
  18. package/dist/core/plan/sql-format/format-utils.d.ts +37 -0
  19. package/dist/core/plan/sql-format/format-utils.js +274 -0
  20. package/dist/core/plan/sql-format/formatters.d.ts +20 -0
  21. package/dist/core/plan/sql-format/formatters.js +737 -0
  22. package/dist/core/plan/sql-format/index.d.ts +2 -0
  23. package/dist/core/plan/sql-format/index.js +98 -0
  24. package/dist/core/plan/sql-format/keyword-case.d.ts +2 -0
  25. package/dist/core/plan/sql-format/keyword-case.js +868 -0
  26. package/dist/core/plan/sql-format/protect.d.ts +3 -0
  27. package/dist/core/plan/sql-format/protect.js +269 -0
  28. package/dist/core/plan/sql-format/sql-scanner.d.ts +59 -0
  29. package/dist/core/plan/sql-format/sql-scanner.js +202 -0
  30. package/dist/core/plan/sql-format/tokenizer.d.ts +22 -0
  31. package/dist/core/plan/sql-format/tokenizer.js +118 -0
  32. package/dist/core/plan/sql-format/types.d.ts +28 -0
  33. package/dist/core/plan/sql-format/types.js +1 -0
  34. package/dist/core/plan/sql-format/wrap.d.ts +2 -0
  35. package/dist/core/plan/sql-format/wrap.js +165 -0
  36. package/dist/core/plan/sql-format.d.ts +2 -0
  37. package/dist/core/plan/sql-format.js +1 -0
  38. package/dist/core/plan/statements.d.ts +2 -1
  39. package/dist/core/plan/statements.js +6 -2
  40. package/dist/core/postgres-config.d.ts +15 -0
  41. package/dist/core/postgres-config.js +30 -0
  42. package/dist/index.d.ts +2 -0
  43. package/dist/index.js +1 -0
  44. package/package.json +37 -22
  45. package/src/cli/app.ts +28 -0
  46. package/src/cli/bin/cli.ts +9 -0
  47. package/src/cli/commands/apply.ts +101 -0
  48. package/src/cli/commands/plan.ts +195 -0
  49. package/src/cli/commands/sync.ts +185 -0
  50. package/src/cli/formatters/index.ts +5 -0
  51. package/src/cli/formatters/tree/tree-builder.ts +380 -0
  52. package/src/cli/formatters/tree/tree-renderer.ts +372 -0
  53. package/src/cli/formatters/tree/tree.ts +237 -0
  54. package/src/cli/utils/integrations.ts +42 -0
  55. package/src/cli/utils.ts +231 -0
  56. package/src/core/catalog.diff.ts +246 -0
  57. package/src/core/catalog.model.ts +384 -0
  58. package/src/core/change.types.ts +44 -0
  59. package/src/core/context.ts +26 -0
  60. package/src/core/depend.ts +1870 -0
  61. package/src/core/expand-replace-dependencies.ts +380 -0
  62. package/src/core/fingerprint.ts +204 -0
  63. package/src/core/integrations/filter/dsl.ts +204 -0
  64. package/src/core/integrations/filter/extractors.ts +145 -0
  65. package/src/core/integrations/filter/filter.types.ts +3 -0
  66. package/src/core/integrations/integration-dsl.ts +24 -0
  67. package/src/core/integrations/integration.types.ts +7 -0
  68. package/src/core/integrations/serialize/dsl.ts +77 -0
  69. package/src/core/integrations/serialize/serialize.types.ts +3 -0
  70. package/src/core/integrations/supabase.ts +121 -0
  71. package/src/core/objects/aggregate/aggregate.diff.test.ts +215 -0
  72. package/src/core/objects/aggregate/aggregate.diff.ts +278 -0
  73. package/src/core/objects/aggregate/aggregate.model.ts +317 -0
  74. package/src/core/objects/aggregate/changes/aggregate.alter.test.ts +64 -0
  75. package/src/core/objects/aggregate/changes/aggregate.alter.ts +32 -0
  76. package/src/core/objects/aggregate/changes/aggregate.base.ts +20 -0
  77. package/src/core/objects/aggregate/changes/aggregate.comment.test.ts +86 -0
  78. package/src/core/objects/aggregate/changes/aggregate.comment.ts +62 -0
  79. package/src/core/objects/aggregate/changes/aggregate.create.test.ts +101 -0
  80. package/src/core/objects/aggregate/changes/aggregate.create.ts +329 -0
  81. package/src/core/objects/aggregate/changes/aggregate.drop.test.ts +78 -0
  82. package/src/core/objects/aggregate/changes/aggregate.drop.ts +32 -0
  83. package/src/core/objects/aggregate/changes/aggregate.privilege.test.ts +130 -0
  84. package/src/core/objects/aggregate/changes/aggregate.privilege.ts +146 -0
  85. package/src/core/objects/aggregate/changes/aggregate.types.ts +12 -0
  86. package/src/core/objects/base.change.ts +62 -0
  87. package/src/core/objects/base.default-privileges.ts +204 -0
  88. package/src/core/objects/base.diff.ts +20 -0
  89. package/src/core/objects/base.model.ts +82 -0
  90. package/src/core/objects/base.privilege-diff.ts +299 -0
  91. package/src/core/objects/base.privilege.ts +184 -0
  92. package/src/core/objects/collation/changes/collation.alter.test.ts +63 -0
  93. package/src/core/objects/collation/changes/collation.alter.ts +79 -0
  94. package/src/core/objects/collation/changes/collation.base.ts +20 -0
  95. package/src/core/objects/collation/changes/collation.comment.ts +68 -0
  96. package/src/core/objects/collation/changes/collation.create.test.ts +51 -0
  97. package/src/core/objects/collation/changes/collation.create.ts +106 -0
  98. package/src/core/objects/collation/changes/collation.drop.test.ts +28 -0
  99. package/src/core/objects/collation/changes/collation.drop.ts +37 -0
  100. package/src/core/objects/collation/changes/collation.types.ts +10 -0
  101. package/src/core/objects/collation/collation.diff.test.ts +100 -0
  102. package/src/core/objects/collation/collation.diff.ts +126 -0
  103. package/src/core/objects/collation/collation.model.ts +224 -0
  104. package/src/core/objects/domain/changes/domain.alter.test.ts +316 -0
  105. package/src/core/objects/domain/changes/domain.alter.ts +286 -0
  106. package/src/core/objects/domain/changes/domain.base.ts +20 -0
  107. package/src/core/objects/domain/changes/domain.comment.ts +59 -0
  108. package/src/core/objects/domain/changes/domain.create.test.ts +65 -0
  109. package/src/core/objects/domain/changes/domain.create.ts +118 -0
  110. package/src/core/objects/domain/changes/domain.drop.test.ts +30 -0
  111. package/src/core/objects/domain/changes/domain.drop.ts +34 -0
  112. package/src/core/objects/domain/changes/domain.privilege.ts +171 -0
  113. package/src/core/objects/domain/changes/domain.types.ts +12 -0
  114. package/src/core/objects/domain/domain.diff.test.ts +284 -0
  115. package/src/core/objects/domain/domain.diff.ts +358 -0
  116. package/src/core/objects/domain/domain.model.ts +190 -0
  117. package/src/core/objects/event-trigger/changes/event-trigger.alter.test.ts +50 -0
  118. package/src/core/objects/event-trigger/changes/event-trigger.alter.ts +82 -0
  119. package/src/core/objects/event-trigger/changes/event-trigger.base.ts +20 -0
  120. package/src/core/objects/event-trigger/changes/event-trigger.comment.ts +66 -0
  121. package/src/core/objects/event-trigger/changes/event-trigger.create.test.ts +24 -0
  122. package/src/core/objects/event-trigger/changes/event-trigger.create.ts +72 -0
  123. package/src/core/objects/event-trigger/changes/event-trigger.drop.test.ts +22 -0
  124. package/src/core/objects/event-trigger/changes/event-trigger.drop.ts +34 -0
  125. package/src/core/objects/event-trigger/changes/event-trigger.types.ts +10 -0
  126. package/src/core/objects/event-trigger/event-trigger.diff.test.ts +126 -0
  127. package/src/core/objects/event-trigger/event-trigger.diff.ts +126 -0
  128. package/src/core/objects/event-trigger/event-trigger.model.ts +106 -0
  129. package/src/core/objects/extension/changes/extension.alter.test.ts +58 -0
  130. package/src/core/objects/extension/changes/extension.alter.ts +78 -0
  131. package/src/core/objects/extension/changes/extension.base.ts +20 -0
  132. package/src/core/objects/extension/changes/extension.comment.ts +64 -0
  133. package/src/core/objects/extension/changes/extension.create.test.ts +25 -0
  134. package/src/core/objects/extension/changes/extension.create.ts +63 -0
  135. package/src/core/objects/extension/changes/extension.drop.test.ts +23 -0
  136. package/src/core/objects/extension/changes/extension.drop.ts +34 -0
  137. package/src/core/objects/extension/changes/extension.types.ts +10 -0
  138. package/src/core/objects/extension/extension.diff.test.ts +42 -0
  139. package/src/core/objects/extension/extension.diff.ts +90 -0
  140. package/src/core/objects/extension/extension.model.ts +280 -0
  141. package/src/core/objects/foreign-data-wrapper/foreign-data-wrapper/changes/foreign-data-wrapper.alter.test.ts +125 -0
  142. package/src/core/objects/foreign-data-wrapper/foreign-data-wrapper/changes/foreign-data-wrapper.alter.ts +101 -0
  143. package/src/core/objects/foreign-data-wrapper/foreign-data-wrapper/changes/foreign-data-wrapper.base.ts +20 -0
  144. package/src/core/objects/foreign-data-wrapper/foreign-data-wrapper/changes/foreign-data-wrapper.comment.ts +72 -0
  145. package/src/core/objects/foreign-data-wrapper/foreign-data-wrapper/changes/foreign-data-wrapper.create.test.ts +125 -0
  146. package/src/core/objects/foreign-data-wrapper/foreign-data-wrapper/changes/foreign-data-wrapper.create.ts +95 -0
  147. package/src/core/objects/foreign-data-wrapper/foreign-data-wrapper/changes/foreign-data-wrapper.drop.test.ts +23 -0
  148. package/src/core/objects/foreign-data-wrapper/foreign-data-wrapper/changes/foreign-data-wrapper.drop.ts +36 -0
  149. package/src/core/objects/foreign-data-wrapper/foreign-data-wrapper/changes/foreign-data-wrapper.privilege.ts +172 -0
  150. package/src/core/objects/foreign-data-wrapper/foreign-data-wrapper/changes/foreign-data-wrapper.types.ts +12 -0
  151. package/src/core/objects/foreign-data-wrapper/foreign-data-wrapper/foreign-data-wrapper.diff.test.ts +179 -0
  152. package/src/core/objects/foreign-data-wrapper/foreign-data-wrapper/foreign-data-wrapper.diff.ts +341 -0
  153. package/src/core/objects/foreign-data-wrapper/foreign-data-wrapper/foreign-data-wrapper.model.ts +149 -0
  154. package/src/core/objects/foreign-data-wrapper/foreign-data-wrapper.types.ts +10 -0
  155. package/src/core/objects/foreign-data-wrapper/foreign-table/changes/foreign-table.alter.test.ts +309 -0
  156. package/src/core/objects/foreign-data-wrapper/foreign-table/changes/foreign-table.alter.ts +341 -0
  157. package/src/core/objects/foreign-data-wrapper/foreign-table/changes/foreign-table.base.ts +20 -0
  158. package/src/core/objects/foreign-data-wrapper/foreign-table/changes/foreign-table.comment.ts +72 -0
  159. package/src/core/objects/foreign-data-wrapper/foreign-table/changes/foreign-table.create.test.ts +201 -0
  160. package/src/core/objects/foreign-data-wrapper/foreign-table/changes/foreign-table.create.ts +81 -0
  161. package/src/core/objects/foreign-data-wrapper/foreign-table/changes/foreign-table.drop.test.ts +43 -0
  162. package/src/core/objects/foreign-data-wrapper/foreign-table/changes/foreign-table.drop.ts +37 -0
  163. package/src/core/objects/foreign-data-wrapper/foreign-table/changes/foreign-table.privilege.ts +181 -0
  164. package/src/core/objects/foreign-data-wrapper/foreign-table/changes/foreign-table.types.ts +12 -0
  165. package/src/core/objects/foreign-data-wrapper/foreign-table/foreign-table.diff.test.ts +813 -0
  166. package/src/core/objects/foreign-data-wrapper/foreign-table/foreign-table.diff.ts +406 -0
  167. package/src/core/objects/foreign-data-wrapper/foreign-table/foreign-table.model.ts +242 -0
  168. package/src/core/objects/foreign-data-wrapper/server/changes/server.alter.test.ts +168 -0
  169. package/src/core/objects/foreign-data-wrapper/server/changes/server.alter.ts +126 -0
  170. package/src/core/objects/foreign-data-wrapper/server/changes/server.base.ts +20 -0
  171. package/src/core/objects/foreign-data-wrapper/server/changes/server.comment.ts +60 -0
  172. package/src/core/objects/foreign-data-wrapper/server/changes/server.create.test.ts +131 -0
  173. package/src/core/objects/foreign-data-wrapper/server/changes/server.create.ts +81 -0
  174. package/src/core/objects/foreign-data-wrapper/server/changes/server.drop.test.ts +24 -0
  175. package/src/core/objects/foreign-data-wrapper/server/changes/server.drop.ts +34 -0
  176. package/src/core/objects/foreign-data-wrapper/server/changes/server.privilege.ts +164 -0
  177. package/src/core/objects/foreign-data-wrapper/server/changes/server.types.ts +12 -0
  178. package/src/core/objects/foreign-data-wrapper/server/server.diff.test.ts +167 -0
  179. package/src/core/objects/foreign-data-wrapper/server/server.diff.ts +317 -0
  180. package/src/core/objects/foreign-data-wrapper/server/server.model.ts +133 -0
  181. package/src/core/objects/foreign-data-wrapper/user-mapping/changes/user-mapping.alter.test.ts +82 -0
  182. package/src/core/objects/foreign-data-wrapper/user-mapping/changes/user-mapping.alter.ts +69 -0
  183. package/src/core/objects/foreign-data-wrapper/user-mapping/changes/user-mapping.base.ts +20 -0
  184. package/src/core/objects/foreign-data-wrapper/user-mapping/changes/user-mapping.create.test.ts +85 -0
  185. package/src/core/objects/foreign-data-wrapper/user-mapping/changes/user-mapping.create.ts +66 -0
  186. package/src/core/objects/foreign-data-wrapper/user-mapping/changes/user-mapping.drop.test.ts +53 -0
  187. package/src/core/objects/foreign-data-wrapper/user-mapping/changes/user-mapping.drop.ts +40 -0
  188. package/src/core/objects/foreign-data-wrapper/user-mapping/changes/user-mapping.types.ts +8 -0
  189. package/src/core/objects/foreign-data-wrapper/user-mapping/user-mapping.diff.test.ts +77 -0
  190. package/src/core/objects/foreign-data-wrapper/user-mapping/user-mapping.diff.ts +107 -0
  191. package/src/core/objects/foreign-data-wrapper/user-mapping/user-mapping.model.ts +96 -0
  192. package/src/core/objects/index/changes/index.alter.test.ts +200 -0
  193. package/src/core/objects/index/changes/index.alter.ts +144 -0
  194. package/src/core/objects/index/changes/index.base.ts +20 -0
  195. package/src/core/objects/index/changes/index.comment.ts +63 -0
  196. package/src/core/objects/index/changes/index.create.test.ts +66 -0
  197. package/src/core/objects/index/changes/index.create.ts +68 -0
  198. package/src/core/objects/index/changes/index.drop.test.ts +44 -0
  199. package/src/core/objects/index/changes/index.drop.ts +34 -0
  200. package/src/core/objects/index/changes/index.types.ts +6 -0
  201. package/src/core/objects/index/changes/utils.ts +16 -0
  202. package/src/core/objects/index/index.diff.test.ts +153 -0
  203. package/src/core/objects/index/index.diff.ts +243 -0
  204. package/src/core/objects/index/index.model.ts +370 -0
  205. package/src/core/objects/language/changes/language.alter.test.ts +33 -0
  206. package/src/core/objects/language/changes/language.alter.ts +53 -0
  207. package/src/core/objects/language/changes/language.base.ts +20 -0
  208. package/src/core/objects/language/changes/language.comment.ts +58 -0
  209. package/src/core/objects/language/changes/language.create.test.ts +27 -0
  210. package/src/core/objects/language/changes/language.create.ts +104 -0
  211. package/src/core/objects/language/changes/language.drop.test.ts +25 -0
  212. package/src/core/objects/language/changes/language.drop.ts +39 -0
  213. package/src/core/objects/language/changes/language.privilege.ts +172 -0
  214. package/src/core/objects/language/changes/language.types.ts +12 -0
  215. package/src/core/objects/language/language.diff.test.ts +53 -0
  216. package/src/core/objects/language/language.diff.ts +176 -0
  217. package/src/core/objects/language/language.model.ts +150 -0
  218. package/src/core/objects/materialized-view/changes/materialized-view.alter.test.ts +123 -0
  219. package/src/core/objects/materialized-view/changes/materialized-view.alter.ts +113 -0
  220. package/src/core/objects/materialized-view/changes/materialized-view.base.ts +20 -0
  221. package/src/core/objects/materialized-view/changes/materialized-view.comment.ts +176 -0
  222. package/src/core/objects/materialized-view/changes/materialized-view.create.test.ts +64 -0
  223. package/src/core/objects/materialized-view/changes/materialized-view.create.ts +93 -0
  224. package/src/core/objects/materialized-view/changes/materialized-view.drop.test.ts +34 -0
  225. package/src/core/objects/materialized-view/changes/materialized-view.drop.ts +60 -0
  226. package/src/core/objects/materialized-view/changes/materialized-view.privilege.ts +212 -0
  227. package/src/core/objects/materialized-view/changes/materialized-view.types.ts +12 -0
  228. package/src/core/objects/materialized-view/materialized-view.diff.test.ts +102 -0
  229. package/src/core/objects/materialized-view/materialized-view.diff.ts +451 -0
  230. package/src/core/objects/materialized-view/materialized-view.model.ts +258 -0
  231. package/src/core/objects/procedure/changes/procedure.alter.test.ts +1005 -0
  232. package/src/core/objects/procedure/changes/procedure.alter.ts +287 -0
  233. package/src/core/objects/procedure/changes/procedure.base.ts +20 -0
  234. package/src/core/objects/procedure/changes/procedure.comment.ts +70 -0
  235. package/src/core/objects/procedure/changes/procedure.create.test.ts +48 -0
  236. package/src/core/objects/procedure/changes/procedure.create.ts +92 -0
  237. package/src/core/objects/procedure/changes/procedure.drop.test.ts +85 -0
  238. package/src/core/objects/procedure/changes/procedure.drop.ts +49 -0
  239. package/src/core/objects/procedure/changes/procedure.privilege.ts +188 -0
  240. package/src/core/objects/procedure/changes/procedure.types.ts +12 -0
  241. package/src/core/objects/procedure/procedure.diff.test.ts +161 -0
  242. package/src/core/objects/procedure/procedure.diff.ts +404 -0
  243. package/src/core/objects/procedure/procedure.model.ts +264 -0
  244. package/src/core/objects/procedure/utils.ts +58 -0
  245. package/src/core/objects/publication/changes/publication.alter.test.ts +223 -0
  246. package/src/core/objects/publication/changes/publication.alter.ts +243 -0
  247. package/src/core/objects/publication/changes/publication.base.ts +20 -0
  248. package/src/core/objects/publication/changes/publication.comment.test.ts +70 -0
  249. package/src/core/objects/publication/changes/publication.comment.ts +64 -0
  250. package/src/core/objects/publication/changes/publication.create.test.ts +87 -0
  251. package/src/core/objects/publication/changes/publication.create.ts +82 -0
  252. package/src/core/objects/publication/changes/publication.drop.test.ts +46 -0
  253. package/src/core/objects/publication/changes/publication.drop.ts +29 -0
  254. package/src/core/objects/publication/changes/publication.types.ts +26 -0
  255. package/src/core/objects/publication/publication.diff.test.ts +292 -0
  256. package/src/core/objects/publication/publication.diff.ts +253 -0
  257. package/src/core/objects/publication/publication.model.ts +206 -0
  258. package/src/core/objects/publication/utils.ts +55 -0
  259. package/src/core/objects/rls-policy/changes/rls-policy.alter.test.ts +250 -0
  260. package/src/core/objects/rls-policy/changes/rls-policy.alter.ts +128 -0
  261. package/src/core/objects/rls-policy/changes/rls-policy.base.ts +20 -0
  262. package/src/core/objects/rls-policy/changes/rls-policy.comment.ts +69 -0
  263. package/src/core/objects/rls-policy/changes/rls-policy.create.test.ts +74 -0
  264. package/src/core/objects/rls-policy/changes/rls-policy.create.ts +100 -0
  265. package/src/core/objects/rls-policy/changes/rls-policy.drop.test.ts +28 -0
  266. package/src/core/objects/rls-policy/changes/rls-policy.drop.ts +39 -0
  267. package/src/core/objects/rls-policy/changes/rls-policy.types.ts +10 -0
  268. package/src/core/objects/rls-policy/rls-policy.diff.test.ts +79 -0
  269. package/src/core/objects/rls-policy/rls-policy.diff.ts +121 -0
  270. package/src/core/objects/rls-policy/rls-policy.model.ts +140 -0
  271. package/src/core/objects/role/changes/role.alter.test.ts +346 -0
  272. package/src/core/objects/role/changes/role.alter.ts +110 -0
  273. package/src/core/objects/role/changes/role.base.ts +24 -0
  274. package/src/core/objects/role/changes/role.comment.ts +55 -0
  275. package/src/core/objects/role/changes/role.create.test.ts +52 -0
  276. package/src/core/objects/role/changes/role.create.ts +102 -0
  277. package/src/core/objects/role/changes/role.drop.test.ts +29 -0
  278. package/src/core/objects/role/changes/role.drop.ts +34 -0
  279. package/src/core/objects/role/changes/role.privilege.ts +376 -0
  280. package/src/core/objects/role/changes/role.types.ts +12 -0
  281. package/src/core/objects/role/role.diff.test.ts +44 -0
  282. package/src/core/objects/role/role.diff.ts +479 -0
  283. package/src/core/objects/role/role.model.ts +344 -0
  284. package/src/core/objects/rule/changes/rule.alter.test.ts +78 -0
  285. package/src/core/objects/rule/changes/rule.alter.ts +72 -0
  286. package/src/core/objects/rule/changes/rule.base.ts +20 -0
  287. package/src/core/objects/rule/changes/rule.comment.test.ts +55 -0
  288. package/src/core/objects/rule/changes/rule.comment.ts +62 -0
  289. package/src/core/objects/rule/changes/rule.create.test.ts +59 -0
  290. package/src/core/objects/rule/changes/rule.create.ts +42 -0
  291. package/src/core/objects/rule/changes/rule.drop.test.ts +38 -0
  292. package/src/core/objects/rule/changes/rule.drop.ts +29 -0
  293. package/src/core/objects/rule/changes/rule.types.ts +12 -0
  294. package/src/core/objects/rule/rule.diff.test.ts +132 -0
  295. package/src/core/objects/rule/rule.diff.ts +79 -0
  296. package/src/core/objects/rule/rule.model.ts +173 -0
  297. package/src/core/objects/schema/changes/schema.alter.test.ts +28 -0
  298. package/src/core/objects/schema/changes/schema.alter.ts +45 -0
  299. package/src/core/objects/schema/changes/schema.base.ts +20 -0
  300. package/src/core/objects/schema/changes/schema.comment.ts +56 -0
  301. package/src/core/objects/schema/changes/schema.create.test.ts +22 -0
  302. package/src/core/objects/schema/changes/schema.create.ts +47 -0
  303. package/src/core/objects/schema/changes/schema.drop.test.ts +20 -0
  304. package/src/core/objects/schema/changes/schema.drop.ts +34 -0
  305. package/src/core/objects/schema/changes/schema.privilege.ts +175 -0
  306. package/src/core/objects/schema/changes/schema.types.ts +12 -0
  307. package/src/core/objects/schema/schema.diff.test.ts +42 -0
  308. package/src/core/objects/schema/schema.diff.ts +209 -0
  309. package/src/core/objects/schema/schema.model.ts +107 -0
  310. package/src/core/objects/sequence/changes/sequence.alter.test.ts +151 -0
  311. package/src/core/objects/sequence/changes/sequence.alter.ts +115 -0
  312. package/src/core/objects/sequence/changes/sequence.base.ts +20 -0
  313. package/src/core/objects/sequence/changes/sequence.comment.ts +60 -0
  314. package/src/core/objects/sequence/changes/sequence.create.test.ts +84 -0
  315. package/src/core/objects/sequence/changes/sequence.create.ts +111 -0
  316. package/src/core/objects/sequence/changes/sequence.drop.test.ts +32 -0
  317. package/src/core/objects/sequence/changes/sequence.drop.ts +37 -0
  318. package/src/core/objects/sequence/changes/sequence.privilege.ts +179 -0
  319. package/src/core/objects/sequence/changes/sequence.types.ts +12 -0
  320. package/src/core/objects/sequence/sequence.diff.test.ts +141 -0
  321. package/src/core/objects/sequence/sequence.diff.ts +359 -0
  322. package/src/core/objects/sequence/sequence.model.ts +185 -0
  323. package/src/core/objects/subscription/changes/subscription.alter.test.ts +124 -0
  324. package/src/core/objects/subscription/changes/subscription.alter.ts +110 -0
  325. package/src/core/objects/subscription/changes/subscription.base.ts +20 -0
  326. package/src/core/objects/subscription/changes/subscription.comment.test.ts +67 -0
  327. package/src/core/objects/subscription/changes/subscription.comment.ts +64 -0
  328. package/src/core/objects/subscription/changes/subscription.create.test.ts +77 -0
  329. package/src/core/objects/subscription/changes/subscription.create.ts +69 -0
  330. package/src/core/objects/subscription/changes/subscription.drop.test.ts +46 -0
  331. package/src/core/objects/subscription/changes/subscription.drop.ts +20 -0
  332. package/src/core/objects/subscription/changes/subscription.types.ts +22 -0
  333. package/src/core/objects/subscription/subscription.diff.test.ts +232 -0
  334. package/src/core/objects/subscription/subscription.diff.ts +241 -0
  335. package/src/core/objects/subscription/subscription.model.ts +190 -0
  336. package/src/core/objects/subscription/utils.ts +156 -0
  337. package/src/core/objects/table/changes/table.alter.test.ts +823 -0
  338. package/src/core/objects/table/changes/table.alter.ts +806 -0
  339. package/src/core/objects/table/changes/table.base.ts +20 -0
  340. package/src/core/objects/table/changes/table.comment.ts +266 -0
  341. package/src/core/objects/table/changes/table.create.test.ts +150 -0
  342. package/src/core/objects/table/changes/table.create.ts +188 -0
  343. package/src/core/objects/table/changes/table.drop.test.ts +34 -0
  344. package/src/core/objects/table/changes/table.drop.ts +45 -0
  345. package/src/core/objects/table/changes/table.privilege.ts +200 -0
  346. package/src/core/objects/table/changes/table.types.ts +12 -0
  347. package/src/core/objects/table/table.diff.test.ts +711 -0
  348. package/src/core/objects/table/table.diff.ts +953 -0
  349. package/src/core/objects/table/table.model.ts +460 -0
  350. package/src/core/objects/trigger/changes/trigger.alter.test.ts +46 -0
  351. package/src/core/objects/trigger/changes/trigger.alter.ts +76 -0
  352. package/src/core/objects/trigger/changes/trigger.base.ts +20 -0
  353. package/src/core/objects/trigger/changes/trigger.comment.ts +64 -0
  354. package/src/core/objects/trigger/changes/trigger.create.test.ts +43 -0
  355. package/src/core/objects/trigger/changes/trigger.create.ts +85 -0
  356. package/src/core/objects/trigger/changes/trigger.drop.test.ts +43 -0
  357. package/src/core/objects/trigger/changes/trigger.drop.ts +39 -0
  358. package/src/core/objects/trigger/changes/trigger.types.ts +10 -0
  359. package/src/core/objects/trigger/trigger.diff.test.ts +83 -0
  360. package/src/core/objects/trigger/trigger.diff.ts +116 -0
  361. package/src/core/objects/trigger/trigger.model.ts +252 -0
  362. package/src/core/objects/type/composite-type/changes/composite-type.alter.test.ts +202 -0
  363. package/src/core/objects/type/composite-type/changes/composite-type.alter.ts +174 -0
  364. package/src/core/objects/type/composite-type/changes/composite-type.base.ts +20 -0
  365. package/src/core/objects/type/composite-type/changes/composite-type.comment.ts +145 -0
  366. package/src/core/objects/type/composite-type/changes/composite-type.create.test.ts +101 -0
  367. package/src/core/objects/type/composite-type/changes/composite-type.create.ts +95 -0
  368. package/src/core/objects/type/composite-type/changes/composite-type.drop.test.ts +33 -0
  369. package/src/core/objects/type/composite-type/changes/composite-type.drop.ts +37 -0
  370. package/src/core/objects/type/composite-type/changes/composite-type.privilege.ts +175 -0
  371. package/src/core/objects/type/composite-type/changes/composite-type.types.ts +12 -0
  372. package/src/core/objects/type/composite-type/composite-type.diff.test.ts +191 -0
  373. package/src/core/objects/type/composite-type/composite-type.diff.ts +372 -0
  374. package/src/core/objects/type/composite-type/composite-type.model.ts +252 -0
  375. package/src/core/objects/type/enum/changes/enum.alter.test.ts +104 -0
  376. package/src/core/objects/type/enum/changes/enum.alter.ts +91 -0
  377. package/src/core/objects/type/enum/changes/enum.base.ts +20 -0
  378. package/src/core/objects/type/enum/changes/enum.comment.ts +64 -0
  379. package/src/core/objects/type/enum/changes/enum.create.test.ts +28 -0
  380. package/src/core/objects/type/enum/changes/enum.create.ts +56 -0
  381. package/src/core/objects/type/enum/changes/enum.drop.test.ts +25 -0
  382. package/src/core/objects/type/enum/changes/enum.drop.ts +34 -0
  383. package/src/core/objects/type/enum/changes/enum.privilege.ts +175 -0
  384. package/src/core/objects/type/enum/changes/enum.types.ts +12 -0
  385. package/src/core/objects/type/enum/enum.diff.test.ts +191 -0
  386. package/src/core/objects/type/enum/enum.diff.ts +396 -0
  387. package/src/core/objects/type/enum/enum.model.ts +194 -0
  388. package/src/core/objects/type/range/changes/range.alter.test.ts +27 -0
  389. package/src/core/objects/type/range/changes/range.alter.ts +51 -0
  390. package/src/core/objects/type/range/changes/range.base.ts +20 -0
  391. package/src/core/objects/type/range/changes/range.comment.ts +64 -0
  392. package/src/core/objects/type/range/changes/range.create.test.ts +51 -0
  393. package/src/core/objects/type/range/changes/range.create.ts +151 -0
  394. package/src/core/objects/type/range/changes/range.drop.test.ts +26 -0
  395. package/src/core/objects/type/range/changes/range.drop.ts +34 -0
  396. package/src/core/objects/type/range/changes/range.privilege.ts +175 -0
  397. package/src/core/objects/type/range/changes/range.types.ts +12 -0
  398. package/src/core/objects/type/range/range.diff.test.ts +70 -0
  399. package/src/core/objects/type/range/range.diff.ts +259 -0
  400. package/src/core/objects/type/range/range.model.ts +187 -0
  401. package/src/core/objects/type/type.types.ts +5 -0
  402. package/src/core/objects/utils.ts +171 -0
  403. package/src/core/objects/view/changes/view.alter.test.ts +110 -0
  404. package/src/core/objects/view/changes/view.alter.ts +112 -0
  405. package/src/core/objects/view/changes/view.base.ts +20 -0
  406. package/src/core/objects/view/changes/view.comment.ts +59 -0
  407. package/src/core/objects/view/changes/view.create.test.ts +65 -0
  408. package/src/core/objects/view/changes/view.create.ts +73 -0
  409. package/src/core/objects/view/changes/view.drop.test.ts +34 -0
  410. package/src/core/objects/view/changes/view.drop.ts +40 -0
  411. package/src/core/objects/view/changes/view.privilege.ts +200 -0
  412. package/src/core/objects/view/changes/view.types.ts +12 -0
  413. package/src/core/objects/view/view.diff.test.ts +91 -0
  414. package/src/core/objects/view/view.diff.ts +365 -0
  415. package/src/core/objects/view/view.model.ts +276 -0
  416. package/src/core/plan/apply.ts +190 -0
  417. package/src/core/plan/create.ts +432 -0
  418. package/src/core/plan/hierarchy.ts +574 -0
  419. package/src/core/plan/index.ts +29 -0
  420. package/src/core/plan/io.ts +20 -0
  421. package/src/core/plan/risk.ts +48 -0
  422. package/src/core/plan/serialize.ts +195 -0
  423. package/src/core/plan/sql-format/constants.ts +13 -0
  424. package/src/core/plan/sql-format/fixtures.ts +2806 -0
  425. package/src/core/plan/sql-format/format-comment-literals.test.ts +96 -0
  426. package/src/core/plan/sql-format/format-functions.test.ts +127 -0
  427. package/src/core/plan/sql-format/format-lowercase-coverage.test.ts +67 -0
  428. package/src/core/plan/sql-format/format-off.test.ts +809 -0
  429. package/src/core/plan/sql-format/format-pretty-lower-leading.test.ts +1056 -0
  430. package/src/core/plan/sql-format/format-pretty-narrow.test.ts +1283 -0
  431. package/src/core/plan/sql-format/format-pretty-preserve.test.ts +1052 -0
  432. package/src/core/plan/sql-format/format-pretty-upper.test.ts +1045 -0
  433. package/src/core/plan/sql-format/format-stress.test.ts +616 -0
  434. package/src/core/plan/sql-format/format-utils.test.ts +91 -0
  435. package/src/core/plan/sql-format/format-utils.ts +391 -0
  436. package/src/core/plan/sql-format/formatters.ts +921 -0
  437. package/src/core/plan/sql-format/index.ts +149 -0
  438. package/src/core/plan/sql-format/keyword-case.test.ts +118 -0
  439. package/src/core/plan/sql-format/keyword-case.ts +1085 -0
  440. package/src/core/plan/sql-format/protect.test.ts +127 -0
  441. package/src/core/plan/sql-format/protect.ts +337 -0
  442. package/src/core/plan/sql-format/sql-scanner.test.ts +240 -0
  443. package/src/core/plan/sql-format/sql-scanner.ts +252 -0
  444. package/src/core/plan/sql-format/tokenizer.test.ts +68 -0
  445. package/src/core/plan/sql-format/tokenizer.ts +152 -0
  446. package/src/core/plan/sql-format/types.ts +31 -0
  447. package/src/core/plan/sql-format/wrap.test.ts +119 -0
  448. package/src/core/plan/sql-format/wrap.ts +196 -0
  449. package/src/core/plan/sql-format.ts +2 -0
  450. package/src/core/plan/statements.ts +22 -0
  451. package/src/core/plan/types.ts +165 -0
  452. package/src/core/postgres-config.ts +169 -0
  453. package/src/core/sort/custom-constraints.ts +161 -0
  454. package/src/core/sort/debug-visualization.ts +239 -0
  455. package/src/core/sort/dependency-filter.ts +224 -0
  456. package/src/core/sort/graph-builder.ts +223 -0
  457. package/src/core/sort/graph-utils.ts +51 -0
  458. package/src/core/sort/logical-sort.ts +590 -0
  459. package/src/core/sort/sort-changes.ts +234 -0
  460. package/src/core/sort/topological-sort.ts +184 -0
  461. package/src/core/sort/types.ts +112 -0
  462. package/src/core/sort/utils.ts +69 -0
  463. package/src/index.ts +14 -0
@@ -0,0 +1,20 @@
1
+ import { BaseChange } from "../../base.change.ts";
2
+ import type { Table } from "../table.model.ts";
3
+
4
+ abstract class BaseTableChange extends BaseChange {
5
+ abstract readonly table: Table;
6
+ abstract readonly scope: "object" | "comment" | "privilege";
7
+ readonly objectType: "table" = "table";
8
+ }
9
+
10
+ export abstract class CreateTableChange extends BaseTableChange {
11
+ readonly operation = "create" as const;
12
+ }
13
+
14
+ export abstract class AlterTableChange extends BaseTableChange {
15
+ readonly operation = "alter" as const;
16
+ }
17
+
18
+ export abstract class DropTableChange extends BaseTableChange {
19
+ readonly operation = "drop" as const;
20
+ }
@@ -0,0 +1,266 @@
1
+ import { quoteLiteral } from "../../base.change.ts";
2
+ import type { ColumnProps } from "../../base.model.ts";
3
+ import { stableId } from "../../utils.ts";
4
+ import type { Table, TableConstraintProps } from "../table.model.ts";
5
+ import { CreateTableChange, DropTableChange } from "./table.base.ts";
6
+
7
+ /**
8
+ * Create a table/column/constraint comment.
9
+ *
10
+ * @see https://www.postgresql.org/docs/17/sql-comment.html
11
+ *
12
+ * Synopsis
13
+ * ```sql
14
+ * COMMENT ON
15
+ * {
16
+ * COLUMN relation_name.column_name |
17
+ * CONSTRAINT constraint_name ON table_name |
18
+ * TABLE object_name
19
+ * } IS { string_literal | NULL }
20
+ *
21
+ * ```
22
+ */
23
+
24
+ export type CommentTable =
25
+ | CreateCommentOnColumn
26
+ | CreateCommentOnConstraint
27
+ | CreateCommentOnTable
28
+ | DropCommentOnColumn
29
+ | DropCommentOnConstraint
30
+ | DropCommentOnTable;
31
+
32
+ /**
33
+ * COMMENT ON TABLE ... IS ...
34
+ */
35
+ export class CreateCommentOnTable extends CreateTableChange {
36
+ public readonly table: Table;
37
+ public readonly scope = "comment" as const;
38
+
39
+ constructor(props: { table: Table }) {
40
+ super();
41
+ this.table = props.table;
42
+ }
43
+
44
+ get creates() {
45
+ return [stableId.comment(this.table.stableId)];
46
+ }
47
+
48
+ get requires() {
49
+ return [this.table.stableId];
50
+ }
51
+
52
+ serialize(): string {
53
+ return [
54
+ "COMMENT ON TABLE",
55
+ `${this.table.schema}.${this.table.name}`,
56
+ "IS",
57
+ // biome-ignore lint/style/noNonNullAssertion: table comment is not nullable in this case
58
+ quoteLiteral(this.table.comment!),
59
+ ].join(" ");
60
+ }
61
+ }
62
+
63
+ /**
64
+ * COMMENT ON TABLE ... IS ...
65
+ */
66
+ export class DropCommentOnTable extends DropTableChange {
67
+ public readonly table: Table;
68
+ public readonly scope = "comment" as const;
69
+
70
+ constructor(props: { table: Table }) {
71
+ super();
72
+ this.table = props.table;
73
+ }
74
+
75
+ get drops() {
76
+ return [stableId.comment(this.table.stableId)];
77
+ }
78
+
79
+ get requires() {
80
+ return [stableId.comment(this.table.stableId), this.table.stableId];
81
+ }
82
+
83
+ serialize(): string {
84
+ return [
85
+ "COMMENT ON TABLE",
86
+ `${this.table.schema}.${this.table.name}`,
87
+ "IS NULL",
88
+ ].join(" ");
89
+ }
90
+ }
91
+
92
+ /**
93
+ * COMMENT ON COLUMN ... IS ...
94
+ */
95
+ export class CreateCommentOnColumn extends CreateTableChange {
96
+ public readonly table: Table;
97
+ public readonly column: ColumnProps;
98
+ public readonly scope = "comment" as const;
99
+
100
+ constructor(props: { table: Table; column: ColumnProps }) {
101
+ super();
102
+ this.table = props.table;
103
+ this.column = props.column;
104
+ }
105
+
106
+ get creates() {
107
+ const columnStableId = stableId.column(
108
+ this.table.schema,
109
+ this.table.name,
110
+ this.column.name,
111
+ );
112
+ return [stableId.comment(columnStableId)];
113
+ }
114
+
115
+ get requires() {
116
+ return [
117
+ stableId.column(this.table.schema, this.table.name, this.column.name),
118
+ ];
119
+ }
120
+
121
+ serialize(): string {
122
+ return [
123
+ "COMMENT ON COLUMN",
124
+ `${this.table.schema}.${this.table.name}.${this.column.name}`,
125
+ "IS",
126
+ // biome-ignore lint/style/noNonNullAssertion: column comment is not nullable in this case
127
+ quoteLiteral(this.column.comment!),
128
+ ].join(" ");
129
+ }
130
+ }
131
+
132
+ /**
133
+ * COMMENT ON COLUMN ... IS ...
134
+ */
135
+ export class DropCommentOnColumn extends DropTableChange {
136
+ public readonly table: Table;
137
+ public readonly column: ColumnProps;
138
+ public readonly scope = "comment" as const;
139
+
140
+ constructor(props: { table: Table; column: ColumnProps }) {
141
+ super();
142
+ this.table = props.table;
143
+ this.column = props.column;
144
+ }
145
+
146
+ get drops() {
147
+ const columnStableId = stableId.column(
148
+ this.table.schema,
149
+ this.table.name,
150
+ this.column.name,
151
+ );
152
+ return [stableId.comment(columnStableId)];
153
+ }
154
+
155
+ get requires() {
156
+ const columnStableId = stableId.column(
157
+ this.table.schema,
158
+ this.table.name,
159
+ this.column.name,
160
+ );
161
+ return [stableId.comment(columnStableId), columnStableId];
162
+ }
163
+
164
+ serialize(): string {
165
+ return [
166
+ "COMMENT ON COLUMN",
167
+ `${this.table.schema}.${this.table.name}.${this.column.name}`,
168
+ "IS NULL",
169
+ ].join(" ");
170
+ }
171
+ }
172
+
173
+ /**
174
+ * COMMENT ON CONSTRAINT ... IS ...
175
+ */
176
+ export class CreateCommentOnConstraint extends CreateTableChange {
177
+ public readonly table: Table;
178
+ public readonly constraint: TableConstraintProps;
179
+ public readonly scope = "comment" as const;
180
+
181
+ constructor(props: {
182
+ table: Table;
183
+ constraint: TableConstraintProps;
184
+ }) {
185
+ super();
186
+ this.table = props.table;
187
+ this.constraint = props.constraint;
188
+ }
189
+
190
+ get creates() {
191
+ const constraintStableId = stableId.constraint(
192
+ this.table.schema,
193
+ this.table.name,
194
+ this.constraint.name,
195
+ );
196
+ return [stableId.comment(constraintStableId)];
197
+ }
198
+
199
+ get requires() {
200
+ return [
201
+ stableId.constraint(
202
+ this.table.schema,
203
+ this.table.name,
204
+ this.constraint.name,
205
+ ),
206
+ ];
207
+ }
208
+
209
+ serialize(): string {
210
+ return [
211
+ "COMMENT ON CONSTRAINT",
212
+ this.constraint.name,
213
+ "ON",
214
+ `${this.table.schema}.${this.table.name}`,
215
+ "IS",
216
+ // biome-ignore lint/style/noNonNullAssertion: constraint comment is not nullable in this case
217
+ quoteLiteral(this.constraint.comment!),
218
+ ].join(" ");
219
+ }
220
+ }
221
+
222
+ /**
223
+ * COMMENT ON CONSTRAINT ... IS ...
224
+ */
225
+ export class DropCommentOnConstraint extends DropTableChange {
226
+ public readonly table: Table;
227
+ public readonly constraint: TableConstraintProps;
228
+ public readonly scope = "comment" as const;
229
+
230
+ constructor(props: {
231
+ table: Table;
232
+ constraint: TableConstraintProps;
233
+ }) {
234
+ super();
235
+ this.table = props.table;
236
+ this.constraint = props.constraint;
237
+ }
238
+
239
+ get drops() {
240
+ const constraintStableId = stableId.constraint(
241
+ this.table.schema,
242
+ this.table.name,
243
+ this.constraint.name,
244
+ );
245
+ return [stableId.comment(constraintStableId)];
246
+ }
247
+
248
+ get requires() {
249
+ const constraintStableId = stableId.constraint(
250
+ this.table.schema,
251
+ this.table.name,
252
+ this.constraint.name,
253
+ );
254
+ return [stableId.comment(constraintStableId), constraintStableId];
255
+ }
256
+
257
+ serialize(): string {
258
+ return [
259
+ "COMMENT ON CONSTRAINT",
260
+ this.constraint.name,
261
+ "ON",
262
+ `${this.table.schema}.${this.table.name}`,
263
+ "IS NULL",
264
+ ].join(" ");
265
+ }
266
+ }
@@ -0,0 +1,150 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { Table, type TableProps } from "../table.model.ts";
3
+ import { CreateTable } from "./table.create.ts";
4
+
5
+ const base: TableProps = {
6
+ schema: "public",
7
+ name: "t",
8
+ persistence: "p",
9
+ row_security: false,
10
+ force_row_security: false,
11
+ has_indexes: false,
12
+ has_rules: false,
13
+ has_triggers: false,
14
+ has_subclasses: false,
15
+ is_populated: true,
16
+ replica_identity: "d",
17
+ is_partition: false,
18
+ options: null,
19
+ partition_bound: null,
20
+ partition_by: null,
21
+ owner: "o1",
22
+ parent_schema: null,
23
+ parent_name: null,
24
+ columns: [],
25
+ privileges: [],
26
+ };
27
+
28
+ describe.concurrent("table.create", () => {
29
+ test("minimal create with no columns", () => {
30
+ const t = new Table(base);
31
+ const change = new CreateTable({ table: t });
32
+ expect(change.serialize()).toBe("CREATE TABLE public.t ()");
33
+ });
34
+
35
+ test("TEMPORARY with columns, inherits and options", () => {
36
+ const t = new Table({
37
+ ...base,
38
+ persistence: "t",
39
+ parent_schema: "public",
40
+ parent_name: "parent",
41
+ options: ["fillfactor=90", "autovacuum_enabled=true"],
42
+ columns: [
43
+ {
44
+ name: "c1",
45
+ position: 1,
46
+ data_type: "integer",
47
+ data_type_str: "integer",
48
+ is_custom_type: false,
49
+ custom_type_type: null,
50
+ custom_type_category: null,
51
+ custom_type_schema: null,
52
+ custom_type_name: null,
53
+ not_null: true,
54
+ is_identity: false,
55
+ is_identity_always: false,
56
+ is_generated: false,
57
+ collation: null,
58
+ default: "0",
59
+ comment: null,
60
+ },
61
+ {
62
+ name: "c2",
63
+ position: 2,
64
+ data_type: "text",
65
+ data_type_str: "text",
66
+ is_custom_type: false,
67
+ custom_type_type: null,
68
+ custom_type_category: null,
69
+ custom_type_schema: null,
70
+ custom_type_name: null,
71
+ not_null: false,
72
+ is_identity: false,
73
+ is_identity_always: false,
74
+ is_generated: false,
75
+ collation: '"en_US"',
76
+ default: null,
77
+ comment: null,
78
+ },
79
+ {
80
+ name: "c3",
81
+ position: 3,
82
+ data_type: "integer",
83
+ data_type_str: "integer",
84
+ is_custom_type: false,
85
+ custom_type_type: null,
86
+ custom_type_category: null,
87
+ custom_type_schema: null,
88
+ custom_type_name: null,
89
+ not_null: false,
90
+ is_identity: true,
91
+ is_identity_always: true,
92
+ is_generated: false,
93
+ collation: null,
94
+ default: null,
95
+ comment: null,
96
+ },
97
+ {
98
+ name: "c4",
99
+ position: 4,
100
+ data_type: "text",
101
+ data_type_str: "text",
102
+ is_custom_type: false,
103
+ custom_type_type: null,
104
+ custom_type_category: null,
105
+ custom_type_schema: null,
106
+ custom_type_name: null,
107
+ not_null: false,
108
+ is_identity: false,
109
+ is_identity_always: false,
110
+ is_generated: true,
111
+ collation: null,
112
+ default: "lower((name))",
113
+ comment: null,
114
+ },
115
+ {
116
+ name: "c5",
117
+ position: 5,
118
+ data_type: "integer",
119
+ data_type_str: "integer",
120
+ is_custom_type: false,
121
+ custom_type_type: null,
122
+ custom_type_category: null,
123
+ custom_type_schema: null,
124
+ custom_type_name: null,
125
+ not_null: false,
126
+ is_identity: true,
127
+ is_identity_always: false,
128
+ is_generated: false,
129
+ collation: null,
130
+ default: null,
131
+ comment: null,
132
+ },
133
+ ],
134
+ });
135
+
136
+ const change = new CreateTable({ table: t });
137
+ expect(change.serialize()).toBe(
138
+ 'CREATE TEMPORARY TABLE public.t (c1 integer DEFAULT 0 NOT NULL, c2 text COLLATE "en_US", c3 integer GENERATED ALWAYS AS IDENTITY, c4 text GENERATED ALWAYS AS (lower((name))) STORED, c5 integer GENERATED BY DEFAULT AS IDENTITY) INHERITS (public.parent) WITH (fillfactor=90, autovacuum_enabled=true)',
139
+ );
140
+ });
141
+
142
+ test("UNLOGGED minimal create (no columns)", () => {
143
+ const t = new Table({
144
+ ...base,
145
+ persistence: "u",
146
+ });
147
+ const change = new CreateTable({ table: t });
148
+ expect(change.serialize()).toBe("CREATE UNLOGGED TABLE public.t ()");
149
+ });
150
+ });
@@ -0,0 +1,188 @@
1
+ import { isUserDefinedTypeSchema, stableId } from "../../utils.ts";
2
+ import type { Table } from "../table.model.ts";
3
+ import { CreateTableChange } from "./table.base.ts";
4
+
5
+ /**
6
+ * Create a table.
7
+ *
8
+ * @see https://www.postgresql.org/docs/17/sql-createtable.html
9
+ *
10
+ * Synopsis
11
+ * ```sql
12
+ * CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name ( [
13
+ * { column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ]
14
+ * | table_constraint
15
+ * | LIKE source_table [ like_option ... ] }
16
+ * [, ... ]
17
+ * ] )
18
+ * [ INHERITS ( parent_table [, ... ] ) ]
19
+ * [ PARTITION BY { RANGE | LIST | HASH } ( { column_name | ( expression ) } [, ... ] ) ]
20
+ * [ USING method ]
21
+ * [ WITH ( storage_parameter [= value] [, ... ] ) | WITHOUT OIDS ]
22
+ * [ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
23
+ * [ TABLESPACE tablespace_name ]
24
+ * ```
25
+ */
26
+ export class CreateTable extends CreateTableChange {
27
+ public readonly table: Table;
28
+ public readonly scope = "object" as const;
29
+
30
+ constructor(props: { table: Table }) {
31
+ super();
32
+ this.table = props.table;
33
+ }
34
+
35
+ get creates() {
36
+ return [
37
+ this.table.stableId,
38
+ ...this.table.columns.map((col) =>
39
+ stableId.column(this.table.schema, this.table.name, col.name),
40
+ ),
41
+ ];
42
+ }
43
+
44
+ get requires() {
45
+ const dependencies = new Set<string>();
46
+
47
+ // Schema dependency
48
+ dependencies.add(stableId.schema(this.table.schema));
49
+
50
+ // Owner dependency
51
+ dependencies.add(stableId.role(this.table.owner));
52
+
53
+ // Parent table dependency (for inheritance or partitioning)
54
+ if (this.table.parent_schema && this.table.parent_name) {
55
+ dependencies.add(
56
+ stableId.table(this.table.parent_schema, this.table.parent_name),
57
+ );
58
+ }
59
+
60
+ // Column type dependencies (user-defined types only)
61
+ for (const col of this.table.columns) {
62
+ if (
63
+ col.is_custom_type &&
64
+ col.custom_type_schema &&
65
+ col.custom_type_name
66
+ ) {
67
+ dependencies.add(
68
+ stableId.type(col.custom_type_schema, col.custom_type_name),
69
+ );
70
+ }
71
+
72
+ // Collation dependency (if non-default)
73
+ if (col.collation) {
74
+ // Collations are stored as schema-qualified strings like "public.collation_name"
75
+ // Note: The collation string may be quoted, so we need to handle that
76
+ const unquotedCollation = col.collation.replace(/^"|"$/g, "");
77
+ const collationParts = unquotedCollation.split(".");
78
+ if (collationParts.length === 2) {
79
+ const [collationSchema, collationName] = collationParts;
80
+ if (isUserDefinedTypeSchema(collationSchema)) {
81
+ dependencies.add(
82
+ stableId.collation(collationSchema, collationName),
83
+ );
84
+ }
85
+ }
86
+ }
87
+ }
88
+
89
+ return Array.from(dependencies);
90
+ }
91
+
92
+ serialize(): string {
93
+ const parts: string[] = ["CREATE"];
94
+
95
+ // Add TEMPORARY/UNLOGGED based on persistence
96
+ if (this.table.persistence === "t") {
97
+ parts.push("TEMPORARY");
98
+ } else if (this.table.persistence === "u") {
99
+ parts.push("UNLOGGED");
100
+ }
101
+
102
+ parts.push("TABLE");
103
+
104
+ // Add schema and name
105
+ parts.push(`${this.table.schema}.${this.table.name}`);
106
+
107
+ // If this is a partition (child) table, emit PARTITION OF ... FOR VALUES ...
108
+ if (
109
+ this.table.parent_schema &&
110
+ this.table.parent_name &&
111
+ this.table.partition_bound
112
+ ) {
113
+ return [
114
+ ...parts,
115
+ "PARTITION OF",
116
+ `${this.table.parent_schema}.${this.table.parent_name}`,
117
+ this.table.partition_bound,
118
+ ].join(" ");
119
+ }
120
+
121
+ // Add columns definition
122
+ if (this.table.columns.length === 0) {
123
+ parts.push("()");
124
+ } else {
125
+ const columnDefinitions = this.table.columns.map((col) => {
126
+ const tokens: string[] = [];
127
+
128
+ // Column name
129
+ tokens.push(col.name);
130
+
131
+ // Data type (use formatted type string from the catalog)
132
+ tokens.push(col.data_type_str);
133
+
134
+ // Collation
135
+ if (col.collation) {
136
+ tokens.push("COLLATE", col.collation);
137
+ }
138
+
139
+ // Identity / generated / default
140
+ if (col.is_identity) {
141
+ tokens.push(
142
+ col.is_identity_always
143
+ ? "GENERATED ALWAYS AS IDENTITY"
144
+ : "GENERATED BY DEFAULT AS IDENTITY",
145
+ );
146
+ } else if (col.is_generated && col.default) {
147
+ // Generated stored columns expose their expression via pg_attrdef
148
+ tokens.push(`GENERATED ALWAYS AS (${col.default}) STORED`);
149
+ } else if (col.default) {
150
+ tokens.push("DEFAULT", col.default);
151
+ }
152
+
153
+ // Nullability
154
+ if (col.not_null) {
155
+ tokens.push("NOT NULL");
156
+ }
157
+
158
+ return tokens.join(" ");
159
+ });
160
+
161
+ parts.push(`(${columnDefinitions.join(", ")})`);
162
+ }
163
+
164
+ // Add INHERITS if parent table exists (non-partition inheritance only)
165
+ if (
166
+ this.table.parent_schema &&
167
+ this.table.parent_name &&
168
+ !this.table.partition_bound
169
+ ) {
170
+ parts.push(
171
+ "INHERITS",
172
+ `(${this.table.parent_schema}.${this.table.parent_name})`,
173
+ );
174
+ }
175
+
176
+ // Add PARTITION BY if this is a partitioned table (parent)
177
+ if (this.table.partition_by) {
178
+ parts.push("PARTITION BY", this.table.partition_by);
179
+ }
180
+
181
+ // Add storage parameters if specified
182
+ if (this.table.options && this.table.options.length > 0) {
183
+ parts.push("WITH", `(${this.table.options.join(", ")})`);
184
+ }
185
+
186
+ return parts.join(" ");
187
+ }
188
+ }
@@ -0,0 +1,34 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { Table, type TableProps } from "../table.model.ts";
3
+ import { DropTable } from "./table.drop.ts";
4
+
5
+ const base: TableProps = {
6
+ schema: "public",
7
+ name: "t",
8
+ persistence: "p",
9
+ row_security: false,
10
+ force_row_security: false,
11
+ has_indexes: false,
12
+ has_rules: false,
13
+ has_triggers: false,
14
+ has_subclasses: false,
15
+ is_populated: true,
16
+ replica_identity: "d",
17
+ is_partition: false,
18
+ options: null,
19
+ partition_bound: null,
20
+ partition_by: null,
21
+ owner: "o1",
22
+ parent_schema: null,
23
+ parent_name: null,
24
+ columns: [],
25
+ privileges: [],
26
+ };
27
+
28
+ describe.concurrent("table.drop", () => {
29
+ test("drop table basic", () => {
30
+ const t = new Table(base);
31
+ const change = new DropTable({ table: t });
32
+ expect(change.serialize()).toBe("DROP TABLE public.t");
33
+ });
34
+ });
@@ -0,0 +1,45 @@
1
+ import { stableId } from "../../utils.ts";
2
+ import type { Table } from "../table.model.ts";
3
+ import { DropTableChange } from "./table.base.ts";
4
+
5
+ /**
6
+ * Drop a table.
7
+ *
8
+ * @see https://www.postgresql.org/docs/17/sql-droptable.html
9
+ *
10
+ * Synopsis
11
+ * ```sql
12
+ * DROP TABLE [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ]
13
+ * ```
14
+ */
15
+ export class DropTable extends DropTableChange {
16
+ public readonly table: Table;
17
+ public readonly scope = "object" as const;
18
+
19
+ constructor(props: { table: Table }) {
20
+ super();
21
+ this.table = props.table;
22
+ }
23
+
24
+ get drops() {
25
+ return [
26
+ this.table.stableId,
27
+ ...this.table.columns.map((column) =>
28
+ stableId.column(this.table.schema, this.table.name, column.name),
29
+ ),
30
+ ];
31
+ }
32
+
33
+ get requires() {
34
+ return [
35
+ this.table.stableId,
36
+ ...this.table.columns.map((col) =>
37
+ stableId.column(this.table.schema, this.table.name, col.name),
38
+ ),
39
+ ];
40
+ }
41
+
42
+ serialize(): string {
43
+ return ["DROP TABLE", `${this.table.schema}.${this.table.name}`].join(" ");
44
+ }
45
+ }