[lxc-devel] [lxd/master] Adds db:"ignore" tag to mapper and ignores Instance.Snapshot field

tomponline on Github lxc-bot at linuxcontainers.org
Wed Sep 25 13:37:57 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 301 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190925/346c1fd0/attachment.bin>
-------------- next part --------------
From 375ff2a35a3ca3837e5c223f3d7662e466abc706 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Wed, 25 Sep 2019 14:35:30 +0100
Subject: [PATCH 1/3] shared/generate/db: Adds support for `db:"ignore"` tag on
 fields

- Fields with a `db:"ignore"` tag will not be used in generated queries.

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 shared/generate/db/parse.go | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/shared/generate/db/parse.go b/shared/generate/db/parse.go
index 348c376697..b785c75bcf 100644
--- a/shared/generate/db/parse.go
+++ b/shared/generate/db/parse.go
@@ -187,7 +187,10 @@ func parseStruct(str *ast.StructType) ([]*Field, error) {
 			return nil, err
 		}
 
-		fields = append(fields, field)
+		// Don't add field if it has been ignored.
+		if field != nil {
+			fields = append(fields, field)
+		}
 	}
 
 	return fields, nil
@@ -200,6 +203,15 @@ func parseField(f *ast.Field) (*Field, error) {
 		//return nil, fmt.Errorf("Unexported field name %q", name.Name)
 	}
 
+	// Ignore fields that are marked with a tag of `db:"ingore"`
+	if f.Tag != nil {
+		tag := f.Tag.Value
+		tagValue := reflect.StructTag(tag[1 : len(tag)-1]).Get("db")
+		if tagValue == "ignore" {
+			return nil, nil
+		}
+	}
+
 	typeName := parseType(f.Type)
 	if typeName == "" {
 		return nil, fmt.Errorf("Unsupported type for field %q", name.Name)

From d6f15ff83817536ca2a4fce6155400639ca458f3 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Wed, 25 Sep 2019 14:36:19 +0100
Subject: [PATCH 2/3] lxd/db/containers: Adds db:ignore tag to
 Instance.Snapshot field

- Snapshot field is a virtual field and should not be used with queries.

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/db/containers.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lxd/db/containers.go b/lxd/db/containers.go
index 9bda7f6e3f..8b0c15daca 100644
--- a/lxd/db/containers.go
+++ b/lxd/db/containers.go
@@ -71,7 +71,7 @@ type Instance struct {
 	Name         string `db:"primary=yes"`
 	Node         string `db:"join=nodes.name"`
 	Type         instance.Type
-	Snapshot     bool
+	Snapshot     bool `db:"ignore"`
 	Architecture int
 	Ephemeral    bool
 	CreationDate time.Time

From 2b559494f3bf70d389bc918b294d2b3f68d2cc10 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Wed, 25 Sep 2019 14:37:03 +0100
Subject: [PATCH 3/3] lxd/db/instances: Re-ran db generate

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/db/instances.mapper.go | 31 ++++++++++++++-----------------
 1 file changed, 14 insertions(+), 17 deletions(-)

diff --git a/lxd/db/instances.mapper.go b/lxd/db/instances.mapper.go
index 74389ef9e7..c2e87af02e 100644
--- a/lxd/db/instances.mapper.go
+++ b/lxd/db/instances.mapper.go
@@ -5,13 +5,10 @@ package db
 import (
 	"database/sql"
 	"fmt"
-
-	"github.com/pkg/errors"
-
 	"github.com/lxc/lxd/lxd/db/cluster"
 	"github.com/lxc/lxd/lxd/db/query"
-	"github.com/lxc/lxd/lxd/instance"
 	"github.com/lxc/lxd/shared/api"
+	"github.com/pkg/errors"
 )
 
 var _ = api.ServerEnvironment{}
@@ -166,7 +163,7 @@ func (c *ClusterTx) InstanceList(filter InstanceFilter) ([]Instance, error) {
 	if filter.Node != "" {
 		criteria["Node"] = filter.Node
 	}
-	if filter.Type != instance.TypeAny {
+	if filter.Type != -1 {
 		criteria["Type"] = filter.Type
 	}
 
@@ -174,18 +171,18 @@ func (c *ClusterTx) InstanceList(filter InstanceFilter) ([]Instance, error) {
 	var stmt *sql.Stmt
 	var args []interface{}
 
-	if criteria["Project"] != nil && criteria["Name"] != nil && criteria["Type"] != nil {
-		stmt = c.stmt(instanceObjectsByProjectAndNameAndType)
+	if criteria["Project"] != nil && criteria["Node"] != nil && criteria["Type"] != nil {
+		stmt = c.stmt(instanceObjectsByProjectAndNodeAndType)
 		args = []interface{}{
 			filter.Project,
-			filter.Name,
+			filter.Node,
 			filter.Type,
 		}
-	} else if criteria["Project"] != nil && criteria["Node"] != nil && criteria["Type"] != nil {
-		stmt = c.stmt(instanceObjectsByProjectAndNodeAndType)
+	} else if criteria["Project"] != nil && criteria["Name"] != nil && criteria["Type"] != nil {
+		stmt = c.stmt(instanceObjectsByProjectAndNameAndType)
 		args = []interface{}{
 			filter.Project,
-			filter.Node,
+			filter.Name,
 			filter.Type,
 		}
 	} else if criteria["Project"] != nil && criteria["Name"] != nil {
@@ -194,18 +191,18 @@ func (c *ClusterTx) InstanceList(filter InstanceFilter) ([]Instance, error) {
 			filter.Project,
 			filter.Name,
 		}
-	} else if criteria["Project"] != nil && criteria["Type"] != nil {
-		stmt = c.stmt(instanceObjectsByProjectAndType)
-		args = []interface{}{
-			filter.Project,
-			filter.Type,
-		}
 	} else if criteria["Node"] != nil && criteria["Type"] != nil {
 		stmt = c.stmt(instanceObjectsByNodeAndType)
 		args = []interface{}{
 			filter.Node,
 			filter.Type,
 		}
+	} else if criteria["Project"] != nil && criteria["Type"] != nil {
+		stmt = c.stmt(instanceObjectsByProjectAndType)
+		args = []interface{}{
+			filter.Project,
+			filter.Type,
+		}
 	} else if criteria["Type"] != nil {
 		stmt = c.stmt(instanceObjectsByType)
 		args = []interface{}{


More information about the lxc-devel mailing list