Skip to content

Commit 8918c39

Browse files
feat(schema): adds default and SERIAL for postgres
1 parent f84a58e commit 8918c39

3 files changed

Lines changed: 26 additions & 6 deletions

File tree

apps/desktop/src/entities/connection/components/table/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface Column {
1111
scale?: number | null
1212
unique?: string
1313
primaryKey?: string
14+
defaultValue?: string | null
1415
foreign?: {
1516
name: string
1617
schema: string

apps/desktop/src/entities/connection/generators/formats/sql.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,11 @@ function buildColumnParts(
115115
typeDef: string,
116116
dialect: ConnectionType,
117117
pkColumns: string[],
118+
defaultValue?: string | null,
118119
): { parts: string[], foreignKey: string | null } {
119120
const quoted = quoteIdentifier(c.id, dialect)
120121
const parts = [quoted, typeDef]
121122

122-
if (!c.isNullable) {
123-
parts.push('NOT NULL')
124-
}
125-
126123
if (c.primaryKey) {
127124
pkColumns.push(quoted)
128125
if (dialect === ConnectionType.MySQL && /int|serial/i.test(c.type ?? '')) {
@@ -133,6 +130,14 @@ function buildColumnParts(
133130
}
134131
}
135132

133+
if (defaultValue !== undefined && defaultValue !== null) {
134+
parts.push(`DEFAULT ${defaultValue}`)
135+
}
136+
137+
if (!c.isNullable) {
138+
parts.push('NOT NULL')
139+
}
140+
136141
if (c.primaryKey && dialect !== ConnectionType.ClickHouse) {
137142
parts.push('PRIMARY KEY')
138143
}
@@ -212,8 +217,21 @@ export function generateSchemaSQL({
212217
const columnLines: string[] = []
213218

214219
for (const c of columns) {
215-
const typeDef = getTypeDef(c, table, enums, dialect, usedEnums)
216-
const { parts, foreignKey } = buildColumnParts(c, typeDef, dialect, pkColumns)
220+
let typeDef = getTypeDef(c, table, enums, dialect, usedEnums)
221+
let defaultValue = c.defaultValue
222+
223+
if (dialect === ConnectionType.Postgres && defaultValue?.toLowerCase().startsWith('nextval')) {
224+
if (typeDef === 'INTEGER') {
225+
typeDef = 'SERIAL'
226+
defaultValue = null
227+
}
228+
else if (typeDef === 'BIGINT') {
229+
typeDef = 'BIGSERIAL'
230+
defaultValue = null
231+
}
232+
}
233+
234+
const { parts, foreignKey } = buildColumnParts(c, typeDef, dialect, pkColumns, defaultValue)
217235
columnLines.push(` ${parts.join(' ')}`)
218236
if (foreignKey)
219237
foreignKeys.push(` ${foreignKey}`)

apps/desktop/src/routes/_protected/database/$id/table/-queries/use-columns-query.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export function useTableColumns({ connection, table, schema }: { connection: typ
2121
return {
2222
...column,
2323
primaryKey: primaryConstraint?.name,
24+
defaultValue: column.default,
2425
unique: uniqueConstraint?.name,
2526
foreign: foreignConstraint && foreignConstraint.foreignSchema && foreignConstraint.foreignTable && foreignConstraint.foreignColumn
2627
? {

0 commit comments

Comments
 (0)