Skip to content

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

fix: add auto-increment field detection in smart-query import (#935)

Jonathan Fishner57b3b8777fd0
diff --git a/src/lib/data/import-metadata/import/fields.ts b/src/lib/data/import-metadata/import/fields.ts
index 01c901c8..53d9830d 100644
--- a/src/lib/data/import-metadata/import/fields.ts
+++ b/src/lib/data/import-metadata/import/fields.ts
@@ -57,6 +57,9 @@ export const createFieldsFromMetadata = ({
...(col.precision?.scale ? { scale: col.precision.scale } : {}),
...(col.default ? { default: col.default } : {}),
...(col.collation ? { collation: col.collation } : {}),
+ ...(col.is_identity !== undefined
+ ? { increment: col.is_identity }
+ : {}),
createdAt: Date.now(),
comments: col.comment ? col.comment : undefined,
})
diff --git a/src/lib/data/import-metadata/metadata-types/column-info.ts b/src/lib/data/import-metadata/metadata-types/column-info.ts
index 4e22a20f..45eefb8b 100644
--- a/src/lib/data/import-metadata/metadata-types/column-info.ts
+++ b/src/lib/data/import-metadata/metadata-types/column-info.ts
@@ -15,6 +15,7 @@ export interface ColumnInfo {
default?: string | null; // Default value for the column, nullable
collation?: string | null;
comment?: string | null;
+ is_identity?: boolean; // Indicates if the column is auto-increment/identity
}
export const ColumnInfoSchema: z.ZodType<ColumnInfo> = z.object({
@@ -35,4 +36,5 @@ export const ColumnInfoSchema: z.ZodType<ColumnInfo> = z.object({
default: z.string().nullable().optional(),
collation: z.string().nullable().optional(),
comment: z.string().nullable().optional(),
+ is_identity: z.boolean().optional(),
});
diff --git a/src/lib/data/import-metadata/scripts/cockroachdb-script.ts b/src/lib/data/import-metadata/scripts/cockroachdb-script.ts
index a220dc6b..9e8050da 100644
--- a/src/lib/data/import-metadata/scripts/cockroachdb-script.ts
+++ b/src/lib/data/import-metadata/scripts/cockroachdb-script.ts
@@ -127,7 +127,13 @@ cols AS (
',"default":"', null,
'","collation":"', COALESCE(cols.COLLATION_NAME::TEXT, ''),
'","comment":"', COALESCE(replace(replace(dsc.description::TEXT, '"', '\\"'), '\\x', '\\\\x'), ''),
- '"}')), ',') AS cols_metadata
+ '","is_identity":', CASE
+ WHEN cols.is_identity = 'YES' THEN 'true'
+ WHEN cols.column_default IS NOT NULL AND cols.column_default LIKE 'nextval(%' THEN 'true'
+ WHEN cols.column_default LIKE 'unique_rowid()%' THEN 'true'
+ ELSE 'false'
+ END,
+ '}')), ',') AS cols_metadata
FROM information_schema.columns cols
LEFT JOIN pg_catalog.pg_class c
ON c.relname = cols.table_name
diff --git a/src/lib/data/import-metadata/scripts/maria-script.ts b/src/lib/data/import-metadata/scripts/maria-script.ts
index 65067c97..8ad70966 100644
--- a/src/lib/data/import-metadata/scripts/maria-script.ts
+++ b/src/lib/data/import-metadata/scripts/maria-script.ts
@@ -69,7 +69,9 @@ SELECT CAST(CONCAT(
',"ordinal_position":', cols.ordinal_position,
',"nullable":', IF(cols.is_nullable = 'YES', 'true', 'false'),
',"default":"', ${withExtras ? withDefault : withoutDefault},
- '","collation":"', IFNULL(cols.collation_name, ''), '"}')
+ '","collation":"', IFNULL(cols.collation_name, ''),
+ '","is_identity":', IF(cols.extra LIKE '%auto_increment%', 'true', 'false'),
+ '"}')
) FROM (
SELECT cols.table_schema,
cols.table_name,
@@ -81,7 +83,8 @@ SELECT CAST(CONCAT(
cols.ordinal_position,
cols.is_nullable,
cols.column_default,
- cols.collation_name
+ cols.collation_name,
+ cols.extra
FROM information_schema.columns cols
WHERE cols.table_schema = DATABASE()
) AS cols), ''),
diff --git a/src/lib/data/import-metadata/scripts/mysql-script.ts b/src/lib/data/import-metadata/scripts/mysql-script.ts
index 1967f26f..9839c838 100644
--- a/src/lib/data/import-metadata/scripts/mysql-script.ts
+++ b/src/lib/data/import-metadata/scripts/mysql-script.ts
@@ -92,7 +92,9 @@ export const getMySQLQuery = (
',"ordinal_position":', cols.ordinal_position,
',"nullable":', IF(cols.is_nullable = 'YES', 'true', 'false'),
',"default":"', ${withExtras ? withDefault : withoutDefault},
- '","collation":"', IFNULL(cols.collation_name, ''), '"}'
+ '","collation":"', IFNULL(cols.collation_name, ''),
+ '","is_identity":', IF(cols.extra LIKE '%auto_increment%', 'true', 'false'),
+ '}'
)))))
), indexes as (
(SELECT (@indexes:=NULL),
diff --git a/src/lib/data/import-metadata/scripts/postgres-script.ts b/src/lib/data/import-metadata/scripts/postgres-script.ts
index 0b0924d0..25df1864 100644
--- a/src/lib/data/import-metadata/scripts/postgres-script.ts
+++ b/src/lib/data/import-metadata/scripts/postgres-script.ts
@@ -194,7 +194,12 @@ cols AS (
',"default":"', ${withExtras ? withDefault : withoutDefault},
'","collation":"', COALESCE(cols.COLLATION_NAME, ''),
'","comment":"', ${withExtras ? withComments : withoutComments},
- '"}')), ',') AS cols_metadata
+ '","is_identity":', CASE
+ WHEN cols.is_identity = 'YES' THEN 'true'
+ WHEN cols.column_default IS NOT NULL AND cols.column_default LIKE 'nextval(%' THEN 'true'
+ ELSE 'false'
+ END,
+ '}')), ',') AS cols_metadata
FROM information_schema.columns cols
LEFT JOIN pg_catalog.pg_class c
ON c.relname = cols.table_name
diff --git a/src/lib/data/import-metadata/scripts/sqlite-script.ts b/src/lib/data/import-metadata/scripts/sqlite-script.ts
index 3ece3a73..c1be6cfb 100644
--- a/src/lib/data/import-metadata/scripts/sqlite-script.ts
+++ b/src/lib/data/import-metadata/scripts/sqlite-script.ts
@@ -119,7 +119,13 @@ WITH fk_info AS (
END
ELSE null
END,
- 'default', ${withExtras ? withDefault : withoutDefault}
+ 'default', ${withExtras ? withDefault : withoutDefault},
+ 'is_identity',
+ CASE
+ WHEN p.pk = 1 AND LOWER(p.type) LIKE '%int%' THEN json('true')
+ WHEN LOWER((SELECT sql FROM sqlite_master WHERE name = m.name)) LIKE '%' || p.name || '%autoincrement%' THEN json('true')
+ ELSE json('false')
+ END
)
) AS cols_metadata
FROM
@@ -292,7 +298,13 @@ WITH fk_info AS (
END
ELSE null
END,
- 'default', ${withExtras ? withDefault : withoutDefault}
+ 'default', ${withExtras ? withDefault : withoutDefault},
+ 'is_identity',
+ CASE
+ WHEN p.pk = 1 AND LOWER(p.type) LIKE '%int%' THEN json('true')
+ WHEN LOWER((SELECT sql FROM sqlite_master WHERE name = m.name)) LIKE '%' || p.name || '%autoincrement%' THEN json('true')
+ ELSE json('false')
+ END
)
) AS cols_metadata
FROM
diff --git a/src/lib/data/import-metadata/scripts/sqlserver-script.ts b/src/lib/data/import-metadata/scripts/sqlserver-script.ts
index b6a6ee04..d29ac0c2 100644
--- a/src/lib/data/import-metadata/scripts/sqlserver-script.ts
+++ b/src/lib/data/import-metadata/scripts/sqlserver-script.ts
@@ -91,6 +91,11 @@ cols AS (
WHEN cols.COLLATION_NAME IS NULL THEN 'null'
ELSE '"' + STRING_ESCAPE(cols.COLLATION_NAME, 'json') + '"'
END +
+ ', "is_identity": ' + CASE
+ WHEN COLUMNPROPERTY(OBJECT_ID(cols.TABLE_SCHEMA + '.' + cols.TABLE_NAME), cols.COLUMN_NAME, 'IsIdentity') = 1
+ THEN 'true'
+ ELSE 'false'
+ END +
N'}') COLLATE DATABASE_DEFAULT
), N','
) +