Ejemplo n.º 1
0
def populate_db(mysql_config: Dict[str, str],
                drop_exist: bool = False) -> None:
    db = MySQLDatabase(**mysql_config)
    db.bind(MODELS, bind_refs=False, bind_backrefs=False)
    db.connect()
    if drop_exist:
        db.drop_tables(MODELS)
    db.create_tables(MODELS)
Ejemplo n.º 2
0
class Database:

    TABLES = [
        Schedule,
    ]

    def __init__(self, type_of_db, secret=None):

        global db

        if type_of_db == 'sqlite':
            self.db = SqliteDatabase('schedule.db')
        elif type_of_db == 'aws':
            secret = _get_secret(RDS_SECRET)
            Database._create(secret)

            self.db = MySQLDatabase('punch_clock',
                                    host=secret['host'],
                                    port=secret['port'],
                                    user=secret['username'],
                                    passwd=secret['password'])

        self.db.connect()
        database_proxy.initialize(self.db)
        self._create_tables()
        db = self.db

    def _create_tables(self):
        self.db.create_tables(self.TABLES)

    @classmethod
    def _create(klass, secret):
        # PeeWee doesn't have a way to create an actual MySQL database,
        # so we have to rely on another library for that.
        conn = pymysql.connect(host=secret['host'],
                               user=secret['username'],
                               password=secret['password'])
        try:
            conn.cursor().execute('CREATE DATABASE punch_clock')
        except Exception:
            # Database probably already exists.
            pass
        conn.close()

    def delete(self):
        self.db.drop_tables(self.TABLES)
Ejemplo n.º 3
0
        indexes = ((('did', ), True), )


ALL_TABLES = [
    Profile, Equipment, EditHistory, ComputerDetail, WorkOrder, OrderHistory,
    ItConfig, CaptchaMeta, EmailHistory, PatrolMeta, PatrolDetail,
    DepartmentContact
]

if __name__ == '__main__':
    # MySQL_DB.drop_tables([WorkOrder, OrderHistory])
    # MySQL_DB.create_tables([WorkOrder, OrderHistory])
    # MySQL_DB.drop_tables([ItConfig])
    # MySQL_DB.create_tables([ItConfig])
    # ItConfig.insert({ItConfig.key: "sendSms", ItConfig.value: "0"}).execute()
    # ItConfig.insert({ItConfig.key: "sendEmail", ItConfig.value: "0"}).execute()
    # MySQL_DB.drop_tables([OrderHistory, WorkOrder, CaptchaMeta, EmailHistory, PatrolMeta, PatrolDetail])
    # MySQL_DB.create_tables([OrderHistory, WorkOrder, CaptchaMeta, EmailHistory, PatrolMeta, PatrolDetail])
    # MySQL_DB.drop_tables([PatrolMeta, PatrolDetail, EmailHistory, CaptchaMeta])
    # MySQL_DB.create_tables([PatrolMeta, PatrolDetail, EmailHistory, CaptchaMeta])
    # MySQL_DB.drop_tables([ComputerDetail])
    # MySQL_DB.create_tables([ComputerDetail])
    _tables = [
        # DepartmentContact,
        PatrolMeta,
        PatrolDetail
    ]
    MySQL_DB.drop_tables(_tables)
    MySQL_DB.create_tables(_tables)
    pass