Ejemplo n.º 1
0
    async def create_all(self, module):
        """ A coroutine that create all the models of a module

        Args:
            module: a module that defines several models

        Return:
            A list of created Model name

        For example:
            from tests import models
            await db.create_all(models)
        """

        self._checker()
        succeed = []
        for key, value in vars(module).items():
            if hasattr(value, '__base__') and value.__base__ is self.Model:
                if not await value.exist():
                    await value.create()
                    Logger.info(
                        "created Model '{table_name}' in db: '{db}'".format(
                            table_name=key, db=self.db_info.info.db.db
                        )
                    )
                    succeed.append(key)
                else:
                    Logger.error("table '{}' already exists".format(key))
        return succeed
Ejemplo n.º 2
0
    async def drop_all(self, module):
        """ A coroutine that drop all the models of a module

        Args:
            module: a module that defines several models

        Return:
            A list of droped Model name

        For example:
            from tests import models
            await db.drop_all(models)
        """

        self._checker()
        succeed = []
        for key, value in vars(module).items():
            if hasattr(value, '__base__') and value.__base__ is self.Model:
                if await value.exist():
                    await value.drop()
                    Logger.info(
                        "dropped Model '{model_name}' from db: '{db}'".format(
                            model_name=key, db=self.db_info.info.db.db
                        )
                    )
                    succeed.append(key)
                else:
                    Logger.error(
                        message="drop table '{}' does not exist".format(key)
                    )
        return succeed
Ejemplo n.º 3
0
Archivo: model.py Proyecto: taacth/trod
 def _get_value(self, key):
     value = self.__getattr__(key)
     if value is None:
         field = self.__table__.field_dict[key]
         value = (field() if callable(field) else field.default)
         Logger.info(f'Using default value for {key}: {value}')
         setattr(self, key, value)
     return value
Ejemplo n.º 4
0
    async def batch_drop(self, *models):
        """ A coroutine that batch drop same model.

        Args:
            models: one or more models

        Return:
            A list of droped table name

        For example:
            from tests.models import User, Order
            await db.batch_drop(User, Order)
        """

        self._checker()
        succeed = []
        if not models:
            Logger.warning("parameter 'models' is empty, 'batch_drop' nothing to do")
            return succeed

        for model in models:
            if not issubclass(model, self.Model):
                raise ValueError(
                    'drop model type must be {}, get {}'.format(self.Model, model)
                )
            if await model.exist():
                await model.drop()
                Logger.info(
                    "dropped table '{table_name}' from db: '{db}'".format(
                        table_name=model.__meta__.table,
                        db=self.db_info.info.db.db
                    )
                )
                succeed.append(model.__meta__.table)
            else:
                Logger.error(
                    message="drop table '{}' does not exist".format(model.__meta__.table)
                )
        return succeed