Skip to content

Commit e1fd6ad

Browse files
committed
Update the schema to rename groups to usergroups
Update the schema to rename groups to usergroups. This allows for the table name and means to access it to be consistent across all databases. This is due to mysql reserving the name groups as a keyword. Signed-off-by: Billy Olsen <[email protected]>
1 parent 6df28e6 commit e1fd6ad

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

mysql.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"database/sql"
5+
"fmt"
56

67
_ "github.com/go-sql-driver/mysql"
78

@@ -50,9 +51,9 @@ CREATE TABLE IF NOT EXISTS users (
5051
statement.Exec()
5152
statement, _ = db.Prepare("CREATE UNIQUE INDEX idx_user_name on users(name)")
5253
statement.Exec()
53-
statement, _ = db.Prepare("CREATE TABLE IF NOT EXISTS `groups` (id INTEGER AUTO_INCREMENT PRIMARY KEY, name VARCHAR(64) NOT NULL, gidnumber INTEGER NOT NULL)")
54+
statement, _ = db.Prepare("CREATE TABLE IF NOT EXISTS usergroups (id INTEGER AUTO_INCREMENT PRIMARY KEY, name VARCHAR(64) NOT NULL, gidnumber INTEGER NOT NULL)")
5455
statement.Exec()
55-
statement, _ = db.Prepare("CREATE UNIQUE INDEX idx_group_name on `groups`(name)")
56+
statement, _ = db.Prepare("CREATE UNIQUE INDEX idx_group_name on usergroups(name)")
5657
statement.Exec()
5758
statement, _ = db.Prepare("CREATE TABLE IF NOT EXISTS includegroups (id INTEGER AUTO_INCREMENT PRIMARY KEY, parentgroupid INTEGER NOT NULL, includegroupid INTEGER NOT NULL)")
5859
statement.Exec()
@@ -66,4 +67,24 @@ func (b MysqlBackend) MigrateSchema(db *sql.DB, checker func(*sql.DB, string) bo
6667
statement, _ := db.Prepare("ALTER TABLE users ADD COLUMN sshkeys TEXT DEFAULT ('')")
6768
statement.Exec()
6869
}
70+
71+
if TableExists(db, "`groups`") {
72+
// Drop the table created during schema creation
73+
statement, _ := db.Prepare("DROP TABLE usergroups")
74+
statement.Exec()
75+
76+
statement, _ = db.Prepare("ALTER TABLE `groups` RENAME usergroups")
77+
statement.Exec()
78+
}
79+
}
80+
81+
// Indicates whether the table exists or not
82+
func TableExists(db *sql.DB, tableName string) bool {
83+
var found string
84+
err := db.QueryRow(fmt.Sprintf("SELECT COUNT(id) FROM %s", tableName)).Scan(
85+
&found)
86+
if err != nil {
87+
return false
88+
}
89+
return true
6990
}

0 commit comments

Comments
 (0)