Skip to content

Commit

Permalink
web: Fix more dolt queries
Browse files Browse the repository at this point in the history
  • Loading branch information
tbantle22 committed Jun 6, 2024
1 parent d3b4874 commit 7783cac
Show file tree
Hide file tree
Showing 16 changed files with 363 additions and 251 deletions.
25 changes: 9 additions & 16 deletions web/components/CellButtons/testData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@ import {
RowForDataTableFragment,
} from "@gen/graphql-types";
import { TableParams } from "@lib/params";
import { sprintf } from "@lib/sprintf";
import { Props } from "./useGetDoltDiffQuery";

const replaceToken = "$";
// eslint-disable-next-line no-return-assign
const sprintf = (str: string, ...argv: any[]): string =>
!argv.length
? str
: // eslint-disable-next-line no-param-reassign
sprintf((str = str.replace(replaceToken, argv.shift())), ...argv);

// dolt_diff constants for all tables
const doltDiffColumns: ColumnForDataTableFragment[] = [
{
Expand Down Expand Up @@ -246,7 +239,7 @@ export const saCellDiffQuery = sprintf(
);

export const saCellHistoryQuery = sprintf(
`SELECT \`$\`, $ FROM \`dolt_history_$\` WHERE \`$\` = '$' AND \`$\` = '$' AND \`$\` = '$' $`,
"SELECT `$`, $ FROM `dolt_history_$` WHERE `$` = '$' AND `$` = '$' AND `$` = '$' $",
saClickedCell,
historyCols,
saTableName,
Expand Down Expand Up @@ -295,7 +288,7 @@ export const saRowDiffQuery = sprintf(
);

export const saRowHistoryQuery = sprintf(
`SELECT \`$\`, \`$\`, \`$\`, \`$\`, $ FROM \`dolt_history_$\` WHERE \`$\` = '$' AND \`$\` = '$' AND \`$\` = '$' $`,
"SELECT `$`, `$`, `$`, `$`, $ FROM `dolt_history_$` WHERE `$` = '$' AND `$` = '$' AND `$` = '$' $",
saPK1,
saPK2,
saPK3,
Expand All @@ -312,7 +305,7 @@ export const saRowHistoryQuery = sprintf(
);

export const saDiffForCommitsQuery = sprintf(
`SELECT diff_type, \`from_$\`, \`to_$\`, \`from_$\`, \`to_$\`, \`from_$\`, \`to_$\`, \`from_$\`, \`to_$\`, $\nFROM \`dolt_diff_$\`\nWHERE from_commit="$" AND to_commit="$"`,
'SELECT diff_type, `from_$`, `to_$`, `from_$`, `to_$`, `from_$`, `to_$`, `from_$`, `to_$`, $\nFROM `dolt_diff_$`\nWHERE from_commit="$" AND to_commit="$"',
saPK1,
saPK1,
saPK2,
Expand All @@ -328,7 +321,7 @@ export const saDiffForCommitsQuery = sprintf(
);

export const saCommitDiffForCommitsQuery = sprintf(
`SELECT diff_type, \`from_$\`, \`to_$\`, \`from_$\`, \`to_$\`, \`from_$\`, \`to_$\`, \`from_$\`, \`to_$\`, $\nFROM \`dolt_commit_diff_$\`\nWHERE from_commit="$" AND to_commit="$"`,
'SELECT diff_type, `from_$`, `to_$`, `from_$`, `to_$`, `from_$`, `to_$`, `from_$`, `to_$`, $\nFROM `dolt_commit_diff_$`\nWHERE from_commit="$" AND to_commit="$"',
saPK1,
saPK1,
saPK2,
Expand All @@ -344,7 +337,7 @@ export const saCommitDiffForCommitsQuery = sprintf(
);

const saBaseDiffHistoryQuery = sprintf(
`SELECT \`$\`, \`$\`, \`$\`, \`$\`, $ FROM \`dolt_history_$\``,
"SELECT `$`, `$`, `$`, `$`, $ FROM `dolt_history_$`",
saPK1,
saPK2,
saPK3,
Expand All @@ -354,21 +347,21 @@ const saBaseDiffHistoryQuery = sprintf(
);

export const saDiffHistoryQuery = sprintf(
`$ $`,
"$ $",
saBaseDiffHistoryQuery,
historyOrderBy,
);

export const saDiffForCommitsWithClausesQuery = sprintf(
`$ AND \`diff_type\` = 'added' AND \`to_$\` = '$'\n$`,
"$ AND `diff_type` = 'added' AND `to_$` = '$'\n$",
saDiffForCommitsQuery,
saPK1,
saPK1Val,
diffOrderBy,
);

export const saDiffHistoryWithClausesQuery = sprintf(
`$ WHERE \`$\` = '$' $`,
"$ WHERE `$` = '$' $",
saBaseDiffHistoryQuery,
saPK1,
saPK1Val,
Expand Down
11 changes: 3 additions & 8 deletions web/components/CellButtons/useGetDoltDiffQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
RowForDataTableFragment,
} from "@gen/graphql-types";
import useSqlBuilder from "@hooks/useSqlBuilder";
import { mapColsToColumnNames } from "@hooks/useSqlBuilder/util";
import { getSqlOrderBy, mapColsToColumnNames } from "@hooks/useSqlBuilder/util";
import useSqlParser from "@hooks/useSqlParser";
import { TableParams } from "@lib/params";
import { convertTimestamp } from "./utils";
Expand Down Expand Up @@ -49,18 +49,13 @@ export function useGetDoltDiffQuery(props: Props): () => string {
from: [{ table: tableName }],
});
const withWhere = `${sel} WHERE ${getWhereClause(colsWithNamesAndVals, props.cidx, props.isPK)}`;
console.log("WITH WHERE", withWhere);
const parsed = parseSelectQuery(withWhere);

if (!parsed) return "";

return convertToSqlSelect({
...parsed,
orderby: [
{
type: "DESC",
expr: { type: "column_ref", column: "to_commit_date" },
},
],
orderby: [getSqlOrderBy("to_commit_date", "DESC")],
});
};
return generate;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.sqlIcon {
@apply text-lg font-normal;
}

.sqlLink {
@apply font-thin text-primary hover:text-primary;
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { DropdownItem } from "@components/DatabaseOptionsDropdown";
import Link from "@components/links/Link";
import { SmallLoader } from "@dolthub/react-components";
import { prettyJSON } from "@dolthub/web-utils";
import {
ColumnForDataTableFragment,
CommitDiffType,
useDataTableQuery,
} from "@gen/graphql-types";
import useSqlParser from "@hooks/useSqlParser";
import { RequiredRefsParams } from "@lib/params";
import { sqlQuery } from "@lib/urls";
import { AiOutlineConsoleSql } from "@react-icons/all-files/ai/AiOutlineConsoleSql";
import { getDoltCommitDiffQuery } from "../DiffTableStats/utils";
import { HiddenColIndexes } from "../utils";
import css from "./index.module.css";
import { HiddenColIndexes } from "./utils";
import { useGetDoltCommitDiffQuery } from "./useGetDoltCommitDiffQuery";

type Props = {
params: RequiredRefsParams & {
Expand All @@ -27,6 +29,12 @@ type InnerProps = Props & {
};

function Inner(props: InnerProps) {
const { parseSelectQuery } = useSqlParser();
const generateQuery = useGetDoltCommitDiffQuery(props);
const q =
'SELECT `diff_type`, `from_pk`, `to_pk`, `from_col1`, `to_col1`, `from_commit`, `from_commit_date`, `to_commit`, `to_commit_date` FROM `dolt_commit_diff_test2` WHERE `from_commit` = DOLT_MERGE_BASE("main", "test") AND `to_commit` = HASHOF("test")';
console.log("PARSED", prettyJSON(parseSelectQuery(q)));

return (
<DropdownItem
icon={<AiOutlineConsoleSql className={css.sqlIcon} />}
Expand All @@ -35,7 +43,7 @@ function Inner(props: InnerProps) {
<Link
{...sqlQuery({
...props.params,
q: getDoltCommitDiffQuery(props),
q: generateQuery(),
})}
className={css.sqlLink}
>
Expand Down
75 changes: 75 additions & 0 deletions web/components/DiffTable/DataDiff/ViewSqlLink/testData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { ColumnForDataTableFragment, CommitDiffType } from "@gen/graphql-types";
import { fakeCommitId } from "@hooks/useCommitListForBranch/mocks";
import { RequiredRefsParams, TableParams } from "@lib/params";
import { sprintf } from "@lib/sprintf";
import { Props } from "./useGetDoltCommitDiffQuery";

type Params = RequiredRefsParams & { tableName: string; refName: string };

const tableParams: TableParams = {
databaseName: "dbname",
refName: "master",
tableName: "test-table",
};

export const params: Params = {
...tableParams,
fromRefName: fakeCommitId(),
toRefName: fakeCommitId(),
};

export const tableCols: ColumnForDataTableFragment[] = [
{
__typename: "Column",
name: "id",
isPrimaryKey: true,
type: "INT",
},
{
__typename: "Column",
name: "name",
isPrimaryKey: false,
type: "VARCHAR(16383)",
},
];

export const noDiffTagsOrRemovedColsProps: Props = {
params,
columns: tableCols,
hiddenColIndexes: [],
};

export const noDiffTagsOrRemovedColsExpected = sprintf(
"SELECT `diff_type`, `from_id`, `to_id`, `from_name`, `to_name`, `from_commit`, `from_commit_date`, `to_commit`, `to_commit_date` FROM `dolt_commit_diff_$` WHERE `from_commit` = '$' AND `to_commit` = '$'",
params.tableName,
params.fromRefName,
params.toRefName,
);

export const noDiffTagsAndRemovedColsProps: Props = {
params,
columns: tableCols,
hiddenColIndexes: [1],
};

export const noDiffTagsAndRemovedColsExpected = sprintf(
"SELECT `diff_type`, `from_id`, `to_id`, `from_commit`, `from_commit_date`, `to_commit`, `to_commit_date` FROM `dolt_commit_diff_$` WHERE `from_commit` = '$' AND `to_commit` = '$'",
params.tableName,
params.fromRefName,
params.toRefName,
);

export const noDiffTagsAndRemovedColsForPullProps: Props = {
params,
columns: tableCols,
hiddenColIndexes: [1],
type: CommitDiffType.ThreeDot,
};

export const noDiffTagsAndRemovedColsForPullExpected = sprintf(
"SELECT `diff_type`, `from_id`, `to_id`, `from_commit`, `from_commit_date`, `to_commit`, `to_commit_date` FROM `dolt_commit_diff_$` WHERE `from_commit` = DOLT_MERGE_BASE('$', '$') AND `to_commit` = HASHOF('$')",
params.tableName,
params.toRefName,
params.fromRefName,
params.fromRefName,
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { MockedProvider } from "@apollo/client/testing";
import { databaseDetailsMock } from "@components/util/NotDoltWrapper/mocks";
import { renderHook } from "@testing-library/react";
import { ReactNode } from "react";
import {
noDiffTagsAndRemovedColsExpected,
noDiffTagsAndRemovedColsForPullExpected,
noDiffTagsAndRemovedColsForPullProps,
noDiffTagsAndRemovedColsProps,
noDiffTagsOrRemovedColsExpected,
noDiffTagsOrRemovedColsProps,
} from "./testData";
import { Props, useGetDoltCommitDiffQuery } from "./useGetDoltCommitDiffQuery";

function renderUseGetDoltCommitDiffQuery(
props: Props,
isPostgres = false,
): () => string {
const wrapper = ({ children }: { children: ReactNode }) => (
<MockedProvider mocks={[databaseDetailsMock(true, true, isPostgres)]}>
{children}
</MockedProvider>
);

const { result } = renderHook(() => useGetDoltCommitDiffQuery(props), {
wrapper,
});
return result.current;
}

const tests: Array<{ desc: string; props: Props; expected: string }> = [
{
desc: "no diff tags or removed columns",
props: noDiffTagsOrRemovedColsProps,
expected: noDiffTagsOrRemovedColsExpected,
},
{
desc: "no diff tags and removed columns",
props: noDiffTagsAndRemovedColsProps,
expected: noDiffTagsAndRemovedColsExpected,
},
{
desc: "no diff tags and removed columns for pull",
props: noDiffTagsAndRemovedColsForPullProps,
expected: noDiffTagsAndRemovedColsForPullExpected,
},
];

describe("test getDoltCommitDiffQuery for diff tables", () => {
tests.forEach(({ desc, props, expected }) => {
it(`[mysql] generates dolt_commit_diff query for ${desc}`, () => {
const generateQuery = renderUseGetDoltCommitDiffQuery(props);
expect(generateQuery()).toBe(expected);
});
});
});
Loading

0 comments on commit 7783cac

Please sign in to comment.