def init_db(self, txn=None): """Connect to database and create tables. Parameters ---------- txn : sa.engine.Connection, optional The transaction to execute in. If this is not provided, a new transaction will be started with the engine provided. Returns ------- metadata : sa.MetaData The metadata that describes the new assets db. """ with ExitStack() as stack: if txn is None: txn = stack.enter_context(self.engine.begin()) tables_already_exist = self._all_tables_present(txn) metadata = generate_asset_db_metadata(bind=txn) # Create the SQL tables if they do not already exist. metadata.create_all(checkfirst=True) version_info = metadata.tables['version_info'] if tables_already_exist: check_version_info(version_info, ASSET_DB_VERSION) else: write_version_info(version_info, ASSET_DB_VERSION) return metadata
def init_db(self, engine): """Connect to database and create tables. Parameters ---------- engine : Engine An engine to a SQL database. constraints : bool, optional If True, create SQL ForeignKey and PrimaryKey constraints. """ tables_already_exist = self.check_for_tables(engine) metadata = generate_asset_db_metadata(bind=engine) for table_name in asset_db_table_names: setattr(self, table_name, metadata.tables[table_name]) # Create the SQL tables if they do not already exist. metadata.create_all(checkfirst=True) if tables_already_exist: check_version_info(self.version_info, ASSET_DB_VERSION) else: write_version_info(self.version_info, ASSET_DB_VERSION) return metadata