mirror of
https://github.com/drawdb-io/drawdb.git
synced 2025-07-18 10:11:24 +00:00
Fix postgres import when alter expr isnt iterable (#495)
* Fix postgres import when alter expr isnt interable * Remove hardcoded constants * Fix type export and import
This commit is contained in:
@@ -85,7 +85,7 @@ export function getTypeString(
|
||||
if (field.type === "DATETIME") {
|
||||
return `timestamp`;
|
||||
}
|
||||
if (dbToTypes[currentDb][field.type].isSized) {
|
||||
if (dbToTypes[currentDb][field.type].isSized && field.size) {
|
||||
const type =
|
||||
field.type === "BINARY"
|
||||
? "bit"
|
||||
@@ -94,7 +94,11 @@ export function getTypeString(
|
||||
: field.type.toLowerCase();
|
||||
return `${type}(${field.size})`;
|
||||
}
|
||||
if (dbToTypes[currentDb][field.type].hasPrecision && field.size !== "") {
|
||||
if (
|
||||
dbToTypes[currentDb][field.type].hasPrecision &&
|
||||
field.size &&
|
||||
field.size.trim() !== ""
|
||||
) {
|
||||
return `${field.type.toLowerCase()}${field.size ? `(${field.size})` : ""}`;
|
||||
}
|
||||
return field.type.toLowerCase();
|
||||
|
@@ -1,5 +1,5 @@
|
||||
import { nanoid } from "nanoid";
|
||||
import { Cardinality, DB } from "../../data/constants";
|
||||
import { Cardinality, Constraint, DB } from "../../data/constants";
|
||||
import { dbToTypes } from "../../data/datatypes";
|
||||
import { buildSQLFromAST } from "./shared";
|
||||
|
||||
@@ -13,6 +13,7 @@ const affinity = {
|
||||
INTEGER: "INT",
|
||||
MEDIUMINT: "INTEGER",
|
||||
BIT: "BOOLEAN",
|
||||
"CHATACTER VARYING": "VARCHAR",
|
||||
},
|
||||
{ get: (target, prop) => (prop in target ? target[prop] : "BLOB") },
|
||||
),
|
||||
@@ -50,8 +51,11 @@ export function fromPostgres(ast, diagramDb = DB.GENERIC) {
|
||||
d.definition.dataType,
|
||||
),
|
||||
)?.name;
|
||||
if (!type && !dbToTypes[diagramDb][d.definition.dataType])
|
||||
type = affinity[diagramDb][d.definition.dataType.toUpperCase()];
|
||||
|
||||
type ??=
|
||||
dbToTypes[diagramDb][d.definition.dataType.toUpperCase()].type;
|
||||
type ??= affinity[diagramDb][d.definition.dataType.toUpperCase()];
|
||||
|
||||
field.type = type;
|
||||
|
||||
if (d.definition.expr && d.definition.expr.type === "expr_list") {
|
||||
@@ -144,8 +148,8 @@ export function fromPostgres(ast, diagramDb = DB.GENERIC) {
|
||||
relationship.endTableId = endTable.id;
|
||||
relationship.endFieldId = endField.id;
|
||||
relationship.startFieldId = startField.id;
|
||||
let updateConstraint = "No action";
|
||||
let deleteConstraint = "No action";
|
||||
let updateConstraint = Constraint.NONE;
|
||||
let deleteConstraint = Constraint.NONE;
|
||||
d.reference_definition.on_action.forEach((c) => {
|
||||
if (c.type === "on update") {
|
||||
updateConstraint = c.value.value;
|
||||
@@ -178,8 +182,8 @@ export function fromPostgres(ast, diagramDb = DB.GENERIC) {
|
||||
const endTableName = d.reference_definition.table[0].table;
|
||||
const endFieldName =
|
||||
d.reference_definition.definition[0].column.expr.value;
|
||||
let updateConstraint = "No action";
|
||||
let deleteConstraint = "No action";
|
||||
let updateConstraint = Constraint.NONE;
|
||||
let deleteConstraint = Constraint.NONE;
|
||||
d.reference_definition.on_action.forEach((c) => {
|
||||
if (c.type === "on update") {
|
||||
updateConstraint = c.value.value;
|
||||
@@ -281,6 +285,7 @@ export function fromPostgres(ast, diagramDb = DB.GENERIC) {
|
||||
}
|
||||
}
|
||||
} else if (e.type === "alter") {
|
||||
if (Array.isArray(e.expr)) {
|
||||
e.expr.forEach((expr) => {
|
||||
if (
|
||||
expr.action === "add" &&
|
||||
@@ -296,8 +301,8 @@ export function fromPostgres(ast, diagramDb = DB.GENERIC) {
|
||||
const endFieldName =
|
||||
expr.create_definitions.reference_definition.definition[0].column
|
||||
.expr.value;
|
||||
let updateConstraint = "No action";
|
||||
let deleteConstraint = "No action";
|
||||
let updateConstraint = Constraint.NONE;
|
||||
let deleteConstraint = Constraint.NONE;
|
||||
expr.create_definitions.reference_definition.on_action.forEach(
|
||||
(c) => {
|
||||
if (c.type === "on update") {
|
||||
@@ -320,7 +325,9 @@ 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);
|
||||
const endField = endTable.fields.find(
|
||||
(f) => f.name === endFieldName,
|
||||
);
|
||||
if (!endField) return;
|
||||
|
||||
const startField = startTable.fields.find(
|
||||
@@ -349,6 +356,7 @@ export function fromPostgres(ast, diagramDb = DB.GENERIC) {
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (Array.isArray(ast)) {
|
||||
|
Reference in New Issue
Block a user