Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove runtime.Caller from schema.DotGo #14705

Merged
merged 9 commits into from
Dec 20, 2024
4 changes: 3 additions & 1 deletion lxd/db/cluster/update.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cluster

//go:generate lxd-generate db schema cluster

import (
"context"
"database/sql"
Expand Down Expand Up @@ -30,7 +32,7 @@ func FreshSchema() string {
// SchemaDotGo refreshes the schema.go file in this package, using the updates
// defined here.
func SchemaDotGo() error {
return schema.DotGo(updates, "schema")
return schema.DotGo(updates, "cluster", "schema.go")
}

// SchemaVersion is the current version of the cluster database schema.
Expand Down
6 changes: 5 additions & 1 deletion lxd/db/generate/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ func newDbSchema() *cobra.Command {
Use: "schema",
Short: "Generate database schema by applying updates.",
RunE: func(cmd *cobra.Command, args []string) error {
return db.UpdateSchema()
if len(args) < 1 {
return fmt.Errorf(`Schema kind must be provided (must be "node", or "cluster")`)
}

return db.UpdateSchema(args[0])
},
}

Expand Down
14 changes: 9 additions & 5 deletions lxd/db/generate/db/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import (
)

// UpdateSchema updates the schema.go file of the cluster and node databases.
func UpdateSchema() error {
err := cluster.SchemaDotGo()
if err != nil {
return fmt.Errorf("Update cluster database schema: %w", err)
func UpdateSchema(kind string) error {
var err error
switch kind {
case "node":
err = node.SchemaDotGo()
case "cluster":
err = cluster.SchemaDotGo()
default:
return fmt.Errorf(`No such schema kind %q (must be "node", or "cluster")`, kind)
}

err = node.SchemaDotGo()
if err != nil {
return fmt.Errorf("Update node database schema: %w", err)
}
Expand Down
4 changes: 3 additions & 1 deletion lxd/db/node/update.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package node

//go:generate lxd-generate db schema node

import (
"context"
"database/sql"
Expand Down Expand Up @@ -31,7 +33,7 @@ func FreshSchema() string {
// SchemaDotGo refreshes the schema.go file in this package, using the updates
// defined here.
func SchemaDotGo() error {
return schema.DotGo(updates, "schema")
return schema.DotGo(updates, "node", "schema.go")
}

/* Database updates are one-time actions that are needed to move an
Expand Down
7 changes: 0 additions & 7 deletions lxd/db/schema.go

This file was deleted.

24 changes: 5 additions & 19 deletions lxd/db/schema/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,20 @@ import (
"database/sql"
"fmt"
"os"
"path"
"runtime"
"strings"

_ "github.com/mattn/go-sqlite3" // For opening the in-memory database
)

// DotGo writes '<name>.go' source file in the package of the calling function, containing
// SQL statements that match the given schema updates.
// DotGo writes '<filename>' in the current directory, containing SQL statements that match the given schema updates.
//
// The <name>.go file contains a "flattened" render of all given updates and
// The <filename> contains a "flattened" render of all given updates and
// can be used to initialize brand new databases using Schema.Fresh().
func DotGo(updates map[int]Update, name string) error {
func DotGo(updates map[int]Update, pkg string, filename string) error {
// Apply all the updates that we have on a pristine database and dump
// the resulting schema.
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
return fmt.Errorf("failed to open schema.go for writing: %w", err)
return fmt.Errorf("failed to open in-memory SQLite database: %w", err)
}

schema := NewFromMap(updates)
Expand All @@ -36,21 +32,11 @@ func DotGo(updates map[int]Update, name string) error {
return err
}

// Passing 1 to runtime.Caller identifies our caller.
_, filename, _, _ := runtime.Caller(1)

// runtime.Caller returns source file path starting from github.com.
// Translate into relative path to source file.
if strings.HasPrefix(filename, "github.com") {
filename = strings.TrimPrefix(filename, "github.com/canonical/lxd/lxd/db/")
}

file, err := os.Create(path.Join(path.Dir(filename), name+".go"))
file, err := os.Create(filename)
if err != nil {
return fmt.Errorf("failed to open Go file for writing: %w", err)
}

pkg := path.Base(path.Dir(filename))
_, err = file.Write([]byte(fmt.Sprintf(dotGoTemplate, pkg, dump)))
if err != nil {
return fmt.Errorf("failed to write to Go file: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion lxd/db/schema/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestDotGo(t *testing.T) {
2: updateInsertValue,
}

require.NoError(t, schema.DotGo(updates, "xyz"))
require.NoError(t, schema.DotGo(updates, "xyz", "xyz.go"))
require.Equal(t, true, shared.PathExists("xyz.go"))
require.NoError(t, os.Remove("xyz.go"))
}
Loading