Skip to content

Commit

Permalink
everything should use schema name instead of "main" schema
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal committed Sep 3, 2024
1 parent 8cb6644 commit b8c70f0
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 39 deletions.
5 changes: 4 additions & 1 deletion src/components/gui/schema-editor/column-fk-popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import { Separator } from "../../ui/separator";
export default function ColumnForeignKeyPopup({
constraint,
disabled,
schemaName,
onChange,
}: Readonly<{
constraint: DatabaseForeignKeyClause;
schemaName: string;
disabled: boolean;
onChange: ColumnChangeEvent;
}>) {
Expand All @@ -33,6 +35,7 @@ export default function ColumnForeignKeyPopup({
<div className="flex flex-col gap-2 mt-2">
<Label className="text-xs font-normal">Foreign Table Name</Label>
<TableCombobox
schemaName={schemaName}
value={constraint.foreignTableName}
disabled={disabled}
onChange={(newTable) => {
Expand Down Expand Up @@ -64,7 +67,7 @@ export default function ColumnForeignKeyPopup({
},
});
}}
schemaName={"main"}
schemaName={schemaName}
tableName={constraint.foreignTableName}
/>
</div>
Expand Down
2 changes: 2 additions & 0 deletions src/components/gui/schema-editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,11 @@ export default function SchemaEditor({
columns={value.columns}
onChange={onChange}
onAddColumn={onAddColumn}
schemaName={value.schemaName}
/>
<ColumnsProvider value={value.columns}>
<SchemaEditorConstraintList
schemaName={value.schemaName}
constraints={value.constraints}
onChange={onChange}
disabled={!isCreateScript}
Expand Down
15 changes: 13 additions & 2 deletions src/components/gui/schema-editor/schema-editor-column-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ function changeColumnOnIndex(
function ColumnItem({
value,
idx,
schemaName,
onChange,
}: {
value: DatabaseTableColumnChange;
idx: number;
schemaName?: string;
onChange: Dispatch<SetStateAction<DatabaseTableSchemaChange>>;
}) {
const disabled = !!value.old;
Expand Down Expand Up @@ -198,11 +200,12 @@ function ColumnItem({
/>
)}

{column.constraint?.foreignKey && (
{column.constraint?.foreignKey && schemaName && (
<ColumnForeignKeyPopup
constraint={column.constraint.foreignKey}
disabled={disabled}
onChange={change}
schemaName={schemaName}
/>
)}

Expand Down Expand Up @@ -294,10 +297,12 @@ function ColumnItem({
export default function SchemaEditorColumnList({
columns,
onChange,
schemaName,
onAddColumn,
}: Readonly<{
columns: DatabaseTableColumnChange[];
onChange: Dispatch<SetStateAction<DatabaseTableSchemaChange>>;
schemaName?: string;
onAddColumn: () => void;
}>) {
const headerStyle = "text-xs p-2 text-left bg-secondary border";
Expand All @@ -318,7 +323,13 @@ export default function SchemaEditorColumnList({
</thead>
<tbody>
{columns.map((col, idx) => (
<ColumnItem idx={idx} value={col} key={idx} onChange={onChange} />
<ColumnItem
idx={idx}
value={col}
key={idx}
onChange={onChange}
schemaName={schemaName}
/>
))}
</tbody>
<tfoot>
Expand Down
20 changes: 16 additions & 4 deletions src/components/gui/schema-editor/schema-editor-constraint-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,15 @@ function ColumnForeignKey({
constraint,
onChange,
disabled,
schemaName,
}: Readonly<{
constraint: DatabaseTableColumnConstraint;
onChange: ConstraintChangeHandler;
disabled?: boolean;
schemaName: string;
}>) {
const { columns } = useColumnList();
const { currentSchema } = useSchema();
const { schema } = useSchema();

const columnMemo = useMemo(() => {
return [...new Set(columns.map((c) => c.new?.name ?? c.old?.name ?? ""))];
Expand All @@ -86,7 +88,7 @@ function ColumnForeignKey({
const fkTableName = constraint.foreignKey?.foreignTableName;

if (fkTableName) {
const fkTableSchema = currentSchema.find(
const fkTableSchema = (schema[schemaName] ?? []).find(
(s) => s.type === "table" && s.name === fkTableName
);

Expand All @@ -96,7 +98,7 @@ function ColumnForeignKey({
}

return [];
}, [constraint, currentSchema]);
}, [constraint, schemaName, schema]);

const onConstraintChange = useCallback(
(newColumn: string[]) => {
Expand Down Expand Up @@ -137,6 +139,7 @@ function ColumnForeignKey({
<td className="border">
<TableCombobox
borderless
schemaName={schemaName}
disabled={disabled}
onChange={onFkTableNameChange}
value={constraint.foreignKey?.foreignTableName}
Expand Down Expand Up @@ -287,12 +290,14 @@ function RemovableConstraintItem({

function ColumnItemBody({
onChange,
schemaName,
constraint,
disabled,
}: PropsWithChildren<{
idx: number;
onChange: Dispatch<SetStateAction<DatabaseTableSchemaChange>>;
constraint: DatabaseTableConstraintChange;
schemaName?: string;
disabled?: boolean;
}>) {
const onChangeConstraint = useCallback(
Expand All @@ -315,12 +320,13 @@ function ColumnItemBody({
const currentConstraint = constraint.new ?? constraint.old;
if (!currentConstraint) return null;

if (currentConstraint.foreignKey) {
if (currentConstraint.foreignKey && schemaName) {
return (
<ColumnForeignKey
disabled={disabled}
constraint={currentConstraint}
onChange={onChangeConstraint}
schemaName={schemaName}
/>
);
}
Expand Down Expand Up @@ -362,19 +368,22 @@ function ColumnItem({
constraint,
onChange,
idx,
schemaName,
disabled,
}: Readonly<{
constraint: DatabaseTableConstraintChange;
onChange: Dispatch<SetStateAction<DatabaseTableSchemaChange>>;
idx: number;
disabled?: boolean;
schemaName?: string;
}>) {
return (
<RemovableConstraintItem idx={idx} onChange={onChange}>
<ColumnItemBody
constraint={constraint}
onChange={onChange}
idx={idx}
schemaName={schemaName}
disabled={disabled}
/>
</RemovableConstraintItem>
Expand All @@ -384,10 +393,12 @@ function ColumnItem({
export default function SchemaEditorConstraintList({
constraints,
onChange,
schemaName,
disabled,
}: Readonly<{
constraints: DatabaseTableConstraintChange[];
onChange: Dispatch<SetStateAction<DatabaseTableSchemaChange>>;
schemaName?: string;
disabled?: boolean;
}>) {
const headerClassName = "text-xs p-2 text-left bg-secondary border";
Expand Down Expand Up @@ -429,6 +440,7 @@ export default function SchemaEditorConstraintList({
constraint={constraint}
onChange={onChange}
disabled={disabled}
schemaName={schemaName}
/>
);
})}
Expand Down
6 changes: 4 additions & 2 deletions src/components/gui/schema-sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import SchemaList from "./schema-sidebar-list";
import ListButtonItem from "./list-button-item";
import { Separator } from "../ui/separator";
import { openTab } from "@/messages/open-tab";
import { useSchema } from "@/context/schema-provider";

export default function SchemaView() {
const [search, setSearch] = useState("");
const { currentSchemaName } = useSchema();

const onNewTable = useCallback(() => {
openTab({
type: "schema",
schemaName: "main",
schemaName: currentSchemaName,
});
}, []);
}, [currentSchemaName]);

return (
<div className="flex flex-col overflow-hidden grow">
Expand Down
13 changes: 10 additions & 3 deletions src/components/gui/table-cell/generic-cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,27 @@ interface TableCellProps<T = unknown> {
}

interface SneakpeakProps {
fkSchemaName: string;
fkTableName: string;
fkColumnName: string;
value: DatabaseValue;
}

function SnippetRow({ fkTableName, fkColumnName, value }: SneakpeakProps) {
function SnippetRow({
fkSchemaName,
fkTableName,
fkColumnName,
value,
}: SneakpeakProps) {
const { databaseDriver } = useDatabaseDriver();
const [data, setData] = useState<DatabaseResultSet>();

useEffect(() => {
databaseDriver
.findFirst("main", fkTableName, { [fkColumnName]: value })
.findFirst(fkSchemaName, fkTableName, { [fkColumnName]: value })
.then(setData)
.catch(console.error);
}, [databaseDriver, fkTableName, fkColumnName, value]);
}, [databaseDriver, fkSchemaName, fkTableName, fkColumnName, value]);

if (!data) {
return (
Expand Down Expand Up @@ -208,6 +214,7 @@ export default function GenericCell({
return (
<div className="flex items-center shrink-0 cursor-pointer ml-2">
<ForeignKeyColumnSnippet
fkSchemaName={header.foreignKey.foreignSchemaName}

Check failure on line 217 in src/components/gui/table-cell/generic-cell.tsx

View workflow job for this annotation

GitHub Actions / quality-check

Type 'string | undefined' is not assignable to type 'string'.
fkColumnName={header.foreignKey.foreignColumns[0] as string}
fkTableName={header.foreignKey.foreignTableName}
value={value}
Expand Down
2 changes: 1 addition & 1 deletion src/components/gui/table-combobox/TableColumnCombobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default function TableColumnCombobox({
.catch(() => {
setSchema({
tableName,
schemaName: "main",
schemaName,
columns: [],
pk: [],
autoIncrement: false,
Expand Down
6 changes: 4 additions & 2 deletions src/components/gui/table-combobox/TableCombobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ import { useSchema } from "@/context/schema-provider";
export default function TableCombobox({
value,
onChange,
schemaName,
disabled,
borderless,
}: Readonly<{
value?: string;
schemaName: string;
onChange: (v: string) => void;
disabled?: boolean;
borderless?: boolean;
}>) {
const [open, setOpen] = useState(false);
const { currentSchema, refresh } = useSchema();
const { schema, refresh } = useSchema();

return (
<Popover open={open} onOpenChange={setOpen}>
Expand All @@ -48,7 +50,7 @@ export default function TableCombobox({

<CommandEmpty>No table found.</CommandEmpty>
<CommandGroup className="max-h-[250px] overflow-y-auto">
{currentSchema.map((table) => (
{(schema[schemaName] ?? []).map((table) => (
<CommandItem
key={table.name}
value={table.name}
Expand Down
6 changes: 5 additions & 1 deletion src/components/gui/tabs/trigger-tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ export default function TriggerTab({
</SelectContent>
</Select>
</div>
<TableCombobox value={trigger?.tableName} onChange={noop} />
<TableCombobox
schemaName={schemaName}
value={trigger?.tableName}
onChange={noop}
/>
</div>
</div>
<div className="grow overflow-hidden">
Expand Down
2 changes: 1 addition & 1 deletion src/components/lib/sql-execute-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export async function commitChange({

try {
const result = await driver.updateTableData(
tableSchema.schemaName ?? "main",
tableSchema.schemaName,
tableName,
plans.map((p) => p.plan),
tableSchema
Expand Down
1 change: 1 addition & 0 deletions src/drivers/base-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export type DatabaseForeignKeyAction =
| "NO_ACTION";

export interface DatabaseForeignKeyClause {
foreignSchemaName?: string;
foreignTableName?: string;
foreignColumns?: string[];
columns?: string[];
Expand Down
4 changes: 2 additions & 2 deletions src/drivers/sqlite-base-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export abstract class SqliteLikeBaseDriver extends CommonSQLImplement {
type: "table",
schemaName,
name: row.name,
tableSchema: parseCreateTableScript(row.sql),
tableSchema: parseCreateTableScript(schemaName, row.sql),
});
} catch {
tmp.push({ type: "table", name: row.name, schemaName });
Expand Down Expand Up @@ -125,7 +125,7 @@ export abstract class SqliteLikeBaseDriver extends CommonSQLImplement {
if (def) {
const createScript = def.sql;
return {
...parseCreateTableScript(createScript),
...parseCreateTableScript(schemaName, createScript),
createScript,
schemaName,
};
Expand Down
4 changes: 2 additions & 2 deletions src/drivers/sqlite/sql-parse-table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import {

// Parse column constraint
function pcc(sql: string) {
return parseColumnConstraint(buildSyntaxCursor(sql));
return parseColumnConstraint("main", buildSyntaxCursor(sql));
}

function p(sql: string) {
return parseCreateTableScript(sql);
return parseCreateTableScript("main", sql);
}

it("parse column constraint", () => {

Check failure on line 20 in src/drivers/sqlite/sql-parse-table.test.ts

View workflow job for this annotation

GitHub Actions / Tests annotations (🧪 jest-coverage-report-action)

parse column constraint

Error: expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 @@ -4,8 +4,9 @@ "user_id", ], "foreignColumns": Array [ "id", ], + "foreignSchemaName": "main", "foreignTableName": "users", }, } at Object.toEqual (/home/runner/work/libsql-studio/libsql-studio/src/drivers/sqlite/sql-parse-table.test.ts:80:5) at Promise.then.completed (/home/runner/work/libsql-studio/libsql-studio/node_modules/jest-circus/build/utils.js:298:28) at new Promise (<anonymous>) at callAsyncCircusFn (/home/runner/work/libsql-studio/libsql-studio/node_modules/jest-circus/build/utils.js:231:10) at _callCircusTest (/home/runner/work/libsql-studio/libsql-studio/node_modules/jest-circus/build/run.js:316:40) at processTicksAndRejections (node:internal/process/task_queues:95:5) at _runTest (/home/runner/work/libsql-studio/libsql-studio/node_modules/jest-circus/build/run.js:252:3) at _runTestsForDescribeBlock (/home/runner/work/libsql-studio/libsql-studio/node_modules/jest-circus/build/run.js:126:9) at run (/home/runner/work/libsql-studio/libsql-studio/node_modules/jest-circus/build/run.js:71:3) at runAndTransformResultsToJestFormat (/home/runner/work/libsql-studio/libsql-studio/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21) at jestAdapter (/home/runner/work/libsql-studio/libsql-studio/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19) at runTestInternal (/home/runner/work/libsql-studio/libsql-studio/node_modules/jest-runner/build/runTest.js:367:16) at runTest (/home/runner/work/libsql-studio/libsql-studio/node_modules/jest-runner/build/runTest.js:444:34) at Object.worker (/home/runner/work/libsql-studio/libsql-studio/node_modules/jest-runner/build/testWorker.js:106:12)
Expand Down
Loading

0 comments on commit b8c70f0

Please sign in to comment.