Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
VITE_SUPABASE_URL=
VITE_SUPABASE_PUBLISHABLE_KEY=

# Database connection for migrations (Settings → Database in Supabase dashboard)
DATABASE_URL=postgresql://postgres.[project-ref]:[password]@aws-0-[region].pooler.supabase.com:6543/postgres
31 changes: 31 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,37 @@ jobs:
- name: Unit & Integration Tests
run: npm test -- --reporter=verbose

migrate-check:
name: Migration Check
runs-on: ubuntu-latest
env:
DATABASE_URL: postgresql://postgres:postgres@127.0.0.1:54322/postgres
steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm

- uses: supabase/setup-cli@v1
with:
version: latest

- run: npm ci

- name: Start Supabase
run: supabase init --with-intellij-settings false && supabase start

- name: Migrate up
run: npm run migrate:up

- name: Migrate down
run: npm run migrate:down

- name: Migrate up again (verify reversibility)
run: npm run migrate:up

build:
name: Build
runs-on: ubuntu-latest
Expand Down
26 changes: 26 additions & 0 deletions .github/workflows/deploy-migrations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Deploy Migrations

on:
push:
branches: [main]
paths:
- 'migrations/**'

jobs:
migrate:
name: Run Migrations
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm

- run: npm ci

- name: Apply pending migrations
run: npm run migrate:up
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
34 changes: 21 additions & 13 deletions supabase/schema.sql → migrations/1772922678763_initial-schema.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
-- Swarm Intelligence Platform - Supabase Schema
-- Run this in the Supabase SQL Editor to set up the database
-- Up Migration

-- 1. Profiles (auto-created on sign-up)
CREATE TABLE IF NOT EXISTS profiles (
-- Profiles (auto-created on sign-up via trigger)
CREATE TABLE profiles (
id UUID PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE,
display_name TEXT NOT NULL DEFAULT '',
avatar_url TEXT,
Expand All @@ -18,8 +17,8 @@ CREATE POLICY "Anyone can read profiles"
CREATE POLICY "Users can update own profile"
ON profiles FOR UPDATE USING (auth.uid() = id);

-- 2. Saved simulations
CREATE TABLE IF NOT EXISTS saved_simulations (
-- Saved simulations
CREATE TABLE saved_simulations (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
title TEXT NOT NULL,
Expand Down Expand Up @@ -51,13 +50,13 @@ CREATE POLICY "Users can delete own simulations"
ON saved_simulations FOR DELETE
USING (auth.uid() = user_id);

CREATE INDEX IF NOT EXISTS idx_saved_simulations_user ON saved_simulations(user_id);
CREATE INDEX idx_saved_simulations_user ON saved_simulations(user_id);

-- 3. Shared links
CREATE TABLE IF NOT EXISTS shared_links (
-- Shared links
CREATE TABLE shared_links (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
simulation_id UUID NOT NULL REFERENCES saved_simulations(id) ON DELETE CASCADE,
share_token TEXT NOT NULL UNIQUE DEFAULT encode(gen_random_bytes(12), 'hex'),
share_token TEXT NOT NULL UNIQUE DEFAULT replace(gen_random_uuid()::text, '-', ''),
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
expires_at TIMESTAMPTZ
);
Expand Down Expand Up @@ -85,9 +84,9 @@ CREATE POLICY "Users can delete own shared links"
)
);

CREATE INDEX IF NOT EXISTS idx_shared_links_token ON shared_links(share_token);
CREATE INDEX idx_shared_links_token ON shared_links(share_token);

-- 4. Auto-create profile on sign-up
-- Auto-create profile on sign-up
CREATE OR REPLACE FUNCTION handle_new_user()
RETURNS TRIGGER AS $$
BEGIN
Expand All @@ -101,7 +100,16 @@ BEGIN
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;

DROP TRIGGER IF EXISTS on_auth_user_created ON auth.users;
CREATE TRIGGER on_auth_user_created
AFTER INSERT ON auth.users
FOR EACH ROW EXECUTE FUNCTION handle_new_user();

-- Down Migration

DROP TRIGGER IF EXISTS on_auth_user_created ON auth.users;
DROP FUNCTION IF EXISTS handle_new_user();
DROP INDEX IF EXISTS idx_shared_links_token;
DROP TABLE IF EXISTS shared_links;
DROP INDEX IF EXISTS idx_saved_simulations_user;
DROP TABLE IF EXISTS saved_simulations;
DROP TABLE IF EXISTS profiles;
Loading
Loading