enumValuesFromDump parses dump.sql and extracts all literals from the `CREATE TYPE api_key_scope AS ENUM (...)` block.
(path string)
| 96 | // enumValuesFromDump parses dump.sql and extracts all literals from the |
| 97 | // `CREATE TYPE api_key_scope AS ENUM (...)` block. |
| 98 | func enumValuesFromDump(path string) (map[string]struct{}, error) { |
| 99 | f, err := os.Open(path) |
| 100 | if err != nil { |
| 101 | return nil, err |
| 102 | } |
| 103 | defer f.Close() |
| 104 | |
| 105 | const enumHead = "CREATE TYPE api_key_scope AS ENUM (" |
| 106 | litRe := regexp.MustCompile(`'([^']+)'`) |
| 107 | |
| 108 | values := make(map[string]struct{}) |
| 109 | inEnum := false |
| 110 | s := bufio.NewScanner(f) |
| 111 | for s.Scan() { |
| 112 | line := strings.TrimSpace(s.Text()) |
| 113 | if !inEnum { |
| 114 | if strings.Contains(line, enumHead) { |
| 115 | inEnum = true |
| 116 | } |
| 117 | continue |
| 118 | } |
| 119 | if strings.HasPrefix(line, ");") { |
| 120 | // End of enum block |
| 121 | return values, nil |
| 122 | } |
| 123 | // Collect single-quoted literals on this line. |
| 124 | for _, m := range litRe.FindAllStringSubmatch(line, -1) { |
| 125 | if len(m) > 1 { |
| 126 | values[m[1]] = struct{}{} |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | if err := s.Err(); err != nil { |
| 131 | return nil, err |
| 132 | } |
| 133 | if !inEnum { |
| 134 | return nil, xerrors.New("api_key_scope enum block not found in dump") |
| 135 | } |
| 136 | return values, nil |
| 137 | } |