- Notifications
You must be signed in to change notification settings - Fork1k
feat(coderd/database): generate foreign key constraints and add database.IsForeignKeyViolation#9657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. note: I haven't performed any DRY refactoring on this piece here; I figure de-duplication is premature. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. 💯 agreed. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -84,6 +84,11 @@ return %s | ||
return xerrors.Errorf("generate unique constraints: %w", err) | ||
} | ||
err = generateForeignKeyConstraints() | ||
if err != nil { | ||
return xerrors.Errorf("generate foreign key constraints: %w", err) | ||
} | ||
return nil | ||
} | ||
@@ -125,7 +130,7 @@ func generateUniqueConstraints() error { | ||
s := &bytes.Buffer{} | ||
_, _ = fmt.Fprint(s, `// Code generated byscripts/dbgen/main.go. DO NOT EDIT. | ||
package database | ||
`) | ||
_, _ = fmt.Fprint(s, ` | ||
@@ -160,6 +165,78 @@ const ( | ||
return os.WriteFile(outputPath, data, 0o600) | ||
} | ||
// generateForeignKeyConstraints generates the ForeignKeyConstraint enum. | ||
func generateForeignKeyConstraints() error { | ||
localPath, err := localFilePath() | ||
if err != nil { | ||
return err | ||
} | ||
databasePath := filepath.Join(localPath, "..", "..", "..", "coderd", "database") | ||
dump, err := os.Open(filepath.Join(databasePath, "dump.sql")) | ||
if err != nil { | ||
return err | ||
} | ||
defer dump.Close() | ||
var foreignKeyConstraints []string | ||
dumpScanner := bufio.NewScanner(dump) | ||
query := "" | ||
for dumpScanner.Scan() { | ||
line := strings.TrimSpace(dumpScanner.Text()) | ||
switch { | ||
case strings.HasPrefix(line, "--"): | ||
case line == "": | ||
case strings.HasSuffix(line, ";"): | ||
query += line | ||
if strings.Contains(query, "FOREIGN KEY") { | ||
foreignKeyConstraints = append(foreignKeyConstraints, query) | ||
} | ||
query = "" | ||
default: | ||
query += line + " " | ||
} | ||
} | ||
if err := dumpScanner.Err(); err != nil { | ||
return err | ||
} | ||
s := &bytes.Buffer{} | ||
_, _ = fmt.Fprint(s, `// Code generated by scripts/dbgen/main.go. DO NOT EDIT. | ||
package database | ||
`) | ||
_, _ = fmt.Fprint(s, ` | ||
// ForeignKeyConstraint represents a named foreign key constraint on a table. | ||
type ForeignKeyConstraint string | ||
// ForeignKeyConstraint enums. | ||
const ( | ||
`) | ||
for _, query := range foreignKeyConstraints { | ||
name := "" | ||
switch { | ||
case strings.Contains(query, "ALTER TABLE") && strings.Contains(query, "ADD CONSTRAINT"): | ||
name = strings.Split(query, " ")[6] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. parsing is pretty flimsy, but I guess the rest of the file is no better... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Yeah... it works for the moment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I don't think it's worth improving, SQL is pretty stable 😄. | ||
default: | ||
return xerrors.Errorf("unknown foreign key constraint format: %s", query) | ||
} | ||
_, _ = fmt.Fprintf(s, "\tForeignKey%s ForeignKeyConstraint = %q // %s\n", nameFromSnakeCase(name), name, query) | ||
} | ||
_, _ = fmt.Fprint(s, ")\n") | ||
outputPath := filepath.Join(databasePath, "foreign_key_constraint.go") | ||
data, err := imports.Process(outputPath, s.Bytes(), &imports.Options{ | ||
Comments: true, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
return os.WriteFile(outputPath, data, 0o600) | ||
} | ||
type stubParams struct { | ||
FuncName string | ||
Parameters string | ||
@@ -560,6 +637,14 @@ func nameFromSnakeCase(s string) string { | ||
ret += "JWT" | ||
case "idx": | ||
ret += "Index" | ||
case "api": | ||
ret += "API" | ||
case "uuid": | ||
ret += "UUID" | ||
case "gitsshkeys": | ||
ret += "GitSSHKeys" | ||
case "fkey": | ||
// ignore | ||
default: | ||
ret += strings.Title(ss) | ||
} | ||