mirror of
https://github.com/fleetdm/fleet
synced 2026-05-24 09:28:54 +00:00
This PR separates the table migrations from the data population migrations. Table migrations run before data migrations. Now, we have the ability to create the database tables without populating them with data. This can be useful for running "unit" tests against a MySQL store that doesn't have any pre-populated data. When performing real migrations, or for more "integration" style testing, the data migrations can also be executed. Note there are some special cases that must be observed with these migrations, and the README is updated to reflect those.
96 lines
2.3 KiB
Go
96 lines
2.3 KiB
Go
package cli
|
|
|
|
import (
|
|
"github.com/WatchBeam/clock"
|
|
kitlog "github.com/go-kit/kit/log"
|
|
"github.com/kolide/kolide-ose/server/config"
|
|
"github.com/kolide/kolide-ose/server/datastore/mysql"
|
|
"github.com/kolide/kolide-ose/server/kolide"
|
|
"github.com/kolide/kolide-ose/server/pubsub"
|
|
"github.com/kolide/kolide-ose/server/service"
|
|
"github.com/spf13/cobra"
|
|
"golang.org/x/net/context"
|
|
)
|
|
|
|
func createPrepareCmd(configManager config.Manager) *cobra.Command {
|
|
|
|
var prepareCmd = &cobra.Command{
|
|
Use: "prepare",
|
|
Short: "Subcommands for initializing kolide infrastructure",
|
|
Long: `
|
|
Subcommands for initializing kolide infrastructure
|
|
|
|
To setup kolide infrastructure, use one of the available commands.
|
|
`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
cmd.Help()
|
|
},
|
|
}
|
|
|
|
var dbCmd = &cobra.Command{
|
|
Use: "db",
|
|
Short: "Given correct database configurations, prepare the databases for use",
|
|
Long: ``,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
config := configManager.LoadConfig()
|
|
ds, err := mysql.New(config.Mysql, clock.C)
|
|
if err != nil {
|
|
initFatal(err, "creating db connection")
|
|
}
|
|
|
|
if err := ds.MigrateTables(); err != nil {
|
|
initFatal(err, "migrating db schema")
|
|
}
|
|
|
|
if err := ds.MigrateData(); err != nil {
|
|
initFatal(err, "migrating builtin data")
|
|
}
|
|
},
|
|
}
|
|
|
|
prepareCmd.AddCommand(dbCmd)
|
|
|
|
var testDataCmd = &cobra.Command{
|
|
Use: "test-data",
|
|
Short: "Generate test data",
|
|
Long: ``,
|
|
Run: func(cmd *cobra.Command, arg []string) {
|
|
config := configManager.LoadConfig()
|
|
ds, err := mysql.New(config.Mysql, clock.C)
|
|
if err != nil {
|
|
initFatal(err, "creating db connection")
|
|
}
|
|
|
|
var (
|
|
name = "admin"
|
|
username = "admin"
|
|
password = "secret"
|
|
email = "admin@kolide.co"
|
|
enabled = true
|
|
isAdmin = true
|
|
)
|
|
admin := kolide.UserPayload{
|
|
Name: &name,
|
|
Username: &username,
|
|
Password: &password,
|
|
Email: &email,
|
|
Enabled: &enabled,
|
|
Admin: &isAdmin,
|
|
}
|
|
svc, err := service.NewService(ds, pubsub.NewInmemQueryResults(), kitlog.NewNopLogger(), config, nil, clock.C)
|
|
if err != nil {
|
|
initFatal(err, "creating service")
|
|
}
|
|
|
|
_, err = svc.NewAdminCreatedUser(context.Background(), admin)
|
|
if err != nil {
|
|
initFatal(err, "saving new user")
|
|
}
|
|
},
|
|
}
|
|
|
|
prepareCmd.AddCommand(testDataCmd)
|
|
|
|
return prepareCmd
|
|
|
|
}
|