Skip to content

Database diagrams editor that allows you to visualize and design your DB with a single query.

fix: add defaults in import for all databases (#1014)

Jonathan Fishnercef9fb6e8376
diff --git a/src/lib/data/import-metadata/scripts/cockroachdb-script.ts b/src/lib/data/import-metadata/scripts/cockroachdb-script.ts
index 9e8050da..8d34d95d 100644
--- a/src/lib/data/import-metadata/scripts/cockroachdb-script.ts
+++ b/src/lib/data/import-metadata/scripts/cockroachdb-script.ts
@@ -24,9 +24,9 @@ WITH fk_info AS (
',"table":"', replace(table_name::TEXT, '"', ''), '"',
',"column":"', replace(fk_column::TEXT, '"', ''), '"',
',"foreign_key_name":"', foreign_key_name::TEXT, '"',
- ',"reference_schema":"', COALESCE(reference_schema::TEXT, 'public'), '"',
- ',"reference_table":"', reference_table::TEXT, '"',
- ',"reference_column":"', reference_column::TEXT, '"',
+ ',"reference_schema":"', COALESCE(replace(reference_schema::TEXT, '"', ''), 'public'), '"',
+ ',"reference_table":"', replace(reference_table::TEXT, '"', ''), '"',
+ ',"reference_column":"', replace(reference_column::TEXT, '"', ''), '"',
',"fk_def":"', replace(fk_def::TEXT, '"', ''),
'"}')), ',') as fk_metadata
FROM (
@@ -124,7 +124,7 @@ cols AS (
ELSE 'null'
END,
',"nullable":', CASE WHEN (cols.IS_NULLABLE = 'YES') THEN true ELSE false END::TEXT,
- ',"default":"', null,
+ ',"default":"', COALESCE(replace(replace(cols.column_default::TEXT, '"', '\\"'), '\\x', '\\\\x'), ''),
'","collation":"', COALESCE(cols.COLLATION_NAME::TEXT, ''),
'","comment":"', COALESCE(replace(replace(dsc.description::TEXT, '"', '\\"'), '\\x', '\\\\x'), ''),
'","is_identity":', CASE
diff --git a/src/lib/data/import-metadata/scripts/maria-script.ts b/src/lib/data/import-metadata/scripts/maria-script.ts
index 0cd4d15f..4e6932e1 100644
--- a/src/lib/data/import-metadata/scripts/maria-script.ts
+++ b/src/lib/data/import-metadata/scripts/maria-script.ts
@@ -1,5 +1,6 @@
-const withExtras = false;
-const withDefault = `IFNULL(REPLACE(REPLACE(cols.column_default, '\\\\', ''), '"', 'ֿֿֿ\\"'), '')`;
+const withDefault = true;
+
+const withDefaultExpr = `IFNULL(REPLACE(REPLACE(cols.column_default, '\\\\', ''), '"', 'ֿֿֿ\\"'), '')`;
const withoutDefault = `""`;
export const mariaDBQuery = `SET SESSION group_concat_max_len = 10000000;
@@ -68,7 +69,7 @@ SELECT CAST(CONCAT(
',"scale":', IFNULL(cols.numeric_scale, 'null'), '}'), 'null'),
',"ordinal_position":', cols.ordinal_position,
',"nullable":', IF(cols.is_nullable = 'YES', 'true', 'false'),
- ',"default":"', ${withExtras ? withDefault : withoutDefault},
+ ',"default":"', ${withDefault ? withDefaultExpr : withoutDefault},
'","collation":"', IFNULL(cols.collation_name, ''),
'","is_identity":', IF(cols.extra LIKE '%auto_increment%', 'true', 'false'),
',"comment":"', REPLACE(REPLACE(IFNULL(cols.column_comment, ''), '"', '\\"'), '\\n', ' '), '"}')
diff --git a/src/lib/data/import-metadata/scripts/mysql-script.ts b/src/lib/data/import-metadata/scripts/mysql-script.ts
index 7c7b3c47..f4763455 100644
--- a/src/lib/data/import-metadata/scripts/mysql-script.ts
+++ b/src/lib/data/import-metadata/scripts/mysql-script.ts
@@ -8,9 +8,9 @@ export const getMySQLQuery = (
const databaseEdition: DatabaseEdition | undefined =
options.databaseEdition;
- const withExtras = false;
+ const withDefault = true;
- const withDefault = `IFNULL(REPLACE(REPLACE(cols.column_default, '\\\\', ''), '"', 'ֿֿֿ\\"'), '')`;
+ const withDefaultExpr = `IFNULL(REPLACE(REPLACE(cols.column_default, '\\\\', ''), '"', 'ֿֿֿ\\"'), '')`;
const withoutDefault = `""`;
const newMySQLQuery = `WITH fk_info as (
@@ -91,7 +91,7 @@ export const getMySQLQuery = (
END,
',"ordinal_position":', cols.ordinal_position,
',"nullable":', IF(cols.is_nullable = 'YES', 'true', 'false'),
- ',"default":"', ${withExtras ? withDefault : withoutDefault},
+ ',"default":"', ${withDefault ? withDefaultExpr : withoutDefault},
'","collation":"', IFNULL(cols.collation_name, ''),
'","is_identity":', IF(cols.extra LIKE '%auto_increment%', 'true', 'false'),
',"comment":"', REPLACE(REPLACE(IFNULL(cols.column_comment, ''), '"', '\\\\"'), '\\n', ' '),
@@ -220,7 +220,7 @@ export const getMySQLQuery = (
',"scale":', IFNULL(cols.numeric_scale, 'null'), '}'), 'null'),
',"ordinal_position":', cols.ordinal_position,
',"nullable":', IF(cols.is_nullable = 'YES', 'true', 'false'),
- ',"default":"', ${withExtras ? withDefault : withoutDefault},
+ ',"default":"', ${withDefault ? withDefaultExpr : withoutDefault},
'","collation":"', IFNULL(cols.collation_name, ''),
'","comment":"', REPLACE(REPLACE(IFNULL(cols.column_comment, ''), '"', '\\"'), '\\n', ' '), '"}')
) FROM (
diff --git a/src/lib/data/import-metadata/scripts/postgres-script.ts b/src/lib/data/import-metadata/scripts/postgres-script.ts
index 37d6f967..36c8999e 100644
--- a/src/lib/data/import-metadata/scripts/postgres-script.ts
+++ b/src/lib/data/import-metadata/scripts/postgres-script.ts
@@ -64,8 +64,9 @@ export const getPostgresQuery = (
`;
const withExtras = false;
+ const withDefault = true;
- const withDefault = `COALESCE(replace(replace(cols.column_default, '"', '\\"'), '\\x', '\\\\x'), '')`;
+ const withDefaultExpr = `COALESCE(replace(replace(cols.column_default, '"', '\\"'), '\\x', '\\\\x'), '')`;
const withoutDefault = `null`;
const withComments = `COALESCE(replace(replace(dsc.description, '"', '\\"'), '\\x', '\\\\x'), '')`;
@@ -78,9 +79,9 @@ WITH fk_info${databaseEdition ? '_' + databaseEdition : ''} AS (
',"table":"', replace(table_name::text, '"', ''), '"',
',"column":"', replace(fk_column::text, '"', ''), '"',
',"foreign_key_name":"', foreign_key_name, '"',
- ',"reference_schema":"', COALESCE(reference_schema, 'public'), '"',
- ',"reference_table":"', reference_table, '"',
- ',"reference_column":"', reference_column, '"',
+ ',"reference_schema":"', COALESCE(replace(reference_schema, '"', ''), 'public'), '"',
+ ',"reference_table":"', replace(reference_table, '"', ''), '"',
+ ',"reference_column":"', replace(reference_column, '"', ''), '"',
',"fk_def":"', replace(fk_def, '"', ''),
'"}')), ',') as fk_metadata
FROM (
@@ -197,7 +198,7 @@ cols AS (
ELSE 'null'
END,
',"nullable":', CASE WHEN (cols.IS_NULLABLE = 'YES') THEN 'true' ELSE 'false' END,
- ',"default":"', ${withExtras ? withDefault : withoutDefault},
+ ',"default":"', ${withDefault ? withDefaultExpr : withoutDefault},
'","collation":"', COALESCE(cols.COLLATION_NAME, ''),
'","comment":"', ${withExtras ? withComments : withoutComments},
'","is_identity":', CASE
diff --git a/src/lib/data/import-metadata/scripts/sqlserver-script.ts b/src/lib/data/import-metadata/scripts/sqlserver-script.ts
index d29ac0c2..20c20176 100644
--- a/src/lib/data/import-metadata/scripts/sqlserver-script.ts
+++ b/src/lib/data/import-metadata/scripts/sqlserver-script.ts
@@ -1,8 +1,8 @@
import { DatabaseEdition } from '@/lib/domain/database-edition';
-const withExtras = false;
+const withDefault = true;
-const withDefault = `'"' + STRING_ESCAPE(COALESCE(REPLACE(CAST(cols.COLUMN_DEFAULT AS NVARCHAR(MAX)), '"', '\\"'), ''), 'json') + '"'`;
+const withDefaultExpr = `'"' + STRING_ESCAPE(COALESCE(REPLACE(CAST(cols.COLUMN_DEFAULT AS NVARCHAR(MAX)), '"', '\\"'), ''), 'json') + '"'`;
const withoutDefault = `'""'`;
const sqlServerQuery = `${`/* SQL Server 2017 and above edition (14.0, 15.0, 16.0, 17.0)*/`}
@@ -86,7 +86,7 @@ cols AS (
ELSE 'null'
END +
', "nullable": ' + CASE WHEN cols.IS_NULLABLE = 'YES' THEN 'true' ELSE 'false' END +
- ', "default": ' + ${withExtras ? withDefault : withoutDefault} +
+ ', "default": ' + ${withDefault ? withDefaultExpr : withoutDefault} +
', "collation": ' + CASE
WHEN cols.COLLATION_NAME IS NULL THEN 'null'
ELSE '"' + STRING_ESCAPE(cols.COLLATION_NAME, 'json') + '"'
@@ -284,7 +284,7 @@ cols AS (
ELSE 'null'
END +
', "nullable": ' + CASE WHEN cols.IS_NULLABLE = 'YES' THEN 'true' ELSE 'false' END +
- ', "default": ' + ${withExtras ? withDefault : withoutDefault} +
+ ', "default": ' + ${withDefault ? withDefaultExpr : withoutDefault} +
', "collation": ' +
CASE
WHEN cols.COLLATION_NAME IS NULL THEN 'null'