general cleanup - removed old comments, enforced naming conventions etc
Some checks are pending
check / check (push) Waiting to run
Some checks are pending
check / check (push) Waiting to run
This commit is contained in:
16
backend/infra/migrations/001_add_user_id.sql
Normal file
16
backend/infra/migrations/001_add_user_id.sql
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
ALTER TABLE addresses
|
||||
ADD COLUMN user_id VARCHAR(128);
|
||||
|
||||
UPDATE addresses
|
||||
SET user_id = 'legacy_user'
|
||||
WHERE user_id IS NULL;
|
||||
|
||||
ALTER TABLE addresses
|
||||
ALTER COLUMN user_id SET NOT NULL;
|
||||
|
||||
CREATE INDEX idx_addresses_user_id ON addresses(user_id);
|
||||
|
||||
ALTER TABLE addresses DROP CONSTRAINT IF EXISTS addresses_address_key;
|
||||
|
||||
ALTER TABLE addresses ADD CONSTRAINT addresses_user_address_unique UNIQUE (user_id, address);
|
||||
13
backend/infra/migrations/002_add_notification_config.sql
Normal file
13
backend/infra/migrations/002_add_notification_config.sql
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
CREATE TABLE user_notification_configs (
|
||||
user_id VARCHAR(128) PRIMARY KEY,
|
||||
discord_webhook_url TEXT,
|
||||
telegram_chat_id VARCHAR(128),
|
||||
telegram_bot_token VARCHAR(255),
|
||||
email VARCHAR(255),
|
||||
notification_enabled BOOLEAN DEFAULT TRUE,
|
||||
created_at TIMESTAMP DEFAULT NOW(),
|
||||
updated_at TIMESTAMP DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_notification_configs_enabled ON user_notification_configs(notification_enabled);
|
||||
2
backend/infra/migrations/003_add_slack_webhook.sql
Normal file
2
backend/infra/migrations/003_add_slack_webhook.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE user_notification_configs
|
||||
ADD COLUMN IF NOT EXISTS slack_webhook_url TEXT;
|
||||
3
backend/infra/migrations/004_alert_event_dedup.sql
Normal file
3
backend/infra/migrations/004_alert_event_dedup.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_alert_events_dedup
|
||||
ON alert_events (alert_rule_id, tx_hash)
|
||||
WHERE tx_hash IS NOT NULL;
|
||||
13
backend/infra/migrations/005_create_users_table.sql
Normal file
13
backend/infra/migrations/005_create_users_table.sql
Normal file
@@ -0,0 +1,13 @@
|
||||
-- Migration 005: Create users table with UUID primary key
|
||||
-- This establishes a local user record for each Firebase-authenticated user.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
firebase_uid VARCHAR(128) NOT NULL UNIQUE,
|
||||
email VARCHAR(255) NOT NULL,
|
||||
display_name VARCHAR(255),
|
||||
created_at TIMESTAMP DEFAULT NOW(),
|
||||
updated_at TIMESTAMP DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_users_firebase_uid ON users(firebase_uid);
|
||||
73
backend/infra/migrations/006_migrate_user_ids_to_uuid.sql
Normal file
73
backend/infra/migrations/006_migrate_user_ids_to_uuid.sql
Normal file
@@ -0,0 +1,73 @@
|
||||
-- Migration 006: Migrate user_id columns from Firebase UID strings to user UUIDs
|
||||
--
|
||||
-- Prerequisites: migration 005 (users table) must be applied first.
|
||||
-- This migration backfills the users table from existing data, then swaps the
|
||||
-- VARCHAR user_id columns for UUID foreign keys referencing users(id).
|
||||
|
||||
BEGIN;
|
||||
|
||||
-- 1. Backfill users table from existing Firebase UIDs in addresses
|
||||
INSERT INTO users (firebase_uid, email)
|
||||
SELECT DISTINCT user_id, ''
|
||||
FROM addresses
|
||||
WHERE user_id IS NOT NULL
|
||||
ON CONFLICT (firebase_uid) DO NOTHING;
|
||||
|
||||
-- 2. Backfill from notification configs (catches users with configs but no addresses)
|
||||
INSERT INTO users (firebase_uid, email)
|
||||
SELECT DISTINCT user_id, ''
|
||||
FROM user_notification_configs
|
||||
WHERE user_id IS NOT NULL
|
||||
ON CONFLICT (firebase_uid) DO NOTHING;
|
||||
|
||||
-- ============================================================
|
||||
-- 3. Migrate addresses.user_id from VARCHAR to UUID
|
||||
-- ============================================================
|
||||
|
||||
ALTER TABLE addresses ADD COLUMN user_uuid UUID;
|
||||
|
||||
UPDATE addresses a
|
||||
SET user_uuid = u.id
|
||||
FROM users u
|
||||
WHERE u.firebase_uid = a.user_id;
|
||||
|
||||
-- Drop old constraints and column
|
||||
ALTER TABLE addresses DROP CONSTRAINT IF EXISTS addresses_user_id_address_key;
|
||||
DROP INDEX IF EXISTS idx_addresses_user_id;
|
||||
ALTER TABLE addresses DROP COLUMN user_id;
|
||||
|
||||
-- Rename and constrain
|
||||
ALTER TABLE addresses RENAME COLUMN user_uuid TO user_id;
|
||||
ALTER TABLE addresses ALTER COLUMN user_id SET NOT NULL;
|
||||
ALTER TABLE addresses ADD CONSTRAINT fk_addresses_user
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
|
||||
ALTER TABLE addresses ADD CONSTRAINT addresses_user_id_address_key
|
||||
UNIQUE (user_id, address);
|
||||
CREATE INDEX idx_addresses_user_id ON addresses(user_id);
|
||||
|
||||
-- ============================================================
|
||||
-- 4. Migrate user_notification_configs.user_id from VARCHAR to UUID
|
||||
-- ============================================================
|
||||
|
||||
-- Drop the PK first (it's on user_id)
|
||||
ALTER TABLE user_notification_configs DROP CONSTRAINT IF EXISTS user_notification_configs_pkey;
|
||||
DROP INDEX IF EXISTS idx_notification_configs_enabled;
|
||||
|
||||
ALTER TABLE user_notification_configs ADD COLUMN user_uuid UUID;
|
||||
|
||||
UPDATE user_notification_configs nc
|
||||
SET user_uuid = u.id
|
||||
FROM users u
|
||||
WHERE u.firebase_uid = nc.user_id;
|
||||
|
||||
ALTER TABLE user_notification_configs DROP COLUMN user_id;
|
||||
ALTER TABLE user_notification_configs RENAME COLUMN user_uuid TO user_id;
|
||||
ALTER TABLE user_notification_configs ALTER COLUMN user_id SET NOT NULL;
|
||||
ALTER TABLE user_notification_configs ADD CONSTRAINT user_notification_configs_pkey
|
||||
PRIMARY KEY (user_id);
|
||||
ALTER TABLE user_notification_configs ADD CONSTRAINT fk_notification_configs_user
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
|
||||
CREATE INDEX idx_notification_configs_enabled
|
||||
ON user_notification_configs(notification_enabled);
|
||||
|
||||
COMMIT;
|
||||
8
backend/infra/migrations/007_add_stripe_to_users.sql
Normal file
8
backend/infra/migrations/007_add_stripe_to_users.sql
Normal file
@@ -0,0 +1,8 @@
|
||||
-- Migration 007: Add Stripe subscription fields to users table
|
||||
|
||||
ALTER TABLE users ADD COLUMN IF NOT EXISTS stripe_customer_id VARCHAR(255);
|
||||
ALTER TABLE users ADD COLUMN IF NOT EXISTS stripe_subscription_id VARCHAR(255);
|
||||
ALTER TABLE users ADD COLUMN IF NOT EXISTS subscription_status VARCHAR(50) DEFAULT 'none';
|
||||
ALTER TABLE users ADD COLUMN IF NOT EXISTS subscription_created_at TIMESTAMP;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_users_stripe_customer_id ON users(stripe_customer_id);
|
||||
Reference in New Issue
Block a user