Skip to content

Commit

Permalink
feat: keep column order
Browse files Browse the repository at this point in the history
  • Loading branch information
wirekang committed Dec 29, 2022
1 parent b2951f1 commit aaf9ea2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/dialect/mysql/mysql-introspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class MysqlIntrospector implements DatabaseIntrospector {
])
.where('table_schema', '=', sql`database()`)
.orderBy('table_name')
.orderBy('column_name')
.orderBy('ordinal_position')
.castTo<RawColumnMetadata>()

if (!options.withInternalKyselyTables) {
Expand Down
2 changes: 1 addition & 1 deletion src/dialect/postgres/postgres-introspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class PostgresIntrospector implements DatabaseIntrospector {
.where('a.attisdropped', '!=', true)
.orderBy('ns.nspname')
.orderBy('c.relname')
.orderBy('a.attname')
.orderBy('a.attnum')
.castTo<RawColumnMetadata>()

if (!options.withInternalKyselyTables) {
Expand Down
2 changes: 1 addition & 1 deletion src/dialect/sqlite/sqlite-introspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class SqliteIntrospector implements DatabaseIntrospector {
}>`pragma_table_info(${table})`.as('table_info')
)
.select(['name', 'type', 'notnull', 'dflt_value'])
.orderBy('name')
.orderBy('cid')
.execute()

return {
Expand Down
74 changes: 38 additions & 36 deletions test/node/src/introspect.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { sql } from '../../../'

import {
BUILT_IN_DIALECTS,
clearDatabase,
Expand Down Expand Up @@ -48,6 +47,14 @@ for (const dialect of BUILT_IN_DIALECTS) {
name: 'person',
schema: 'public',
columns: [
{
name: 'id',
dataType: 'int4',
dataTypeSchema: 'pg_catalog',
isNullable: false,
isAutoIncrementing: true,
hasDefaultValue: true,
},
{
name: 'first_name',
dataType: 'varchar',
Expand All @@ -57,21 +64,14 @@ for (const dialect of BUILT_IN_DIALECTS) {
hasDefaultValue: false,
},
{
name: 'gender',
name: 'middle_name',
dataType: 'varchar',
dataTypeSchema: 'pg_catalog',
isNullable: false,
isNullable: true,
isAutoIncrementing: false,
hasDefaultValue: false,
},
{
name: 'id',
dataType: 'int4',
dataTypeSchema: 'pg_catalog',
isNullable: false,
isAutoIncrementing: true,
hasDefaultValue: true,
},

{
name: 'last_name',
dataType: 'varchar',
Expand All @@ -81,10 +81,10 @@ for (const dialect of BUILT_IN_DIALECTS) {
hasDefaultValue: false,
},
{
name: 'middle_name',
name: 'gender',
dataType: 'varchar',
dataTypeSchema: 'pg_catalog',
isNullable: true,
isNullable: false,
isAutoIncrementing: false,
hasDefaultValue: false,
},
Expand Down Expand Up @@ -195,6 +195,13 @@ for (const dialect of BUILT_IN_DIALECTS) {
name: 'person',
schema: 'kysely_test',
columns: [
{
name: 'id',
dataType: 'int',
isNullable: false,
isAutoIncrementing: true,
hasDefaultValue: false,
},
{
name: 'first_name',
dataType: 'varchar',
Expand All @@ -203,30 +210,24 @@ for (const dialect of BUILT_IN_DIALECTS) {
hasDefaultValue: false,
},
{
name: 'gender',
name: 'middle_name',
dataType: 'varchar',
isNullable: false,
isNullable: true,
isAutoIncrementing: false,
hasDefaultValue: false,
},
{
name: 'id',
dataType: 'int',
isNullable: false,
isAutoIncrementing: true,
hasDefaultValue: false,
},
{
name: 'last_name',
dataType: 'varchar',
isNullable: true,
isAutoIncrementing: false,
hasDefaultValue: false,
},

{
name: 'middle_name',
name: 'gender',
dataType: 'varchar',
isNullable: true,
isNullable: false,
isAutoIncrementing: false,
hasDefaultValue: false,
},
Expand Down Expand Up @@ -307,24 +308,24 @@ for (const dialect of BUILT_IN_DIALECTS) {
name: 'person',
columns: [
{
name: 'first_name',
dataType: 'varchar(255)',
name: 'id',
dataType: 'INTEGER',
isNullable: true,
isAutoIncrementing: false,
isAutoIncrementing: true,
hasDefaultValue: false,
},
{
name: 'gender',
dataType: 'varchar(50)',
isNullable: false,
name: 'first_name',
dataType: 'varchar(255)',
isNullable: true,
isAutoIncrementing: false,
hasDefaultValue: false,
},
{
name: 'id',
dataType: 'INTEGER',
name: 'middle_name',
dataType: 'varchar(255)',
isNullable: true,
isAutoIncrementing: true,
isAutoIncrementing: false,
hasDefaultValue: false,
},
{
Expand All @@ -334,10 +335,11 @@ for (const dialect of BUILT_IN_DIALECTS) {
isAutoIncrementing: false,
hasDefaultValue: false,
},

{
name: 'middle_name',
dataType: 'varchar(255)',
isNullable: true,
name: 'gender',
dataType: 'varchar(50)',
isNullable: false,
isAutoIncrementing: false,
hasDefaultValue: false,
},
Expand Down

0 comments on commit aaf9ea2

Please sign in to comment.