Skip to content

Commit

Permalink
devdb: render manifest using Go API (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm authored Oct 12, 2024
1 parent 942dd29 commit db2a9e3
Show file tree
Hide file tree
Showing 7 changed files with 545 additions and 355 deletions.
53 changes: 44 additions & 9 deletions api/v1alpha1/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,23 +145,58 @@ func getSecrectValue(
return string(val.Data[ref.Key]), nil
}

// Driver defines the database driver.
type Driver string

const (
DriverPostgres Driver = "postgres"
DriverMySQL Driver = "mysql"
DriverMariaDB Driver = "mariadb"
DriverSQLite Driver = "sqlite"
DriverSQLServer Driver = "sqlserver"
)

// DriverBySchema returns the driver from the given schema.
// it remove the schema modifier if present.
// e.g. mysql+unix -> mysql
// it also handles aliases.
// e.g. mariadb -> mysql
func DriverBySchema(schema string) string {
func DriverBySchema(schema string) Driver {
p := strings.SplitN(schema, "+", 2)
switch drv := strings.ToLower(p[0]); drv {
case "libsql":
return "sqlite"
case "maria", "mariadb":
return "mysql"
case "postgresql":
return "postgres"
case "sqlite", "libsql":
return DriverSQLite
case "mysql":
return DriverMySQL
case "mariadb", "maria":
return DriverMariaDB
case "postgres", "postgresql":
return DriverPostgres
case "sqlserver", "azuresql", "mssql":
return "sqlserver"
return DriverSQLServer
default:
panic(fmt.Sprintf("unsupported driver %q", drv))
}
}

// String returns the string representation of the driver.
func (d Driver) String() string {
return string(d)
}

// SchemaBound returns true if the driver requires a schema.
func (d Driver) SchemaBound(u url.URL) bool {
switch d {
case DriverSQLite:
return true
case DriverPostgres:
return u.Query().Get("search_path") != ""
case DriverMySQL, DriverMariaDB:
return u.Path != ""
case DriverSQLServer:
m := u.Query().Get("mode")
return m == "" || strings.ToLower(m) == "schema"
default:
return drv
panic(fmt.Sprintf("unsupported driver %q", d))
}
}
Loading

0 comments on commit db2a9e3

Please sign in to comment.