From ff48386cc8f6c7e17939cb29a67fd2e6e630b671 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Tue, 2 Apr 2024 18:38:22 +0200 Subject: [PATCH] store used data transfer as big integer we originally stored these values as MB but since we use bytes now, an integer field is not enough. Fixes #1575 Signed-off-by: Nicola Murino --- internal/dataprovider/bolt.go | 14 +++++++++++--- internal/dataprovider/mysql.go | 24 ++++++++++++++++++++++++ internal/dataprovider/pgsql.go | 26 ++++++++++++++++++++++++++ internal/dataprovider/sqlcommon.go | 2 +- internal/dataprovider/sqlite.go | 28 ++++++++++++++++++++++++++-- 5 files changed, 88 insertions(+), 6 deletions(-) diff --git a/internal/dataprovider/bolt.go b/internal/dataprovider/bolt.go index 7c0b5758..01ad0b3e 100644 --- a/internal/dataprovider/bolt.go +++ b/internal/dataprovider/bolt.go @@ -37,7 +37,7 @@ import ( ) const ( - boltDatabaseVersion = 28 + boltDatabaseVersion = 29 ) var ( @@ -3139,6 +3139,10 @@ func (p *BoltProvider) migrateDatabase() error { providerLog(logger.LevelError, "%v", err) logger.ErrorToConsole("%v", err) return err + case version == 28: + logger.InfoToConsole("updating database schema version: %d -> 29", version) + providerLog(logger.LevelInfo, "updating database schema version: %d -> 29", version) + return updateBoltDatabaseVersion(p.dbHandle, 29) default: if version > boltDatabaseVersion { providerLog(logger.LevelError, "database schema version %d is newer than the supported one: %d", version, @@ -3160,6 +3164,10 @@ func (p *BoltProvider) revertDatabase(targetVersion int) error { //nolint:gocycl return errors.New("current version match target version, nothing to do") } switch dbVersion.Version { + case 29: + logger.InfoToConsole("downgrading database schema version: %d -> 28", dbVersion.Version) + providerLog(logger.LevelInfo, "downgrading database schema version: %d -> 28", dbVersion.Version) + return updateBoltDatabaseVersion(p.dbHandle, 28) default: return fmt.Errorf("database schema version not handled: %v", dbVersion.Version) } @@ -3900,7 +3908,7 @@ func getBoltDatabaseVersion(dbHandle *bolt.DB) (schemaVersion, error) { return dbVersion, err } -/*func updateBoltDatabaseVersion(dbHandle *bolt.DB, version int) error { +func updateBoltDatabaseVersion(dbHandle *bolt.DB, version int) error { err := dbHandle.Update(func(tx *bolt.Tx) error { bucket := tx.Bucket(dbVersionBucket) if bucket == nil { @@ -3916,4 +3924,4 @@ func getBoltDatabaseVersion(dbHandle *bolt.DB) (schemaVersion, error) { return bucket.Put(dbVersionKey, buf) }) return err -}*/ +} diff --git a/internal/dataprovider/mysql.go b/internal/dataprovider/mysql.go index dd808bd9..659637d9 100644 --- a/internal/dataprovider/mysql.go +++ b/internal/dataprovider/mysql.go @@ -194,6 +194,10 @@ const ( "CREATE INDEX `{{prefix}}ip_lists_deleted_at_idx` ON `{{ip_lists}}` (`deleted_at`);" + "CREATE INDEX `{{prefix}}ip_lists_first_last_idx` ON `{{ip_lists}}` (`first`, `last`);" + "INSERT INTO {{schema_version}} (version) VALUES (28);" + mysqlV29SQL = "ALTER TABLE `{{users}}` MODIFY `used_download_data_transfer` bigint NOT NULL;" + + "ALTER TABLE `{{users}}` MODIFY `used_upload_data_transfer` bigint NOT NULL;" + mysqlV29DownSQL = "ALTER TABLE `{{users}}` MODIFY `used_upload_data_transfer` integer NOT NULL;" + + "ALTER TABLE `{{users}}` MODIFY `used_download_data_transfer` integer NOT NULL;" ) // MySQLProvider defines the auth provider for MySQL/MariaDB database @@ -794,6 +798,8 @@ func (p *MySQLProvider) migrateDatabase() error { providerLog(logger.LevelError, "%v", err) logger.ErrorToConsole("%v", err) return err + case version == 28: + return updateMySQLDatabaseFrom28To29(p.dbHandle) default: if version > sqlDatabaseVersion { providerLog(logger.LevelError, "database schema version %d is newer than the supported one: %d", version, @@ -816,6 +822,8 @@ func (p *MySQLProvider) revertDatabase(targetVersion int) error { } switch dbVersion.Version { + case 29: + return downgradeMySQLDatabaseFrom29To28(p.dbHandle) default: return fmt.Errorf("database schema version not handled: %d", dbVersion.Version) } @@ -853,3 +861,19 @@ func (p *MySQLProvider) normalizeError(err error, fieldType int) error { } return err } + +func updateMySQLDatabaseFrom28To29(dbHandle *sql.DB) error { + logger.InfoToConsole("updating database schema version: 28 -> 29") + providerLog(logger.LevelInfo, "updating database schema version: 28 -> 29") + + sql := strings.ReplaceAll(mysqlV29SQL, "{{users}}", sqlTableUsers) + return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, strings.Split(sql, ";"), 29, true) +} + +func downgradeMySQLDatabaseFrom29To28(dbHandle *sql.DB) error { + logger.InfoToConsole("downgrading database schema version: 29 -> 28") + providerLog(logger.LevelInfo, "downgrading database schema version: 29 -> 28") + + sql := strings.ReplaceAll(mysqlV29DownSQL, "{{users}}", sqlTableUsers) + return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, strings.Split(sql, ";"), 28, false) +} diff --git a/internal/dataprovider/pgsql.go b/internal/dataprovider/pgsql.go index fe25081f..539ce2ba 100644 --- a/internal/dataprovider/pgsql.go +++ b/internal/dataprovider/pgsql.go @@ -209,6 +209,12 @@ INSERT INTO {{schema_version}} (version) VALUES (28); ` // not supported in CockroachDB ipListsLikeIndex = `CREATE INDEX "{{prefix}}ip_lists_ipornet_like_idx" ON "{{ip_lists}}" ("ipornet" varchar_pattern_ops);` + pgsqlV29SQL = `ALTER TABLE "{{users}}" ALTER COLUMN "used_download_data_transfer" TYPE bigint; +ALTER TABLE "{{users}}" ALTER COLUMN "used_upload_data_transfer" TYPE bigint; +` + pgsqlV29DownSQL = `ALTER TABLE "{{users}}" ALTER COLUMN "used_upload_data_transfer" TYPE integer; +ALTER TABLE "{{users}}" ALTER COLUMN "used_download_data_transfer" TYPE integer; +` ) // PGSQLProvider defines the auth provider for PostgreSQL database @@ -813,6 +819,8 @@ func (p *PGSQLProvider) migrateDatabase() error { //nolint:dupl providerLog(logger.LevelError, "%v", err) logger.ErrorToConsole("%v", err) return err + case version == 28: + return updatePGSQLDatabaseFrom28To29(p.dbHandle) default: if version > sqlDatabaseVersion { providerLog(logger.LevelError, "database schema version %d is newer than the supported one: %d", version, @@ -835,6 +843,8 @@ func (p *PGSQLProvider) revertDatabase(targetVersion int) error { } switch dbVersion.Version { + case 29: + return downgradePGSQLDatabaseFrom29To28(p.dbHandle) default: return fmt.Errorf("database schema version not handled: %d", dbVersion.Version) } @@ -872,3 +882,19 @@ func (p *PGSQLProvider) normalizeError(err error, fieldType int) error { } return err } + +func updatePGSQLDatabaseFrom28To29(dbHandle *sql.DB) error { + logger.InfoToConsole("updating database schema version: 28 -> 29") + providerLog(logger.LevelInfo, "updating database schema version: 28 -> 29") + + sql := strings.ReplaceAll(pgsqlV29SQL, "{{users}}", sqlTableUsers) + return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, []string{sql}, 29, true) +} + +func downgradePGSQLDatabaseFrom29To28(dbHandle *sql.DB) error { + logger.InfoToConsole("downgrading database schema version: 29 -> 28") + providerLog(logger.LevelInfo, "downgrading database schema version: 29 -> 28") + + sql := strings.ReplaceAll(pgsqlV29DownSQL, "{{users}}", sqlTableUsers) + return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, []string{sql}, 28, false) +} diff --git a/internal/dataprovider/sqlcommon.go b/internal/dataprovider/sqlcommon.go index 8daf1efc..3a5e5c1c 100644 --- a/internal/dataprovider/sqlcommon.go +++ b/internal/dataprovider/sqlcommon.go @@ -35,7 +35,7 @@ import ( ) const ( - sqlDatabaseVersion = 28 + sqlDatabaseVersion = 29 defaultSQLQueryTimeout = 10 * time.Second longSQLQueryTimeout = 60 * time.Second ) diff --git a/internal/dataprovider/sqlite.go b/internal/dataprovider/sqlite.go index 5b7ed1d3..4a21c21c 100644 --- a/internal/dataprovider/sqlite.go +++ b/internal/dataprovider/sqlite.go @@ -92,8 +92,8 @@ CREATE TABLE "{{users}}" ("id" integer NOT NULL PRIMARY KEY, "username" varchar( "upload_bandwidth" integer NOT NULL, "download_bandwidth" integer NOT NULL, "last_login" bigint NOT NULL, "filters" text NULL, "filesystem" text NULL, "additional_info" text NULL, "created_at" bigint NOT NULL, "updated_at" bigint NOT NULL, "email" varchar(255) NULL, "upload_data_transfer" integer NOT NULL, -"download_data_transfer" integer NOT NULL, "total_data_transfer" integer NOT NULL, "used_upload_data_transfer" integer NOT NULL, -"used_download_data_transfer" integer NOT NULL, "deleted_at" bigint NOT NULL, "first_download" bigint NOT NULL, +"download_data_transfer" integer NOT NULL, "total_data_transfer" integer NOT NULL, "used_upload_data_transfer" bigint NOT NULL, +"used_download_data_transfer" bigint NOT NULL, "deleted_at" bigint NOT NULL, "first_download" bigint NOT NULL, "first_upload" bigint NOT NULL, "last_password_change" bigint NOT NULL, "role_id" integer NULL REFERENCES "{{roles}}" ("id") ON DELETE SET NULL); CREATE TABLE "{{groups_folders_mapping}}" ("id" integer NOT NULL PRIMARY KEY, "folder_id" integer NOT NULL REFERENCES "{{folders}}" ("id") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, @@ -714,6 +714,8 @@ func (p *SQLiteProvider) migrateDatabase() error { //nolint:dupl providerLog(logger.LevelError, "%v", err) logger.ErrorToConsole("%v", err) return err + case version == 28: + return updateSQLiteDatabaseFrom28To29(p.dbHandle) default: if version > sqlDatabaseVersion { providerLog(logger.LevelError, "database schema version %d is newer than the supported one: %d", version, @@ -736,6 +738,8 @@ func (p *SQLiteProvider) revertDatabase(targetVersion int) error { } switch dbVersion.Version { + case 29: + return downgradeSQLiteDatabaseFrom29To28(p.dbHandle) default: return fmt.Errorf("database schema version not handled: %d", dbVersion.Version) } @@ -773,6 +777,26 @@ func (p *SQLiteProvider) normalizeError(err error, fieldType int) error { return err } +func updateSQLiteDatabaseFrom28To29(dbHandle *sql.DB) error { + logger.InfoToConsole("updating database schema version: 28 -> 29") + providerLog(logger.LevelInfo, "updating database schema version: 28 -> 29") + + ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout) + defer cancel() + + return sqlCommonUpdateDatabaseVersion(ctx, dbHandle, 29) +} + +func downgradeSQLiteDatabaseFrom29To28(dbHandle *sql.DB) error { + logger.InfoToConsole("downgrading database schema version: 29 -> 28") + providerLog(logger.LevelInfo, "downgrading database schema version: 29 -> 28") + + ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout) + defer cancel() + + return sqlCommonUpdateDatabaseVersion(ctx, dbHandle, 28) +} + /*func setPragmaFK(dbHandle *sql.DB, value string) error { ctx, cancel := context.WithTimeout(context.Background(), longSQLQueryTimeout) defer cancel()