|
| 1 | +// Package mysql provides functionality for extracting database schema information |
| 2 | +// from MySQL databases. |
| 3 | +package mysql |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "database/sql" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + |
| 11 | + "github.com/denchenko/dberd" |
| 12 | + _ "github.com/go-sql-driver/mysql" // import mysql driver |
| 13 | +) |
| 14 | + |
| 15 | +// Ensure Source implements dberd interfaces. |
| 16 | +var ( |
| 17 | + _ dberd.Source = (*Source)(nil) |
| 18 | +) |
| 19 | + |
| 20 | +// Source represents a MySQL database source for schema extraction. |
| 21 | +type Source struct { |
| 22 | + db *sql.DB |
| 23 | + closer io.Closer |
| 24 | +} |
| 25 | + |
| 26 | +// NewSource creates a new MySQL source from a connection string. |
| 27 | +func NewSource(connStr string) (*Source, error) { |
| 28 | + db, err := sql.Open("mysql", connStr) |
| 29 | + if err != nil { |
| 30 | + return nil, fmt.Errorf("opening mysql connection: %w", err) |
| 31 | + } |
| 32 | + |
| 33 | + return &Source{ |
| 34 | + db: db, |
| 35 | + closer: db, |
| 36 | + }, nil |
| 37 | +} |
| 38 | + |
| 39 | +// NewSourceFromDB creates a new MySQL source from an existing database connection. |
| 40 | +// This is useful when you want to reuse an existing database connection |
| 41 | +// for schema extraction purposes. |
| 42 | +func NewSourceFromDB(db *sql.DB) *Source { |
| 43 | + return &Source{ |
| 44 | + db: db, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// Close closes the database connection if it was created by NewSource. |
| 49 | +// If the connection was provided externally (via NewSourceFromDB), this is a no-op. |
| 50 | +func (s *Source) Close() error { |
| 51 | + if s.closer == nil { |
| 52 | + return nil |
| 53 | + } |
| 54 | + |
| 55 | + return s.closer.Close() |
| 56 | +} |
| 57 | + |
| 58 | +// ExtractSchema extracts the complete database schema including tables and their references. |
| 59 | +func (s *Source) ExtractSchema(ctx context.Context) (schema dberd.Schema, err error) { |
| 60 | + schema.Tables, err = s.extractTables(ctx) |
| 61 | + if err != nil { |
| 62 | + return dberd.Schema{}, fmt.Errorf("extracting tables: %w", err) |
| 63 | + } |
| 64 | + |
| 65 | + schema.References, err = s.extractReferences(ctx) |
| 66 | + if err != nil { |
| 67 | + return dberd.Schema{}, fmt.Errorf("extracting references: %w", err) |
| 68 | + } |
| 69 | + |
| 70 | + return schema, nil |
| 71 | +} |
| 72 | + |
| 73 | +const extractTablesQuery = ` |
| 74 | + SELECT |
| 75 | + TABLE_SCHEMA, |
| 76 | + TABLE_NAME, |
| 77 | + COLUMN_NAME, |
| 78 | + COLUMN_TYPE, |
| 79 | + IS_NULLABLE, |
| 80 | + COLUMN_DEFAULT, |
| 81 | + COLUMN_COMMENT, |
| 82 | + COLUMN_KEY = 'PRI' as is_primary |
| 83 | + FROM information_schema.COLUMNS |
| 84 | + WHERE TABLE_SCHEMA NOT IN ('information_schema', 'performance_schema', 'mysql', 'sys') |
| 85 | + ORDER BY TABLE_SCHEMA, TABLE_NAME, ORDINAL_POSITION;` |
| 86 | + |
| 87 | +type tableRow struct { |
| 88 | + tableSchema string |
| 89 | + tableName string |
| 90 | + columnName string |
| 91 | + columnType string |
| 92 | + isNullable string |
| 93 | + columnDefault *string |
| 94 | + columnComment string |
| 95 | + isPrimary bool |
| 96 | +} |
| 97 | + |
| 98 | +// extractTables queries the database for table and column information and converts it to dberd.Table format. |
| 99 | +func (s *Source) extractTables(ctx context.Context) ([]dberd.Table, error) { |
| 100 | + rows, err := s.db.QueryContext(ctx, extractTablesQuery) |
| 101 | + if err != nil { |
| 102 | + return nil, fmt.Errorf("querying tables: %w", err) |
| 103 | + } |
| 104 | + defer rows.Close() |
| 105 | + |
| 106 | + tablesRows := make([]tableRow, 0, 100) // Assuming tables rows. |
| 107 | + |
| 108 | + for rows.Next() { |
| 109 | + var r tableRow |
| 110 | + if err := rows.Scan( |
| 111 | + &r.tableSchema, |
| 112 | + &r.tableName, |
| 113 | + &r.columnName, |
| 114 | + &r.columnType, |
| 115 | + &r.isNullable, |
| 116 | + &r.columnDefault, |
| 117 | + &r.columnComment, |
| 118 | + &r.isPrimary, |
| 119 | + ); err != nil { |
| 120 | + return nil, fmt.Errorf("scanning tables row: %w", err) |
| 121 | + } |
| 122 | + |
| 123 | + tablesRows = append(tablesRows, r) |
| 124 | + } |
| 125 | + |
| 126 | + if err := rows.Err(); err != nil { |
| 127 | + return nil, fmt.Errorf("tables rows error: %w", err) |
| 128 | + } |
| 129 | + |
| 130 | + return tableRowsToSchemaTables(tablesRows), nil |
| 131 | +} |
| 132 | + |
| 133 | +// tableRowsToSchemaTables converts a slice of tableRow into a slice of dberd.Table. |
| 134 | +func tableRowsToSchemaTables(tableRows []tableRow) []dberd.Table { |
| 135 | + // Pre-allocate map with estimated size |
| 136 | + tableMap := make(map[string]*dberd.Table, len(tableRows)/10) // Assuming average 10 columns per table |
| 137 | + |
| 138 | + for _, row := range tableRows { |
| 139 | + tableKey := row.tableSchema + "." + row.tableName |
| 140 | + |
| 141 | + table, exists := tableMap[tableKey] |
| 142 | + if !exists { |
| 143 | + table = &dberd.Table{ |
| 144 | + Name: tableKey, |
| 145 | + Columns: make([]dberd.Column, 0, 10), |
| 146 | + } |
| 147 | + tableMap[tableKey] = table |
| 148 | + } |
| 149 | + |
| 150 | + definition := row.columnType |
| 151 | + if row.isNullable == "NO" { |
| 152 | + definition += " NOT NULL" |
| 153 | + } |
| 154 | + if row.columnDefault != nil && *row.columnDefault != "" { |
| 155 | + definition += " DEFAULT " + *row.columnDefault |
| 156 | + } |
| 157 | + |
| 158 | + column := dberd.Column{ |
| 159 | + Name: row.columnName, |
| 160 | + Definition: definition, |
| 161 | + IsPrimary: row.isPrimary, |
| 162 | + } |
| 163 | + |
| 164 | + if row.columnComment != "" { |
| 165 | + column.Comment = row.columnComment |
| 166 | + } |
| 167 | + |
| 168 | + table.Columns = append(table.Columns, column) |
| 169 | + } |
| 170 | + |
| 171 | + // Pre-allocate slice with exact size |
| 172 | + tables := make([]dberd.Table, 0, len(tableMap)) |
| 173 | + for _, table := range tableMap { |
| 174 | + tables = append(tables, *table) |
| 175 | + } |
| 176 | + |
| 177 | + return tables |
| 178 | +} |
| 179 | + |
| 180 | +const extractReferencesQuery = ` |
| 181 | + SELECT |
| 182 | + TABLE_SCHEMA, |
| 183 | + TABLE_NAME, |
| 184 | + COLUMN_NAME, |
| 185 | + REFERENCED_TABLE_SCHEMA, |
| 186 | + REFERENCED_TABLE_NAME, |
| 187 | + REFERENCED_COLUMN_NAME |
| 188 | + FROM information_schema.KEY_COLUMN_USAGE |
| 189 | + WHERE REFERENCED_TABLE_SCHEMA IS NOT NULL |
| 190 | + AND TABLE_SCHEMA NOT IN ('information_schema', 'performance_schema', 'mysql', 'sys') |
| 191 | + ORDER BY TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME;` |
| 192 | + |
| 193 | +type referenceRow struct { |
| 194 | + tableSchema string |
| 195 | + tableName string |
| 196 | + columnName string |
| 197 | + referencedSchema string |
| 198 | + referencedTableName string |
| 199 | + referencedColumn string |
| 200 | +} |
| 201 | + |
| 202 | +// extractReferences queries the database for foreign key relationships and converts them to dberd.Reference format. |
| 203 | +func (s *Source) extractReferences(ctx context.Context) ([]dberd.Reference, error) { |
| 204 | + rows, err := s.db.QueryContext(ctx, extractReferencesQuery) |
| 205 | + if err != nil { |
| 206 | + return nil, fmt.Errorf("querying references: %w", err) |
| 207 | + } |
| 208 | + defer rows.Close() |
| 209 | + |
| 210 | + referenceRows := make([]referenceRow, 0, 50) // Assuming reasonable number of references |
| 211 | + |
| 212 | + for rows.Next() { |
| 213 | + var r referenceRow |
| 214 | + if err := rows.Scan( |
| 215 | + &r.tableSchema, |
| 216 | + &r.tableName, |
| 217 | + &r.columnName, |
| 218 | + &r.referencedSchema, |
| 219 | + &r.referencedTableName, |
| 220 | + &r.referencedColumn, |
| 221 | + ); err != nil { |
| 222 | + return nil, fmt.Errorf("scanning references row: %w", err) |
| 223 | + } |
| 224 | + |
| 225 | + referenceRows = append(referenceRows, r) |
| 226 | + } |
| 227 | + |
| 228 | + if err := rows.Err(); err != nil { |
| 229 | + return nil, fmt.Errorf("references rows error: %w", err) |
| 230 | + } |
| 231 | + |
| 232 | + return referenceRowsToSchemaReferences(referenceRows), nil |
| 233 | +} |
| 234 | + |
| 235 | +// referenceRowsToSchemaReferences converts a slice of referenceRow into a slice of dberd.Reference. |
| 236 | +func referenceRowsToSchemaReferences(referenceRows []referenceRow) []dberd.Reference { |
| 237 | + references := make([]dberd.Reference, 0, len(referenceRows)) |
| 238 | + |
| 239 | + for _, row := range referenceRows { |
| 240 | + reference := dberd.Reference{ |
| 241 | + Source: dberd.TableColumn{ |
| 242 | + Table: row.tableSchema + "." + row.tableName, |
| 243 | + Column: row.columnName, |
| 244 | + }, |
| 245 | + Target: dberd.TableColumn{ |
| 246 | + Table: row.referencedSchema + "." + row.referencedTableName, |
| 247 | + Column: row.referencedColumn, |
| 248 | + }, |
| 249 | + } |
| 250 | + references = append(references, reference) |
| 251 | + } |
| 252 | + |
| 253 | + return references |
| 254 | +} |
0 commit comments