getMaterializedViews gets all materialized views of a database.
(txn *sql.Tx, schemaName string, _ map[db.TableKey][]*storepb.ColumnMetadata)
| 1096 | |
| 1097 | // getMaterializedViews gets all materialized views of a database. |
| 1098 | func getMaterializedViews(txn *sql.Tx, schemaName string, _ map[db.TableKey][]*storepb.ColumnMetadata) ([]*storepb.MaterializedViewMetadata, error) { |
| 1099 | var materializedViews []*storepb.MaterializedViewMetadata |
| 1100 | |
| 1101 | // Get materialized view comments |
| 1102 | materializedViewCommentMap, err := getMaterializedViewComments(txn, schemaName) |
| 1103 | if err != nil { |
| 1104 | return nil, errors.Wrapf(err, "failed to get materialized view comments") |
| 1105 | } |
| 1106 | |
| 1107 | query := fmt.Sprintf(` |
| 1108 | SELECT OWNER, MVIEW_NAME, QUERY |
| 1109 | FROM sys.all_mviews |
| 1110 | WHERE OWNER = '%s' |
| 1111 | ORDER BY MVIEW_NAME |
| 1112 | `, schemaName) |
| 1113 | |
| 1114 | slog.Debug("running get materialized views query") |
| 1115 | rows, err := txn.Query(query) |
| 1116 | if err != nil { |
| 1117 | return nil, util.FormatErrorWithQuery(err, query) |
| 1118 | } |
| 1119 | defer rows.Close() |
| 1120 | |
| 1121 | for rows.Next() { |
| 1122 | materializedView := &storepb.MaterializedViewMetadata{} |
| 1123 | var schemaName string |
| 1124 | if err := rows.Scan(&schemaName, &materializedView.Name, &materializedView.Definition); err != nil { |
| 1125 | return nil, err |
| 1126 | } |
| 1127 | // Oracle may include a trailing C-string null terminator via the go-ora driver. |
| 1128 | materializedView.Definition = strings.TrimRight(materializedView.Definition, "\x00") |
| 1129 | |
| 1130 | // Ensure the definition ends with a newline to match expected format |
| 1131 | if materializedView.Definition != "" && !strings.HasSuffix(materializedView.Definition, "\n") { |
| 1132 | materializedView.Definition += "\n" |
| 1133 | } |
| 1134 | |
| 1135 | // Add comment if available |
| 1136 | key := db.TableKey{Schema: schemaName, Table: materializedView.Name} |
| 1137 | if comment, ok := materializedViewCommentMap[key]; ok { |
| 1138 | materializedView.Comment = comment |
| 1139 | } |
| 1140 | |
| 1141 | materializedViews = append(materializedViews, materializedView) |
| 1142 | } |
| 1143 | if err := rows.Err(); err != nil { |
| 1144 | return nil, util.FormatErrorWithQuery(err, query) |
| 1145 | } |
| 1146 | if err := rows.Close(); err != nil { |
| 1147 | return nil, errors.Wrapf(err, "failed to close rows") |
| 1148 | } |
| 1149 | |
| 1150 | // Fetch all materialized view dependencies in a single query to avoid N+1 problem |
| 1151 | materializedViewDependenciesMap, err := getAllMaterializedViewDependencies(txn, schemaName) |
| 1152 | if err != nil { |
| 1153 | return nil, errors.Wrapf(err, "failed to get materialized view dependencies") |
| 1154 | } |
| 1155 |
no test coverage detected