From a1a29f7c0b5746aa5676e546b8ebae7518d9070b Mon Sep 17 00:00:00 2001 From: Aspen Smith Date: Sun, 24 Mar 2024 10:32:15 -0400 Subject: feat(web/panettone): Add a system for database migrations Add a system for writing, running, and tracking database migrations (changes to the database schema) over time, inspired by but significantly simpler than postmodern-passenger-pigeon. Migrations can be generated by running (PANETTONE.MODEL:GENERATE-MIGRATION "name"), and are numerically ordered lisp files that define (at least) a function called UP, which runs the migration. The migrations that have been run against the database are tracked in the `migrations` table, and when the `(PANETTONE.MODEL:MIGRATE)` function is called (as it is on startup), all migrations that have not yet been run are run within a transaction. This includes one migration `1-init-schema.lisp`, which migrates the database (idempotently) to the current state of the schema. Change-Id: Id243a47763abea649784b12f25a6d05c2267381c Reviewed-on: https://cl.tvl.fyi/c/depot/+/11253 Tested-by: BuildkiteCI Reviewed-by: sterni --- web/panettone/src/migrations/1-init-schema.lisp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 web/panettone/src/migrations/1-init-schema.lisp (limited to 'web/panettone/src/migrations') diff --git a/web/panettone/src/migrations/1-init-schema.lisp b/web/panettone/src/migrations/1-init-schema.lisp new file mode 100644 index 000000000000..3be6c4fcc0d0 --- /dev/null +++ b/web/panettone/src/migrations/1-init-schema.lisp @@ -0,0 +1,23 @@ +"Initialize the database schema from before migrations were added" + +(defun ddl/create-issue-status () + "Issue DDL to create the `issue-status' type, if it doesn't exist" + (unless (query (:select (:exists (:select 1 + :from 'pg_type + :where (:= 'typname "issue_status")))) + :single) + (query (sql-compile + `(:create-enum issue-status ,panettone.model:+issue-statuses+))))) + +(defun ddl/create-tables () + "Issue DDL to create all tables, if they don't already exist." + (dolist (table '(panettone.model:issue + panettone.model:issue-comment + panettone.model:issue-event + panettone.model:user-settings)) + (unless (table-exists-p (dao-table-name table)) + (create-table table)))) + +(defun up () + (ddl/create-issue-status) + (ddl/create-tables)) -- cgit 1.4.1