Skip to content

Commit f72ae1b

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 f72ae1b

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

mysql.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ CREATE TABLE IF NOT EXISTS users (
5050
statement.Exec()
5151
statement, _ = db.Prepare("CREATE UNIQUE INDEX idx_user_name on users(name)")
5252
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)")
53+
statement, _ = db.Prepare("CREATE TABLE IF NOT EXISTS usergroups (id INTEGER AUTO_INCREMENT PRIMARY KEY, name VARCHAR(64) NOT NULL, gidnumber INTEGER NOT NULL)")
5454
statement.Exec()
55-
statement, _ = db.Prepare("CREATE UNIQUE INDEX idx_group_name on `groups`(name)")
55+
statement, _ = db.Prepare("CREATE UNIQUE INDEX idx_group_name on usergroups(name)")
5656
statement.Exec()
5757
statement, _ = db.Prepare("CREATE TABLE IF NOT EXISTS includegroups (id INTEGER AUTO_INCREMENT PRIMARY KEY, parentgroupid INTEGER NOT NULL, includegroupid INTEGER NOT NULL)")
5858
statement.Exec()
@@ -66,4 +66,24 @@ func (b MysqlBackend) MigrateSchema(db *sql.DB, checker func(*sql.DB, string) bo
6666
statement, _ := db.Prepare("ALTER TABLE users ADD COLUMN sshkeys TEXT DEFAULT ('')")
6767
statement.Exec()
6868
}
69+
70+
if TableExists(db, "`groups`") {
71+
// Drop the table created during schema creation
72+
statement, _ := db.Prepare("DROP TABLE usergroups")
73+
statement.Exec()
74+
75+
statement, _ = db.Prepare("ALTER TABLE `groups` RENAME usergroups")
76+
statement.Exec()
77+
}
78+
}
79+
80+
// Indicates whether the table exists or not
81+
func TableExists(db *sql.DB, tableName string) bool {
82+
var found string
83+
err := db.QueryRow(fmt.Sprintf("SELECT COUNT(id) FROM %s", tableName)).Scan(
84+
&found)
85+
if err != nil {
86+
return false
87+
}
88+
return true
6989
}

0 commit comments

Comments
 (0)