From 61e89af131bae392ccb223bfc6ca8f85a4f0ccb4 Mon Sep 17 00:00:00 2001 From: Matt <116982774+Burzah@users.noreply.github.com> Date: Sun, 14 Jul 2024 12:44:35 +0000 Subject: [PATCH] Optimizes IP Address Storage in DB (#25751) * Initial commit * comment fix * Updates new columns position in table * Updates insert statement for connection_log * 57 to 58 - Handles changes post merge conflict --- SQL/paradise_schema.sql | 2 +- SQL/updates/57-58.sql | 17 +++++++++++++++++ code/__DEFINES/misc_defines.dm | 2 +- code/__HELPERS/unsorted.dm | 4 ++-- config/example/config.toml | 2 +- 5 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 SQL/updates/57-58.sql diff --git a/SQL/paradise_schema.sql b/SQL/paradise_schema.sql index 831fb0a5a46..85fdb75727a 100644 --- a/SQL/paradise_schema.sql +++ b/SQL/paradise_schema.sql @@ -506,7 +506,7 @@ CREATE TABLE `connection_log` ( `id` int(11) NOT NULL AUTO_INCREMENT, `datetime` datetime NOT NULL, `ckey` varchar(32) NOT NULL, - `ip` varchar(32) NOT NULL, + `ip` INT UNSIGNED NOT NULL, `computerid` varchar(32) NOT NULL, `server_id` VARCHAR(50) NULL DEFAULT NULL, `result` ENUM('ESTABLISHED','DROPPED - IPINTEL','DROPPED - BANNED','DROPPED - INVALID') NOT NULL DEFAULT 'ESTABLISHED' COLLATE 'utf8mb4_general_ci', diff --git a/SQL/updates/57-58.sql b/SQL/updates/57-58.sql new file mode 100644 index 00000000000..deea8799fe7 --- /dev/null +++ b/SQL/updates/57-58.sql @@ -0,0 +1,17 @@ +# Updating DB from 57-58 ~Burza +# Changes the data type for the `ip` column in `connection_log` from VARCHAR to INT + +# Creates a temporary column for our new data type +ALTER TABLE `connection_log` +ADD COLUMN `ip_int` INT UNSIGNED NULL; + +# Copies the data from the `ip` column to `ip_int` as our new data type +UPDATE `connection_log` +SET `ip_int` = INET_ATON(`ip`); + +# Deletes the old `ip` column from the table +ALTER TABLE `connection_log` DROP COLUMN `ip`; + +# Updates the name of the temporary column to take place of the original +ALTER TABLE `connection_log` +CHANGE COLUMN `ip_int` `ip` INT UNSIGNED NOT NULL AFTER `ckey`; diff --git a/code/__DEFINES/misc_defines.dm b/code/__DEFINES/misc_defines.dm index 79c14093424..a926bb706cf 100644 --- a/code/__DEFINES/misc_defines.dm +++ b/code/__DEFINES/misc_defines.dm @@ -420,7 +420,7 @@ #define INVESTIGATE_HOTMIC "hotmic" // The SQL version required by this version of the code -#define SQL_VERSION 57 +#define SQL_VERSION 58 // Vending machine stuff #define CAT_NORMAL (1<<0) diff --git a/code/__HELPERS/unsorted.dm b/code/__HELPERS/unsorted.dm index 8c472d6228e..71b31e65e28 100644 --- a/code/__HELPERS/unsorted.dm +++ b/code/__HELPERS/unsorted.dm @@ -1921,9 +1921,9 @@ GLOBAL_DATUM_INIT(dview_mob, /mob/dview, new) /proc/log_connection(ckey, ip, cid, connection_type) ASSERT(connection_type in list(CONNECTION_TYPE_ESTABLISHED, CONNECTION_TYPE_DROPPED_IPINTEL, CONNECTION_TYPE_DROPPED_BANNED, CONNECTION_TYPE_DROPPED_INVALID)) - var/datum/db_query/query_accesslog = SSdbcore.NewQuery("INSERT INTO connection_log (`datetime`, `ckey`, `ip`, `computerid`, `result`, `server_id`) VALUES(Now(), :ckey, :ip, :cid, :result, :server_id)", list( + var/datum/db_query/query_accesslog = SSdbcore.NewQuery("INSERT INTO connection_log (`datetime`, `ckey`, `ip`, `computerid`, `result`, `server_id`) VALUES(Now(), :ckey, INET_ATON(:ip), :cid, :result, :server_id)", list( "ckey" = ckey, - "ip" = "[ip ? ip : ""]", // This is important. NULL is not the same as "", and if you directly open the `.dmb` file, you get a NULL IP. + "ip" = "[ip ? ip : "127.0.0.1"]", "cid" = cid, "result" = connection_type, "server_id" = GLOB.configuration.system.instance_id diff --git a/config/example/config.toml b/config/example/config.toml index 28050252d5a..44a15c765a0 100644 --- a/config/example/config.toml +++ b/config/example/config.toml @@ -176,7 +176,7 @@ ipc_screens = [ # Enable/disable the database on a whole sql_enabled = false # SQL version. If this is a mismatch, round start will be delayed -sql_version = 57 +sql_version = 58 # SQL server address. Can be an IP or DNS name sql_address = "127.0.0.1" # SQL server port