( db: DB, inputSchema: string, tablesFilter: (table: string) => boolean = (table) => true, progressCallback?: ( stage: IntrospectStage, count: number, status: IntrospectStatus, ) => void, )
| 562 | } |
| 563 | |
| 564 | export const fromDatabase = async ( |
| 565 | db: DB, |
| 566 | inputSchema: string, |
| 567 | tablesFilter: (table: string) => boolean = (table) => true, |
| 568 | progressCallback?: ( |
| 569 | stage: IntrospectStage, |
| 570 | count: number, |
| 571 | status: IntrospectStatus, |
| 572 | ) => void, |
| 573 | ): Promise<MySqlSchemaInternal> => { |
| 574 | const result: Record<string, Table> = {}; |
| 575 | const internals: MySqlKitInternals = { tables: {}, indexes: {} }; |
| 576 | |
| 577 | const columns = await db.query(`select * from information_schema.columns |
| 578 | where table_schema = '${inputSchema}' and table_name != '__drizzle_migrations' |
| 579 | order by table_name, ordinal_position;`); |
| 580 | |
| 581 | const response = columns as RowDataPacket[]; |
| 582 | |
| 583 | const schemas: string[] = []; |
| 584 | |
| 585 | let columnsCount = 0; |
| 586 | let tablesCount = new Set(); |
| 587 | let indexesCount = 0; |
| 588 | let foreignKeysCount = 0; |
| 589 | let checksCount = 0; |
| 590 | let viewsCount = 0; |
| 591 | |
| 592 | const idxs = await db.query( |
| 593 | `select * from INFORMATION_SCHEMA.STATISTICS |
| 594 | WHERE INFORMATION_SCHEMA.STATISTICS.TABLE_SCHEMA = '${inputSchema}' and INFORMATION_SCHEMA.STATISTICS.INDEX_NAME != 'PRIMARY';`, |
| 595 | ); |
| 596 | |
| 597 | const idxRows = idxs as RowDataPacket[]; |
| 598 | |
| 599 | for (const column of response) { |
| 600 | if (!tablesFilter(column['TABLE_NAME'] as string)) continue; |
| 601 | |
| 602 | columnsCount += 1; |
| 603 | if (progressCallback) { |
| 604 | progressCallback('columns', columnsCount, 'fetching'); |
| 605 | } |
| 606 | const schema: string = column['TABLE_SCHEMA']; |
| 607 | const tableName = column['TABLE_NAME']; |
| 608 | |
| 609 | tablesCount.add(`${schema}.${tableName}`); |
| 610 | if (progressCallback) { |
| 611 | progressCallback('columns', tablesCount.size, 'fetching'); |
| 612 | } |
| 613 | const columnName: string = column['COLUMN_NAME']; |
| 614 | const isNullable = column['IS_NULLABLE'] === 'YES'; // 'YES', 'NO' |
| 615 | const dataType = column['DATA_TYPE']; // varchar |
| 616 | const columnType = column['COLUMN_TYPE']; // varchar(256) |
| 617 | const isPrimary = column['COLUMN_KEY'] === 'PRI'; // 'PRI', '' |
| 618 | const columnDefault: string = column['COLUMN_DEFAULT']; |
| 619 | const collation: string = column['CHARACTER_SET_NAME']; |
| 620 | const geenratedExpression: string = column['GENERATION_EXPRESSION']; |
| 621 |
no test coverage detected