From a005b7e7b7240a049f30fb0ab615812bef545f96 Mon Sep 17 00:00:00 2001 From: Michal Nicpon <39177923+michalnicp@users.noreply.github.com> Date: Tue, 10 May 2022 09:03:04 -0600 Subject: [PATCH] fix schema dump (#5562) --- ...0210927143116_AddBundleIdentifierColumn.go | 13 --------- .../mysql/migrations/tables/migration.go | 28 ++++++++++++++++++- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/server/datastore/mysql/migrations/tables/20210927143116_AddBundleIdentifierColumn.go b/server/datastore/mysql/migrations/tables/20210927143116_AddBundleIdentifierColumn.go index f4019655d0..34d30a0da3 100644 --- a/server/datastore/mysql/migrations/tables/20210927143116_AddBundleIdentifierColumn.go +++ b/server/datastore/mysql/migrations/tables/20210927143116_AddBundleIdentifierColumn.go @@ -10,19 +10,6 @@ func init() { MigrationClient.AddMigration(Up_20210927143116, Down_20210927143116) } -func columnExists(tx *sql.Tx, table, column string) bool { - var count int - err := tx.QueryRow( - `SELECT count(*) FROM information_schema.columns WHERE COLUMN_NAME = ? AND table_name = ? LIMIT 1;`, - column, table, - ).Scan(&count) - if err != nil { - return false - } - - return count == 1 -} - func Up_20210927143116(tx *sql.Tx) error { if columnExists(tx, "software", "bundle_identifier") { return nil diff --git a/server/datastore/mysql/migrations/tables/migration.go b/server/datastore/mysql/migrations/tables/migration.go index ccd315f353..cbcf1a99bb 100644 --- a/server/datastore/mysql/migrations/tables/migration.go +++ b/server/datastore/mysql/migrations/tables/migration.go @@ -1,7 +1,33 @@ package tables -import "github.com/fleetdm/goose" +import ( + "database/sql" + + "github.com/fleetdm/goose" +) var ( MigrationClient = goose.New("migration_status_tables", goose.MySqlDialect{}) ) + +func columnExists(tx *sql.Tx, table, column string) bool { + var count int + err := tx.QueryRow( + ` +SELECT + count(*) +FROM + information_schema.columns +WHERE + TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = ? + AND COLUMN_NAME = ? +`, + table, column, + ).Scan(&count) + if err != nil { + return false + } + + return count > 0 +}