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 schema command from lxd-generate #14707

Merged
merged 7 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ ifeq ($(shell command -v goimports),)
endif
cd lxd/db/generate && go build -v -trimpath -o $(GOPATH)/bin/lxd-generate -tags "$(TAG_SQLITE3)" $(DEBUG) && cd -
go generate ./...
goimports -w ./lxd/db/
@echo "Code generation completed"

.PHONY: update-api
Expand Down
2 changes: 2 additions & 0 deletions lxd/db/cluster/auth_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
//go:generate mapper method -i -e auth_group DeleteOne-by-Name
//go:generate mapper method -i -e auth_group Update
//go:generate mapper method -i -e auth_group Rename
//go:generate goimports -w auth_groups.mapper.go
//go:generate goimports -w auth_groups.interface.mapper.go

// AuthGroup is the database representation of an api.AuthGroup.
type AuthGroup struct {
Expand Down
3 changes: 3 additions & 0 deletions lxd/db/cluster/cluster_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import (
//go:generate mapper method -i -e cluster_group Update
//go:generate mapper method -i -e cluster_group DeleteOne-by-Name

//go:generate goimports -w cluster_groups.mapper.go
//go:generate goimports -w cluster_groups.interface.mapper.go

// ClusterGroup is a value object holding db-related details about a cluster group.
type ClusterGroup struct {
ID int
Expand Down
2 changes: 2 additions & 0 deletions lxd/db/cluster/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ package cluster
//go:generate mapper method -i -e config Create struct=Config
//go:generate mapper method -i -e config Update struct=Config
//go:generate mapper method -i -e config DeleteMany
//go:generate goimports -w config.mapper.go
//go:generate goimports -w config.interface.mapper.go

// Config is a reference struct representing one configuration entry of another entity.
type Config struct {
Expand Down
2 changes: 2 additions & 0 deletions lxd/db/cluster/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
//go:generate mapper method -i -e device Create struct=Device
//go:generate mapper method -i -e device Update struct=Device
//go:generate mapper method -i -e device DeleteMany
//go:generate goimports -w devices.mapper.go
//go:generate goimports -w devices.interface.mapper.go

// DeviceType represents the types of supported devices.
type DeviceType int
Expand Down
2 changes: 2 additions & 0 deletions lxd/db/cluster/identities.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ import (
//go:generate mapper method -i -e identity DeleteOne-by-AuthMethod-and-Identifier
//go:generate mapper method -i -e identity DeleteMany-by-Name-and-Type
//go:generate mapper method -i -e identity Update struct=Identity
//go:generate goimports -w identities.mapper.go
//go:generate goimports -w identities.interface.mapper.go

// AuthMethod is a database representation of an authentication method.
//
Expand Down
2 changes: 2 additions & 0 deletions lxd/db/cluster/identity_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package cluster
//go:generate mapper method -i -e identity_project DeleteMany struct=Identity
//go:generate mapper method -i -e identity_project Create struct=Identity
//go:generate mapper method -i -e identity_project Update struct=Identity
//go:generate goimports -w identity_projects.mapper.go
//go:generate goimports -w identity_projects.interface.mapper.go

// IdentityProject is an association table struct that associates
// identities to projects.
Expand Down
4 changes: 3 additions & 1 deletion lxd/db/cluster/identity_provider_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
//go:generate mapper method -i -e identity_provider_group DeleteOne-by-Name
//go:generate mapper method -i -e identity_provider_group Update
//go:generate mapper method -i -e identity_provider_group Rename
//go:generate goimports -w identity_provider_groups.mapper.go
//go:generate goimports -w identity_provider_groups.interface.mapper.go

// IdentityProviderGroup is the database representation of an api.IdentityProviderGroup.
type IdentityProviderGroup struct {
Expand Down Expand Up @@ -146,7 +148,7 @@ func GetDistinctAuthGroupNamesFromIDPGroupNames(ctx context.Context, tx *sql.Tx,
return nil, nil
}

var args []any
args := make([]any, 0, len(idpGroupNames))
for _, idpGroupName := range idpGroupNames {
args = append(args, idpGroupName)
}
Expand Down
2 changes: 2 additions & 0 deletions lxd/db/cluster/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
//
//go:generate mapper method -i -e image GetMany
//go:generate mapper method -i -e image GetOne
//go:generate goimports -w images.mapper.go
//go:generate goimports -w images.interface.mapper.go

// Image is a value object holding db-related details about an image.
type Image struct {
Expand Down
2 changes: 2 additions & 0 deletions lxd/db/cluster/instance_profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
//go:generate mapper method -i -e instance_profile GetMany struct=Instance
//go:generate mapper method -i -e instance_profile Create struct=Instance
//go:generate mapper method -i -e instance_profile DeleteMany struct=Instance
//go:generate goimports -w instance_profiles.mapper.go
//go:generate goimports -w instance_profiles.interface.mapper.go

// InstanceProfile is an association table struct that associates Instances
// to Profiles.
Expand Down
2 changes: 2 additions & 0 deletions lxd/db/cluster/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import (
//go:generate mapper method -i -e instance Rename
//go:generate mapper method -i -e instance DeleteOne-by-Project-and-Name
//go:generate mapper method -i -e instance Update references=Config,Device
//go:generate goimports -w instances.mapper.go
//go:generate goimports -w instances.interface.mapper.go

// Instance is a value object holding db-related details about an instance.
type Instance struct {
Expand Down
2 changes: 2 additions & 0 deletions lxd/db/cluster/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package cluster
//go:generate mapper stmt -e node id
//
//go:generate mapper method -i -e node ID
//go:generate goimports -w nodes.mapper.go
//go:generate goimports -w nodes.interface.mapper.go

// Node represents a LXD cluster node.
type Node struct {
Expand Down
2 changes: 2 additions & 0 deletions lxd/db/cluster/nodes_cluster_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ package cluster
//
//go:generate mapper method -e node_cluster_group GetMany
//go:generate mapper method -e node_cluster_group DeleteOne-by-GroupID
//go:generate goimports -w nodes_cluster_groups.mapper.go
//go:generate goimports -w nodes_cluster_groups.interface.mapper.go

// NodeClusterGroup associates a node to a cluster group.
type NodeClusterGroup struct {
Expand Down
2 changes: 2 additions & 0 deletions lxd/db/cluster/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
//go:generate mapper method -i -e operation CreateOrReplace
//go:generate mapper method -i -e operation DeleteOne-by-UUID
//go:generate mapper method -i -e operation DeleteMany-by-NodeID
//go:generate goimports -w operations.mapper.go
//go:generate goimports -w operations.interface.mapper.go

// Operation holds information about a single LXD operation running on a node
// in the cluster.
Expand Down
2 changes: 2 additions & 0 deletions lxd/db/cluster/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
//go:generate mapper method -i -e profile Rename
//go:generate mapper method -i -e profile Update references=Config,Device
//go:generate mapper method -i -e profile DeleteOne-by-Project-and-Name
//go:generate goimports -w profiles.mapper.go
//go:generate goimports -w profiles.interface.mapper.go

// Profile is a value object holding db-related details about a profile.
type Profile struct {
Expand Down
2 changes: 2 additions & 0 deletions lxd/db/cluster/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
//go:generate mapper method -i -e project ID struct=Project
//go:generate mapper method -i -e project Rename
//go:generate mapper method -i -e project DeleteOne-by-Name
//go:generate goimports -w projects.mapper.go
//go:generate goimports -w projects.interface.mapper.go

// ProjectFeature indicates the behaviour of a project feature.
type ProjectFeature struct {
Expand Down
2 changes: 2 additions & 0 deletions lxd/db/cluster/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
//go:generate mapper method -i -e instance_snapshot Create references=Config,Device
//go:generate mapper method -i -e instance_snapshot Rename
//go:generate mapper method -i -e instance_snapshot DeleteOne-by-Project-and-Instance-and-Name
//go:generate goimports -w snapshots.mapper.go
//go:generate goimports -w snapshots.interface.mapper.go

// InstanceSnapshot is a value object holding db-related details about a snapshot.
type InstanceSnapshot struct {
Expand Down
4 changes: 3 additions & 1 deletion lxd/db/cluster/update.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cluster

//go:generate lxd-generate db schema cluster
// Have to run goimports first because the .mapper files are being generated at the same time.
// This causes "freshschema" to be unable to import the cluster package.
//go:generate go run ../freshschema/main.go cluster

import (
"context"
Expand Down
2 changes: 2 additions & 0 deletions lxd/db/cluster/warnings.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
//go:generate mapper method -i -e warning DeleteMany-by-EntityType-and-EntityID
//go:generate mapper method -i -e warning ID
//go:generate mapper method -i -e warning Exists struct=Warning
//go:generate goimports -w warnings.mapper.go
//go:generate goimports -w warnings.interface.mapper.go

// Warning is a value object holding db-related details about a warning.
type Warning struct {
Expand Down
18 changes: 16 additions & 2 deletions lxd/db/generate/db/schema.go → lxd/db/freshschema/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
package db
package main

import (
"fmt"
"os"

"github.com/canonical/lxd/lxd/db/cluster"
"github.com/canonical/lxd/lxd/db/node"
)

func main() {
err := freshSchema(os.Args[1:])
if err != nil {
_, _ = os.Stderr.Write([]byte(err.Error()))
os.Exit(1)
}
}

// UpdateSchema updates the schema.go file of the cluster and node databases.
func UpdateSchema(kind string) error {
func freshSchema(args []string) error {
if len(args) < 1 {
return fmt.Errorf(`Schema kind must be provided (must be "node", or "cluster")`)
}

kind := args[0]
var err error
switch kind {
case "node":
Expand Down
17 changes: 0 additions & 17 deletions lxd/db/generate/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func newDb() *cobra.Command {
},
}

cmd.AddCommand(newDbSchema())
cmd.AddCommand(newDbMapper())

// Workaround for subcommand usage errors. See: https://github.com/spf13/cobra/issues/706
Expand All @@ -31,22 +30,6 @@ func newDb() *cobra.Command {
return cmd
}

func newDbSchema() *cobra.Command {
cmd := &cobra.Command{
Use: "schema",
Short: "Generate database schema by applying updates.",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf(`Schema kind must be provided (must be "node", or "cluster")`)
}

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

return cmd
}

func newDbMapper() *cobra.Command {
cmd := &cobra.Command{
Use: "mapper [sub-command]",
Expand Down
2 changes: 1 addition & 1 deletion lxd/db/node/update.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package node

//go:generate lxd-generate db schema node
//go:generate go run ../freshschema/main.go node

import (
"context"
Expand Down
Loading