Flask-Files/app/migrations/add_share_table.py
2025-03-23 03:29:05 +01:00

36 lines
No EOL
1.2 KiB
Python

"""
Add Share table for file and folder sharing
"""
from sqlalchemy import inspect
from .. import db
class AddShareTable:
"""Migration to add the shares table"""
version = 2
description = "Add Share table for file/folder sharing"
def should_run(self, inspector):
"""Check if the shares table exists"""
return 'shares' not in inspector.get_table_names()
def run(self):
"""Create the shares table"""
try:
db.engine.execute('''
CREATE TABLE shares (
id INTEGER PRIMARY KEY,
item_type VARCHAR(10) NOT NULL,
item_id INTEGER NOT NULL,
owner_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
permission VARCHAR(10) NOT NULL DEFAULT 'view',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (owner_id) REFERENCES users (id),
FOREIGN KEY (user_id) REFERENCES users (id)
)
''')
return True
except Exception as e:
print(f"Error creating shares table: {str(e)}")
return False