This commit is contained in:
Timothy Jaeryang Baek committed 2026-05-14 03:06:37 +09:00
1 parent 245e0ee029
commit 81f611fb73
6 files changed
+84 -1

No files matched your search

@@ -18,7 +18,18 @@ branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None depends_on: Union[str, Sequence[str], None] = None
def _index_exists(inspector, index_name, table_name):
"""Check if an index already exists on the given table."""
indexes = inspector.get_indexes(table_name)
return any(idx['name'] == index_name for idx in indexes)
def upgrade() -> None: def upgrade() -> None:
conn = op.get_bind()
inspector = sa.inspect(conn)
tables = inspector.get_table_names()
if 'calendar' not in tables:
op.create_table( op.create_table(
'calendar', 'calendar',
sa.Column('id', sa.Text(), nullable=False), sa.Column('id', sa.Text(), nullable=False),
@@ -32,8 +43,12 @@ def upgrade() -> None:
sa.Column('updated_at', sa.BigInteger(), nullable=False), sa.Column('updated_at', sa.BigInteger(), nullable=False),
sa.PrimaryKeyConstraint('id'), sa.PrimaryKeyConstraint('id'),
) )
if 'calendar' in inspector.get_table_names():
if not _index_exists(inspector, 'ix_calendar_user', 'calendar'):
op.create_index('ix_calendar_user', 'calendar', ['user_id'], unique=False) op.create_index('ix_calendar_user', 'calendar', ['user_id'], unique=False)
if 'calendar_event' not in tables:
op.create_table( op.create_table(
'calendar_event', 'calendar_event',
sa.Column('id', sa.Text(), nullable=False), sa.Column('id', sa.Text(), nullable=False),
@@ -54,9 +69,14 @@ def upgrade() -> None:
sa.Column('updated_at', sa.BigInteger(), nullable=False), sa.Column('updated_at', sa.BigInteger(), nullable=False),
sa.PrimaryKeyConstraint('id'), sa.PrimaryKeyConstraint('id'),
) )
if 'calendar_event' in inspector.get_table_names():
if not _index_exists(inspector, 'ix_calendar_event_calendar', 'calendar_event'):
op.create_index('ix_calendar_event_calendar', 'calendar_event', ['calendar_id', 'start_at'], unique=False) op.create_index('ix_calendar_event_calendar', 'calendar_event', ['calendar_id', 'start_at'], unique=False)
if not _index_exists(inspector, 'ix_calendar_event_user_date', 'calendar_event'):
op.create_index('ix_calendar_event_user_date', 'calendar_event', ['user_id', 'start_at'], unique=False) op.create_index('ix_calendar_event_user_date', 'calendar_event', ['user_id', 'start_at'], unique=False)
if 'calendar_event_attendee' not in tables:
op.create_table( op.create_table(
'calendar_event_attendee', 'calendar_event_attendee',
sa.Column('id', sa.Text(), nullable=False), sa.Column('id', sa.Text(), nullable=False),
@@ -69,6 +89,9 @@ def upgrade() -> None:
sa.PrimaryKeyConstraint('id'), sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('event_id', 'user_id', name='uq_event_attendee'), sa.UniqueConstraint('event_id', 'user_id', name='uq_event_attendee'),
) )
if 'calendar_event_attendee' in inspector.get_table_names():
if not _index_exists(inspector, 'ix_calendar_event_attendee_user', 'calendar_event_attendee'):
op.create_index('ix_calendar_event_attendee_user', 'calendar_event_attendee', ['user_id', 'status'], unique=False) op.create_index('ix_calendar_event_attendee_user', 'calendar_event_attendee', ['user_id', 'status'], unique=False)
@@ -19,7 +19,13 @@ depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None: def upgrade() -> None:
conn = op.get_bind()
inspector = sa.inspect(conn)
columns = [col['name'] for col in inspector.get_columns('chat')]
if 'tasks' not in columns:
op.add_column('chat', sa.Column('tasks', sa.JSON(), nullable=True)) op.add_column('chat', sa.Column('tasks', sa.JSON(), nullable=True))
if 'summary' not in columns:
op.add_column('chat', sa.Column('summary', sa.Text(), nullable=True)) op.add_column('chat', sa.Column('summary', sa.Text(), nullable=True))
@@ -17,6 +17,11 @@ depends_on = None
def upgrade(): def upgrade():
conn = op.get_bind()
inspector = sa.inspect(conn)
columns = [col['name'] for col in inspector.get_columns('chat')]
if 'last_read_at' not in columns:
op.add_column('chat', sa.Column('last_read_at', sa.BigInteger(), nullable=True)) op.add_column('chat', sa.Column('last_read_at', sa.BigInteger(), nullable=True))
# Set existing chats to be marked as read # Set existing chats to be marked as read
op.execute('UPDATE chat SET last_read_at = updated_at') op.execute('UPDATE chat SET last_read_at = updated_at')
@@ -61,8 +61,11 @@ access_grant_t = sa.table(
def upgrade(): def upgrade():
conn = op.get_bind() conn = op.get_bind()
inspector = sa.inspect(conn)
tables = inspector.get_table_names()
# 1. Create shared_chat table # 1. Create shared_chat table (idempotent)
if 'shared_chat' not in tables:
op.create_table( op.create_table(
'shared_chat', 'shared_chat',
sa.Column('id', sa.Text(), primary_key=True), sa.Column('id', sa.Text(), primary_key=True),
@@ -96,6 +99,14 @@ def upgrade():
if not original: if not original:
continue continue
# Check if shared_chat record already exists (idempotent)
existing_shared = conn.execute(
sa.select(shared_chat_t.c.id).where(
shared_chat_t.c.id == share_token
)
).fetchone()
if not existing_shared:
# Insert snapshot into shared_chat # Insert snapshot into shared_chat
conn.execute( conn.execute(
shared_chat_t.insert().values( shared_chat_t.insert().values(
@@ -109,6 +120,20 @@ def upgrade():
) )
) )
# Check if access_grant record already exists (idempotent)
existing_grant = conn.execute(
sa.select(access_grant_t.c.id).where(
sa.and_(
access_grant_t.c.resource_type == 'shared_chat',
access_grant_t.c.resource_id == original_chat_id,
access_grant_t.c.principal_type == 'user',
access_grant_t.c.principal_id == '*',
access_grant_t.c.permission == 'read',
)
)
).fetchone()
if not existing_grant:
# Create user:*:read grant for backward compat # Create user:*:read grant for backward compat
conn.execute( conn.execute(
access_grant_t.insert().values( access_grant_t.insert().values(
@@ -16,7 +16,18 @@ branch_labels = None
depends_on = None depends_on = None
def _index_exists(inspector, index_name, table_name):
"""Check if an index already exists on the given table (works for both SQLite and PostgreSQL)."""
indexes = inspector.get_indexes(table_name)
return any(idx['name'] == index_name for idx in indexes)
def upgrade(): def upgrade():
conn = op.get_bind()
inspector = sa.inspect(conn)
tables = inspector.get_table_names()
if 'automation' not in tables:
op.create_table( op.create_table(
'automation', 'automation',
sa.Column('id', sa.Text(), primary_key=True), sa.Column('id', sa.Text(), primary_key=True),
@@ -30,8 +41,13 @@ def upgrade():
sa.Column('created_at', sa.BigInteger(), nullable=False), sa.Column('created_at', sa.BigInteger(), nullable=False),
sa.Column('updated_at', sa.BigInteger(), nullable=False), sa.Column('updated_at', sa.BigInteger(), nullable=False),
) )
# Re-check tables in case we just created it
if 'automation' in inspector.get_table_names():
if not _index_exists(inspector, 'ix_automation_next_run', 'automation'):
op.create_index('ix_automation_next_run', 'automation', ['next_run_at']) op.create_index('ix_automation_next_run', 'automation', ['next_run_at'])
if 'automation_run' not in tables:
op.create_table( op.create_table(
'automation_run', 'automation_run',
sa.Column('id', sa.Text(), primary_key=True), sa.Column('id', sa.Text(), primary_key=True),
@@ -41,6 +57,9 @@ def upgrade():
sa.Column('error', sa.Text(), nullable=True), sa.Column('error', sa.Text(), nullable=True),
sa.Column('created_at', sa.BigInteger(), nullable=False), sa.Column('created_at', sa.BigInteger(), nullable=False),
) )
if 'automation_run' in inspector.get_table_names():
if not _index_exists(inspector, 'ix_automation_run_automation_id', 'automation_run'):
op.create_index( op.create_index(
'ix_automation_run_automation_id', 'ix_automation_run_automation_id',
'automation_run', 'automation_run',
@@ -16,6 +16,11 @@ depends_on = None
def upgrade(): def upgrade():
conn = op.get_bind()
inspector = sa.inspect(conn)
columns = [col['name'] for col in inspector.get_columns('note')]
if 'is_pinned' not in columns:
op.add_column('note', sa.Column('is_pinned', sa.Boolean(), nullable=True)) op.add_column('note', sa.Column('is_pinned', sa.Boolean(), nullable=True))