database.py 文件源码

python
阅读 18 收藏 0 点赞 0 评论 0

项目:infoset-ng 作者: PalisadoesFoundation 项目源码 文件源码
def run(self):
        """Setup database.

        Args:
            None

        Returns:
            None

        """
        # Initialize key variables
        use_mysql = True
        pool_size = 25
        max_overflow = 25
        config = self.config

        mappings = [Agent, Department, Device, Billcode, DeviceAgent, Datapoint, AgentName] 

        # Create DB connection pool
        if use_mysql is True:
            # Add MySQL to the pool
            engine = create_engine(
                URL, echo=False,
                encoding='utf8',
                max_overflow=max_overflow,
                pool_size=pool_size, pool_recycle=3600)

            # Try to create the database
            shared.print_ok('Attempting to create database tables')
            try:
                sql_string = (
                    'ALTER DATABASE %s CHARACTER SET utf8mb4 '
                    'COLLATE utf8mb4_general_ci') % (config.db_name())
                engine.execute(sql_string)
            except:
                log_message = (
                    'Cannot connect to database %s. '
                    'Verify database server is started. '
                    'Verify database is created. '
                    'Verify that the configured database authentication '
                    'is correct.') % (config.db_name())
                log.log2die(1046, log_message)

            # Apply schemas
            shared.print_ok('Generating Schemas.')

            with open('infoset.sql', 'w') as infoset_mysql:
                for mapping in mappings:
                    print(CreateTable(mapping.__table__))
                    infoset_mysql.write(str(CreateTable(mapping.__table__)))
            infoset_mysql.close()

            # Insert database entries
            self._insert_agent_device()
            self._insert_billcode()
            self._insert_department()
            self._insert_datapoint()
            self._insert_config()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号