From 6ed2698f766dfbe3d9af7428f5d957d6f1397ba2 Mon Sep 17 00:00:00 2001 From: Mark Laing Date: Fri, 20 Dec 2024 10:36:22 +0000 Subject: [PATCH 1/9] lxd/db/schema: Expect filename and package as arguments. There is no reason to get the calling function. We can expect the filename and package to be passed in. Signed-off-by: Mark Laing --- lxd/db/schema/update.go | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/lxd/db/schema/update.go b/lxd/db/schema/update.go index 4c4d6e01c26d..1928c4201a08 100644 --- a/lxd/db/schema/update.go +++ b/lxd/db/schema/update.go @@ -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 '.go' source file in the package of the calling function, containing -// SQL statements that match the given schema updates. +// DotGo writes '' in the current directory, containing SQL statements that match the given schema updates. // -// The .go file contains a "flattened" render of all given updates and +// The 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) @@ -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) From e55e06beca407ee54a7f6f64df4f687b1ea3e811 Mon Sep 17 00:00:00 2001 From: Mark Laing Date: Fri, 20 Dec 2024 10:37:11 +0000 Subject: [PATCH 2/9] lxd/db/generate/db: Expect database kind as argument. Signed-off-by: Mark Laing --- lxd/db/generate/db/schema.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lxd/db/generate/db/schema.go b/lxd/db/generate/db/schema.go index f10ed9989d43..872056c6e16f 100644 --- a/lxd/db/generate/db/schema.go +++ b/lxd/db/generate/db/schema.go @@ -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) } From 7d52588a8a0ea6d63f667345a910d6ed4fc89bcf Mon Sep 17 00:00:00 2001 From: Mark Laing Date: Fri, 20 Dec 2024 10:37:31 +0000 Subject: [PATCH 3/9] lxd/db/generate: Expect schema kind as argument. Signed-off-by: Mark Laing --- lxd/db/generate/db.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lxd/db/generate/db.go b/lxd/db/generate/db.go index f9357985e1ee..7c3b210e2b8b 100644 --- a/lxd/db/generate/db.go +++ b/lxd/db/generate/db.go @@ -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]) }, } From 17238f69b4119d16ba175c98619dafc8293be846 Mon Sep 17 00:00:00 2001 From: Mark Laing Date: Fri, 20 Dec 2024 10:39:51 +0000 Subject: [PATCH 4/9] lxd/db/cluster: Pass package and filename into `schema.DotGo`. Note that we don't need the full file path to the `schema.go` file because we'll have a generate directive inside this directory. Signed-off-by: Mark Laing --- lxd/db/cluster/update.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lxd/db/cluster/update.go b/lxd/db/cluster/update.go index 9431f7a53969..948d57ece86c 100644 --- a/lxd/db/cluster/update.go +++ b/lxd/db/cluster/update.go @@ -30,7 +30,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. From 008f5e8733f4df35aa41d6cf988b01279489c00e Mon Sep 17 00:00:00 2001 From: Mark Laing Date: Fri, 20 Dec 2024 10:40:05 +0000 Subject: [PATCH 5/9] lxd/db/node: Pass package and filename into `schema.DotGo`. Note that we don't need the full file path to the `schema.go` file because we'll have a generate directive inside this directory. Signed-off-by: Mark Laing --- lxd/db/node/update.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lxd/db/node/update.go b/lxd/db/node/update.go index 45736e518737..0ccfb4c6c5f9 100644 --- a/lxd/db/node/update.go +++ b/lxd/db/node/update.go @@ -31,7 +31,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 From 29249024a9a86afd574d74a2cd24db47a6609bb2 Mon Sep 17 00:00:00 2001 From: Mark Laing Date: Fri, 20 Dec 2024 10:40:32 +0000 Subject: [PATCH 6/9] lxd/db: Remove schema generation directive from db package. Signed-off-by: Mark Laing --- lxd/db/schema.go | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 lxd/db/schema.go diff --git a/lxd/db/schema.go b/lxd/db/schema.go deleted file mode 100644 index b1df955dca16..000000000000 --- a/lxd/db/schema.go +++ /dev/null @@ -1,7 +0,0 @@ -//go:build linux && cgo && !agent - -package db - -// Directive for regenerating both the cluster and node database schemas. -// -//go:generate lxd-generate db schema From 6f44258ec0263f6b69688af94c05959a70c80f26 Mon Sep 17 00:00:00 2001 From: Mark Laing Date: Fri, 20 Dec 2024 10:40:53 +0000 Subject: [PATCH 7/9] lxd/db/cluster: Add schema generation directive to cluster package. Signed-off-by: Mark Laing --- lxd/db/cluster/update.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lxd/db/cluster/update.go b/lxd/db/cluster/update.go index 948d57ece86c..1885b71199ef 100644 --- a/lxd/db/cluster/update.go +++ b/lxd/db/cluster/update.go @@ -1,5 +1,7 @@ package cluster +//go:generate lxd-generate db schema cluster + import ( "context" "database/sql" From d2b2878feca2c9ac7a2d75a42430caccf82cb2d4 Mon Sep 17 00:00:00 2001 From: Mark Laing Date: Fri, 20 Dec 2024 10:41:17 +0000 Subject: [PATCH 8/9] lxd/db/node: Add schema generation directive to node package. Signed-off-by: Mark Laing --- lxd/db/node/update.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lxd/db/node/update.go b/lxd/db/node/update.go index 0ccfb4c6c5f9..241fed093afb 100644 --- a/lxd/db/node/update.go +++ b/lxd/db/node/update.go @@ -1,5 +1,7 @@ package node +//go:generate lxd-generate db schema node + import ( "context" "database/sql" From cebd8fa19be316a9e3300341b319a7f55918d934 Mon Sep 17 00:00:00 2001 From: Mark Laing Date: Fri, 20 Dec 2024 10:44:41 +0000 Subject: [PATCH 9/9] lxd/db/schema: Update DotGo test. Signed-off-by: Mark Laing --- lxd/db/schema/update_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lxd/db/schema/update_test.go b/lxd/db/schema/update_test.go index 8279d429da8d..7629a6724946 100644 --- a/lxd/db/schema/update_test.go +++ b/lxd/db/schema/update_test.go @@ -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")) }