示例#1
0
文件: tests.py 项目: oxfemale/tg-bot
    def drop_tables():
        database_engine = models.DataBase._meta.database

        # !!!!! When there is an inheritance from the model to tg_bot, it will delete
        from classes.database.models import DataBase
        _models = DataBase.get_models()

        DataBase._meta.database.init("test.db")
        models.DataBase._meta.database.init("test.db")

        peewee_migrate.MigrateHistory._meta.database = models.DataBase._meta.database
        peewee_migrate.MigrateHistory._meta.database.init("test.db")

        for model in models.DataBase.get_models() + [
                peewee_migrate.MigrateHistory
        ] + _models:
            if model.table_exists():
                model.drop_table(safe=False)

        # This will be cut out later, when I stop using the sql file for tables!!!
        import run
        sql_file_path = os.path.join(settings.BASE_DIR,
                                     "etc/data/bases/database.sql")
        if os.path.exists(sql_file_path):
            with open(sql_file_path, encoding="utf-8") as f:
                sql = f.read()

            run.create_db("test.db", sql)
        # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

        migrate_dir = os.path.join(settings.BASE_DIR, "etc/migrations")
        router = peewee_migrate.Router(models.DataBase._meta.database,
                                       migrate_dir=migrate_dir)
        router.logger.disabled = True
        router.run()
示例#2
0
import peewee
import peewee_migrate
import playhouse.sqlite_ext
import telegram

import constants
import telegram_utils

logger = logging.getLogger(__name__)

database = peewee.SqliteDatabase('dex.sqlite')

database.connect()

router = peewee_migrate.Router(database,
                               migrate_table='migration',
                               logger=logger)


def get_current_datetime() -> str:
    return datetime.datetime.now().strftime(constants.GENERIC_DATE_TIME_FORMAT)


class BaseModel(peewee.Model):
    rowid = playhouse.sqlite_ext.RowIDField()

    created_at = peewee.DateTimeField(default=get_current_datetime)
    updated_at = peewee.DateTimeField()

    class Meta:
        database = database
示例#3
0
def init_database(logger=logger):
    if not TEST:
        DATA_DIR.mkdir(exist_ok=True)
    peewee_migrate.Router(db, migrate_dir=str(MIGRATIONS_DIR),
                          logger=logger).run()