forked from andika/membership-be
139 lines
3.9 KiB
Markdown
139 lines
3.9 KiB
Markdown
# 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;
|
|
```
|