# Path: kittystore/sa/model.py
# class List(Base):
# class User(Base):
# class Sender(Base):
# class Email(Base):
# class EmailFull(Base):
# class Attachment(Base):
# class Thread(Base):
# class Category(Base):
# class Vote(Base):
#     def get_recent_dates(self):
#     def recent_participants_count(self):
#     def recent_threads_count(self):
#     def get_month_activity(self, year, month):
#     def on_new_message(event): # will be called unbound (no self as 1st arg)
#     def messages(self):
#     def get_votes_in_list(self, list_name):
#         def getvotes():
#     def __init__(self, *args, **kw):
#     def full(self):
#     def sender_name(self):
#     def user_id(self):
#     def _get_votes_query(self):
#     def likes(self):
#     def dislikes(self):
#     def likestatus(self):
#     def vote(self, value, user_id):
#     def get_vote_by_user_id(self, user_id):
#     def starting_email(self):
#     def last_email(self):
#     def _get_participants(self):
#     def participants(self):
#     def participants_count(self):
#     def get_emails(self, sort="date", limit=None, offset=None):
#     def email_ids(self):
#     def email_id_hashes(self):
#     def replies_after(self, date):
#     def _get_category(self):
#     def _set_category(self, name):
#     def __len__(self):
#     def emails_count(self):
#     def subject(self):
#     def _getvotes(self):
#     def likes(self):
#     def dislikes(self):
#     def likestatus(self):
#     def on_new_message(event): # will be called unbound (no self as 1st argument)
#     def on_new_thread(event): # will be called unbound (no self as 1st argument)
# def Thread_before_insert(mapper, connection, target):


from uuid import UUID
from alembic import op, context
from mailman.database import types
from kittystore.sa.model import Base
import sqlalchemy as sa

def rebuild_fkeys(cascade):
    # Add or remove onupdate=CASCADE on some foreign keys.
    # We need to be online or we can't reflect the constraint names.
    if (context.is_offline_mode()
        or op.get_context().dialect.name != 'postgresql'):
        return
    connection = op.get_bind()
    md = sa.MetaData()
    md.reflect(bind=connection)
    for fkey in FKEYS_CASCADE:
        keyname = None
        for existing_fk in md.tables[fkey["from_t"]].foreign_keys:
            if existing_fk.constraint.columns == fkey["from_c"]:
                keyname = existing_fk.name
        assert keyname is not None
        op.drop_constraint(keyname, fkey["from_t"])
        op.create_foreign_key(keyname,
            fkey["from_t"], fkey["to_t"], fkey["from_c"], fkey["to_c"],
            onupdate=cascade, ondelete=cascade)


def upgrade():
    # Convert existing data into UUID strings
    if not context.is_offline_mode():
        connection = op.get_bind()
        # Create a new MetaData instance here because the data is not proper
        # UUIDs yet so it'll error out.
        metadata = sa.MetaData()
        metadata.bind = connection

# Path: kittystore/sa/model.py
# class List(Base):
# class User(Base):
# class Sender(Base):
# class Email(Base):
# class EmailFull(Base):
# class Attachment(Base):
# class Thread(Base):
# class Category(Base):
# class Vote(Base):
#     def get_recent_dates(self):
#     def recent_participants_count(self):
#     def recent_threads_count(self):
#     def get_month_activity(self, year, month):
#     def on_new_message(event): # will be called unbound (no self as 1st arg)
#     def messages(self):
#     def get_votes_in_list(self, list_name):
#         def getvotes():
#     def __init__(self, *args, **kw):
#     def full(self):
#     def sender_name(self):
#     def user_id(self):
#     def _get_votes_query(self):
#     def likes(self):
#     def dislikes(self):
#     def likestatus(self):
#     def vote(self, value, user_id):
#     def get_vote_by_user_id(self, user_id):
#     def starting_email(self):
#     def last_email(self):
#     def _get_participants(self):
#     def participants(self):
#     def participants_count(self):
#     def get_emails(self, sort="date", limit=None, offset=None):
#     def email_ids(self):
#     def email_id_hashes(self):
#     def replies_after(self, date):
#     def _get_category(self):
#     def _set_category(self, name):
#     def __len__(self):
#     def emails_count(self):
#     def subject(self):
#     def _getvotes(self):
#     def likes(self):
#     def dislikes(self):
#     def likestatus(self):
#     def on_new_message(event): # will be called unbound (no self as 1st argument)
#     def on_new_thread(event): # will be called unbound (no self as 1st argument)
# def Thread_before_insert(mapper, connection, target):


# Path: kittystore/sa/alembic/versions/d1992a75f51_user_id_uuid.py
from uuid import UUID
from alembic import op, context
from mailman.database import types
from kittystore.sa.model import Base
import sqlalchemy as sa


def rebuild_fkeys(cascade):
    # Add or remove onupdate=CASCADE on some foreign keys.
    # We need to be online or we can't reflect the constraint names.
    if (context.is_offline_mode()
        or op.get_context().dialect.name != 'postgresql'):
        return
    connection = op.get_bind()
    md = sa.MetaData()
    md.reflect(bind=connection)
    for fkey in FKEYS_CASCADE:
        keyname = None
        for existing_fk in md.tables[fkey["from_t"]].foreign_keys:
            if existing_fk.constraint.columns == fkey["from_c"]:
                keyname = existing_fk.name
        assert keyname is not None
        op.drop_constraint(keyname, fkey["from_t"])
        op.create_foreign_key(keyname,
            fkey["from_t"], fkey["to_t"], fkey["from_c"], fkey["to_c"],
            onupdate=cascade, ondelete=cascade)


def upgrade():
    # Convert existing data into UUID strings
    if not context.is_offline_mode():
        connection = op.get_bind()
        # Create a new MetaData instance here because the data is not proper
        # UUIDs yet so it'll error out.
        metadata = sa.MetaData()
        metadata.bind = connection

        User = Base.metadata.tables["user"].tometadata(metadata)