generateCheckConstraints generates the CheckConstraint enum.
()
| 206 | |
| 207 | // generateCheckConstraints generates the CheckConstraint enum. |
| 208 | func generateCheckConstraints() error { |
| 209 | localPath, err := localFilePath() |
| 210 | if err != nil { |
| 211 | return err |
| 212 | } |
| 213 | databasePath := filepath.Join(localPath, "..", "..", "..", "coderd", "database") |
| 214 | dumpPath := filepath.Join(databasePath, "dump.sql") |
| 215 | outputPath := filepath.Join(databasePath, "check_constraint.go") |
| 216 | |
| 217 | var ( |
| 218 | tableRegex = regexp.MustCompile(`CREATE TABLE\s+([^\s]+)`) |
| 219 | checkRegex = regexp.MustCompile(`CONSTRAINT\s+([^\s]+)\s+CHECK`) |
| 220 | ) |
| 221 | fn := func(query string) ([]constraint, error) { |
| 222 | constraints := []constraint{} |
| 223 | |
| 224 | tableMatches := tableRegex.FindStringSubmatch(query) |
| 225 | if len(tableMatches) > 0 { |
| 226 | table := tableMatches[1] |
| 227 | |
| 228 | // Find every CONSTRAINT xxx CHECK occurrence. |
| 229 | matches := checkRegex.FindAllStringSubmatch(query, -1) |
| 230 | for _, match := range matches { |
| 231 | constraints = append(constraints, constraint{ |
| 232 | name: match[1], |
| 233 | comment: table, |
| 234 | }) |
| 235 | } |
| 236 | } |
| 237 | return constraints, nil |
| 238 | } |
| 239 | |
| 240 | return generateConstraints(dumpPath, outputPath, constraintTypeCheck, fn) |
| 241 | } |
no test coverage detected