mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 00:45:19 +00:00
33858d7301
# Checklist for submitter If some of the following don't apply, delete the relevant line. - [ ] Changes file added for user-visible changes in `changes/` or `orbit/changes/`. See [Changes files](https://fleetdm.com/docs/contributing/committing-changes#changes-files) for more information. - [ ] Documented any API changes (docs/Using-Fleet/REST-API.md or docs/Contributing/API-for-contributors.md) - [ ] Documented any permissions changes (docs/Using Fleet/manage-access.md) - [ ] Input data is properly validated, `SELECT *` is avoided, SQL injection is prevented (using placeholders for values in statements) - [ ] Added support on fleet's osquery simulator `cmd/osquery-perf` for new osquery data ingestion features. - [ ] Added/updated tests - [ ] Manual QA for all new/changed functionality - For Orbit and Fleet Desktop changes: - [ ] Manual QA must be performed in the three main OSs, macOS, Windows and Linux. - [ ] Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)). Signed-off-by: guoguangwu <guoguangwu@magic-shield.com>
80 lines
2.0 KiB
Go
80 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"time"
|
|
|
|
"github.com/WatchBeam/clock"
|
|
"github.com/fleetdm/fleet/v4/server/config"
|
|
"github.com/fleetdm/fleet/v4/server/datastore/mysql"
|
|
"github.com/go-kit/kit/log"
|
|
)
|
|
|
|
const (
|
|
testUsername = "root"
|
|
testPassword = "toor"
|
|
testAddress = "localhost:3307"
|
|
)
|
|
|
|
func panicif(err error) {
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
if len(os.Args) != 2 {
|
|
panic("not enough arguments")
|
|
}
|
|
fmt.Println("dumping schema to", os.Args[1])
|
|
|
|
// Create the database (must use raw MySQL client to do this)
|
|
db, err := sql.Open(
|
|
"mysql",
|
|
fmt.Sprintf("%s:%s@tcp(%s)/?multiStatements=true", testUsername, testPassword, testAddress),
|
|
)
|
|
panicif(err)
|
|
defer db.Close()
|
|
_, err = db.Exec("DROP DATABASE IF EXISTS schemadb; CREATE DATABASE schemadb;")
|
|
panicif(err)
|
|
|
|
// Create a datastore client in order to run migrations as usual
|
|
config := config.MysqlConfig{
|
|
Username: testUsername,
|
|
Password: testPassword,
|
|
Address: testAddress,
|
|
Database: "schemadb",
|
|
}
|
|
ds, err := mysql.New(config, clock.NewMockClock(), mysql.Logger(log.NewNopLogger()), mysql.LimitAttempts(1))
|
|
panicif(err)
|
|
defer ds.Close()
|
|
panicif(ds.MigrateTables(context.Background()))
|
|
|
|
// Set created_at/updated_at for migrations and app_config_json to prevent the schema from being changed every time
|
|
// This schema is to test anyway
|
|
fixedDate := time.Date(2020, 01, 01, 01, 01, 01, 01, time.UTC)
|
|
_, err = db.Exec(`USE schemadb`)
|
|
panicif(err)
|
|
_, err = db.Exec(`UPDATE app_config_json SET created_at = ?, updated_at = ?`, fixedDate, fixedDate)
|
|
panicif(err)
|
|
_, err = db.Exec(`UPDATE migration_status_tables SET tstamp = ?`, fixedDate)
|
|
panicif(err)
|
|
|
|
// Dump schema to dumpfile
|
|
cmd := exec.Command(
|
|
"docker-compose", "exec", "-T", "mysql_test",
|
|
// Command run inside container
|
|
"mysqldump", "-u"+testUsername, "-p"+testPassword, "schemadb", "--compact", "--skip-comments",
|
|
)
|
|
var stdoutBuf bytes.Buffer
|
|
cmd.Stdout = &stdoutBuf
|
|
panicif(cmd.Run())
|
|
|
|
panicif(os.WriteFile(os.Args[1], stdoutBuf.Bytes(), 0o655))
|
|
}
|