Skip to content
This repository was archived by the owner on Mar 1, 2026. It is now read-only.

Commit 51f4707

Browse files
committed
fix(cli): preserve column order during MySQL pull
Ensures database columns are sorted by their ordinal position during the introspection process. This maintains the original schema structure and provides a consistent output that matches the physical database layout.
1 parent 8e478d1 commit 51f4707

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

  • packages/cli/src/actions/pull/provider

packages/cli/src/actions/pull/provider/mysql.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,18 @@ export const mysql: IntrospectionProvider = {
135135
const columns = typeof row.columns === 'string' ? JSON.parse(row.columns) : row.columns;
136136
const indexes = typeof row.indexes === 'string' ? JSON.parse(row.indexes) : row.indexes;
137137

138+
// Sort columns by ordinal_position to preserve database column order
139+
const sortedColumns = (columns || []).sort(
140+
(a: { ordinal_position?: number }, b: { ordinal_position?: number }) =>
141+
(a.ordinal_position ?? 0) - (b.ordinal_position ?? 0)
142+
);
143+
138144
tables.push({
139145
schema: row.schema || '',
140146
name: row.name,
141147
type: row.type as 'table' | 'view',
142148
definition: row.definition,
143-
columns: columns || [],
149+
columns: sortedColumns,
144150
indexes: indexes || [],
145151
});
146152
}
@@ -283,6 +289,7 @@ SELECT
283289
SELECT JSON_ARRAYAGG(col_json)
284290
FROM (
285291
SELECT JSON_OBJECT(
292+
'ordinal_position', c.ORDINAL_POSITION,
286293
'name', c.COLUMN_NAME,
287294
'datatype', c.DATA_TYPE,
288295
'datatype_schema', c.TABLE_SCHEMA,

0 commit comments

Comments
 (0)