collabrix/backend/scripts/add_file_permissions.py
DGSoft 93b98cfb5c Initial commit: Team Chat System with Code Snippet Library
- Complete chat application similar to Microsoft Teams
- Code snippet library with syntax highlighting
- Real-time messaging with WebSockets
- File upload with Office integration
- Department-based permissions
- Dark/Light theme support
- Production deployment with SSL/Reverse Proxy
- Docker containerization
- PostgreSQL database with SQLModel ORM
2025-12-09 22:25:03 +01:00

71 lines
2.1 KiB
Python

"""
Migration script to add file permissions and WebDAV support
"""
import psycopg2
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
import os
from dotenv import load_dotenv
load_dotenv()
# Get database URL from environment
db_url = os.getenv("DATABASE_URL", "postgresql://postgres:your_password@192.168.0.19:5432/OfficeDesk")
# Parse connection details
# Format: postgresql://user:password@host:port/dbname
parts = db_url.replace("postgresql://", "").split("@")
user_pass = parts[0].split(":")
host_db = parts[1].split("/")
host_port = host_db[0].split(":")
# Database connection
conn = psycopg2.connect(
dbname=host_db[1],
user=user_pass[0],
password=user_pass[1],
host=host_port[0],
port=host_port[1] if len(host_port) > 1 else "5432"
)
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cursor = conn.cursor()
print("Adding file permissions columns to file_attachment table...")
# Add new columns
alterations = [
"ALTER TABLE file_attachment ADD COLUMN IF NOT EXISTS webdav_path VARCHAR",
"ALTER TABLE file_attachment ADD COLUMN IF NOT EXISTS upload_permission VARCHAR DEFAULT 'read'",
"ALTER TABLE file_attachment ADD COLUMN IF NOT EXISTS uploader_id INTEGER REFERENCES \"user\"(id)",
"ALTER TABLE file_attachment ADD COLUMN IF NOT EXISTS is_editable BOOLEAN DEFAULT FALSE",
]
for sql in alterations:
try:
cursor.execute(sql)
print(f"✓ Executed: {sql[:60]}...")
except Exception as e:
print(f"✗ Error: {e}")
# Create file_permission table for user-specific permissions
create_table_sql = """
CREATE TABLE IF NOT EXISTS file_permission (
id SERIAL PRIMARY KEY,
file_id INTEGER REFERENCES file_attachment(id) ON DELETE CASCADE,
user_id INTEGER REFERENCES "user"(id) ON DELETE CASCADE,
permission VARCHAR DEFAULT 'read',
granted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE(file_id, user_id)
);
"""
try:
cursor.execute(create_table_sql)
print("✓ Created file_permission table")
except Exception as e:
print(f"✗ Error creating table: {e}")
cursor.close()
conn.close()
print("\n✅ Migration completed!")