def doltdb(): db_path = os.path.join(os.path.dirname(__file__), "foo") try: db = Dolt.init(db_path) df_v1 = pd.DataFrame({"A": [1, 1, 1], "B": [1, 1, 1]}) write_pandas( dolt=db, table="bar", df=df_v1.reset_index(), primary_key=["index"], import_mode="create", ) db.add("bar") db.commit("Initialize bar") df_v2 = pd.DataFrame({"A": [2, 2, 2], "B": [2, 2, 2]}) write_pandas( dolt=db, table="bar", df=df_v2.reset_index(), primary_key=["index"], import_mode="create", ) db.add("bar") db.commit("Edit bar") yield db_path finally: if os.path.exists(db_path): shutil.rmtree(db_path)
def _get_db(self, config: DoltConfig): if config.id in self._dbcache: return self._dbcache[config.id] # TODO: clone remote try: Dolt.init(repo_dir=config.database) except DoltException as e: pass if ".dolt" not in os.listdir(config.database): raise ValueError( f"Passed a path {config.database} that is not a Dolt database directory" ) doltdb = Dolt(repo_dir=config.database) current_branch, branches = doltdb.branch() logger.info( f"Dolt database in {config.database} at branch {current_branch.name}, using branch {config.branch}" ) if config.branch == current_branch.name: pass elif config.branch not in [branch.name for branch in branches]: raise ValueError(f"Passed branch '{config.branch}' that does not exist") else: doltdb.checkout(config.branch, checkout_branch=False) if not doltdb.status().is_clean: raise Exception( "DoltDT as context manager requires clean working set for transaction semantics" ) if not config.commit: config.commit = self._get_latest_commit_hash(doltdb) self._dbcache[config.id] = doltdb return doltdb
def doltdb(): db_path = os.path.join(os.path.dirname(__file__), "foo") try: db = Dolt.init(db_path) db.sql("create table t1 (a bigint primary key, b bigint, c bigint)") db.sql("insert into t1 values (1,1,1), (2,2,2)") db.sql("select dolt_add('t1')") db.sql("select dolt_commit('-m', 'initialize t1')") db.sql("insert into t1 values (3,3,3)") db.sql("select dolt_add('t1')") db.sql("select dolt_commit('-m', 'edit t1')") yield db_path finally: if os.path.exists(db_path): shutil.rmtree(db_path)
def _init_helper(path: str, ext: str = None): repo_path, repo_data_dir = get_repo_path_tmp_path(path, ext) return Dolt.init(repo_path)
def test_repo_name_trailing_slash(tmp_path): repo_path, repo_data_dir = get_repo_path_tmp_path(tmp_path) assert Dolt.init(str(repo_path) + "/").repo_name == "test_repo_name_trailing_slash0" shutil.rmtree(repo_data_dir)
def test_init(tmp_path): repo_path, repo_data_dir = get_repo_path_tmp_path(tmp_path) assert not os.path.exists(repo_data_dir) Dolt.init(repo_path) assert os.path.exists(repo_data_dir) shutil.rmtree(repo_data_dir)