From 88541ba728634bd0bc1484ac3c190ae1c00ae4b8 Mon Sep 17 00:00:00 2001 From: Clayton Rosenthal Date: Wed, 31 May 2023 19:18:46 -0700 Subject: [PATCH 1/2] add check for db and table not tested, just drafting concept for now --- mysql/mysql.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/mysql/mysql.go b/mysql/mysql.go index 0273491..19b17e6 100644 --- a/mysql/mysql.go +++ b/mysql/mysql.go @@ -44,9 +44,16 @@ func (db *DB) Open(dataSourceName string, opt ...database.Option) error { if err != nil { return errors.Wrap(err, "error connecting to mysql") } - _, err = _db.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s`", opts.Database)) + _db_check, err = _db.Exec(fmt.Sprintf("SHOW DATABASES LIKE `%s`", opts.Database)) if err != nil { - return errors.Wrapf(err, "error creating database %s (if not exists)", opts.Database) + return errors.Wrapf(err, "error checking if database %s exists", opts.Database) + } + // db doesn't exist, create it + if len(_db_check.allAffectedRows()) == 0 { + _, err = _db.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s`", opts.Database)) + if err != nil { + return errors.Wrapf(err, "error creating database %s (if not exists)", opts.Database) + } } parsedDSN.DBName = opts.Database db.db, err = sql.Open("mysql", parsedDSN.FormatDSN()) @@ -82,6 +89,10 @@ func createTableQry(bucket []byte) string { return fmt.Sprintf("CREATE TABLE IF NOT EXISTS `%s`(nkey VARBINARY(255), nvalue BLOB, PRIMARY KEY (nkey));", bucket) } +func checkTableQry(bucket []byte) string { + return fmt.Sprintf("SHOW TABLES LIKE `%s`;", bucket) +} + func deleteTableQry(bucket []byte) string { return fmt.Sprintf("DROP TABLE `%s`", bucket) } @@ -261,9 +272,15 @@ func (db *DB) Update(tx *database.Tx) error { // CreateTable creates a table in the database. func (db *DB) CreateTable(bucket []byte) error { - _, err := db.db.Exec(createTableQry(bucket)) + _check, err := db.db.Exec(checkTableQry(bucket)) if err != nil { - return errors.Wrapf(err, "failed to create table %s", bucket) + return errors.Wrapf(err, "failed to check table %s", bucket) + } + if len(_check.allAffectedRows()) == 0 { + _, err := db.db.Exec(createTableQry(bucket)) + if err != nil { + return errors.Wrapf(err, "failed to create table %s", bucket) + } } return nil } From 62544802608f27e68259cbae6afc7082b0079377 Mon Sep 17 00:00:00 2001 From: Clayton Rosenthal Date: Fri, 2 Jun 2023 15:27:31 -0700 Subject: [PATCH 2/2] fixing check --- mysql/mysql.go | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/mysql/mysql.go b/mysql/mysql.go index 19b17e6..0573198 100644 --- a/mysql/mysql.go +++ b/mysql/mysql.go @@ -44,17 +44,18 @@ func (db *DB) Open(dataSourceName string, opt ...database.Option) error { if err != nil { return errors.Wrap(err, "error connecting to mysql") } - _db_check, err = _db.Exec(fmt.Sprintf("SHOW DATABASES LIKE `%s`", opts.Database)) - if err != nil { - return errors.Wrapf(err, "error checking if database %s exists", opts.Database) - } - // db doesn't exist, create it - if len(_db_check.allAffectedRows()) == 0 { - _, err = _db.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s`", opts.Database)) - if err != nil { - return errors.Wrapf(err, "error creating database %s (if not exists)", opts.Database) - } + _db_check, err := _db.Query(fmt.Sprintf("SHOW DATABASES LIKE `%s`", opts.Database)) + if err != nil { + return errors.Wrapf(err, "error checking if database %s exists", opts.Database) + } + // db doesn't exist, create it + if !_db_check.Next() { + _, err = _db.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s`", opts.Database)) + if err != nil { + return errors.Wrapf(err, "error creating database %s (if not exists)", opts.Database) + } } + parsedDSN.DBName = opts.Database db.db, err = sql.Open("mysql", parsedDSN.FormatDSN()) if err != nil { @@ -270,19 +271,20 @@ func (db *DB) Update(tx *database.Tx) error { return nil } -// CreateTable creates a table in the database. -func (db *DB) CreateTable(bucket []byte) error { - _check, err := db.db.Exec(checkTableQry(bucket)) +// CreateTable creates a table in the database. +func (db *DB) CreateTable(bucket []byte) error { + _check, err := db.db.Query(checkTableQry(bucket)) if err != nil { - return errors.Wrapf(err, "failed to check table %s", bucket) - } - if len(_check.allAffectedRows()) == 0 { - _, err := db.db.Exec(createTableQry(bucket)) + return errors.Wrapf(err, "failed to check table %s", bucket) + } + // table doesn't exist, create it + if !_check.Next() { + _, err := db.db.Exec(createTableQry(bucket)) if err != nil { return errors.Wrapf(err, "failed to create table %s", bucket) } - } - return nil + } + return nil } // DeleteTable deletes a table in the database.