From 01faaa992fb2814ab72e0d3ed40266fb40c478bd Mon Sep 17 00:00:00 2001 From: Eggbertx Date: Wed, 9 Apr 2025 22:29:59 -0700 Subject: [PATCH] Add spoilered column to threads table and improve initdb scripts to remove comments and properly get sql directory relative to script directory --- sql/initdb_master.sql | 2 +- sql/initdb_mysql.sql | 10 +--------- sql/initdb_postgres.sql | 10 +--------- sql/initdb_sqlite3.sql | 10 +--------- tools/build_initdb.py | 40 ++++++++++++++++++++++++++-------------- 5 files changed, 30 insertions(+), 42 deletions(-) diff --git a/sql/initdb_master.sql b/sql/initdb_master.sql index 43f4d273..90a327b6 100644 --- a/sql/initdb_master.sql +++ b/sql/initdb_master.sql @@ -4,7 +4,6 @@ -- Versioning numbering goes by whole numbers. Upgrade script migrate existing databases between versions -- Foreign and unique constraints must be named so they can be dropped. -- MySQL requires constraint names to be unique globally, hence the long constraint names. --- Database version: 1 CREATE TABLE DBPREFIXdatabase_version( component VARCHAR(40) NOT NULL PRIMARY KEY, @@ -56,6 +55,7 @@ CREATE TABLE DBPREFIXthreads( stickied BOOL NOT NULL DEFAULT FALSE, anchored BOOL NOT NULL DEFAULT FALSE, cyclical BOOL NOT NULL DEFAULT FALSE, + spoilered BOOL NOT NULL DEFAULT FALSE, last_bump TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, deleted_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, is_deleted BOOL NOT NULL DEFAULT FALSE, diff --git a/sql/initdb_mysql.sql b/sql/initdb_mysql.sql index 709def13..cfa2d59c 100644 --- a/sql/initdb_mysql.sql +++ b/sql/initdb_mysql.sql @@ -1,11 +1,3 @@ --- Gochan master template for new database script --- Contains macros in the form [curlybrace open]macro text[curlybrace close] --- Macros are substituted by build_initdb.py to the supported database files. Must not contain extra spaces --- Versioning numbering goes by whole numbers. Upgrade script migrate existing databases between versions --- Foreign and unique constraints must be named so they can be dropped. --- MySQL requires constraint names to be unique globally, hence the long constraint names. --- Database version: 1 - CREATE TABLE DBPREFIXdatabase_version( component VARCHAR(40) NOT NULL PRIMARY KEY, version INT NOT NULL @@ -56,6 +48,7 @@ CREATE TABLE DBPREFIXthreads( stickied BOOL NOT NULL DEFAULT FALSE, anchored BOOL NOT NULL DEFAULT FALSE, cyclical BOOL NOT NULL DEFAULT FALSE, + spoilered BOOL NOT NULL DEFAULT FALSE, last_bump TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, deleted_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, is_deleted BOOL NOT NULL DEFAULT FALSE, @@ -293,6 +286,5 @@ CREATE TABLE DBPREFIXfilter_hits( ON DELETE CASCADE ); - INSERT INTO DBPREFIXdatabase_version(component, version) VALUES('gochan', 4); diff --git a/sql/initdb_postgres.sql b/sql/initdb_postgres.sql index 4de193c2..b1db2a99 100644 --- a/sql/initdb_postgres.sql +++ b/sql/initdb_postgres.sql @@ -1,11 +1,3 @@ --- Gochan master template for new database script --- Contains macros in the form [curlybrace open]macro text[curlybrace close] --- Macros are substituted by build_initdb.py to the supported database files. Must not contain extra spaces --- Versioning numbering goes by whole numbers. Upgrade script migrate existing databases between versions --- Foreign and unique constraints must be named so they can be dropped. --- MySQL requires constraint names to be unique globally, hence the long constraint names. --- Database version: 1 - CREATE TABLE DBPREFIXdatabase_version( component VARCHAR(40) NOT NULL PRIMARY KEY, version INT NOT NULL @@ -56,6 +48,7 @@ CREATE TABLE DBPREFIXthreads( stickied BOOL NOT NULL DEFAULT FALSE, anchored BOOL NOT NULL DEFAULT FALSE, cyclical BOOL NOT NULL DEFAULT FALSE, + spoilered BOOL NOT NULL DEFAULT FALSE, last_bump TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, deleted_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, is_deleted BOOL NOT NULL DEFAULT FALSE, @@ -293,6 +286,5 @@ CREATE TABLE DBPREFIXfilter_hits( ON DELETE CASCADE ); - INSERT INTO DBPREFIXdatabase_version(component, version) VALUES('gochan', 4); diff --git a/sql/initdb_sqlite3.sql b/sql/initdb_sqlite3.sql index 234dbce5..f470ebc2 100644 --- a/sql/initdb_sqlite3.sql +++ b/sql/initdb_sqlite3.sql @@ -1,11 +1,3 @@ --- Gochan master template for new database script --- Contains macros in the form [curlybrace open]macro text[curlybrace close] --- Macros are substituted by build_initdb.py to the supported database files. Must not contain extra spaces --- Versioning numbering goes by whole numbers. Upgrade script migrate existing databases between versions --- Foreign and unique constraints must be named so they can be dropped. --- MySQL requires constraint names to be unique globally, hence the long constraint names. --- Database version: 1 - CREATE TABLE DBPREFIXdatabase_version( component VARCHAR(40) NOT NULL PRIMARY KEY, version INT NOT NULL @@ -56,6 +48,7 @@ CREATE TABLE DBPREFIXthreads( stickied BOOL NOT NULL DEFAULT FALSE, anchored BOOL NOT NULL DEFAULT FALSE, cyclical BOOL NOT NULL DEFAULT FALSE, + spoilered BOOL NOT NULL DEFAULT FALSE, last_bump TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, deleted_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, is_deleted BOOL NOT NULL DEFAULT FALSE, @@ -293,6 +286,5 @@ CREATE TABLE DBPREFIXfilter_hits( ON DELETE CASCADE ); - INSERT INTO DBPREFIXdatabase_version(component, version) VALUES('gochan', 4); diff --git a/tools/build_initdb.py b/tools/build_initdb.py index aad03b88..322f2c95 100755 --- a/tools/build_initdb.py +++ b/tools/build_initdb.py @@ -2,6 +2,7 @@ import argparse from os import path +import re class macro(): """ Use a macro like this {exact macro name} """ @@ -38,6 +39,17 @@ def compileOutIfs(text, flag): return newText +def compileOutComments(text:str): + lines = text.splitlines() + out = "" + for line in lines: + if line != "" and line is not None: + out += re.sub(r"--.*$", "", line) + "\n" + if line.endswith(";"): + out += "\n" + return out.strip() + "\n" + + def hasError(text): if '{' in text or '}' in text: return True @@ -45,14 +57,14 @@ def hasError(text): def dofile(filestart): - print("building " + filestart + "master.sql file") + print(f"processing {filestart}master.sql file") masterfile = "" - with open(filestart + "master.sql", 'r') as masterfileIn: # skipcq: PTC-W6004 + with open(f"{filestart}master.sql", 'r') as masterfileIn: # skipcq: PTC-W6004 masterfile = masterfileIn.read() - postgresProcessed = compileOutIfs(masterfile, "POSTGRES") - mysqlProcessed = compileOutIfs(masterfile, "MYSQL") - sqlite3Processed = compileOutIfs(masterfile, "SQLITE3") + postgresProcessed = compileOutComments(compileOutIfs(masterfile, "POSTGRES")) + mysqlProcessed = compileOutComments(compileOutIfs(masterfile, "MYSQL")) + sqlite3Processed = compileOutComments(compileOutIfs(masterfile, "SQLITE3")) for item in macros: macroCode = "{" + item.macroname + "}" @@ -60,30 +72,30 @@ def dofile(filestart): mysqlProcessed = mysqlProcessed.replace(macroCode, item.mysql) sqlite3Processed = sqlite3Processed.replace(macroCode, item.sqlite3) - error = hasError(postgresProcessed) - error = error or hasError(mysqlProcessed) + if hasError(postgresProcessed) or hasError(mysqlProcessed): + raise Exception("Files still contain curly braces after macro processing") + print(f"building {filestart}postgres.sql") with open(filestart + "postgres.sql", 'w') as i: i.write(postgresProcessed) + print(f"building {filestart}mysql.sql") with open(filestart + "sqlite3.sql", 'w') as i: i.write(sqlite3Processed) + print(f"building {filestart}sqlite3.sql") with open(filestart + "mysql.sql", 'w') as i: i.write(mysqlProcessed) - if error: - input( - "Error processing macros, files still contain curly braces (might be in comments?)\n", - "press any key to continue") - if __name__ == "__main__": + sql_dir = path.normpath(path.join(path.dirname(path.abspath(__file__)), "..", "sql")) + parser = argparse.ArgumentParser(description="gochan build script") - dofile(path.join("..", "sql", "initdb_")) + dofile(path.join(sql_dir, "initdb_")) parser.add_argument("--preapril2020", action="store_true", help="Also build the legacy (pre-April 2020 migration) database schema used for testing gochan-migrate.") args = parser.parse_args() if args.preapril2020: - dofile(path.join("..", "sql", "preapril2020migration", "oldDBMigration_")) + dofile(path.join(sql_dir, "preapril2020migration", "oldDBMigration_"))