Skip to content

Commit

Permalink
Merge pull request #73 from gochan-org/mysql-compatibility
Browse files Browse the repository at this point in the history
Fix compatibility with mainline MySQL
  • Loading branch information
Eggbertx authored Mar 28, 2023
2 parents 424cb2d + e14e1f1 commit 5de1d61
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 49 deletions.
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gochan.js",
"version": "3.5.0",
"version": "3.5.1",
"description": "",
"type": "module",
"source": "js/gochan.js",
Expand Down
2 changes: 1 addition & 1 deletion html/error/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<h1>404: File not found</h1>
<img src="./lol 404.gif" border="0" alt="">
<p>The requested file could not be found on this server.</p>
<hr/>Site powered by <a href="https://github.com/gochan-org/gochan" target="_blank">Gochan</a> v3.5.0
<hr/>Site powered by <a href="https://github.com/gochan-org/gochan" target="_blank">Gochan</a> v3.5.1
</body>
</html>
2 changes: 1 addition & 1 deletion html/error/500.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<h1>Error 500: Internal Server error</h1>
<img src="./server500.gif" border="0" alt="">
<p>The server encountered an error while trying to serve the page, and we apologize for the inconvenience. The <a href="https://en.wikipedia.org/wiki/Idiot">system administrator</a> will try to fix things as soon they get around to it, whenever that is. Hopefully soon.</p>
<hr/>Site powered by <a href="https://github.com/gochan-org/gochan" target="_blank">Gochan</a> v3.5.0
<hr/>Site powered by <a href="https://github.com/gochan-org/gochan" target="_blank">Gochan</a> v3.5.1
</body>
</html>
2 changes: 1 addition & 1 deletion html/error/502.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<h1>Error 502: Bad gateway</h1>
<img src="./server500.gif" border="0" alt="">
<p>The server encountered an error while trying to serve the page, and we apologize for the inconvenience. The <a href="https://en.wikipedia.org/wiki/Idiot">system administrator</a> will try to fix things as soon they get around to it, whenever that is. Hopefully soon.</p>
<hr/>Site powered by <a href="https://github.com/gochan-org/gochan" target="_blank">Gochan</a> v3.5.0
<hr/>Site powered by <a href="https://github.com/gochan-org/gochan" target="_blank">Gochan</a> v3.5.1
</body>
</html>
93 changes: 77 additions & 16 deletions pkg/gcsql/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"os"
"regexp"
"strings"

"github.com/gochan-org/gochan/pkg/config"
)

var (
Expand All @@ -27,15 +29,20 @@ func RunSQLFile(path string) error {
sqlStr := regexp.MustCompile("--.*\n?").ReplaceAllString(string(sqlBytes), " ")
sqlArr := strings.Split(gcdb.replacer.Replace(sqlStr), ";")

tx, err := BeginTx()
if err != nil {
return err
}
defer tx.Rollback()
for _, statement := range sqlArr {
statement = strings.Trim(statement, " \n\r\t")
if len(statement) > 0 {
if _, err = gcdb.db.Exec(statement); err != nil {
if _, err = ExecTxSQL(tx, statement); err != nil {
return err
}
}
}
return nil
return tx.Commit()
}

// TODO: get gochan-migration working so this doesn't have to sit here
Expand All @@ -46,33 +53,87 @@ func tmpSqlAdjust() error {
switch gcdb.driver {
case "mysql":
query = `SELECT COUNT(*) FROM information_schema.TABLE_CONSTRAINTS
WHERE CONSTRAINT_NAME = 'wordfilters_staff_id_fk'
WHERE CONSTRAINT_NAME = 'wordfilters_board_id_fk'
AND TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'DBPREFIXwordfilters'`
numConstraints := 3
var numConstraints int
if err = gcdb.QueryRowSQL(query,
interfaceSlice(),
interfaceSlice(&numConstraints)); err != nil {
return err
}
if numConstraints > 0 {
query = `ALTER TABLE DBPREFIXwordfilters DROP FOREIGN KEY IF EXISTS wordfilters_board_id_fk`
query = `ALTER TABLE DBPREFIXwordfilters DROP FOREIGN KEY wordfilters_board_id_fk`
} else {
query = ""
}
query = `SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'DBPREFIXwordfilters'
AND COLUMN_NAME = 'board_dirs'`
var numColumns int
if err = gcdb.QueryRowSQL(query,
interfaceSlice(),
interfaceSlice(&numColumns)); err != nil {
return err
}
if numColumns == 0 {
query = `ALTER TABLE DBPREFIXwordfilters ADD COLUMN board_dirs varchar(255) DEFAULT '*'`
if _, err = ExecSQL(query); err != nil {
return err
}
}

// Yay, collation! Everybody loves MySQL's default collation!
criticalConfig := config.GetSystemCriticalConfig()
query = `ALTER DATABASE ` + criticalConfig.DBname + ` CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci`
if _, err = gcdb.db.Exec(query); err != nil {
return err
}

query = `SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = ?`
rows, err := QuerySQL(query, criticalConfig.DBname)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var tableName string
err = rows.Scan(&tableName)
if err != nil {
return err
}
query = `ALTER TABLE ` + tableName + ` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci`
if _, err = gcdb.db.Exec(query); err != nil {
return err
}
}
err = nil
case "postgres":
query = `ALTER TABLE DBPREFIXwordfilters DROP CONSTRAINT IF EXISTS board_id_fk`
_, err = ExecSQL(`ALTER TABLE DBPREFIXwordfilters DROP CONSTRAINT IF EXISTS board_id_fk`)
if err != nil {
return err
}
query = `ALTER TABLE DBPREFIXwordfilters ADD COLUMN IF NOT EXISTS board_dirs varchar(255) DEFAULT '*'`
if _, err = ExecSQL(query); err != nil {
return err
}
case "sqlite3":
_, err = ExecSQL(`PRAGMA foreign_keys = ON`)
return err
}
if _, err = gcdb.ExecSQL(query); err != nil {
return err
}
query = `ALTER TABLE DBPREFIXwordfilters DROP COLUMN IF EXISTS board_id`
if _, err = gcdb.ExecSQL(query); err != nil {
return err
if err != nil {
return err
}
query = `SELECT COUNT(*) FROM PRAGMA_TABLE_INFO('DBPREFIXwordfilters') WHERE name = 'board_dirs'`
var numColumns int
if err = QueryRowSQL(query, interfaceSlice(), interfaceSlice(&numColumns)); err != nil {
return err
}
if numColumns == 0 {
query = `ALTER TABLE DBPREFIXwordfilters ADD COLUMN board_dirs varchar(255) DEFAULT '*'`
if _, err = ExecSQL(query); err != nil {
return err
}
}
}
query = `ALTER TABLE DBPREFIXwordfilters ADD COLUMN IF NOT EXISTS board_dirs varchar(255) DEFAULT '*'`
_, err = gcdb.ExecSQL(query)

return err
}
2 changes: 1 addition & 1 deletion pkg/gcsql/provisioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func CheckAndInitializeDatabase(dbType string) error {
func buildNewDatabase(dbType string) error {
var err error
if err = initDB("initdb_" + dbType + ".sql"); err != nil {
return errors.New("database initialization failed: " + err.Error())
return err
}
if err = createDefaultAdminIfNoStaff(); err != nil {
return errors.New("failed creating default admin account: " + err.Error())
Expand Down
12 changes: 6 additions & 6 deletions sql/initdb_master.sql
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ CREATE TABLE DBPREFIXstaff(
CREATE TABLE DBPREFIXsessions(
id {serial pk},
staff_id {fk to serial} NOT NULL,
expires TIMESTAMP NOT NULL,
expires TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
data VARCHAR(45) NOT NULL,
CONSTRAINT sessions_staff_id_fk FOREIGN KEY(staff_id) REFERENCES DBPREFIXstaff(id) ON DELETE CASCADE
);
Expand Down Expand Up @@ -147,9 +147,9 @@ CREATE TABLE DBPREFIXip_ban(
is_thread_ban BOOL NOT NULL,
is_active BOOL NOT NULL,
ip VARCHAR(45) NOT NULL,
issued_at TIMESTAMP NOT NULL,
appeal_at TIMESTAMP NOT NULL,
expires_at TIMESTAMP NOT NULL,
issued_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
appeal_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
permanent BOOL NOT NULL,
staff_note VARCHAR(255) NOT NULL,
message TEXT NOT NULL,
Expand All @@ -165,8 +165,8 @@ CREATE TABLE DBPREFIXip_ban_audit(
staff_id {fk to serial} NOT NULL,
is_active BOOL NOT NULL,
is_thread_ban BOOL NOT NULL,
expires_at TIMESTAMP NOT NULL,
appeal_at TIMESTAMP NOT NULL,
expires_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
appeal_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
permanent BOOL NOT NULL,
staff_note VARCHAR(255) NOT NULL,
message TEXT NOT NULL,
Expand Down
12 changes: 6 additions & 6 deletions sql/initdb_mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ CREATE TABLE DBPREFIXstaff(
CREATE TABLE DBPREFIXsessions(
id BIGINT NOT NULL AUTO_INCREMENT UNIQUE PRIMARY KEY,
staff_id BIGINT NOT NULL,
expires TIMESTAMP NOT NULL,
expires TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
data VARCHAR(45) NOT NULL,
CONSTRAINT sessions_staff_id_fk FOREIGN KEY(staff_id) REFERENCES DBPREFIXstaff(id) ON DELETE CASCADE
);
Expand Down Expand Up @@ -147,9 +147,9 @@ CREATE TABLE DBPREFIXip_ban(
is_thread_ban BOOL NOT NULL,
is_active BOOL NOT NULL,
ip VARCHAR(45) NOT NULL,
issued_at TIMESTAMP NOT NULL,
appeal_at TIMESTAMP NOT NULL,
expires_at TIMESTAMP NOT NULL,
issued_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
appeal_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
permanent BOOL NOT NULL,
staff_note VARCHAR(255) NOT NULL,
message TEXT NOT NULL,
Expand All @@ -165,8 +165,8 @@ CREATE TABLE DBPREFIXip_ban_audit(
staff_id BIGINT NOT NULL,
is_active BOOL NOT NULL,
is_thread_ban BOOL NOT NULL,
expires_at TIMESTAMP NOT NULL,
appeal_at TIMESTAMP NOT NULL,
expires_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
appeal_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
permanent BOOL NOT NULL,
staff_note VARCHAR(255) NOT NULL,
message TEXT NOT NULL,
Expand Down
12 changes: 6 additions & 6 deletions sql/initdb_postgres.sql
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ CREATE TABLE DBPREFIXstaff(
CREATE TABLE DBPREFIXsessions(
id BIGSERIAL PRIMARY KEY,
staff_id BIGINT NOT NULL,
expires TIMESTAMP NOT NULL,
expires TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
data VARCHAR(45) NOT NULL,
CONSTRAINT sessions_staff_id_fk FOREIGN KEY(staff_id) REFERENCES DBPREFIXstaff(id) ON DELETE CASCADE
);
Expand Down Expand Up @@ -147,9 +147,9 @@ CREATE TABLE DBPREFIXip_ban(
is_thread_ban BOOL NOT NULL,
is_active BOOL NOT NULL,
ip VARCHAR(45) NOT NULL,
issued_at TIMESTAMP NOT NULL,
appeal_at TIMESTAMP NOT NULL,
expires_at TIMESTAMP NOT NULL,
issued_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
appeal_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
permanent BOOL NOT NULL,
staff_note VARCHAR(255) NOT NULL,
message TEXT NOT NULL,
Expand All @@ -165,8 +165,8 @@ CREATE TABLE DBPREFIXip_ban_audit(
staff_id BIGINT NOT NULL,
is_active BOOL NOT NULL,
is_thread_ban BOOL NOT NULL,
expires_at TIMESTAMP NOT NULL,
appeal_at TIMESTAMP NOT NULL,
expires_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
appeal_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
permanent BOOL NOT NULL,
staff_note VARCHAR(255) NOT NULL,
message TEXT NOT NULL,
Expand Down
12 changes: 6 additions & 6 deletions sql/initdb_sqlite3.sql
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ CREATE TABLE DBPREFIXstaff(
CREATE TABLE DBPREFIXsessions(
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
staff_id BIGINT NOT NULL,
expires TIMESTAMP NOT NULL,
expires TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
data VARCHAR(45) NOT NULL,
CONSTRAINT sessions_staff_id_fk FOREIGN KEY(staff_id) REFERENCES DBPREFIXstaff(id) ON DELETE CASCADE
);
Expand Down Expand Up @@ -147,9 +147,9 @@ CREATE TABLE DBPREFIXip_ban(
is_thread_ban BOOL NOT NULL,
is_active BOOL NOT NULL,
ip VARCHAR(45) NOT NULL,
issued_at TIMESTAMP NOT NULL,
appeal_at TIMESTAMP NOT NULL,
expires_at TIMESTAMP NOT NULL,
issued_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
appeal_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
permanent BOOL NOT NULL,
staff_note VARCHAR(255) NOT NULL,
message TEXT NOT NULL,
Expand All @@ -165,8 +165,8 @@ CREATE TABLE DBPREFIXip_ban_audit(
staff_id BIGINT NOT NULL,
is_active BOOL NOT NULL,
is_thread_ban BOOL NOT NULL,
expires_at TIMESTAMP NOT NULL,
appeal_at TIMESTAMP NOT NULL,
expires_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
appeal_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
permanent BOOL NOT NULL,
staff_note VARCHAR(255) NOT NULL,
message TEXT NOT NULL,
Expand Down
1 change: 1 addition & 0 deletions vagrant/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Vagrant.configure("2") do |config|
config.vm.provision :shell, path: "bootstrap.sh", env: {
:DBTYPE => DBTYPE,
:GOPATH => "/home/vagrant/go",
:MYSQL_MAINLINE => ENV.fetch("GC_MYSQL_MAINLINE", ""),
:FROMDOCKER => ""
}, args: "install"
end
17 changes: 14 additions & 3 deletions vagrant/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,27 @@ apt-get -y update && apt-get -y upgrade

if [ "$DBTYPE" == "mysql" ]; then
# Using MySQL (stable)
apt-get -y install mariadb-server mariadb-client
if [ "$MYSQL_MAINLINE" == "1" ]; then
echo "using mainline MySQL instead of MariaDB"
apt-get -y install mysql-server mysql-client
else
echo "using MariaDB fork of MySQL (default)"
apt-get -y install mariadb-server mariadb-client
fi
mysql -uroot <<- EOF
CREATE DATABASE IF NOT EXISTS gochan;
GRANT USAGE ON *.* TO gochan IDENTIFIED BY 'gochan'; \
GRANT ALL PRIVILEGES ON gochan.* TO gochan; \
SET PASSWORD FOR 'gochan'@'%' = PASSWORD('gochan');
FLUSH PRIVILEGES;
EOF
systemctl enable mariadb
systemctl start mariadb &
if [ "$MYSQL_MAINLINE" == "1" ]; then
systemctl enable mysql
systemctl start mysql &
else
systemctl enable mariadb
systemctl start mariadb &
fi
wait
if [ -d /lib/systemd ]; then
cp /vagrant/sample-configs/gochan-mysql.service /lib/systemd/system/gochan.service
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.5.0
3.5.1

0 comments on commit 5de1d61

Please sign in to comment.