feat: organize SQL scripts into structured migration folders
- Create organized migration folder structure: - database/ - Core schema changes and migrations - tests/ - Test scripts and verification queries - directus/ - Directus CMS configuration scripts - fixes/ - Database fixes and patches - archive/ - Historical and deprecated scripts - Move 60+ SQL files from root to appropriate folders - Add comprehensive README with usage guidelines - Consolidate old migrations_archive into new archive folder - Maintain clear separation of concerns for different script types Benefits: - Cleaner project root directory - Easier to find specific types of SQL scripts - Better organization for maintenance and development - Clear documentation for migration procedures - Proper separation of production vs development scripts
This commit is contained in:
parent
73019a0e6c
commit
0bb1dd4055
165
migrations/README.md
Normal file
165
migrations/README.md
Normal file
|
|
@ -0,0 +1,165 @@
|
||||||
|
# Database Migrations & SQL Scripts
|
||||||
|
|
||||||
|
This directory contains all SQL scripts organized by purpose and category.
|
||||||
|
|
||||||
|
## 📁 **Directory Structure**
|
||||||
|
|
||||||
|
### 🗄️ **`database/`**
|
||||||
|
Core database schema changes and migration scripts that modify the database structure.
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- `create_verification_tokens.sql` - Creates email verification tokens table
|
||||||
|
- `fix_constraint.sql` - Fixes database constraint syntax errors
|
||||||
|
- `update_user_status.sql` - Updates user status enum with new moderation states
|
||||||
|
|
||||||
|
**Usage:**
|
||||||
|
```bash
|
||||||
|
# Apply database migrations
|
||||||
|
psql -d postgres -f migrations/database/create_verification_tokens.sql
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 🧪 **`tests/`**
|
||||||
|
Test scripts, verification queries, and diagnostic SQL used for testing and debugging.
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- `check_*.sql` - Various database inspection queries
|
||||||
|
- `test_*.sql` - Test scripts for features
|
||||||
|
- `count_*.sql` - Count queries for verification
|
||||||
|
- `verify_*.sql` - Verification scripts
|
||||||
|
- `final_*.sql` - Final system tests
|
||||||
|
|
||||||
|
**Usage:**
|
||||||
|
```bash
|
||||||
|
# Run verification tests
|
||||||
|
psql -d postgres -f migrations/tests/check_tables.sql
|
||||||
|
psql -d postgres -f migrations/tests/test_moderation_integration.sql
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 🎨 **`directus/`**
|
||||||
|
Directus CMS configuration, collections, permissions, and UI setup scripts.
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- `add_directus_*.sql` - Directus collections and fields
|
||||||
|
- `fix_directus_*.sql` - Directus configuration fixes
|
||||||
|
- `use_existing_policy.sql` - Directus policy setup
|
||||||
|
- `fix_permissions_correct.sql` - Permission fixes
|
||||||
|
|
||||||
|
**Usage:**
|
||||||
|
```bash
|
||||||
|
# Setup Directus collections
|
||||||
|
psql -d postgres -f migrations/directus/add_directus_collections.sql
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 🔧 **`fixes/`**
|
||||||
|
Database fixes, patches, and repair scripts for specific issues.
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- `fix_collections_complete.sql` - Complete Directus collections fix
|
||||||
|
- `grant_permissions.sql` - Database permission grants
|
||||||
|
|
||||||
|
**Usage:**
|
||||||
|
```bash
|
||||||
|
# Apply database fixes
|
||||||
|
psql -d postgres -f migrations/fixes/fix_collections_complete.sql
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 📦 **`archive/`**
|
||||||
|
Historical scripts, temporary queries, and deprecated SQL files.
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- All historical migration scripts
|
||||||
|
- Temporary diagnostic queries
|
||||||
|
- Deprecated or one-time scripts
|
||||||
|
- Original migrations_archive content
|
||||||
|
|
||||||
|
**Usage:**
|
||||||
|
```bash
|
||||||
|
# Archive scripts - for reference only
|
||||||
|
# Generally should not be run on production
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🚀 **Official Migrations**
|
||||||
|
|
||||||
|
The official Go backend migrations are located in:
|
||||||
|
```
|
||||||
|
go-backend/internal/database/migrations/
|
||||||
|
```
|
||||||
|
|
||||||
|
These are timestamped migrations that are automatically applied by the Go application.
|
||||||
|
|
||||||
|
**Current Official Migrations:**
|
||||||
|
- `20260205000002_user_appeal_system_fixed.up.sql` - User appeal system
|
||||||
|
- `20260205000003_add_email_preferences.up.sql` - Email preferences
|
||||||
|
|
||||||
|
## 📋 **Usage Guidelines**
|
||||||
|
|
||||||
|
### **🔴 Production**
|
||||||
|
- Only use scripts from `database/` and official Go migrations
|
||||||
|
- Test thoroughly in staging first
|
||||||
|
- Always backup before applying
|
||||||
|
|
||||||
|
### **🟡 Staging**
|
||||||
|
- Can use `tests/` and `directus/` scripts
|
||||||
|
- Verify functionality before production
|
||||||
|
|
||||||
|
### **🟢 Development**
|
||||||
|
- All scripts available for testing
|
||||||
|
- Use `archive/` for reference and experimentation
|
||||||
|
|
||||||
|
### **⚠️ Important Notes**
|
||||||
|
- Always review scripts before running
|
||||||
|
- Check for dependencies between scripts
|
||||||
|
- Some scripts may be one-time use only
|
||||||
|
- Archive scripts may be outdated
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🔄 **Migration Order**
|
||||||
|
|
||||||
|
For new deployments, follow this order:
|
||||||
|
|
||||||
|
1. **Database Schema** (`database/`)
|
||||||
|
2. **Directus Setup** (`directus/`)
|
||||||
|
3. **Apply Fixes** (`fixes/`)
|
||||||
|
4. **Run Tests** (`tests/`)
|
||||||
|
5. **Official Go Migrations** (auto-applied)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📝 **Adding New Scripts**
|
||||||
|
|
||||||
|
When adding new SQL scripts:
|
||||||
|
|
||||||
|
1. **Choose appropriate folder** based on purpose
|
||||||
|
2. **Use descriptive naming** with date/prefix
|
||||||
|
3. **Add comments** explaining purpose
|
||||||
|
4. **Test thoroughly** before committing
|
||||||
|
5. **Update this README** if adding new categories
|
||||||
|
|
||||||
|
**Naming Convention:**
|
||||||
|
- `YYYY-MM-DD_description.sql` for migrations
|
||||||
|
- `check_feature.sql` for verification scripts
|
||||||
|
- `test_feature.sql` for test scripts
|
||||||
|
- `fix_issue.sql` for fix scripts
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🛠️ **Maintenance**
|
||||||
|
|
||||||
|
- **Review archive quarterly** - remove outdated scripts
|
||||||
|
- **Update documentation** - keep README current
|
||||||
|
- **Test migrations** - ensure they work with current schema
|
||||||
|
- **Backup before changes** - always backup production data
|
||||||
|
|
||||||
|
**Last Updated:** 2026-02-05
|
||||||
|
**Maintainer:** Development Team
|
||||||
22
migrations/database/create_verification_tokens.sql
Normal file
22
migrations/database/create_verification_tokens.sql
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
-- Create verification_tokens table for email verification
|
||||||
|
CREATE TABLE IF NOT EXISTS verification_tokens (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
token_hash VARCHAR(64) NOT NULL UNIQUE,
|
||||||
|
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||||
|
expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||||
|
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||||
|
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Create indexes for performance
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_verification_tokens_token_hash ON verification_tokens(token_hash);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_verification_tokens_user_id ON verification_tokens(user_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_verification_tokens_expires_at ON verification_tokens(expires_at);
|
||||||
|
|
||||||
|
-- Create function to clean up expired tokens
|
||||||
|
CREATE OR REPLACE FUNCTION cleanup_expired_verification_tokens()
|
||||||
|
RETURNS void AS $$
|
||||||
|
BEGIN
|
||||||
|
DELETE FROM verification_tokens WHERE expires_at < NOW();
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
3
migrations/database/fix_constraint.sql
Normal file
3
migrations/database/fix_constraint.sql
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
-- Fix the constraint syntax error
|
||||||
|
ALTER TABLE users ADD CONSTRAINT users_status_check
|
||||||
|
CHECK (status IN ('active', 'warning', 'suspended', 'banned', 'under_review', 'pending', 'deactivated'));
|
||||||
12
migrations/database/update_user_status.sql
Normal file
12
migrations/database/update_user_status.sql
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
-- Update user status enum to include new moderation statuses
|
||||||
|
ALTER TYPE user_status ADD VALUE 'warning';
|
||||||
|
ALTER TYPE user_status ADD VALUE 'suspended';
|
||||||
|
ALTER TYPE user_status ADD VALUE 'banned';
|
||||||
|
ALTER TYPE user_status ADD VALUE 'under_review';
|
||||||
|
|
||||||
|
-- Drop the old constraint if it exists
|
||||||
|
ALTER TABLE users DROP CONSTRAINT IF EXISTS users_status_check;
|
||||||
|
|
||||||
|
-- Add the new constraint
|
||||||
|
ALTER TABLE users ADD CONSTRAINT users_status_check
|
||||||
|
CHECK (status IN ('active', 'warning', 'suspended', 'banned', 'under_review', 'pending', 'deactivated'));
|
||||||
155
migrations/directus/add_directus_collections.sql
Normal file
155
migrations/directus/add_directus_collections.sql
Normal file
|
|
@ -0,0 +1,155 @@
|
||||||
|
-- Add moderation_flags collection to Directus
|
||||||
|
INSERT INTO directus_collections (
|
||||||
|
collection,
|
||||||
|
meta,
|
||||||
|
icon,
|
||||||
|
note,
|
||||||
|
translation,
|
||||||
|
sort,
|
||||||
|
singleton,
|
||||||
|
archive_filter,
|
||||||
|
archive_app_filter,
|
||||||
|
archive_value,
|
||||||
|
unarchive_filter,
|
||||||
|
unarchive_app_filter,
|
||||||
|
unarchive_value,
|
||||||
|
item_duplication_filter,
|
||||||
|
item_duplication_app_filter,
|
||||||
|
sort_field,
|
||||||
|
accountability
|
||||||
|
) VALUES (
|
||||||
|
'moderation_flags',
|
||||||
|
'{"display_template": null}',
|
||||||
|
'flag',
|
||||||
|
'AI-powered content moderation flags',
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
false,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
'archived',
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
'all',
|
||||||
|
null
|
||||||
|
) ON CONFLICT (collection) DO NOTHING;
|
||||||
|
|
||||||
|
-- Add user_status_history collection to Directus
|
||||||
|
INSERT INTO directus_collections (
|
||||||
|
collection,
|
||||||
|
meta,
|
||||||
|
icon,
|
||||||
|
note,
|
||||||
|
translation,
|
||||||
|
sort,
|
||||||
|
singleton,
|
||||||
|
archive_filter,
|
||||||
|
archive_app_filter,
|
||||||
|
archive_value,
|
||||||
|
unarchive_filter,
|
||||||
|
unarchive_app_filter,
|
||||||
|
unarchive_value,
|
||||||
|
item_duplication_filter,
|
||||||
|
item_duplication_app_filter,
|
||||||
|
sort_field,
|
||||||
|
accountability
|
||||||
|
) VALUES (
|
||||||
|
'user_status_history',
|
||||||
|
'{"display_template": null}',
|
||||||
|
'history',
|
||||||
|
'User status change history for audit trail',
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
false,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
'archived',
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
'all',
|
||||||
|
null
|
||||||
|
) ON CONFLICT (collection) DO NOTHING;
|
||||||
|
|
||||||
|
-- Add fields for moderation_flags collection
|
||||||
|
INSERT INTO directus_fields (
|
||||||
|
collection,
|
||||||
|
field,
|
||||||
|
special,
|
||||||
|
options,
|
||||||
|
meta,
|
||||||
|
interface,
|
||||||
|
note,
|
||||||
|
translation,
|
||||||
|
conditions,
|
||||||
|
required,
|
||||||
|
readonly,
|
||||||
|
hidden,
|
||||||
|
default_value,
|
||||||
|
group,
|
||||||
|
width,
|
||||||
|
validation
|
||||||
|
) VALUES
|
||||||
|
-- id field
|
||||||
|
('moderation_flags', 'id', 'uuid', null, '{"hidden": true}', 'input', null, null, null, false, true, true, null, null, 'full', null),
|
||||||
|
-- post_id field
|
||||||
|
('moderation_flags', 'post_id', 'uuid', null, '{"hidden": false, "interface": "related-values", "options": {"template": "{{post_id}}"}}', 'related-values', 'Reference to the post being moderated', null, null, false, false, false, null, null, 'full', null),
|
||||||
|
-- comment_id field
|
||||||
|
('moderation_flags', 'comment_id', 'uuid', null, '{"hidden": false, "interface": "related-values", "options": {"template": "{{comment_id}}"}}', 'related-values', 'Reference to the comment being moderated', null, null, false, false, false, null, null, 'full', null),
|
||||||
|
-- flag_reason field
|
||||||
|
('moderation_flags', 'flag_reason', 'string', null, '{"hidden": false, "interface": "select", "options": {"choices": {"hate": "Hate", "greed": "Greed", "delusion": "Delusion"}}}', 'select-dropdown', 'Primary reason for flag', null, null, false, false, false, null, null, 'full', null),
|
||||||
|
-- scores field
|
||||||
|
('moderation_flags', 'scores', 'json', null, '{"hidden": false, "interface": "json"}', 'json', 'JSON object containing detailed analysis scores', null, null, false, false, false, null, null, 'full', null),
|
||||||
|
-- status field
|
||||||
|
('moderation_flags', 'status', 'string', null, '{"hidden": false, "interface": "select", "options": {"choices": {"pending": "Pending", "approved": "Approved", "rejected": "Rejected", "escalated": "Escalated"}}}', 'select-dropdown', 'Current moderation status', null, null, false, false, false, 'pending', null, 'full', null),
|
||||||
|
-- reviewed_by field
|
||||||
|
('moderation_flags', 'reviewed_by', 'uuid', null, '{"hidden": false, "interface": "related-values", "options": {"template": "{{reviewed_by}}"}}', 'related-values', 'Admin who reviewed this flag', null, null, false, false, false, null, null, 'full', null),
|
||||||
|
-- reviewed_at field
|
||||||
|
('moderation_flags', 'reviewed_at', 'timestamp', null, '{"hidden": false}', 'datetime', 'When this flag was reviewed', null, null, false, false, false, null, null, 'full', null),
|
||||||
|
-- created_at field
|
||||||
|
('moderation_flags', 'created_at', 'timestamp', null, '{"hidden": false}', 'datetime', 'When this flag was created', null, null, false, false, false, null, null, 'full', null),
|
||||||
|
-- updated_at field
|
||||||
|
('moderation_flags', 'updated_at', 'timestamp', null, '{"hidden": true}', 'datetime', null, null, null, false, false, false, null, null, 'full', null)
|
||||||
|
ON CONFLICT (collection, field) DO NOTHING;
|
||||||
|
|
||||||
|
-- Add fields for user_status_history collection
|
||||||
|
INSERT INTO directus_fields (
|
||||||
|
collection,
|
||||||
|
field,
|
||||||
|
special,
|
||||||
|
options,
|
||||||
|
meta,
|
||||||
|
interface,
|
||||||
|
note,
|
||||||
|
translation,
|
||||||
|
conditions,
|
||||||
|
required,
|
||||||
|
readonly,
|
||||||
|
hidden,
|
||||||
|
default_value,
|
||||||
|
group,
|
||||||
|
width,
|
||||||
|
validation
|
||||||
|
) VALUES
|
||||||
|
-- id field
|
||||||
|
('user_status_history', 'id', 'uuid', null, '{"hidden": true}', 'input', null, null, null, false, true, true, null, null, 'full', null),
|
||||||
|
-- user_id field
|
||||||
|
('user_status_history', 'user_id', 'uuid', null, '{"hidden": false, "interface": "related-values", "options": {"template": "{{user_id}}"}}', 'related-values', 'User whose status changed', null, null, false, false, false, null, null, 'full', null),
|
||||||
|
-- old_status field
|
||||||
|
('user_status_history', 'old_status', 'string', null, '{"hidden": false, "interface": "select", "options": {"choices": {"active": "Active", "suspended": "Suspended", "banned": "Banned"}}}', 'select-dropdown', 'Previous status before change', null, null, false, false, false, null, null, 'full', null),
|
||||||
|
-- new_status field
|
||||||
|
('user_status_history', 'new_status', 'string', null, '{"hidden": false, "interface": "select", "options": {"choices": {"active": "Active", "suspended": "Suspended", "banned": "Banned"}}}', 'select-dropdown', 'New status after change', null, null, false, false, false, null, null, 'full', null),
|
||||||
|
-- reason field
|
||||||
|
('user_status_history', 'reason', 'string', null, '{"hidden": false}', 'input', 'Reason for status change', null, null, false, false, false, null, null, 'full', null),
|
||||||
|
-- changed_by field
|
||||||
|
('user_status_history', 'changed_by', 'uuid', null, '{"hidden": false, "interface": "related-values", "options": {"template": "{{changed_by}}"}}', 'related-values', 'Admin who made the change', null, null, false, false, false, null, null, 'full', null),
|
||||||
|
-- created_at field
|
||||||
|
('user_status_history', 'created_at', 'timestamp', null, '{"hidden": false}', 'datetime', 'When the status change occurred', null, null, false, false, false, null, null, 'full', null)
|
||||||
|
ON CONFLICT (collection, field) DO NOTHING;
|
||||||
91
migrations/directus/add_directus_collections_fixed.sql
Normal file
91
migrations/directus/add_directus_collections_fixed.sql
Normal file
|
|
@ -0,0 +1,91 @@
|
||||||
|
-- Add moderation_flags collection to Directus
|
||||||
|
INSERT INTO directus_collections (
|
||||||
|
collection,
|
||||||
|
icon,
|
||||||
|
note,
|
||||||
|
display_template,
|
||||||
|
hidden,
|
||||||
|
singleton,
|
||||||
|
translations,
|
||||||
|
archive_field,
|
||||||
|
archive_app_filter,
|
||||||
|
archive_value,
|
||||||
|
unarchive_value,
|
||||||
|
sort_field,
|
||||||
|
accountability,
|
||||||
|
color,
|
||||||
|
item_duplication_fields,
|
||||||
|
sort,
|
||||||
|
"group",
|
||||||
|
collapse,
|
||||||
|
preview_url,
|
||||||
|
versioning
|
||||||
|
) VALUES (
|
||||||
|
'moderation_flags',
|
||||||
|
'flag',
|
||||||
|
'AI-powered content moderation flags',
|
||||||
|
null,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
false,
|
||||||
|
'archived',
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
'all',
|
||||||
|
null,
|
||||||
|
'#FF6B6B',
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
false
|
||||||
|
) ON CONFLICT (collection) DO NOTHING;
|
||||||
|
|
||||||
|
-- Add user_status_history collection to Directus
|
||||||
|
INSERT INTO directus_collections (
|
||||||
|
collection,
|
||||||
|
icon,
|
||||||
|
note,
|
||||||
|
display_template,
|
||||||
|
hidden,
|
||||||
|
singleton,
|
||||||
|
translations,
|
||||||
|
archive_field,
|
||||||
|
archive_app_filter,
|
||||||
|
archive_value,
|
||||||
|
unarchive_value,
|
||||||
|
sort_field,
|
||||||
|
accountability,
|
||||||
|
color,
|
||||||
|
item_duplication_fields,
|
||||||
|
sort,
|
||||||
|
"group",
|
||||||
|
collapse,
|
||||||
|
preview_url,
|
||||||
|
versioning
|
||||||
|
) VALUES (
|
||||||
|
'user_status_history',
|
||||||
|
'history',
|
||||||
|
'User status change history for audit trail',
|
||||||
|
null,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
false,
|
||||||
|
'archived',
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
'all',
|
||||||
|
null,
|
||||||
|
'#4ECDC4',
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
false
|
||||||
|
) ON CONFLICT (collection) DO NOTHING;
|
||||||
91
migrations/directus/add_directus_collections_simple.sql
Normal file
91
migrations/directus/add_directus_collections_simple.sql
Normal file
|
|
@ -0,0 +1,91 @@
|
||||||
|
-- Add moderation_flags collection to Directus
|
||||||
|
INSERT INTO directus_collections (
|
||||||
|
collection,
|
||||||
|
icon,
|
||||||
|
note,
|
||||||
|
display_template,
|
||||||
|
hidden,
|
||||||
|
singleton,
|
||||||
|
translations,
|
||||||
|
archive_field,
|
||||||
|
archive_app_filter,
|
||||||
|
archive_value,
|
||||||
|
unarchive_value,
|
||||||
|
sort_field,
|
||||||
|
accountability,
|
||||||
|
color,
|
||||||
|
item_duplication_fields,
|
||||||
|
sort,
|
||||||
|
"group",
|
||||||
|
collapse,
|
||||||
|
preview_url,
|
||||||
|
versioning
|
||||||
|
) VALUES (
|
||||||
|
'moderation_flags',
|
||||||
|
'flag',
|
||||||
|
'AI-powered content moderation flags',
|
||||||
|
null,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
false,
|
||||||
|
'archived',
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
'all',
|
||||||
|
null,
|
||||||
|
'#FF6B6B',
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
false
|
||||||
|
) ON CONFLICT (collection) DO NOTHING;
|
||||||
|
|
||||||
|
-- Add user_status_history collection to Directus
|
||||||
|
INSERT INTO directus_collections (
|
||||||
|
collection,
|
||||||
|
icon,
|
||||||
|
note,
|
||||||
|
display_template,
|
||||||
|
hidden,
|
||||||
|
singleton,
|
||||||
|
translations,
|
||||||
|
archive_field,
|
||||||
|
archive_app_filter,
|
||||||
|
archive_value,
|
||||||
|
unarchive_value,
|
||||||
|
sort_field,
|
||||||
|
accountability,
|
||||||
|
color,
|
||||||
|
item_duplication_fields,
|
||||||
|
sort,
|
||||||
|
"group",
|
||||||
|
collapse,
|
||||||
|
preview_url,
|
||||||
|
versioning
|
||||||
|
) VALUES (
|
||||||
|
'user_status_history',
|
||||||
|
'history',
|
||||||
|
'User status change history for audit trail',
|
||||||
|
null,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
false,
|
||||||
|
'archived',
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
'all',
|
||||||
|
null,
|
||||||
|
'#4ECDC4',
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
false
|
||||||
|
) ON CONFLICT (collection) DO NOTHING;
|
||||||
9
migrations/directus/add_directus_minimal.sql
Normal file
9
migrations/directus/add_directus_minimal.sql
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
-- Add moderation_flags collection to Directus
|
||||||
|
INSERT INTO directus_collections (collection, icon, note, hidden, singleton)
|
||||||
|
VALUES ('moderation_flags', 'flag', 'AI-powered content moderation flags', false, false)
|
||||||
|
ON CONFLICT (collection) DO NOTHING;
|
||||||
|
|
||||||
|
-- Add user_status_history collection to Directus
|
||||||
|
INSERT INTO directus_collections (collection, icon, note, hidden, singleton)
|
||||||
|
VALUES ('user_status_history', 'history', 'User status change history for audit trail', false, false)
|
||||||
|
ON CONFLICT (collection) DO NOTHING;
|
||||||
39
migrations/directus/fix_directus_permissions.sql
Normal file
39
migrations/directus/fix_directus_permissions.sql
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
-- Check table structures and add permissions
|
||||||
|
SELECT 'directus_roles structure:' as info;
|
||||||
|
\d directus_roles;
|
||||||
|
|
||||||
|
SELECT 'directus_permissions structure:' as info;
|
||||||
|
\d directus_permissions;
|
||||||
|
|
||||||
|
-- Get the admin role ID
|
||||||
|
SELECT 'Finding admin role:' as info;
|
||||||
|
SELECT id, name FROM directus_roles WHERE name = 'Administrator' OR name = 'admin';
|
||||||
|
|
||||||
|
-- Add permissions for admin role to access moderation collections
|
||||||
|
INSERT INTO directus_permissions (role, permission, collection, action)
|
||||||
|
SELECT
|
||||||
|
(SELECT id FROM directus_roles WHERE name = 'Administrator' LIMIT 1),
|
||||||
|
'full',
|
||||||
|
'moderation_flags',
|
||||||
|
'*'
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1 FROM directus_permissions
|
||||||
|
WHERE collection = 'moderation_flags'
|
||||||
|
AND role = (SELECT id FROM directus_roles WHERE name = 'Administrator' LIMIT 1)
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO directus_permissions (role, permission, collection, action)
|
||||||
|
SELECT
|
||||||
|
(SELECT id FROM directus_roles WHERE name = 'Administrator' LIMIT 1),
|
||||||
|
'full',
|
||||||
|
'user_status_history',
|
||||||
|
'*'
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1 FROM directus_permissions
|
||||||
|
WHERE collection = 'user_status_history'
|
||||||
|
AND role = (SELECT id FROM directus_roles WHERE name = 'Administrator' LIMIT 1)
|
||||||
|
);
|
||||||
|
|
||||||
|
SELECT 'Permissions added for moderation collections:' as info;
|
||||||
|
SELECT role, permission, collection, action FROM directus_permissions
|
||||||
|
WHERE collection IN ('moderation_flags', 'user_status_history');
|
||||||
64
migrations/directus/fix_directus_ui.sql
Normal file
64
migrations/directus/fix_directus_ui.sql
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
-- Fix Directus UI collection registration
|
||||||
|
-- First, let's see what's currently in directus_collections
|
||||||
|
SELECT 'Current collections in Directus:' as info;
|
||||||
|
SELECT collection, icon, note, hidden FROM directus_collections ORDER BY collection;
|
||||||
|
|
||||||
|
-- Remove any existing entries to avoid conflicts
|
||||||
|
DELETE FROM directus_collections WHERE collection IN ('moderation_flags', 'user_status_history');
|
||||||
|
|
||||||
|
-- Re-add with complete required fields
|
||||||
|
INSERT INTO directus_collections (
|
||||||
|
collection,
|
||||||
|
icon,
|
||||||
|
note,
|
||||||
|
hidden,
|
||||||
|
singleton,
|
||||||
|
translations,
|
||||||
|
archive_field,
|
||||||
|
archive_value,
|
||||||
|
unarchive_field,
|
||||||
|
unarchive_value,
|
||||||
|
sort_field,
|
||||||
|
accountability,
|
||||||
|
item_duplication_field,
|
||||||
|
preview_url,
|
||||||
|
display_template
|
||||||
|
) VALUES
|
||||||
|
(
|
||||||
|
'moderation_flags',
|
||||||
|
'flag',
|
||||||
|
'AI-powered content moderation flags for managing toxic content',
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL
|
||||||
|
),
|
||||||
|
(
|
||||||
|
'user_status_history',
|
||||||
|
'history',
|
||||||
|
'User status change history for audit trail',
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Verify the collections were added
|
||||||
|
SELECT 'Updated collections:' as info;
|
||||||
|
SELECT collection, icon, note, hidden FROM directus_collections WHERE collection IN ('moderation_flags', 'user_status_history');
|
||||||
57
migrations/directus/fix_permissions_correct.sql
Normal file
57
migrations/directus/fix_permissions_correct.sql
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
-- Add permissions using the correct Directus permissions structure
|
||||||
|
-- First, let's see what policies exist for the admin role
|
||||||
|
SELECT 'Existing policies for admin role:' as info;
|
||||||
|
SELECT id, name FROM directus_policies WHERE name ILIKE '%admin%' OR name ILIKE '%administrator%';
|
||||||
|
|
||||||
|
-- Create a policy for admin role to access moderation collections
|
||||||
|
INSERT INTO directus_policies (name, description)
|
||||||
|
SELECT 'Admin Moderation Access', 'Full access to moderation collections for administrators'
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1 FROM directus_policies WHERE name = 'Admin Moderation Access'
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Get the policy ID
|
||||||
|
SELECT 'Created policy ID:' as info;
|
||||||
|
SELECT id, name FROM directus_policies WHERE name = 'Admin Moderation Access';
|
||||||
|
|
||||||
|
-- Add permissions for the policy
|
||||||
|
INSERT INTO directus_permissions (collection, action, permissions, policy)
|
||||||
|
SELECT
|
||||||
|
'moderation_flags',
|
||||||
|
'*',
|
||||||
|
'{"_and": [{"_system": {"_eq": true}}]}',
|
||||||
|
(SELECT id FROM directus_policies WHERE name = 'Admin Moderation Access' LIMIT 1)
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1 FROM directus_permissions
|
||||||
|
WHERE collection = 'moderation_flags'
|
||||||
|
AND policy = (SELECT id FROM directus_policies WHERE name = 'Admin Moderation Access' LIMIT 1)
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO directus_permissions (collection, action, permissions, policy)
|
||||||
|
SELECT
|
||||||
|
'user_status_history',
|
||||||
|
'*',
|
||||||
|
'{"_and": [{"_system": {"_eq": true}}]}',
|
||||||
|
(SELECT id FROM directus_policies WHERE name = 'Admin Moderation Access' LIMIT 1)
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1 FROM directus_permissions
|
||||||
|
WHERE collection = 'user_status_history'
|
||||||
|
AND policy = (SELECT id FROM directus_policies WHERE name = 'Admin Moderation Access' LIMIT 1)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Assign the policy to the admin role
|
||||||
|
INSERT INTO directus_access (role, policy)
|
||||||
|
SELECT
|
||||||
|
(SELECT id FROM directus_roles WHERE name = 'Administrator' LIMIT 1),
|
||||||
|
(SELECT id FROM directus_policies WHERE name = 'Admin Moderation Access' LIMIT 1)
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1 FROM directus_access
|
||||||
|
WHERE role = (SELECT id FROM directus_roles WHERE name = 'Administrator' LIMIT 1)
|
||||||
|
AND policy = (SELECT id FROM directus_policies WHERE name = 'Admin Moderation Access' LIMIT 1)
|
||||||
|
);
|
||||||
|
|
||||||
|
SELECT 'Permissions setup completed:' as info;
|
||||||
|
SELECT p.name as policy_name, pe.collection, pe.action
|
||||||
|
FROM directus_policies p
|
||||||
|
JOIN directus_permissions pe ON p.id = pe.policy
|
||||||
|
WHERE p.name = 'Admin Moderation Access';
|
||||||
50
migrations/directus/use_existing_policy.sql
Normal file
50
migrations/directus/use_existing_policy.sql
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
-- Use the existing Administrator policy to add moderation permissions
|
||||||
|
-- Get the existing Administrator policy ID
|
||||||
|
SELECT 'Administrator policy:' as info;
|
||||||
|
SELECT id, name FROM directus_policies WHERE name = 'Administrator';
|
||||||
|
|
||||||
|
-- Add permissions for moderation collections to the existing Administrator policy
|
||||||
|
INSERT INTO directus_permissions (collection, action, permissions, policy)
|
||||||
|
SELECT
|
||||||
|
'moderation_flags',
|
||||||
|
'*',
|
||||||
|
'{"_and": [{"_system": {"_eq": true}}]}',
|
||||||
|
(SELECT id FROM directus_policies WHERE name = 'Administrator' LIMIT 1)
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1 FROM directus_permissions
|
||||||
|
WHERE collection = 'moderation_flags'
|
||||||
|
AND policy = (SELECT id FROM directus_policies WHERE name = 'Administrator' LIMIT 1)
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO directus_permissions (collection, action, permissions, policy)
|
||||||
|
SELECT
|
||||||
|
'user_status_history',
|
||||||
|
'*',
|
||||||
|
'{"_and": [{"_system": {"_eq": true}}]}',
|
||||||
|
(SELECT id FROM directus_policies WHERE name = 'Administrator' LIMIT 1)
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1 FROM directus_permissions
|
||||||
|
WHERE collection = 'user_status_history'
|
||||||
|
AND policy = (SELECT id FROM directus_policies WHERE name = 'Administrator' LIMIT 1)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Check if admin role is already assigned to Administrator policy
|
||||||
|
SELECT 'Admin role assignments:' as info;
|
||||||
|
SELECT role, policy FROM directus_access WHERE role = (SELECT id FROM directus_roles WHERE name = 'Administrator' LIMIT 1);
|
||||||
|
|
||||||
|
-- If not assigned, assign it
|
||||||
|
INSERT INTO directus_access (role, policy)
|
||||||
|
SELECT
|
||||||
|
(SELECT id FROM directus_roles WHERE name = 'Administrator' LIMIT 1),
|
||||||
|
(SELECT id FROM directus_policies WHERE name = 'Administrator' LIMIT 1)
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1 FROM directus_access
|
||||||
|
WHERE role = (SELECT id FROM directus_roles WHERE name = 'Administrator' LIMIT 1)
|
||||||
|
AND policy = (SELECT id FROM directus_policies WHERE name = 'Administrator' LIMIT 1)
|
||||||
|
);
|
||||||
|
|
||||||
|
SELECT 'Final permissions check:' as info;
|
||||||
|
SELECT p.name as policy_name, pe.collection, pe.action
|
||||||
|
FROM directus_policies p
|
||||||
|
JOIN directus_permissions pe ON p.id = pe.policy
|
||||||
|
WHERE p.name = 'Administrator' AND pe.collection IN ('moderation_flags', 'user_status_history');
|
||||||
38
migrations/fixes/fix_collections_complete.sql
Normal file
38
migrations/fixes/fix_collections_complete.sql
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
-- Complete fix for Directus collections
|
||||||
|
-- First, remove any existing entries
|
||||||
|
DELETE FROM directus_collections WHERE collection IN ('moderation_flags', 'user_status_history');
|
||||||
|
|
||||||
|
-- Add the collections with proper configuration
|
||||||
|
INSERT INTO directus_collections (collection, icon, note, hidden, singleton) VALUES
|
||||||
|
('moderation_flags', 'flag', 'AI-powered content moderation flags', false, false),
|
||||||
|
('user_status_history', 'history', 'User status change history for audit trail', false, false);
|
||||||
|
|
||||||
|
-- Add field configurations for moderation_flags
|
||||||
|
INSERT INTO directus_fields (collection, field, data_type, interface, options) VALUES
|
||||||
|
('moderation_flags', 'id', 'uuid', 'input', NULL),
|
||||||
|
('moderation_flags', 'post_id', 'uuid', 'relationship', '{"collection":"posts"}'),
|
||||||
|
('moderation_flags', 'comment_id', 'uuid', 'relationship', '{"collection":"comments"}'),
|
||||||
|
('moderation_flags', 'flag_reason', 'string', 'dropdown', '{"choices":"hate,greed,delusion,spam,other"}'),
|
||||||
|
('moderation_flags', 'scores', 'json', 'json', NULL),
|
||||||
|
('moderation_flags', 'status', 'string', 'dropdown', '{"choices":"pending,approved,rejected,escalated"}'),
|
||||||
|
('moderation_flags', 'reviewed_by', 'uuid', 'relationship', '{"collection":"directus_users"}'),
|
||||||
|
('moderation_flags', 'reviewed_at', 'timestamp', 'datetime', NULL),
|
||||||
|
('moderation_flags', 'created_at', 'timestamp', 'datetime', NULL),
|
||||||
|
('moderation_flags', 'updated_at', 'timestamp', 'datetime', NULL);
|
||||||
|
|
||||||
|
-- Add field configurations for user_status_history
|
||||||
|
INSERT INTO directus_fields (collection, field, data_type, interface, options) VALUES
|
||||||
|
('user_status_history', 'id', 'uuid', 'input', NULL),
|
||||||
|
('user_status_history', 'user_id', 'uuid', 'relationship', '{"collection":"users"}'),
|
||||||
|
('user_status_history', 'old_status', 'string', 'dropdown', '{"choices":"active,suspended,banned"}'),
|
||||||
|
('user_status_history', 'new_status', 'string', 'dropdown', '{"choices":"active,suspended,banned"}'),
|
||||||
|
('user_status_history', 'reason', 'text', 'input', NULL),
|
||||||
|
('user_status_history', 'changed_by', 'uuid', 'relationship', '{"collection":"directus_users"}'),
|
||||||
|
('user_status_history', 'created_at', 'timestamp', 'datetime', NULL);
|
||||||
|
|
||||||
|
-- Verify the setup
|
||||||
|
SELECT 'Collections configured:' as info;
|
||||||
|
SELECT collection, icon, note FROM directus_collections WHERE collection IN ('moderation_flags', 'user_status_history');
|
||||||
|
|
||||||
|
SELECT 'Fields configured for moderation_flags:' as info;
|
||||||
|
SELECT field, data_type, interface FROM directus_fields WHERE collection = 'moderation_flags';
|
||||||
3
migrations/fixes/grant_permissions.sql
Normal file
3
migrations/fixes/grant_permissions.sql
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO postgres;
|
||||||
|
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO postgres;
|
||||||
|
GRANT USAGE ON SCHEMA public TO postgres;
|
||||||
2
migrations/tests/check_actual_flags.sql
Normal file
2
migrations/tests/check_actual_flags.sql
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
SELECT COUNT(*) as total_flags FROM moderation_flags;
|
||||||
|
SELECT flag_reason, status, created_at FROM moderation_flags ORDER BY created_at DESC;
|
||||||
3
migrations/tests/check_all_emails.sql
Normal file
3
migrations/tests/check_all_emails.sql
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
SELECT email, status, created_at
|
||||||
|
FROM users
|
||||||
|
ORDER BY created_at DESC;
|
||||||
11
migrations/tests/check_collection_config.sql
Normal file
11
migrations/tests/check_collection_config.sql
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
-- Check if Directus collections are properly configured
|
||||||
|
SELECT 'Directus collections configuration:' as info;
|
||||||
|
SELECT collection, icon, note FROM directus_collections WHERE collection IN ('moderation_flags', 'user_status_history');
|
||||||
|
|
||||||
|
-- Check if the actual tables exist and have data
|
||||||
|
SELECT 'Actual database tables:' as info;
|
||||||
|
SELECT table_name, table_type FROM information_schema.tables WHERE table_name IN ('moderation_flags', 'user_status_history');
|
||||||
|
|
||||||
|
-- Check if there are any field configurations
|
||||||
|
SELECT 'Field configurations for moderation_flags:' as info;
|
||||||
|
SELECT field_name, data_type, interface FROM directus_fields WHERE collection = 'moderation_flags' LIMIT 5;
|
||||||
1
migrations/tests/check_collections.sql
Normal file
1
migrations/tests/check_collections.sql
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
SELECT collection, icon, note FROM directus_collections WHERE collection IN ('moderation_flags', 'user_status_history');
|
||||||
2
migrations/tests/check_counts.sql
Normal file
2
migrations/tests/check_counts.sql
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
SELECT COUNT(*) as moderation_flags_count FROM moderation_flags;
|
||||||
|
SELECT COUNT(*) as user_status_count FROM user_status_history;
|
||||||
5
migrations/tests/check_directus.sql
Normal file
5
migrations/tests/check_directus.sql
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
SELECT 'directus_tables' as info, COUNT(*) as count FROM information_schema.tables WHERE table_name LIKE 'directus_%'
|
||||||
|
UNION ALL
|
||||||
|
SELECT 'moderation_flags' as info, COUNT(*) as count FROM moderation_flags
|
||||||
|
UNION ALL
|
||||||
|
SELECT 'user_status_history' as info, COUNT(*) as count FROM user_status_history;
|
||||||
3
migrations/tests/check_directus_collections.sql
Normal file
3
migrations/tests/check_directus_collections.sql
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
SELECT collection, icon, note
|
||||||
|
FROM directus_collections
|
||||||
|
WHERE collection IN ('moderation_flags', 'user_status_history');
|
||||||
9
migrations/tests/check_directus_permissions.sql
Normal file
9
migrations/tests/check_directus_permissions.sql
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
-- Check Directus roles and permissions
|
||||||
|
SELECT 'Directus Roles:' as info;
|
||||||
|
SELECT role, name, description FROM directus_roles;
|
||||||
|
|
||||||
|
SELECT 'Current Permissions for moderation_flags:' as info;
|
||||||
|
SELECT role, permission, collection FROM directus_permissions WHERE collection = 'moderation_flags';
|
||||||
|
|
||||||
|
SELECT 'Current Permissions for user_status_history:' as info;
|
||||||
|
SELECT role, permission, collection FROM directus_permissions WHERE collection = 'user_status_history';
|
||||||
1
migrations/tests/check_directus_schema.sql
Normal file
1
migrations/tests/check_directus_schema.sql
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'directus_collections' ORDER BY ordinal_position;
|
||||||
12
migrations/tests/check_email_summary.sql
Normal file
12
migrations/tests/check_email_summary.sql
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
SELECT
|
||||||
|
COUNT(*) as total_users,
|
||||||
|
COUNT(CASE WHEN status = 'active' THEN 1 END) as active_users,
|
||||||
|
COUNT(CASE WHEN status = 'pending' THEN 1 END) as pending_users,
|
||||||
|
MIN(created_at) as oldest_user,
|
||||||
|
MAX(created_at) as newest_user
|
||||||
|
FROM users;
|
||||||
|
|
||||||
|
SELECT email, status, created_at
|
||||||
|
FROM users
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
LIMIT 10;
|
||||||
2
migrations/tests/check_fields_structure.sql
Normal file
2
migrations/tests/check_fields_structure.sql
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
-- Check the actual structure of directus_fields
|
||||||
|
\d directus_fields;
|
||||||
1
migrations/tests/check_newflow_user.sql
Normal file
1
migrations/tests/check_newflow_user.sql
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
SELECT email, status, email_newsletter, email_contact, created_at FROM users WHERE email = 'newflow@example.com';
|
||||||
1
migrations/tests/check_newuser.sql
Normal file
1
migrations/tests/check_newuser.sql
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
SELECT email, status, created_at FROM users WHERE email = 'newuser@example.com';
|
||||||
1
migrations/tests/check_profile.sql
Normal file
1
migrations/tests/check_profile.sql
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
SELECT p.id, p.handle, p.display_name, u.email FROM profiles p JOIN users u ON p.id = u.id WHERE u.email = 'newuser@example.com';
|
||||||
1
migrations/tests/check_real_turnstile.sql
Normal file
1
migrations/tests/check_real_turnstile.sql
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
SELECT email, status, email_newsletter, email_contact, created_at FROM users WHERE email = 'realturnstile@example.com';
|
||||||
1
migrations/tests/check_tables.sql
Normal file
1
migrations/tests/check_tables.sql
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' ORDER BY table_name;
|
||||||
1
migrations/tests/check_test_users.sql
Normal file
1
migrations/tests/check_test_users.sql
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
SELECT email, status, created_at FROM users WHERE email LIKE 'test%' ORDER BY created_at DESC;
|
||||||
1
migrations/tests/check_users.sql
Normal file
1
migrations/tests/check_users.sql
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
SELECT email, status, created_at FROM users ORDER BY created_at DESC LIMIT 5;
|
||||||
1
migrations/tests/check_verification_token.sql
Normal file
1
migrations/tests/check_verification_token.sql
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
SELECT vt.token_hash, vt.expires_at, u.email FROM verification_tokens vt JOIN users u ON vt.user_id = u.id WHERE u.email = 'newuser@example.com';
|
||||||
1
migrations/tests/count_flags.sql
Normal file
1
migrations/tests/count_flags.sql
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
SELECT COUNT(*) as total_flags FROM moderation_flags;
|
||||||
29
migrations/tests/create_test_flag.sql
Normal file
29
migrations/tests/create_test_flag.sql
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
-- Create a new test moderation flag to see if it appears in Directus
|
||||||
|
INSERT INTO moderation_flags (
|
||||||
|
post_id,
|
||||||
|
flag_reason,
|
||||||
|
scores,
|
||||||
|
status,
|
||||||
|
created_at
|
||||||
|
) VALUES (
|
||||||
|
gen_random_uuid(),
|
||||||
|
'greed',
|
||||||
|
'{"hate": 0.1, "greed": 0.8, "delusion": 0.3}',
|
||||||
|
'pending',
|
||||||
|
NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Verify it was created
|
||||||
|
SELECT 'New test flag created:' as info;
|
||||||
|
SELECT
|
||||||
|
flag_reason,
|
||||||
|
status,
|
||||||
|
scores,
|
||||||
|
created_at
|
||||||
|
FROM moderation_flags
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
LIMIT 1;
|
||||||
|
|
||||||
|
-- Show total count
|
||||||
|
SELECT 'Total flags in database:' as info;
|
||||||
|
SELECT COUNT(*) as total_count FROM moderation_flags;
|
||||||
84
migrations/tests/final_system_test.sql
Normal file
84
migrations/tests/final_system_test.sql
Normal file
|
|
@ -0,0 +1,84 @@
|
||||||
|
-- Final comprehensive test of the AI moderation system
|
||||||
|
|
||||||
|
SELECT '=== AI MODERATION SYSTEM FINAL TEST ===' as test_header;
|
||||||
|
|
||||||
|
-- 1. Check database tables are ready
|
||||||
|
SELECT '1. Database Tables Status:' as section;
|
||||||
|
SELECT table_name, table_type
|
||||||
|
FROM information_schema.tables
|
||||||
|
WHERE table_schema = 'public'
|
||||||
|
AND table_name IN ('moderation_flags', 'user_status_history', 'users')
|
||||||
|
ORDER BY table_name;
|
||||||
|
|
||||||
|
-- 2. Check Directus collections
|
||||||
|
SELECT '2. Directus Collections Status:' as section;
|
||||||
|
SELECT collection, icon, note
|
||||||
|
FROM directus_collections
|
||||||
|
WHERE collection IN ('moderation_flags', 'user_status_history');
|
||||||
|
|
||||||
|
-- 3. Check existing moderation flags
|
||||||
|
SELECT '3. Current Moderation Flags:' as section;
|
||||||
|
SELECT
|
||||||
|
COUNT(*) as total_flags,
|
||||||
|
COUNT(CASE WHEN status = 'pending' THEN 1 END) as pending,
|
||||||
|
COUNT(CASE WHEN status = 'approved' THEN 1 END) as approved,
|
||||||
|
COUNT(CASE WHEN status = 'rejected' THEN 1 END) as rejected
|
||||||
|
FROM moderation_flags;
|
||||||
|
|
||||||
|
-- 4. Check recent moderation activity
|
||||||
|
SELECT '4. Recent Moderation Activity:' as section;
|
||||||
|
SELECT
|
||||||
|
flag_reason,
|
||||||
|
status,
|
||||||
|
JSON_EXTRACT(scores, '$.hate') as hate_score,
|
||||||
|
JSON_EXTRACT(scores, '$.greed') as greed_score,
|
||||||
|
JSON_EXTRACT(scores, '$.delusion') as delusion_score,
|
||||||
|
created_at
|
||||||
|
FROM moderation_flags
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
LIMIT 3;
|
||||||
|
|
||||||
|
-- 5. Test inserting a new moderation flag (simulating AI analysis)
|
||||||
|
SELECT '5. Testing New Flag Insertion:' as section;
|
||||||
|
|
||||||
|
INSERT INTO moderation_flags (
|
||||||
|
post_id,
|
||||||
|
flag_reason,
|
||||||
|
scores,
|
||||||
|
status
|
||||||
|
) VALUES (
|
||||||
|
gen_random_uuid(),
|
||||||
|
'hate',
|
||||||
|
'{"hate": 0.9, "greed": 0.1, "delusion": 0.2}',
|
||||||
|
'pending'
|
||||||
|
);
|
||||||
|
|
||||||
|
-- 6. Verify the flag was inserted with correct Three Poisons scoring
|
||||||
|
SELECT '6. Verification - Latest Flag:' as section;
|
||||||
|
SELECT
|
||||||
|
flag_reason,
|
||||||
|
status,
|
||||||
|
scores,
|
||||||
|
created_at
|
||||||
|
FROM moderation_flags
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
LIMIT 1;
|
||||||
|
|
||||||
|
-- 7. Check user status functionality
|
||||||
|
SELECT '7. User Status Management:' as section;
|
||||||
|
SELECT
|
||||||
|
COUNT(*) as total_users,
|
||||||
|
COUNT(CASE WHEN status = 'active' THEN 1 END) as active_users,
|
||||||
|
COUNT(CASE WHEN status = 'suspended' THEN 1 END) as suspended_users,
|
||||||
|
COUNT(CASE WHEN status = 'banned' THEN 1 END) as banned_users
|
||||||
|
FROM users;
|
||||||
|
|
||||||
|
-- 8. Final system status
|
||||||
|
SELECT '8. SYSTEM STATUS SUMMARY:' as section;
|
||||||
|
SELECT
|
||||||
|
'✅ Database tables created' as database_status,
|
||||||
|
'✅ Directus collections registered' as directus_status,
|
||||||
|
'✅ Moderation flags working' as moderation_status,
|
||||||
|
'✅ Three Poisons scoring active' as scoring_status,
|
||||||
|
'✅ User status management ready' as user_status,
|
||||||
|
'🎉 AI MODERATION SYSTEM FULLY OPERATIONAL' as final_status;
|
||||||
40
migrations/tests/test_backend_integration.sql
Normal file
40
migrations/tests/test_backend_integration.sql
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
-- Test the backend integration by inserting a test post and checking moderation
|
||||||
|
-- First, let's see if there are any existing moderation flags
|
||||||
|
|
||||||
|
SELECT 'Current moderation flags:' as info;
|
||||||
|
SELECT COUNT(*) as total_flags FROM moderation_flags;
|
||||||
|
|
||||||
|
SELECT 'Recent moderation activity:' as info;
|
||||||
|
SELECT
|
||||||
|
flag_reason,
|
||||||
|
status,
|
||||||
|
scores,
|
||||||
|
created_at
|
||||||
|
FROM moderation_flags
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
LIMIT 5;
|
||||||
|
|
||||||
|
-- Test the Three Poisons scoring with a sample flag
|
||||||
|
SELECT 'Inserting test moderation flag:' as info;
|
||||||
|
|
||||||
|
INSERT INTO moderation_flags (
|
||||||
|
post_id,
|
||||||
|
flag_reason,
|
||||||
|
scores,
|
||||||
|
status
|
||||||
|
) VALUES (
|
||||||
|
gen_random_uuid(),
|
||||||
|
'hate',
|
||||||
|
'{"hate": 0.8, "greed": 0.1, "delusion": 0.2}',
|
||||||
|
'pending'
|
||||||
|
);
|
||||||
|
|
||||||
|
SELECT 'Test flag inserted. Current status:' as info;
|
||||||
|
SELECT
|
||||||
|
flag_reason,
|
||||||
|
status,
|
||||||
|
scores,
|
||||||
|
created_at
|
||||||
|
FROM moderation_flags
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
LIMIT 3;
|
||||||
82
migrations/tests/test_comprehensive_moderation.sql
Normal file
82
migrations/tests/test_comprehensive_moderation.sql
Normal file
|
|
@ -0,0 +1,82 @@
|
||||||
|
-- Test comprehensive AI moderation for all content types
|
||||||
|
-- Clear existing test data
|
||||||
|
DELETE FROM moderation_flags WHERE flag_reason IN ('test_text', 'test_image', 'test_video', 'test_comment');
|
||||||
|
|
||||||
|
-- Test 1: Text-only post moderation
|
||||||
|
INSERT INTO moderation_flags (
|
||||||
|
post_id,
|
||||||
|
flag_reason,
|
||||||
|
scores,
|
||||||
|
status,
|
||||||
|
created_at
|
||||||
|
) VALUES (
|
||||||
|
gen_random_uuid(),
|
||||||
|
'test_text',
|
||||||
|
'{"hate": 0.7, "greed": 0.2, "delusion": 0.1}',
|
||||||
|
'pending',
|
||||||
|
NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Test 2: Image content moderation
|
||||||
|
INSERT INTO moderation_flags (
|
||||||
|
post_id,
|
||||||
|
flag_reason,
|
||||||
|
scores,
|
||||||
|
status,
|
||||||
|
created_at
|
||||||
|
) VALUES (
|
||||||
|
gen_random_uuid(),
|
||||||
|
'test_image',
|
||||||
|
'{"hate": 0.3, "greed": 0.6, "delusion": 0.2}',
|
||||||
|
'pending',
|
||||||
|
NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Test 3: Video content moderation
|
||||||
|
INSERT INTO moderation_flags (
|
||||||
|
post_id,
|
||||||
|
flag_reason,
|
||||||
|
scores,
|
||||||
|
status,
|
||||||
|
created_at
|
||||||
|
) VALUES (
|
||||||
|
gen_random_uuid(),
|
||||||
|
'test_video',
|
||||||
|
'{"hate": 0.4, "greed": 0.3, "delusion": 0.5}',
|
||||||
|
'pending',
|
||||||
|
NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Test 4: Comment moderation
|
||||||
|
INSERT INTO moderation_flags (
|
||||||
|
comment_id,
|
||||||
|
flag_reason,
|
||||||
|
scores,
|
||||||
|
status,
|
||||||
|
created_at
|
||||||
|
) VALUES (
|
||||||
|
gen_random_uuid(),
|
||||||
|
'test_comment',
|
||||||
|
'{"hate": 0.8, "greed": 0.1, "delusion": 0.3}',
|
||||||
|
'pending',
|
||||||
|
NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Verify all test flags were created
|
||||||
|
SELECT 'Comprehensive Moderation Test Results:' as info;
|
||||||
|
SELECT
|
||||||
|
flag_reason,
|
||||||
|
CASE
|
||||||
|
WHEN post_id IS NOT NULL AND comment_id IS NULL THEN 'Post'
|
||||||
|
WHEN comment_id IS NOT NULL AND post_id IS NULL THEN 'Comment'
|
||||||
|
ELSE 'Unknown'
|
||||||
|
END as content_type,
|
||||||
|
status,
|
||||||
|
scores,
|
||||||
|
created_at
|
||||||
|
FROM moderation_flags
|
||||||
|
WHERE flag_reason IN ('test_text', 'test_image', 'test_video', 'test_comment')
|
||||||
|
ORDER BY created_at;
|
||||||
|
|
||||||
|
SELECT 'Total flags in system:' as info;
|
||||||
|
SELECT COUNT(*) as total_flags FROM moderation_flags;
|
||||||
15
migrations/tests/test_moderation_integration.sql
Normal file
15
migrations/tests/test_moderation_integration.sql
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
-- Test the AI moderation system by inserting a sample flag
|
||||||
|
INSERT INTO moderation_flags (
|
||||||
|
post_id,
|
||||||
|
flag_reason,
|
||||||
|
scores,
|
||||||
|
status
|
||||||
|
) VALUES (
|
||||||
|
gen_random_uuid(),
|
||||||
|
'hate',
|
||||||
|
'{"hate": 0.8, "greed": 0.1, "delusion": 0.2}',
|
||||||
|
'pending'
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Verify the insertion
|
||||||
|
SELECT * FROM moderation_flags ORDER BY created_at DESC LIMIT 1;
|
||||||
10
migrations/tests/verify_deployment.sql
Normal file
10
migrations/tests/verify_deployment.sql
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
SELECT flag_reason,
|
||||||
|
CASE
|
||||||
|
WHEN post_id IS NOT NULL AND comment_id IS NULL THEN 'Post'
|
||||||
|
WHEN comment_id IS NOT NULL AND post_id IS NULL THEN 'Comment'
|
||||||
|
ELSE 'Unknown'
|
||||||
|
END as content_type,
|
||||||
|
status
|
||||||
|
FROM moderation_flags
|
||||||
|
WHERE flag_reason IN ('test_text', 'test_image', 'test_video', 'test_comment')
|
||||||
|
ORDER BY created_at DESC;
|
||||||
5
migrations/tests/verify_tables.sql
Normal file
5
migrations/tests/verify_tables.sql
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
SELECT 'moderation_flags' as table_name, COUNT(*) as row_count FROM moderation_flags
|
||||||
|
UNION ALL
|
||||||
|
SELECT 'user_status_history' as table_name, COUNT(*) as row_count FROM user_status_history
|
||||||
|
UNION ALL
|
||||||
|
SELECT 'users' as table_name, COUNT(*) as row_count FROM users;
|
||||||
Loading…
Reference in a new issue