-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from dolthub/taylor/diffs
Commit diff page
- Loading branch information
Showing
138 changed files
with
6,258 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Field, Float, ObjectType } from "@nestjs/graphql"; | ||
import { RawRow, RawRows } from "../utils/commonTypes"; | ||
|
||
@ObjectType() | ||
export class DiffStat { | ||
@Field(_type => Float) | ||
rowsUnmodified: number; | ||
|
||
@Field(_type => Float) | ||
rowsAdded: number; | ||
|
||
@Field(_type => Float) | ||
rowsDeleted: number; | ||
|
||
@Field(_type => Float) | ||
rowsModified: number; | ||
|
||
@Field(_type => Float) | ||
cellsModified: number; | ||
|
||
@Field(_type => Float) | ||
rowCount: number; | ||
|
||
@Field(_type => Float) | ||
cellCount: number; | ||
} | ||
|
||
const defaultStat = { | ||
rowsUnmodified: 0, | ||
rowsAdded: 0, | ||
rowsDeleted: 0, | ||
rowsModified: 0, | ||
cellsModified: 0, | ||
rowCount: 0, | ||
cellCount: 0, | ||
}; | ||
|
||
export function fromDoltDiffStat(res: RawRows): DiffStat { | ||
if (!res.length) return defaultStat; | ||
|
||
const reduced = res.reduce((acc: DiffStat, row: RawRow) => { | ||
return { | ||
rowsUnmodified: Number(row.rows_unmodified) + acc.rowsUnmodified, | ||
rowsAdded: Number(row.rows_added) + acc.rowsAdded, | ||
rowsDeleted: Number(row.rows_deleted) + acc.rowsDeleted, | ||
rowsModified: Number(row.rows_modified) + acc.rowsModified, | ||
cellsModified: Number(row.cells_modified) + acc.cellsModified, | ||
rowCount: Number(row.new_row_count) + acc.rowCount, | ||
cellCount: Number(row.new_cell_count) + acc.cellCount, | ||
}; | ||
}, defaultStat); | ||
|
||
return reduced; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const getThreeDotDiffStatQuery = (hasTableName?: boolean): string => | ||
`SELECT * FROM DOLT_DIFF_STAT(?${hasTableName ? `, ?` : ""})`; | ||
|
||
export const getDiffStatQuery = (hasTableName?: boolean): string => | ||
`SELECT * FROM DOLT_DIFF_STAT(?, ?${hasTableName ? `, ?` : ""})`; |
67 changes: 67 additions & 0 deletions
67
packages/graphql-server/src/diffStats/diffStat.resolver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Args, ArgsType, Field, Query, Resolver } from "@nestjs/graphql"; | ||
import { DataSourceService } from "../dataSources/dataSource.service"; | ||
import { CommitDiffType } from "../diffSummaries/diffSummary.enums"; | ||
import { DBArgs } from "../utils/commonTypes"; | ||
import { DiffStat, fromDoltDiffStat } from "./diffStat.model"; | ||
import { getDiffStatQuery, getThreeDotDiffStatQuery } from "./diffStat.queries"; | ||
|
||
@ArgsType() | ||
export class DiffStatArgs extends DBArgs { | ||
@Field() | ||
fromRefName: string; | ||
|
||
@Field() | ||
toRefName: string; | ||
|
||
@Field({ nullable: true }) | ||
refName?: string; | ||
|
||
@Field(_type => CommitDiffType, { nullable: true }) | ||
type?: CommitDiffType; | ||
|
||
@Field({ nullable: true }) | ||
tableName?: string; | ||
} | ||
|
||
@Resolver(_of => DiffStat) | ||
export class DiffStatResolver { | ||
constructor(private readonly dss: DataSourceService) {} | ||
|
||
@Query(_returns => DiffStat) | ||
async diffStat(@Args() args: DiffStatArgs): Promise<DiffStat> { | ||
const type = args.type ?? CommitDiffType.TwoDot; | ||
checkArgs(args); | ||
|
||
return this.dss.query(async query => { | ||
if (type === CommitDiffType.ThreeDot) { | ||
const res = await query(getThreeDotDiffStatQuery(!!args.tableName), [ | ||
`${args.toRefName}...${args.fromRefName}`, | ||
args.tableName, | ||
]); | ||
return fromDoltDiffStat(res); | ||
} | ||
|
||
const res = await query(getDiffStatQuery(!!args.tableName), [ | ||
args.fromRefName, | ||
args.toRefName, | ||
args.tableName, | ||
]); | ||
return fromDoltDiffStat(res); | ||
}, args.databaseName); | ||
} | ||
} | ||
|
||
export function checkArgs(args: DiffStatArgs): void { | ||
if ( | ||
args.type === CommitDiffType.TwoDot && | ||
(isRefKeyword(args.fromRefName) || isRefKeyword(args.toRefName)) && | ||
!args.refName | ||
) { | ||
throw new Error("refName is required for TwoDot diff with ref keyword"); | ||
} | ||
} | ||
|
||
function isRefKeyword(refName: string): boolean { | ||
const upper = refName.toUpperCase(); | ||
return upper === "WORKING" || upper === "HEAD" || upper === "STAGED"; | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/graphql-server/src/diffSummaries/diffSummary.enums.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { registerEnumType } from "@nestjs/graphql"; | ||
|
||
export enum TableDiffType { | ||
Added, | ||
Dropped, | ||
Modified, | ||
Renamed, | ||
} | ||
|
||
registerEnumType(TableDiffType, { name: "TableDiffType" }); | ||
|
||
export function toTableDiffType(t: string): TableDiffType { | ||
switch (t) { | ||
case "added": | ||
return TableDiffType.Added; | ||
case "dropped": | ||
return TableDiffType.Dropped; | ||
case "modified": | ||
return TableDiffType.Modified; | ||
case "renamed": | ||
return TableDiffType.Renamed; | ||
default: | ||
throw new Error(`Unknown table diff type: ${t}`); | ||
} | ||
} | ||
|
||
export enum CommitDiffType { | ||
TwoDot, | ||
ThreeDot, | ||
Unspecified, | ||
} | ||
|
||
registerEnumType(CommitDiffType, { name: "CommitDiffType" }); |
53 changes: 53 additions & 0 deletions
53
packages/graphql-server/src/diffSummaries/diffSummary.model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Field, ID, ObjectType } from "@nestjs/graphql"; | ||
import { RawRow } from "../utils/commonTypes"; | ||
import { TableDiffType, toTableDiffType } from "./diffSummary.enums"; | ||
|
||
@ObjectType() | ||
export class DiffSummary { | ||
@Field(_type => ID) | ||
_id: string; | ||
|
||
@Field() | ||
fromTableName: string; | ||
|
||
@Field() | ||
toTableName: string; | ||
|
||
@Field() | ||
tableName: string; | ||
|
||
@Field(_type => TableDiffType) | ||
tableType: TableDiffType; | ||
|
||
@Field() | ||
hasDataChanges: boolean; | ||
|
||
@Field() | ||
hasSchemaChanges: boolean; | ||
} | ||
|
||
export function fromDoltDiffSummary(row: RawRow): DiffSummary { | ||
const fromTableName = row.from_table_name; | ||
const toTableName = row.to_table_name; | ||
const tableName = getTableName(fromTableName, toTableName); | ||
const _id = `tableDiffSummaries/${tableName}`; | ||
return { | ||
_id, | ||
fromTableName, | ||
toTableName, | ||
tableName, | ||
tableType: toTableDiffType(row.diff_type), | ||
hasDataChanges: row.data_change, | ||
hasSchemaChanges: row.schema_change, | ||
}; | ||
} | ||
|
||
function getTableName(fromTableName: string, toTableName: string): string { | ||
if (!fromTableName.length && !toTableName.length) return ""; | ||
if (!fromTableName.length) return toTableName; | ||
if (!toTableName.length) return fromTableName; | ||
if (fromTableName !== toTableName) { | ||
return toTableName; | ||
} | ||
return toTableName; | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/graphql-server/src/diffSummaries/diffSummary.queries.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const getDiffSummaryQuery = (hasTableName?: boolean): string => | ||
`SELECT * FROM DOLT_DIFF_SUMMARY(?, ?${hasTableName ? `, ?` : ""})`; | ||
|
||
export const getThreeDotDiffSummaryQuery = (hasTableName?: boolean): string => | ||
`SELECT * FROM DOLT_DIFF_SUMMARY(?${hasTableName ? `, ?` : ""})`; |
Oops, something went wrong.