mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore: improve build deadline code (#19203)
- Adds/improves a lot of comments to make the autostop calculation code clearer - Changes the behavior of the enterprise template schedule store to match the behavior of the workspace TTL endpoint when the new TTL is zero - Fixes a bug in the workspace TTL endpoint where it could unset the build deadline, even though a max_deadline was specified - Adds a new constraint to the workspace_builds table that enforces the deadline is non-zero and below the max_deadline if it is set - Adds CHECK constraint enum generation to scripts/dbgen, used for testing the above constraint - Adds Dean and Danielle as CODEOWNERS for the autostop calculation code
This commit is contained in:
+5
-146
@@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"go/format"
|
||||
@@ -80,154 +79,14 @@ return %s
|
||||
return xerrors.Errorf("generate foreign key constraints: %w", err)
|
||||
}
|
||||
|
||||
err = generateCheckConstraints()
|
||||
if err != nil {
|
||||
return xerrors.Errorf("generate check constraints: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// generateUniqueConstraints generates the UniqueConstraint enum.
|
||||
func generateUniqueConstraints() 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 uniqueConstraints []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, "UNIQUE") || strings.Contains(query, "PRIMARY KEY") {
|
||||
uniqueConstraints = append(uniqueConstraints, 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, `
|
||||
// UniqueConstraint represents a named unique constraint on a table.
|
||||
type UniqueConstraint string
|
||||
|
||||
// UniqueConstraint enums.
|
||||
const (
|
||||
`)
|
||||
for _, query := range uniqueConstraints {
|
||||
name := ""
|
||||
switch {
|
||||
case strings.Contains(query, "ALTER TABLE") && strings.Contains(query, "ADD CONSTRAINT"):
|
||||
name = strings.Split(query, " ")[6]
|
||||
case strings.Contains(query, "CREATE UNIQUE INDEX"):
|
||||
name = strings.Split(query, " ")[3]
|
||||
default:
|
||||
return xerrors.Errorf("unknown unique constraint format: %s", query)
|
||||
}
|
||||
_, _ = fmt.Fprintf(s, "\tUnique%s UniqueConstraint = %q // %s\n", nameFromSnakeCase(name), name, query)
|
||||
}
|
||||
_, _ = fmt.Fprint(s, ")\n")
|
||||
|
||||
outputPath := filepath.Join(databasePath, "unique_constraint.go")
|
||||
|
||||
data, err := imports.Process(outputPath, s.Bytes(), &imports.Options{
|
||||
Comments: true,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
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]
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user