From b00948015a3b77daefae0cad5fba79b1933cad81 Mon Sep 17 00:00:00 2001 From: wirekang Date: Thu, 29 Dec 2022 19:39:11 +0900 Subject: [PATCH] feat: keep column order (#262) related #146 --- src/dialect/mysql/mysql-introspector.ts | 2 +- src/dialect/postgres/postgres-introspector.ts | 2 +- src/dialect/sqlite/sqlite-introspector.ts | 2 +- test/node/src/introspect.test.ts | 74 ++++++++++--------- 4 files changed, 41 insertions(+), 39 deletions(-) diff --git a/src/dialect/mysql/mysql-introspector.ts b/src/dialect/mysql/mysql-introspector.ts index cd9c7fb35..572ccaa88 100644 --- a/src/dialect/mysql/mysql-introspector.ts +++ b/src/dialect/mysql/mysql-introspector.ts @@ -46,7 +46,7 @@ export class MysqlIntrospector implements DatabaseIntrospector { ]) .where('table_schema', '=', sql`database()`) .orderBy('table_name') - .orderBy('column_name') + .orderBy('ordinal_position') .castTo() if (!options.withInternalKyselyTables) { diff --git a/src/dialect/postgres/postgres-introspector.ts b/src/dialect/postgres/postgres-introspector.ts index 677f92594..522979e48 100644 --- a/src/dialect/postgres/postgres-introspector.ts +++ b/src/dialect/postgres/postgres-introspector.ts @@ -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() if (!options.withInternalKyselyTables) { diff --git a/src/dialect/sqlite/sqlite-introspector.ts b/src/dialect/sqlite/sqlite-introspector.ts index f8a589a44..a21cead6d 100644 --- a/src/dialect/sqlite/sqlite-introspector.ts +++ b/src/dialect/sqlite/sqlite-introspector.ts @@ -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 { diff --git a/test/node/src/introspect.test.ts b/test/node/src/introspect.test.ts index f8f7453d1..56137c176 100644 --- a/test/node/src/introspect.test.ts +++ b/test/node/src/introspect.test.ts @@ -1,5 +1,4 @@ import { sql } from '../../../' - import { BUILT_IN_DIALECTS, clearDatabase, @@ -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', @@ -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', @@ -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, }, @@ -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', @@ -203,19 +210,12 @@ 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', @@ -223,10 +223,11 @@ for (const dialect of BUILT_IN_DIALECTS) { isAutoIncrementing: false, hasDefaultValue: false, }, + { - name: 'middle_name', + name: 'gender', dataType: 'varchar', - isNullable: true, + isNullable: false, isAutoIncrementing: false, hasDefaultValue: false, }, @@ -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, }, { @@ -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, },