🚀
ShipClojure
  • README
  • Development
    • Getting Started
    • REPL Workflow
    • AI Development with ShipClojure
    • Getting Updates
    • Formatting code
    • ShipClojure Guiding Principles
  • Backend
    • Migrations
    • Secrets
    • Routing
    • ShipClojure Blog
    • Email
  • Frontend
    • UIx + re-frame
    • HTTP Requests with Re-frame
    • Frontend Navigation with Re-frame
    • Toast Notifications
    • Icons
  • Server Side Rendering
    • Static/Landing pages
  • Auth
    • How Auth works
    • Oauth2 providers
  • Deployment
    • Deployment
  • Decisions
    • 001 - Cookie Sessions
    • 002 - Single Page Application Architecture
    • 003 - Re-frame instead of Refx
    • 003 - Move from cookie sessions to JWT Access + refresh tokens
Powered by GitBook
On this page
  • Store
  • Creating a migration
  • Rolling back migrations
  1. Backend

Migrations

PreviousShipClojure Guiding PrinciplesNextSecrets

Last updated 1 month ago

Shipclojure uses to handle migrations. For ease of creating migrations, there is the

Store

By default, ShipClojure stores migrations in a table in the main DB

See db.sql/migrations key in

Creating a migration

Let's say you need to create a new image table in the DB to store user images.

  1. Visit

  2. In the last comment, you'll see imported the migration-config from user

  3. Write in the same comment

  (create-migration (migration-config) "add-images-table" :sql)
  1. Evaluate this in the REPL

  2. Expect to see 2 new files {current-date}-add-images-table.{up,down}.sql in resources/db/migrations

  3. Add the correct up & down configurations, and reset the integrant system. The migrations will be applied automatically

Important: If you want to include multiple statements in the same migration file there is a special notation (--;;) you need to use, otherwise you'll get an error Too many update results were returned:

ALTER TABLE user_account
    DROP COLUMN IF EXISTS image_url;
--;;
CREATE TABLE user_image (
    id TEXT NOT NULL PRIMARY KEY,
    alt_text TEXT,
    content_type TEXT NOT NULL,
    blob BYTEA NOT NULL,
    created_at TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    user_id TEXT NOT NULL REFERENCES user_account(id) ON DELETE CASCADE ON UPDATE CASCADE
);
--;;
CREATE UNIQUE INDEX user_image_user_id_idx ON user_image(user_id);

Rolling back migrations

To roll back migrations, use user/rollback-latest-migration

migratus
migrations namespace
system.edn
migrations.clj