Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,30 @@ name: Build

on:
push:
branches: [ "main" ]
branches: ["main"]
pull_request:
branches: [ "main" ]
types: [ opened, synchronize, reopened ]
branches: ["main"]
types: [opened, synchronize, reopened]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x, 22.x]

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm install
- name: Run eslint
run: npm run lint
- name: Run vite build
run: npm run build
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: "npm"
- run: npm install
- name: Run tests
run: npm test
- name: Run eslint
run: npm run lint
- name: Run vite build
run: npm run build
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dev": "vite",
"build": "vite build",
"lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
"test": "node --test",
"preview": "vite preview"
},
"dependencies": {
Expand Down
14 changes: 9 additions & 5 deletions src/components/EditorHeader/Modal/Modal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import { isRtl } from "../../../i18n/utils/rtl";
import { useExtensions } from "../../../context/ExtensionsContext";
import { importSQL } from "../../../utils/importSQL";
import { parsePostgresSQL } from "../../../utils/importSQL/postgresImplicitReferences";
import {
allowedTypesFor,
normalizeAiDiagram,
Expand Down Expand Up @@ -130,10 +131,10 @@ export default function Modal({
ast = oracleParser.parse(importSource.src);
} else {
const parser = new Parser();

ast = parser.astify(importSource.src, {
database: targetDatabase,
});
ast =
targetDatabase === DB.POSTGRES
? parsePostgresSQL(parser, importSource.src, targetDatabase)
: parser.astify(importSource.src, { database: targetDatabase });
}
} catch (error) {
const message = error.location
Expand Down Expand Up @@ -200,7 +201,10 @@ export default function Modal({

if (!result) return;

const { diagram, warnings } = normalizeAiDiagram(result.diagram, database);
const { diagram, warnings } = normalizeAiDiagram(
result.diagram,
database,
);
const allWarnings = [...(result.warnings ?? []), ...warnings];

applyImportedDiagram(diagram);
Expand Down
40 changes: 30 additions & 10 deletions src/utils/importSQL/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { nanoid } from "nanoid";
import { Cardinality, Constraint, DB } from "../../data/constants";
import { dbToTypes } from "../../data/datatypes";
import { buildSQLFromAST } from "./shared";
import { postgresReferenceFieldNames } from "./postgresImplicitReferences";

const affinity = {
[DB.POSTGRES]: new Proxy(
Expand All @@ -24,6 +25,7 @@ export function fromPostgres(ast, diagramDb = DB.GENERIC) {
const relationships = [];
const types = [];
const enums = [];
const primaryKeys = new Map();

const parseSingleStatement = (e) => {
if (e.type === "create") {
Expand Down Expand Up @@ -134,14 +136,17 @@ export function fromPostgres(ast, diagramDb = DB.GENERIC) {
(c) => c.column.expr.value,
);
const endTableName = d.reference_definition.table[0].table;
const endFieldNames = d.reference_definition.definition.map(
(c) => c.column.expr.value,
);
const startFieldName = startFieldNames[0];

const endTable = tables.find((t) => t.name === endTableName);
if (!endTable) return;

const endFieldNames = postgresReferenceFieldNames(
d.reference_definition,
endTable,
startFieldNames.length,
primaryKeys.get(endTableName),
);
const startFieldName = startFieldNames[0];

const fieldPairs = [];
for (let i = 0; i < startFieldNames.length; i++) {
const sf = table.fields.find(
Expand Down Expand Up @@ -214,8 +219,16 @@ export function fromPostgres(ast, diagramDb = DB.GENERIC) {
const startTableName = table.name;
const startFieldName = field.name;
const endTableName = d.reference_definition.table[0].table;
const endFieldName =
d.reference_definition.definition[0].column.expr.value;
const endTable = tables.find((t) => t.name === endTableName);
if (!endTable) return;

const [endFieldName] = postgresReferenceFieldNames(
d.reference_definition,
endTable,
1,
primaryKeys.get(endTableName),
);
if (!endFieldName) return;
let updateConstraint = Constraint.NONE;
let deleteConstraint = Constraint.NONE;
d.reference_definition.on_action.forEach((c) => {
Expand All @@ -232,9 +245,6 @@ export function fromPostgres(ast, diagramDb = DB.GENERIC) {
}
});

const endTable = tables.find((t) => t.name === endTableName);
if (!endTable) return;

const endField = endTable.fields.find(
(f) => f.name === endFieldName,
);
Expand Down Expand Up @@ -263,6 +273,16 @@ export function fromPostgres(ast, diagramDb = DB.GENERIC) {
relationships.push(relationship);
}
});
const primaryKey = e.create_definitions.find(
(definition) => definition.constraint_type === "primary key",
);
primaryKeys.set(
table.name,
primaryKey?.definition.map((column) => column.column.expr.value) ??
table.fields
.filter((tableField) => tableField.primary)
.map((tableField) => tableField.name),
);
tables.push(table);
} else if (e.keyword === "index") {
const index = {
Expand Down
Loading
Loading