Update New Features

This commit is contained in:
Koncept Kit
2025-12-10 17:52:32 +07:00
parent 005c56b43d
commit f051976881
20 changed files with 2776 additions and 57 deletions

138
migrations/README.md Normal file
View File

@@ -0,0 +1,138 @@
# Database Migrations
This folder contains SQL migration scripts for the membership platform.
## Running the Sprint 1-3 Migration
The `sprint_1_2_3_migration.sql` file adds all necessary columns and tables for the Members Only features (Sprints 1, 2, and 3).
### Prerequisites
- PostgreSQL installed and running
- Database created (e.g., `membership_db`)
- Database connection credentials from your `.env` file
### Option 1: Using psql command line
```bash
# Navigate to the migrations directory
cd /Users/andika/Documents/Works/Koncept\ Kit/KKN/membership-website/backend/migrations
# Run the migration (replace with your database credentials)
psql -U your_username -d membership_db -f sprint_1_2_3_migration.sql
# Or if you have a connection string
psql "postgresql://user:password@localhost:5432/membership_db" -f sprint_1_2_3_migration.sql
```
### Option 2: Using pgAdmin or another GUI tool
1. Open pgAdmin and connect to your database
2. Open the Query Tool
3. Load the `sprint_1_2_3_migration.sql` file
4. Execute the script
### Option 3: Using Python script
```bash
cd /Users/andika/Documents/Works/Koncept\ Kit/KKN/membership-website/backend
# Run the migration using Python
python3 -c "
import psycopg2
import os
from dotenv import load_dotenv
load_dotenv()
DATABASE_URL = os.getenv('DATABASE_URL')
conn = psycopg2.connect(DATABASE_URL)
cur = conn.cursor()
with open('migrations/sprint_1_2_3_migration.sql', 'r') as f:
sql = f.read()
cur.execute(sql)
conn.commit()
cur.close()
conn.close()
print('Migration completed successfully!')
"
```
### What Gets Added
**Users Table:**
- `profile_photo_url` - Stores Cloudflare R2 URL for profile photos
- `social_media_facebook` - Facebook profile/page URL
- `social_media_instagram` - Instagram handle or URL
- `social_media_twitter` - Twitter/X handle or URL
- `social_media_linkedin` - LinkedIn profile URL
**Events Table:**
- `microsoft_calendar_id` - Microsoft Calendar event ID for syncing
- `microsoft_calendar_sync_enabled` - Boolean flag for sync status
**New Tables:**
- `event_galleries` - Stores event photos with captions
- `newsletter_archives` - Stores newsletter documents
- `financial_reports` - Stores annual financial reports
- `bylaws_documents` - Stores organization bylaws
- `storage_usage` - Tracks Cloudflare R2 storage usage
### Verification
After running the migration, verify it worked:
```sql
-- Check users table columns
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name = 'users'
AND column_name IN ('profile_photo_url', 'social_media_facebook');
-- Check new tables exist
SELECT table_name
FROM information_schema.tables
WHERE table_name IN ('event_galleries', 'storage_usage');
-- Check storage_usage has initial record
SELECT * FROM storage_usage;
```
### Troubleshooting
**Error: "relation does not exist"**
- Make sure you're connected to the correct database
- Verify the `users` and `events` tables exist first
**Error: "column already exists"**
- This is safe to ignore - the script uses `IF NOT EXISTS` clauses
**Error: "permission denied"**
- Make sure your database user has ALTER TABLE privileges
- You may need to run as a superuser or database owner
### Rollback (if needed)
If you need to undo the migration:
```sql
-- Remove new columns from users
ALTER TABLE users DROP COLUMN IF EXISTS profile_photo_url;
ALTER TABLE users DROP COLUMN IF EXISTS social_media_facebook;
ALTER TABLE users DROP COLUMN IF EXISTS social_media_instagram;
ALTER TABLE users DROP COLUMN IF EXISTS social_media_twitter;
ALTER TABLE users DROP COLUMN IF EXISTS social_media_linkedin;
-- Remove new columns from events
ALTER TABLE events DROP COLUMN IF EXISTS microsoft_calendar_id;
ALTER TABLE events DROP COLUMN IF EXISTS microsoft_calendar_sync_enabled;
-- Remove new tables
DROP TABLE IF EXISTS event_galleries;
DROP TABLE IF EXISTS newsletter_archives;
DROP TABLE IF EXISTS financial_reports;
DROP TABLE IF EXISTS bylaws_documents;
DROP TABLE IF EXISTS storage_usage;
```

View File

@@ -0,0 +1,38 @@
-- Migration: Add calendar_uid to events table and remove Microsoft Calendar columns
-- Sprint 2: Universal Calendar Export
-- Add new calendar_uid column
ALTER TABLE events ADD COLUMN IF NOT EXISTS calendar_uid VARCHAR;
-- Remove old Microsoft Calendar columns (if they exist)
ALTER TABLE events DROP COLUMN IF EXISTS microsoft_calendar_id;
ALTER TABLE events DROP COLUMN IF EXISTS microsoft_calendar_sync_enabled;
-- Verify migration
DO $$
BEGIN
-- Check if calendar_uid exists
IF EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'events' AND column_name = 'calendar_uid'
) THEN
RAISE NOTICE '✅ calendar_uid column added successfully';
ELSE
RAISE NOTICE '⚠️ calendar_uid column not found';
END IF;
-- Check if old columns are removed
IF NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'events' AND column_name = 'microsoft_calendar_id'
) THEN
RAISE NOTICE '✅ microsoft_calendar_id column removed';
END IF;
IF NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'events' AND column_name = 'microsoft_calendar_sync_enabled'
) THEN
RAISE NOTICE '✅ microsoft_calendar_sync_enabled column removed';
END IF;
END $$;

161
migrations/complete_fix.sql Normal file
View File

@@ -0,0 +1,161 @@
-- Complete Fix for Sprint 1-3 Migration
-- Safe to run multiple times
-- ==============================================
-- Step 1: Add columns to users table
-- ==============================================
DO $$
BEGIN
-- Add profile_photo_url
IF NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'users' AND column_name = 'profile_photo_url'
) THEN
ALTER TABLE users ADD COLUMN profile_photo_url VARCHAR;
RAISE NOTICE 'Added profile_photo_url to users table';
ELSE
RAISE NOTICE 'profile_photo_url already exists in users table';
END IF;
-- Add social_media_facebook
IF NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'users' AND column_name = 'social_media_facebook'
) THEN
ALTER TABLE users ADD COLUMN social_media_facebook VARCHAR;
RAISE NOTICE 'Added social_media_facebook to users table';
ELSE
RAISE NOTICE 'social_media_facebook already exists in users table';
END IF;
-- Add social_media_instagram
IF NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'users' AND column_name = 'social_media_instagram'
) THEN
ALTER TABLE users ADD COLUMN social_media_instagram VARCHAR;
RAISE NOTICE 'Added social_media_instagram to users table';
ELSE
RAISE NOTICE 'social_media_instagram already exists in users table';
END IF;
-- Add social_media_twitter
IF NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'users' AND column_name = 'social_media_twitter'
) THEN
ALTER TABLE users ADD COLUMN social_media_twitter VARCHAR;
RAISE NOTICE 'Added social_media_twitter to users table';
ELSE
RAISE NOTICE 'social_media_twitter already exists in users table';
END IF;
-- Add social_media_linkedin
IF NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'users' AND column_name = 'social_media_linkedin'
) THEN
ALTER TABLE users ADD COLUMN social_media_linkedin VARCHAR;
RAISE NOTICE 'Added social_media_linkedin to users table';
ELSE
RAISE NOTICE 'social_media_linkedin already exists in users table';
END IF;
END $$;
-- ==============================================
-- Step 2: Add columns to events table
-- ==============================================
DO $$
BEGIN
-- Add microsoft_calendar_id
IF NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'events' AND column_name = 'microsoft_calendar_id'
) THEN
ALTER TABLE events ADD COLUMN microsoft_calendar_id VARCHAR;
RAISE NOTICE 'Added microsoft_calendar_id to events table';
ELSE
RAISE NOTICE 'microsoft_calendar_id already exists in events table';
END IF;
-- Add microsoft_calendar_sync_enabled
IF NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'events' AND column_name = 'microsoft_calendar_sync_enabled'
) THEN
ALTER TABLE events ADD COLUMN microsoft_calendar_sync_enabled BOOLEAN DEFAULT FALSE;
RAISE NOTICE 'Added microsoft_calendar_sync_enabled to events table';
ELSE
RAISE NOTICE 'microsoft_calendar_sync_enabled already exists in events table';
END IF;
END $$;
-- ==============================================
-- Step 3: Fix storage_usage initialization
-- ==============================================
-- Delete any incomplete records
DELETE FROM storage_usage WHERE id IS NULL;
-- Insert initial record if table is empty
INSERT INTO storage_usage (id, total_bytes_used, max_bytes_allowed, last_updated)
SELECT
gen_random_uuid(),
0,
10737418240, -- 10GB default
CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM storage_usage);
-- ==============================================
-- Step 4: Verify everything
-- ==============================================
DO $$
DECLARE
user_col_count INT;
event_col_count INT;
storage_count INT;
BEGIN
-- Count users columns
SELECT COUNT(*) INTO user_col_count
FROM information_schema.columns
WHERE table_name = 'users'
AND column_name IN (
'profile_photo_url',
'social_media_facebook',
'social_media_instagram',
'social_media_twitter',
'social_media_linkedin'
);
-- Count events columns
SELECT COUNT(*) INTO event_col_count
FROM information_schema.columns
WHERE table_name = 'events'
AND column_name IN (
'microsoft_calendar_id',
'microsoft_calendar_sync_enabled'
);
-- Count storage_usage records
SELECT COUNT(*) INTO storage_count FROM storage_usage;
-- Report results
RAISE NOTICE '';
RAISE NOTICE '========================================';
RAISE NOTICE 'Migration Verification Results:';
RAISE NOTICE '========================================';
RAISE NOTICE 'Users table: %/5 columns added', user_col_count;
RAISE NOTICE 'Events table: %/2 columns added', event_col_count;
RAISE NOTICE 'Storage usage: % record(s)', storage_count;
RAISE NOTICE '';
IF user_col_count = 5 AND event_col_count = 2 AND storage_count > 0 THEN
RAISE NOTICE '✅ Migration completed successfully!';
ELSE
RAISE NOTICE '⚠️ Migration incomplete. Please check the logs above.';
END IF;
RAISE NOTICE '========================================';
END $$;

View File

@@ -0,0 +1,17 @@
-- Fix storage_usage table initialization
-- This script safely initializes storage_usage if empty
-- Delete any incomplete records first
DELETE FROM storage_usage WHERE id IS NULL;
-- Insert with explicit UUID generation if table is empty
INSERT INTO storage_usage (id, total_bytes_used, max_bytes_allowed, last_updated)
SELECT
gen_random_uuid(),
0,
10737418240, -- 10GB default
CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM storage_usage);
-- Verify the record was created
SELECT * FROM storage_usage;

View File

@@ -0,0 +1,117 @@
-- Sprint 1, 2, 3 Database Migration
-- This script adds all new columns and tables for Members Only features
-- ==============================================
-- Step 1: Add new columns to users table
-- ==============================================
-- Add profile photo and social media columns (Sprint 1)
ALTER TABLE users ADD COLUMN IF NOT EXISTS profile_photo_url VARCHAR;
ALTER TABLE users ADD COLUMN IF NOT EXISTS social_media_facebook VARCHAR;
ALTER TABLE users ADD COLUMN IF NOT EXISTS social_media_instagram VARCHAR;
ALTER TABLE users ADD COLUMN IF NOT EXISTS social_media_twitter VARCHAR;
ALTER TABLE users ADD COLUMN IF NOT EXISTS social_media_linkedin VARCHAR;
-- ==============================================
-- Step 2: Add new columns to events table
-- ==============================================
-- Add Microsoft Calendar integration columns (Sprint 2)
ALTER TABLE events ADD COLUMN IF NOT EXISTS microsoft_calendar_id VARCHAR;
ALTER TABLE events ADD COLUMN IF NOT EXISTS microsoft_calendar_sync_enabled BOOLEAN DEFAULT FALSE;
-- ==============================================
-- Step 3: Create new tables
-- ==============================================
-- EventGallery table (Sprint 3)
CREATE TABLE IF NOT EXISTS event_galleries (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
event_id UUID NOT NULL REFERENCES events(id) ON DELETE CASCADE,
image_url VARCHAR NOT NULL,
image_key VARCHAR NOT NULL,
caption TEXT,
uploaded_by UUID NOT NULL REFERENCES users(id),
file_size_bytes INTEGER NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Create index for faster queries
CREATE INDEX IF NOT EXISTS idx_event_galleries_event_id ON event_galleries(event_id);
CREATE INDEX IF NOT EXISTS idx_event_galleries_uploaded_by ON event_galleries(uploaded_by);
-- NewsletterArchive table (Sprint 4 - preparing ahead)
CREATE TABLE IF NOT EXISTS newsletter_archives (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
title VARCHAR NOT NULL,
description TEXT,
published_date TIMESTAMP WITH TIME ZONE NOT NULL,
document_url VARCHAR NOT NULL,
document_type VARCHAR DEFAULT 'google_docs',
file_size_bytes INTEGER,
created_by UUID NOT NULL REFERENCES users(id),
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_newsletter_archives_published_date ON newsletter_archives(published_date DESC);
CREATE INDEX IF NOT EXISTS idx_newsletter_archives_created_by ON newsletter_archives(created_by);
-- FinancialReport table (Sprint 4 - preparing ahead)
CREATE TABLE IF NOT EXISTS financial_reports (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
year INTEGER NOT NULL,
title VARCHAR NOT NULL,
document_url VARCHAR NOT NULL,
document_type VARCHAR DEFAULT 'google_drive',
file_size_bytes INTEGER,
created_by UUID NOT NULL REFERENCES users(id),
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_financial_reports_year ON financial_reports(year DESC);
CREATE INDEX IF NOT EXISTS idx_financial_reports_created_by ON financial_reports(created_by);
-- BylawsDocument table (Sprint 4 - preparing ahead)
CREATE TABLE IF NOT EXISTS bylaws_documents (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
title VARCHAR NOT NULL,
version VARCHAR NOT NULL,
effective_date TIMESTAMP WITH TIME ZONE NOT NULL,
document_url VARCHAR NOT NULL,
document_type VARCHAR DEFAULT 'google_drive',
file_size_bytes INTEGER,
is_current BOOLEAN DEFAULT TRUE,
created_by UUID NOT NULL REFERENCES users(id),
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_bylaws_documents_is_current ON bylaws_documents(is_current);
CREATE INDEX IF NOT EXISTS idx_bylaws_documents_created_by ON bylaws_documents(created_by);
-- StorageUsage table (Sprint 1)
CREATE TABLE IF NOT EXISTS storage_usage (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
total_bytes_used BIGINT DEFAULT 0,
max_bytes_allowed BIGINT NOT NULL,
last_updated TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Insert initial storage usage record
INSERT INTO storage_usage (total_bytes_used, max_bytes_allowed)
SELECT 0, 10737418240
WHERE NOT EXISTS (SELECT 1 FROM storage_usage);
-- ==============================================
-- Migration Complete
-- ==============================================
-- Verify migrations
DO $$
BEGIN
RAISE NOTICE 'Migration completed successfully!';
RAISE NOTICE 'New columns added to users table: profile_photo_url, social_media_*';
RAISE NOTICE 'New columns added to events table: microsoft_calendar_*';
RAISE NOTICE 'New tables created: event_galleries, newsletter_archives, financial_reports, bylaws_documents, storage_usage';
END $$;

View File

@@ -0,0 +1,54 @@
-- Verification script to check which columns exist
-- Run this to see what's missing
-- Check users table columns
SELECT
'users' as table_name,
column_name,
data_type,
is_nullable
FROM information_schema.columns
WHERE table_name = 'users'
AND column_name IN (
'profile_photo_url',
'social_media_facebook',
'social_media_instagram',
'social_media_twitter',
'social_media_linkedin'
)
ORDER BY column_name;
-- Check events table columns
SELECT
'events' as table_name,
column_name,
data_type,
is_nullable
FROM information_schema.columns
WHERE table_name = 'events'
AND column_name IN (
'microsoft_calendar_id',
'microsoft_calendar_sync_enabled'
)
ORDER BY column_name;
-- Check which tables exist
SELECT
table_name,
'EXISTS' as status
FROM information_schema.tables
WHERE table_name IN (
'event_galleries',
'newsletter_archives',
'financial_reports',
'bylaws_documents',
'storage_usage'
)
ORDER BY table_name;
-- Check storage_usage contents
SELECT
COUNT(*) as record_count,
SUM(total_bytes_used) as total_bytes,
MAX(max_bytes_allowed) as max_bytes
FROM storage_usage;