from playhouse.migrate import *


"""
This migration assumes that downloads.db kept the exact same structure as before.
messages.db should drop the table articlemodelreference in favor of a new field article in the thread-table
Since each thread is constrained to exactly one article this makes the most sense.

This migration assumes that messages.db gets a new field in the table thread:
id  |   thread_ts | article_id

We now need to migrate from the table articlemodelreference and then delete it.
"""


db = SqliteDatabase("/code/.dev/messages.db")
migrator = SqliteMigrator(db)


article_field = IntegerField(null=True)


migrate(
    migrator.add_column('thread', 'article_id', article_field),
    # migrator.drop_column('some_table', 'old_column'),
)



# these are the old models, adapted to the migration

class BaseModel(Model):
    class Meta:
        database = db

class User(BaseModel):
    user_id = CharField(default='', unique=True)   

class Thread(BaseModel):
    """The threads that concern us are only created if the messages that contain urls"""
    thread_ts = FloatField(default = 0)
    article_id = IntegerField(null=True)

    
class Message(BaseModel):
    ts = FloatField(unique=True) #for sorting
    channel_id = CharField(default='')
    user = ForeignKeyField(User, backref="messages")
    text = TextField(default='')
    thread = ForeignKeyField(Thread, backref="messages", default=None)
    file_type = CharField(default='')
    perma_link = CharField(default='')
    is_processed_override = BooleanField(default=False)


class ArticleModelReference(BaseModel):
    message = ForeignKeyField(Message, backref='article_model_references')
    article_model_id = IntegerField(default = 0)




for ref in ArticleModelReference.select():
    ref.message.thread.article_id = ref.article_model_id
    ref.message.thread.save()

db.drop_tables((ArticleModelReference))