@@ -84,6 +84,11 @@ return %s
84
84
return xerrors .Errorf ("generate unique constraints: %w" , err )
85
85
}
86
86
87
+ err = generateForeignKeyConstraints ()
88
+ if err != nil {
89
+ return xerrors .Errorf ("generate foreign key constraints: %w" , err )
90
+ }
91
+
87
92
return nil
88
93
}
89
94
@@ -125,7 +130,7 @@ func generateUniqueConstraints() error {
125
130
126
131
s := & bytes.Buffer {}
127
132
128
- _ , _ = fmt .Fprint (s , `// Code generated by gen/enum . DO NOT EDIT.
133
+ _ , _ = fmt .Fprint (s , `// Code generated by scripts/dbgen/main.go . DO NOT EDIT.
129
134
package database
130
135
` )
131
136
_ , _ = fmt .Fprint (s , `
@@ -160,6 +165,78 @@ const (
160
165
return os .WriteFile (outputPath , data , 0o600 )
161
166
}
162
167
168
+ // generateForeignKeyConstraints generates the ForeignKeyConstraint enum.
169
+ func generateForeignKeyConstraints () error {
170
+ localPath , err := localFilePath ()
171
+ if err != nil {
172
+ return err
173
+ }
174
+ databasePath := filepath .Join (localPath , ".." , ".." , ".." , "coderd" , "database" )
175
+
176
+ dump , err := os .Open (filepath .Join (databasePath , "dump.sql" ))
177
+ if err != nil {
178
+ return err
179
+ }
180
+ defer dump .Close ()
181
+
182
+ var foreignKeyConstraints []string
183
+ dumpScanner := bufio .NewScanner (dump )
184
+ query := ""
185
+ for dumpScanner .Scan () {
186
+ line := strings .TrimSpace (dumpScanner .Text ())
187
+ switch {
188
+ case strings .HasPrefix (line , "--" ):
189
+ case line == "" :
190
+ case strings .HasSuffix (line , ";" ):
191
+ query += line
192
+ if strings .Contains (query , "FOREIGN KEY" ) {
193
+ foreignKeyConstraints = append (foreignKeyConstraints , query )
194
+ }
195
+ query = ""
196
+ default :
197
+ query += line + " "
198
+ }
199
+ }
200
+
201
+ if err := dumpScanner .Err (); err != nil {
202
+ return err
203
+ }
204
+
205
+ s := & bytes.Buffer {}
206
+
207
+ _ , _ = fmt .Fprint (s , `// Code generated by scripts/dbgen/main.go. DO NOT EDIT.
208
+ package database
209
+ ` )
210
+ _ , _ = fmt .Fprint (s , `
211
+ // ForeignKeyConstraint represents a named foreign key constraint on a table.
212
+ type ForeignKeyConstraint string
213
+
214
+ // ForeignKeyConstraint enums.
215
+ const (
216
+ ` )
217
+ for _ , query := range foreignKeyConstraints {
218
+ name := ""
219
+ switch {
220
+ case strings .Contains (query , "ALTER TABLE" ) && strings .Contains (query , "ADD CONSTRAINT" ):
221
+ name = strings .Split (query , " " )[6 ]
222
+ default :
223
+ return xerrors .Errorf ("unknown foreign key constraint format: %s" , query )
224
+ }
225
+ _ , _ = fmt .Fprintf (s , "\t ForeignKey%s ForeignKeyConstraint = %q // %s\n " , nameFromSnakeCase (name ), name , query )
226
+ }
227
+ _ , _ = fmt .Fprint (s , ")\n " )
228
+
229
+ outputPath := filepath .Join (databasePath , "foreign_key_constraint.go" )
230
+
231
+ data , err := imports .Process (outputPath , s .Bytes (), & imports.Options {
232
+ Comments : true ,
233
+ })
234
+ if err != nil {
235
+ return err
236
+ }
237
+ return os .WriteFile (outputPath , data , 0o600 )
238
+ }
239
+
163
240
type stubParams struct {
164
241
FuncName string
165
242
Parameters string
@@ -560,6 +637,14 @@ func nameFromSnakeCase(s string) string {
560
637
ret += "JWT"
561
638
case "idx" :
562
639
ret += "Index"
640
+ case "api" :
641
+ ret += "API"
642
+ case "uuid" :
643
+ ret += "UUID"
644
+ case "gitsshkeys" :
645
+ ret += "GitSSHKeys"
646
+ case "fkey" :
647
+ // ignore
563
648
default :
564
649
ret += strings .Title (ss )
565
650
}
0 commit comments