Skip to content

Commit

Permalink
Merge pull request #330 from dolthub/liuliu/add-fetch
Browse files Browse the repository at this point in the history
Graphql, Web: add fetch option in remotes tab
  • Loading branch information
liuliu-dev authored Jan 2, 2025
2 parents 894ccd0 + 8240134 commit 8d4890f
Show file tree
Hide file tree
Showing 34 changed files with 1,297 additions and 111 deletions.
14 changes: 14 additions & 0 deletions graphql-server/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ type Branch {
table(tableName: String!): Table
tableNames(filterSystemTables: Boolean, schemaName: String): [String!]!
head: String
remote: String
remoteBranch: String
}

"""
Expand Down Expand Up @@ -303,10 +305,20 @@ type PushRes {
message: String!
}

type FetchRes {
success: Boolean!
}

type RemoteBranchDiffCounts {
ahead: Int
behind: Int
}

type Query {
branch(databaseName: String!, branchName: String!): Branch
branchOrDefault(databaseName: String!, branchName: String): Branch
branches(offset: Int, databaseName: String!, sortBy: SortBranchesBy): BranchList!
remoteBranches(offset: Int, databaseName: String!, sortBy: SortBranchesBy): BranchList!
allBranches(offset: Int, databaseName: String!, sortBy: SortBranchesBy): [Branch!]!
defaultBranch(databaseName: String!): Branch
commits(offset: Int, databaseName: String!, refName: String, afterCommitId: String, twoDot: Boolean, excludingCommitsFromRefName: String): CommitList!
Expand All @@ -323,6 +335,8 @@ type Query {
docOrDefaultDoc(refName: String!, databaseName: String!, docType: DocType): Doc
pullWithDetails(databaseName: String!, fromBranchName: String!, toBranchName: String!): PullWithDetails!
remotes(databaseName: String!, offset: Int): RemoteList!
fetchRemote(remoteName: String!, databaseName: String!, branchName: String): FetchRes!
remoteBranchDiffCounts(databaseName: String!, fromRefName: String!, toRefName: String!): RemoteBranchDiffCounts!
rowDiffs(offset: Int, databaseName: String!, fromRefName: String!, toRefName: String!, refName: String, tableName: String!, filterByRowType: DiffRowType, type: CommitDiffType): RowDiffList!
rows(schemaName: String, refName: String!, databaseName: String!, tableName: String!, offset: Int): RowList!
schemaDiff(databaseName: String!, fromRefName: String!, toRefName: String!, refName: String, tableName: String!, type: CommitDiffType): SchemaDiff
Expand Down
8 changes: 8 additions & 0 deletions graphql-server/src/branches/branch.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ export class Branch {

@Field({ nullable: true })
head?: string;

@Field({ nullable: true })
remote?: string;

@Field({ nullable: true })
remoteBranch?: string;
}

@ObjectType()
Expand All @@ -50,6 +56,8 @@ export function fromDoltBranchesRow(
lastUpdated: convertToUTCDate(b.latest_commit_date),
lastCommitter: b.latest_committer,
tableNames: tns,
remote: b.remote,
remoteBranch: b.branch,
};
}

Expand Down
10 changes: 10 additions & 0 deletions graphql-server/src/branches/branch.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ export class BranchResolver {
return fromBranchListRes(res, args);
}

@Query(_returns => BranchList)
async remoteBranches(@Args() args: ListBranchesArgs): Promise<BranchList> {
const conn = this.conn.connection();
const res = await conn.getRemoteBranches({
...args,
offset: args.offset ?? 0,
});
return fromBranchListRes(res, args);
}

@Query(_returns => [Branch])
async allBranches(@Args() args: ListBranchesArgs): Promise<Branch[]> {
const conn = this.conn.connection();
Expand Down
37 changes: 37 additions & 0 deletions graphql-server/src/queryFactory/dolt/doltEntityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,30 @@ async function getDoltBranchesQB(
return sel.limit(args.limit).getRawMany();
}

async function getDoltRemoteBranchesQB(
em: EntityManager,
args: {
limit: number;
offset?: number;
orderBy?: string;
dir?: "ASC" | "DESC";
},
): t.PR {
let sel = em
.createQueryBuilder()
.select("*")
.from("dolt_remote_branches", "");

if (args.orderBy && args.dir) {
sel = sel.addOrderBy(args.orderBy, args.dir);
}
if (args.offset !== undefined) {
sel = sel.offset(args.offset);
}

return sel.limit(args.limit).getRawMany();
}

export async function getDoltBranchesPaginated(
em: EntityManager,
args: t.ListBranchesArgs,
Expand All @@ -101,6 +125,19 @@ export async function getDoltBranchesPaginated(
});
}

export async function getDoltRemoteBranchesPaginated(
em: EntityManager,
args: t.ListBranchesArgs,
): t.PR {
const [orderBy, dir] = getOrderByColForBranches(args.sortBy);
return getDoltRemoteBranchesQB(em, {
limit: ROW_LIMIT + 1,
offset: args.offset,
orderBy,
dir,
});
}

export async function getAllDoltBranches(em: EntityManager): t.PR {
return getDoltBranchesQB(em, { limit: 1000 });
}
Expand Down
28 changes: 26 additions & 2 deletions graphql-server/src/queryFactory/dolt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ export class DoltQueryFactory
);
}

async getRemoteBranches(args: t.ListBranchesArgs): t.PR {
return this.queryForBuilder(
async em => dem.getDoltRemoteBranchesPaginated(em, args),
args.databaseName,
);
}

async getAllBranches(args: t.DBArgs): t.PR {
return this.queryForBuilder(
async em => dem.getAllDoltBranches(em),
Expand Down Expand Up @@ -458,21 +465,38 @@ export class DoltQueryFactory
);
}

async callPullRemote(args: t.PushOrPullRemoteArgs): t.PR {
async callPullRemote(args: t.RemoteMaybeBranchArgs): t.PR {
return this.query(
qh.callPullRemote,
[args.remoteName, args.branchName],
args.databaseName,
);
}

async callPushRemote(args: t.PushOrPullRemoteArgs): t.PR {
async callPushRemote(args: t.RemoteMaybeBranchArgs): t.PR {
return this.query(
qh.callPushRemote,
[args.remoteName, args.branchName],
args.databaseName,
);
}

async callFetchRemote(args: t.RemoteMaybeBranchArgs): t.PR {
return this.query(
qh.callFetchRemote(!!args.branchName),
[args.remoteName, args.branchName],
args.databaseName,
);
}

async getMergeBase(args: t.RefsArgs): Promise<string> {
const res: t.RawRow = await this.query(
qh.mergeBase,
[args.toRefName, args.fromRefName],
args.databaseName,
);
return Object.values(res[0])[0] as string;
}
}

async function getTableInfoWithQR(
Expand Down
3 changes: 3 additions & 0 deletions graphql-server/src/queryFactory/dolt/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ export const callPullRemote = `CALL DOLT_PULL(?, ?)`;

export const callPushRemote = `CALL DOLT_PUSH(?, ?)`;

export const callFetchRemote = (hasBranchName?: boolean) =>
`CALL DOLT_FETCH(?${hasBranchName ? ", ?" : ""})`;

// TAGS

export const callDeleteTag = `CALL DOLT_TAG("-d", ?)`;
Expand Down
19 changes: 17 additions & 2 deletions graphql-server/src/queryFactory/doltgres/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ export class DoltgresQueryFactory
);
}

async getRemoteBranches(args: t.ListBranchesArgs): t.PR {
return this.queryForBuilder(
async em => dem.getDoltRemoteBranchesPaginated(em, args),
args.databaseName,
);
}

async getAllBranches(args: t.DBArgs): t.PR {
return this.queryForBuilder(
async em => dem.getAllDoltBranches(em),
Expand Down Expand Up @@ -449,21 +456,29 @@ export class DoltgresQueryFactory
);
}

async callPullRemote(args: t.PushOrPullRemoteArgs): t.PR {
async callPullRemote(args: t.RemoteMaybeBranchArgs): t.PR {
return this.query(
qh.callPullRemote,
[args.remoteName, args.branchName],
args.databaseName,
);
}

async callPushRemote(args: t.PushOrPullRemoteArgs): t.PR {
async callPushRemote(args: t.RemoteMaybeBranchArgs): t.PR {
return this.query(
qh.callPushRemote,
[args.remoteName, args.branchName],
args.databaseName,
);
}

async callFetchRemote(args: t.RemoteMaybeBranchArgs): t.PR {
return this.query(
qh.callFetchRemote(!!args.branchName),
[args.remoteName, args.branchName],
args.databaseName,
);
}
}

async function getTableInfoWithQR(
Expand Down
5 changes: 5 additions & 0 deletions graphql-server/src/queryFactory/doltgres/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,8 @@ export const callDeleteRemote = `SELECT DOLT_REMOTE('remove', $1::text)`;
export const callPullRemote = `SELECT DOLT_PULL($1::text, $2::text)`;

export const callPushRemote = `SELECT DOLT_PUSH($1::text, $2::text)`;

export const callFetchRemote = (hasBranchName?: boolean) =>
hasBranchName
? `SELECT DOLT_FETCH($1::text, $2::text)`
: `SELECT DOLT_FETCH($1::text)`;
10 changes: 8 additions & 2 deletions graphql-server/src/queryFactory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export declare class QueryFactory {

getBranches(args: t.ListBranchesArgs): t.PR;

getRemoteBranches(args: t.ListBranchesArgs): t.PR;

getAllBranches(args: t.DBArgs): t.PR;

createNewBranch(args: t.BranchArgs & { fromRefName: string }): t.PR;
Expand Down Expand Up @@ -153,7 +155,11 @@ export declare class QueryFactory {

callDeleteRemote(args: t.RemoteArgs): t.PR;

callPullRemote(args: t.PushOrPullRemoteArgs): t.PR;
callPullRemote(args: t.RemoteMaybeBranchArgs): t.PR;

callPushRemote(args: t.RemoteMaybeBranchArgs): t.PR;

callFetchRemote(args: t.RemoteMaybeBranchArgs): t.PR;

callPushRemote(args: t.PushOrPullRemoteArgs): t.PR;
getMergeBase(args: t.RefsArgs): Promise<string>;
}
16 changes: 14 additions & 2 deletions graphql-server/src/queryFactory/mysql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ export class MySQLQueryFactory
return this.getAllBranches(args);
}

async getRemoteBranches(_args: t.ListBranchesArgs): t.PR {
throw notDoltError("remote branches");
}

async getAllBranches(args: t.DBArgs): t.PR {
const branch = await this.getBranch({ ...args, branchName: "main" });
return branch ? [branch] : [];
Expand Down Expand Up @@ -328,11 +332,19 @@ export class MySQLQueryFactory
throw notDoltError("delete remote");
}

async callPullRemote(_: t.PushOrPullRemoteArgs): t.PR {
async callPullRemote(_: t.RemoteMaybeBranchArgs): t.PR {
throw notDoltError("pull remote");
}

async callPushRemote(_: t.PushOrPullRemoteArgs): t.PR {
async callPushRemote(_: t.RemoteMaybeBranchArgs): t.PR {
throw notDoltError("push remote");
}

async callFetchRemote(_: t.RemoteMaybeBranchArgs): t.PR {
throw notDoltError("fetch remote");
}

async getMergeBase(_: t.RefsArgs): Promise<string> {
throw notDoltError("merge base");
}
}
2 changes: 1 addition & 1 deletion graphql-server/src/queryFactory/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type RefMaybeSchemaArgs = RefArgs & { schemaName?: string };
export type BranchArgs = DBArgs & { branchName: string };
export type RemoteArgs = DBArgs & { remoteName: string };
export type AddRemoteArgs = RemoteArgs & { remoteUrl: string };
export type PushOrPullRemoteArgs = RemoteArgs & { branchName?: string };
export type RemoteMaybeBranchArgs = RemoteArgs & { branchName?: string };
export type TagArgs = DBArgs & { tagName: string };
export type TableArgs = RefArgs & { tableName: string };
export type TableMaybeSchemaArgs = TableArgs & { schemaName?: string };
Expand Down
21 changes: 21 additions & 0 deletions graphql-server/src/remotes/remote.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ export class PushRes {
message: string;
}

@ObjectType()
export class FetchRes {
@Field()
success: boolean;
}

@ObjectType()
export class RemoteBranchDiffCounts {
@Field(_type => Int, { nullable: true })
ahead?: number;

@Field(_type => Int, { nullable: true })
behind?: number;
}

export function fromDoltRemotesRow(databaseName: string, r: RawRow): Remote {
return {
_id: `databases/${databaseName}/remotes/${r.name}`,
Expand Down Expand Up @@ -81,3 +96,9 @@ export function fromPushRes(r: RawRow): PushRes {
message: r.message,
};
}

export function fromFetchRes(r: RawRow): FetchRes {
return {
success: r.status === "0",
};
}
Loading

0 comments on commit 8d4890f

Please sign in to comment.