| 151 | } |
| 152 | |
| 153 | func LoadTables(db *sql.DB, tableFilter TableFilter, columnCompressionConfig ColumnCompressionConfig, columnIgnoreConfig ColumnIgnoreConfig, forceIndexConfig ForceIndexConfig, cascadingPaginationColumnConfig *CascadingPaginationColumnConfig) (TableSchemaCache, error) { |
| 154 | logger := LogWithField("tag", "table_schema_cache") |
| 155 | |
| 156 | tableSchemaCache := make(TableSchemaCache) |
| 157 | |
| 158 | dbnames, err := showDatabases(db) |
| 159 | if err != nil { |
| 160 | logger.WithError(err).Error("failed to show databases") |
| 161 | return tableSchemaCache, err |
| 162 | } |
| 163 | |
| 164 | dbnames, err = tableFilter.ApplicableDatabases(dbnames) |
| 165 | if err != nil { |
| 166 | logger.WithError(err).Error("could not apply database filter") |
| 167 | return tableSchemaCache, err |
| 168 | } |
| 169 | |
| 170 | // For each database, get a list of tables from it and cache the table's schema |
| 171 | for _, dbname := range dbnames { |
| 172 | dbLog := logger.WithField("database", dbname) |
| 173 | dbLog.Debug("loading tables from database") |
| 174 | tableNames, err := showTablesFrom(db, dbname) |
| 175 | if err != nil { |
| 176 | dbLog.WithError(err).Error("failed to show tables") |
| 177 | return tableSchemaCache, err |
| 178 | } |
| 179 | |
| 180 | var tableSchemas []*TableSchema |
| 181 | |
| 182 | for _, table := range tableNames { |
| 183 | tableLog := dbLog.WithField("table", table) |
| 184 | tableLog.Debug("fetching table schema") |
| 185 | tableSchema, err := schema.NewTableFromSqlDB(db.DB, dbname, table) |
| 186 | if err != nil { |
| 187 | tableLog.WithError(err).Error("cannot fetch table schema from source db") |
| 188 | return tableSchemaCache, err |
| 189 | } |
| 190 | |
| 191 | // Filter out invisible indexes |
| 192 | visibleIndexes := make([]*schema.Index, 0, len(tableSchema.Indexes)) |
| 193 | for _, index := range tableSchema.Indexes { |
| 194 | if index.Visible { |
| 195 | visibleIndexes = append(visibleIndexes, index) |
| 196 | } |
| 197 | } |
| 198 | tableSchema.Indexes = visibleIndexes |
| 199 | |
| 200 | tableSchemas = append(tableSchemas, &TableSchema{ |
| 201 | Table: tableSchema, |
| 202 | CompressedColumnsForVerification: columnCompressionConfig.CompressedColumnsFor(dbname, table), |
| 203 | IgnoredColumnsForVerification: columnIgnoreConfig.IgnoredColumnsFor(dbname, table), |
| 204 | ForcedIndexForVerification: forceIndexConfig.IndexFor(dbname, table), |
| 205 | }) |
| 206 | } |
| 207 | |
| 208 | tableSchemas, err = tableFilter.ApplicableTables(tableSchemas) |
| 209 | if err != nil { |
| 210 | return tableSchemaCache, nil |