from cpl_core.logging import LoggerABC

from gismo_data.abc.migration_abc import MigrationABC
from gismo_data.db_context import DBContext

class Migration_0_3_1(MigrationABC):

    def __init__(self, logger: LoggerABC, db: DBContext):
        self._logger = logger
        self._db = db
        self._cursor = db.cursor

    def upgrade(self):
        self._logger.debug(__name__, 'Running upgrade')

        self._cursor.execute(
            str(f"""
            CREATE TABLE IF NOT EXISTS `UserJoinedVoiceChannel` (
                `JoinId` BIGINT NOT NULL AUTO_INCREMENT,
                `UserId` BIGINT NOT NULL,
                `DiscordChannelId` BIGINT NOT NULL,
                `JoinedOn` DATETIME(6) NOT NULL,
                `LeavedOn` DATETIME(6),
                `CreatedAt` DATETIME(6),
                `LastModifiedAt` DATETIME(6),
                FOREIGN KEY (`UserId`) REFERENCES Users(`UserId`),
                PRIMARY KEY(`JoinId`)
            );
            """)
        )

    def downgrade(self):
        self._logger.debug(__name__, 'Running downgrade')
        self._cursor.execute('DROP TABLE `UserJoinedVoiceChannel`;')