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
24 changes: 24 additions & 0 deletions backend/fix_migrations.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@echo off
REM Fix all migrations to include uuid-ossp extension

echo Fixing migrations to include uuid-ossp extension...

REM List of migrations that need the fix (excluding init)
set migrations=20260221151200_add_notifications_and_action_logs.sql 20260224153500_add_nonces.sql 20260226000000_add_lending_events.sql 20260226140000_create_user_2fa.sql 20260324120000_add_emergency_access_tracking.sql 20260324170000_add_emergency_contacts.sql 20260324173000_add_emergency_access_audit_logs.sql 20260324180000_add_emergency_access_risk_alerts.sql 20260325100000_add_pools_for_stress_testing.sql 20260325103000_add_governance_tables.sql 20260325190000_add_emergency_access_sessions.sql

for %%f in (%migrations%) do (
echo Checking migrations\%%f
findstr /C:"CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\"" migrations\%%f >nul
if errorlevel 1 (
echo Adding extension to %%f
echo -- Ensure UUID extension is available> temp.sql
echo CREATE EXTENSION IF NOT EXISTS "uuid-ossp";>> temp.sql
echo.>> temp.sql
type migrations\%%f >> temp.sql
move /Y temp.sql migrations\%%f >nul
) else (
echo %%f already has extension
)
)

echo Done!
25 changes: 10 additions & 15 deletions backend/migrations/20260221120000_add_plans_and_logs.sql
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
-- Migration: Add plans and plan_logs tables

CREATE TABLE plans (
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL,
amount NUMERIC(20, 6) NOT NULL,
fee NUMERIC(20, 6) NOT NULL,
net_amount NUMERIC(20, 6) NOT NULL,
status VARCHAR(32) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);
-- Migration: Add plan_logs table
-- NOTE:
-- The plans table is already created in the initial migration.
-- This migration should only add the dependent plan_logs table.

CREATE TABLE plan_logs (
id SERIAL PRIMARY KEY,
plan_id INTEGER NOT NULL REFERENCES plans(id),
plan_id UUID NOT NULL REFERENCES plans(id) ON DELETE CASCADE,
action VARCHAR(64) NOT NULL,
performed_by INTEGER NOT NULL,
timestamp TIMESTAMP NOT NULL DEFAULT NOW()
performed_by UUID NOT NULL,
timestamp TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_plan_logs_plan_id ON plan_logs(plan_id);
CREATE INDEX idx_plan_logs_performed_by ON plan_logs(performed_by);
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
-- Add unique constraint on plan_id in claims table to prevent duplicate claims
-- This ensures only one claim per plan, preventing race condition vulnerabilities

-- First, remove any existing duplicate claims (keep the first one)
-- First, remove any existing duplicate claims (keep the first one by created_at)
DELETE FROM claims
WHERE id NOT IN (
SELECT MIN(id)
SELECT DISTINCT ON (plan_id) id
FROM claims
GROUP BY plan_id
ORDER BY plan_id, created_at ASC
);

-- Drop the existing unique constraint on (plan_id, beneficiary_email)
Expand Down
Binary file modified backend/migrations/20260224153500_add_nonces.sql
Binary file not shown.
Binary file modified backend/migrations/20260226000000_add_lending_events.sql
Binary file not shown.
Binary file modified backend/migrations/20260226140000_create_user_2fa.sql
Binary file not shown.
Binary file not shown.
Binary file modified backend/migrations/20260324170000_add_emergency_contacts.sql
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified backend/migrations/20260325100000_add_pools_for_stress_testing.sql
Binary file not shown.
Binary file modified backend/migrations/20260325103000_add_governance_tables.sql
Binary file not shown.
Binary file not shown.
6 changes: 3 additions & 3 deletions backend/tests/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl TestContext {
let expires_at = chrono::Utc::now() + chrono::Duration::minutes(5);

sqlx::query(
"INSERT INTO user_2fa (user_id, otp_hash, expires_at) VALUES ($1, $2, $3) ON CONFLICT (user_id) DO UPDATE SET otp_hash = $2, expires_at = $3"
"INSERT INTO user_2fa (user_id, otp_hash, expires_at) VALUES ($1, $2, $3) ON CONFLICT (user_id) DO UPDATE SET otp_hash = $2, expires_at = $3",
)
.bind(user_id)
.bind(otp_hash)
Expand All @@ -82,7 +82,7 @@ pub async fn create_test_user(pool: &PgPool, email: &str) -> sqlx::Result<uuid::
let wallet = format!("G{}", &user_id.to_string().replace("-", "")[..55]);

sqlx::query(
"INSERT INTO users (id, email, wallet_address, kyc_status) VALUES ($1, $2, $3, 'approved')"
"INSERT INTO users (id, email, wallet_address, kyc_status) VALUES ($1, $2, $3, 'approved')",
)
.bind(user_id)
.bind(email)
Expand All @@ -99,7 +99,7 @@ pub async fn create_test_admin(pool: &PgPool, email: &str) -> sqlx::Result<uuid:
let password_hash = bcrypt::hash("test_password", bcrypt::DEFAULT_COST).unwrap();

sqlx::query(
"INSERT INTO admins (id, email, password_hash, status) VALUES ($1, $2, $3, 'active')"
"INSERT INTO admins (id, email, password_hash, status) VALUES ($1, $2, $3, 'active')",
)
.bind(admin_id)
.bind(email)
Expand Down
Loading