Skip to content

Commit

Permalink
fix: Bug where describe DB was using unavailable branch name (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
adilansari authored Jan 18, 2023
1 parent 4e72cab commit 610a140
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ const BeginTransactionMethodName = "/tigrisdata.v1.Tigris/BeginTransaction";
* ```
* {
* name: "my_database_branch",
* isTemplated: false
* dynamicCreation: false
* }
* ```
* @example A dynamically generated branch name "my_db_${GIT_BRANCH}" would translate to:
* ```
* export GIT_BRANCH=feature_1
* {
* name: "my_db_feature_1",
* isTemplated: true
* dynamicCreation: true
* }
* ```
*/
Expand All @@ -70,7 +70,7 @@ const NoBranch: TemplatedBranchName = { name: "", dynamicCreation: false };
*/
export class DB {
private readonly _db: string;
private _branchVar: TemplatedBranchName;
private _branch: string;
private readonly grpcClient: TigrisClient;
private readonly config: TigrisClientConfig;
private readonly schemaProcessor: DecoratedSchemaProcessor;
Expand Down Expand Up @@ -107,8 +107,7 @@ export class DB {
this.config = config;
this.schemaProcessor = DecoratedSchemaProcessor.Instance;
this._metadataStorage = getDecoratorMetaStorage();
// TODO: Should we just default to `main` or empty arg or throw an exception here?
this._branchVar = Utility.branchNameFromEnv(config.branch) ?? NoBranch;
this._branch = NoBranch.name;
this._ready = this.initializeDB();
}

Expand All @@ -124,27 +123,29 @@ export class DB {
* @private
*/
private async initializeDB(): Promise<this> {
if (this._branchVar.name === NoBranch.name) {
const branchVar = Utility.branchNameFromEnv(this.config.branch) ?? NoBranch;
if (branchVar.name === NoBranch.name) {
return this;
}
const description = await this.describe();
const branchExists = description.branches.includes(this.branch);
const branchExists = description.branches.includes(branchVar.name);

if (!branchExists) {
if (this._branchVar.dynamicCreation) {
if (branchVar.dynamicCreation) {
try {
await this.createBranch(this.branch);
await this.createBranch(branchVar.name);
} catch (error) {
if ((error as ServiceError).code !== Status.ALREADY_EXISTS) {
throw error;
}
}
Log.event(`Created database branch: ${this.branch}`);
Log.event(`Created database branch: ${branchVar.name}`);
} else {
throw new DatabaseBranchError(this.branch);
throw new DatabaseBranchError(branchVar.name);
}
}
Log.info(`Using database branch: '${this.branch}'`);
Log.info(`Using database branch: '${branchVar.name}'`);
this._branch = branchVar.name;
return this;
}

Expand Down Expand Up @@ -482,7 +483,7 @@ export class DB {
}

get branch(): string {
return this._branchVar.name;
return this._branch;
}

get ready(): Promise<this> {
Expand Down

0 comments on commit 610a140

Please sign in to comment.