71 lines
2.7 KiB
Python
71 lines
2.7 KiB
Python
"""phase23 — support tickets
|
|
|
|
Creates two tables:
|
|
support_tickets — customer-submitted help requests
|
|
support_ticket_replies — admin/staff replies to those tickets
|
|
"""
|
|
|
|
import sqlalchemy as sa
|
|
from alembic import op
|
|
|
|
revision = 'phase23_support_tickets'
|
|
down_revision = 'phase22_comment_visibility'
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def _table_exists(bind, table: str) -> bool:
|
|
result = bind.execute(sa.text(
|
|
"SELECT COUNT(*) FROM information_schema.tables "
|
|
"WHERE table_schema = DATABASE() AND table_name = :t"
|
|
), {'t': table})
|
|
return result.scalar() > 0
|
|
|
|
|
|
def upgrade():
|
|
bind = op.get_bind()
|
|
|
|
if not _table_exists(bind, 'support_tickets'):
|
|
op.execute(sa.text("""
|
|
CREATE TABLE support_tickets (
|
|
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
|
customer_id INT NULL,
|
|
facility_id INT NULL,
|
|
subject VARCHAR(200) NOT NULL,
|
|
body TEXT NOT NULL,
|
|
status VARCHAR(20) NOT NULL DEFAULT 'open',
|
|
created_at DATETIME NOT NULL,
|
|
CONSTRAINT fk_st_customer FOREIGN KEY (customer_id)
|
|
REFERENCES users(id) ON DELETE SET NULL,
|
|
CONSTRAINT fk_st_facility FOREIGN KEY (facility_id)
|
|
REFERENCES facilities(id) ON DELETE SET NULL,
|
|
INDEX ix_support_tickets_customer (customer_id),
|
|
INDEX ix_support_tickets_status (status),
|
|
INDEX ix_support_tickets_created (created_at)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
|
|
"""))
|
|
|
|
if not _table_exists(bind, 'support_ticket_replies'):
|
|
op.execute(sa.text("""
|
|
CREATE TABLE support_ticket_replies (
|
|
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
|
ticket_id INT NOT NULL,
|
|
user_id INT NULL,
|
|
body TEXT NOT NULL,
|
|
created_at DATETIME NOT NULL,
|
|
CONSTRAINT fk_str_ticket FOREIGN KEY (ticket_id)
|
|
REFERENCES support_tickets(id) ON DELETE CASCADE,
|
|
CONSTRAINT fk_str_user FOREIGN KEY (user_id)
|
|
REFERENCES users(id) ON DELETE SET NULL,
|
|
INDEX ix_support_replies_ticket (ticket_id)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
|
|
"""))
|
|
|
|
|
|
def downgrade():
|
|
bind = op.get_bind()
|
|
if _table_exists(bind, 'support_ticket_replies'):
|
|
op.execute(sa.text('DROP TABLE support_ticket_replies'))
|
|
if _table_exists(bind, 'support_tickets'):
|
|
op.execute(sa.text('DROP TABLE support_tickets'))
|