Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: kysely-org/kysely
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 6e1c1f06af48ec2c455bd86e73acb660e96cfcba
Choose a base ref
..
head repository: kysely-org/kysely
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: dff749cde4f087a91c02d3ce00cf3b746b6538ca
Choose a head ref
Showing with 19 additions and 21 deletions.
  1. +13 −20 src/dialect/postgres/postgres-introspector.ts
  2. +6 −1 test/node/src/introspect.test.ts
33 changes: 13 additions & 20 deletions src/dialect/postgres/postgres-introspector.ts
Original file line number Diff line number Diff line change
@@ -60,25 +60,17 @@ export class PostgresIntrospector implements DatabaseIntrospector {
sql<string | null>`col_description(a.attrelid, a.attnum)`.as(
'column_description',
),
// Detect if the column is auto incrementing by finding the sequence
// that is created for `serial` and `bigserial` columns.
this.#db
.selectFrom('pg_class')
.select(sql`true`.as('auto_incrementing'))
// Make sure the sequence is in the same schema as the table.
.whereRef('relnamespace', '=', 'c.relnamespace')
.where('relkind', '=', 'S')
.where('relname', '=', sql`c.relname || '_' || a.attname || '_seq'`)
.as('auto_incrementing'),
sql<
string | null
>`pg_get_serial_sequence(ns.nspname || '.' || c.relname, a.attname)`.as(
'auto_incrementing',
),
])
.where('c.relkind', 'in', [
'r' /*regular table*/,
'v' /*view*/,
'p' /*partitioned table*/,
])
// r == normal table
.where((eb) =>
eb.or([
eb('c.relkind', '=', 'r'),
eb('c.relkind', '=', 'v'),
eb('c.relkind', '=', 'p'),
]),
)
.where('ns.nspname', '!~', '^pg_')
.where('ns.nspname', '!=', 'information_schema')
// No system columns
@@ -96,6 +88,7 @@ export class PostgresIntrospector implements DatabaseIntrospector {
}

const rawColumns = await query.execute()

return this.#parseTableMetadata(rawColumns)
}

@@ -130,7 +123,7 @@ export class PostgresIntrospector implements DatabaseIntrospector {
dataType: it.type,
dataTypeSchema: it.type_schema,
isNullable: !it.not_null,
isAutoIncrementing: !!it.auto_incrementing,
isAutoIncrementing: it.auto_incrementing !== null,
hasDefaultValue: it.has_default,
comment: it.column_description ?? undefined,
}),
@@ -154,6 +147,6 @@ interface RawColumnMetadata {
has_default: boolean
type: string
type_schema: string
auto_incrementing: boolean | null
auto_incrementing: string | null
column_description: string | null
}
7 changes: 6 additions & 1 deletion test/node/src/introspect.test.ts
Original file line number Diff line number Diff line change
@@ -262,7 +262,7 @@ for (const dialect of DIALECTS) {
schema: 'some_schema',
columns: [
{
name: 'some_column',
name: 'some_column_renamed',
dataType: 'int4',
dataTypeSchema: 'pg_catalog',
isNullable: false,
@@ -883,6 +883,11 @@ for (const dialect of DIALECTS) {
.addColumn('some_column', 'serial', (col) => col.primaryKey())
.addColumn('spcies', sql`dtype_schema.species`)
.execute()
// check that a renamed column with sequence is still detected as autoincrement
await ctx.db.schema
.alterTable('some_schema.pet')
.renameColumn('some_column', 'some_column_renamed')
.execute()
await ctx.db.schema
.createTable('some_schema.pet_partition')
.addColumn('part_column', 'text')