From 9987821003084799cb1fb2e8cad8fca5e84b3952 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Sun, 28 Jul 2019 19:29:32 +0200 Subject: [PATCH] rename last_quota_scan to last_quota_update Existing databases must be updated using the script 20190728.sql --- .travis.yml | 2 +- README.md | 2 +- api/schema/openapi.yaml | 14 +++++++------- dataprovider/sqlcommon.go | 4 ++-- dataprovider/sqlqueries.go | 8 ++++---- dataprovider/user.go | 2 +- sql/mysql/20190728.sql | 6 ++++++ sql/pgsql/20190728.sql | 6 ++++++ sql/sqlite/20190728.sql | 6 ++++++ 9 files changed, 34 insertions(+), 16 deletions(-) create mode 100644 sql/mysql/20190728.sql create mode 100644 sql/pgsql/20190728.sql create mode 100644 sql/sqlite/20190728.sql diff --git a/.travis.yml b/.travis.yml index 359397f0..82b460cc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ env: - GO111MODULE=on before_script: - - sqlite3 sftpgo.db 'CREATE TABLE "users" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "username" varchar(255) NOT NULL UNIQUE, "password" varchar(255) NULL, "public_key" text NULL, "home_dir" varchar(255) NOT NULL, "uid" integer NOT NULL, "gid" integer NOT NULL, "max_sessions" integer NOT NULL, "quota_size" bigint NOT NULL, "quota_files" integer NOT NULL, "permissions" text NOT NULL, "used_quota_size" bigint NOT NULL, "used_quota_files" integer NOT NULL, "last_quota_scan" bigint NOT NULL, "upload_bandwidth" integer NOT NULL, "download_bandwidth" integer NOT NULL);' + - sqlite3 sftpgo.db 'CREATE TABLE "users" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "username" varchar(255) NOT NULL UNIQUE, "password" varchar(255) NULL, "public_key" text NULL, "home_dir" varchar(255) NOT NULL, "uid" integer NOT NULL, "gid" integer NOT NULL, "max_sessions" integer NOT NULL, "quota_size" bigint NOT NULL, "quota_files" integer NOT NULL, "permissions" text NOT NULL, "used_quota_size" bigint NOT NULL, "used_quota_files" integer NOT NULL, "last_quota_update" bigint NOT NULL, "upload_bandwidth" integer NOT NULL, "download_bandwidth" integer NOT NULL);' install: - go get -v -t ./... diff --git a/README.md b/README.md index a40ab3dd..1f8d9606 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ The `sftpgo` executable supports the following command line flags: Before starting `sftpgo` a dataprovider must be configured. -Sample SQL scripts to create the required database structure can be found insite the source tree [sql](https://github.com/drakkan/sftpgo/tree/master/sql "sql") directory. +Sample SQL scripts to create the required database structure can be found insite the source tree [sql](https://github.com/drakkan/sftpgo/tree/master/sql "sql") directory. The SQL scripts filename's is, by convention, the date as `YYYYMMDD` and the suffix `.sql`. You need to apply all the SQL scripts for your database ordered by name, for example `20190706.sql` must be applied before `20190728.sql` and so on. The `sftpgo.conf` configuration file contains the following sections: diff --git a/api/schema/openapi.yaml b/api/schema/openapi.yaml index a2c72021..6c4860b5 100644 --- a/api/schema/openapi.yaml +++ b/api/schema/openapi.yaml @@ -565,10 +565,10 @@ components: used_quota_file: type: integer format: int32 - last_quota_scan: + last_quota_update: type: integer format: int64 - description: last quota scan as unix timestamp + description: last quota update as unix timestamp in milliseconds upload_bandwidth: type: integer format: int32 @@ -588,7 +588,7 @@ components: start_time: type: integer format: int64 - description: start time as unix timestamp + description: start time as unix timestamp in milliseconds size: type: integer format: int64 @@ -596,7 +596,7 @@ components: last_activity: type: integer format: int64 - description: last transfer activity as unix timestamp + description: last transfer activity as unix timestamp in milliseconds ConnectionStatus: type: object properties: @@ -615,11 +615,11 @@ components: connection_time: type: integer format: int64 - description: connection time as unix timestamp + description: connection time as unix timestamp in milliseconds last_activity: type: integer format: int64 - description: last client activity as unix timestamp + description: last client activity as unix timestamp in milliseconds active_transfers: type: array items: @@ -633,7 +633,7 @@ components: start_time: type: integer format: int64 - description: scan start time as unix timestamp + description: scan start time as unix timestamp in milliseconds ApiResponse: type: object properties: diff --git a/dataprovider/sqlcommon.go b/dataprovider/sqlcommon.go index 09479840..caceae09 100644 --- a/dataprovider/sqlcommon.go +++ b/dataprovider/sqlcommon.go @@ -240,12 +240,12 @@ func getUserFromDbRow(row *sql.Row, rows *sql.Rows) (User, error) { var err error if row != nil { err = row.Scan(&user.ID, &user.Username, &password, &publicKey, &user.HomeDir, &user.UID, &user.GID, &user.MaxSessions, - &user.QuotaSize, &user.QuotaFiles, &permissions, &user.UsedQuotaSize, &user.UsedQuotaFiles, &user.LastQuotaScan, + &user.QuotaSize, &user.QuotaFiles, &permissions, &user.UsedQuotaSize, &user.UsedQuotaFiles, &user.LastQuotaUpdate, &user.UploadBandwidth, &user.DownloadBandwidth) } else { err = rows.Scan(&user.ID, &user.Username, &password, &publicKey, &user.HomeDir, &user.UID, &user.GID, &user.MaxSessions, - &user.QuotaSize, &user.QuotaFiles, &permissions, &user.UsedQuotaSize, &user.UsedQuotaFiles, &user.LastQuotaScan, + &user.QuotaSize, &user.QuotaFiles, &permissions, &user.UsedQuotaSize, &user.UsedQuotaFiles, &user.LastQuotaUpdate, &user.UploadBandwidth, &user.DownloadBandwidth) } if err != nil { diff --git a/dataprovider/sqlqueries.go b/dataprovider/sqlqueries.go index 393bc552..4dd4d866 100644 --- a/dataprovider/sqlqueries.go +++ b/dataprovider/sqlqueries.go @@ -4,7 +4,7 @@ import "fmt" const ( selectUserFields = "id,username,password,public_key,home_dir,uid,gid,max_sessions,quota_size,quota_files,permissions," + - "used_quota_size,used_quota_files,last_quota_scan,upload_bandwidth,download_bandwidth" + "used_quota_size,used_quota_files,last_quota_update,upload_bandwidth,download_bandwidth" ) func getSQLPlaceholders() []string { @@ -38,10 +38,10 @@ func getUsersQuery(order string, username string) string { func getUpdateQuotaQuery(reset bool) string { if reset { - return fmt.Sprintf(`UPDATE %v SET used_quota_size = %v,used_quota_files = %v,last_quota_scan = %v + return fmt.Sprintf(`UPDATE %v SET used_quota_size = %v,used_quota_files = %v,last_quota_update = %v WHERE username = %v`, config.UsersTable, sqlPlaceholders[0], sqlPlaceholders[1], sqlPlaceholders[2], sqlPlaceholders[3]) } - return fmt.Sprintf(`UPDATE %v SET used_quota_size = used_quota_size + %v,used_quota_files = used_quota_files + %v,last_quota_scan = %v + return fmt.Sprintf(`UPDATE %v SET used_quota_size = used_quota_size + %v,used_quota_files = used_quota_files + %v,last_quota_update = %v WHERE username = %v`, config.UsersTable, sqlPlaceholders[0], sqlPlaceholders[1], sqlPlaceholders[2], sqlPlaceholders[3]) } @@ -52,7 +52,7 @@ func getQuotaQuery() string { func getAddUserQuery() string { return fmt.Sprintf(`INSERT INTO %v (username,password,public_key,home_dir,uid,gid,max_sessions,quota_size,quota_files,permissions, - used_quota_size,used_quota_files,last_quota_scan,upload_bandwidth,download_bandwidth) + used_quota_size,used_quota_files,last_quota_update,upload_bandwidth,download_bandwidth) VALUES (%v,%v,%v,%v,%v,%v,%v,%v,%v,%v,0,0,0,%v,%v)`, config.UsersTable, sqlPlaceholders[0], sqlPlaceholders[1], sqlPlaceholders[2], sqlPlaceholders[3], sqlPlaceholders[4], sqlPlaceholders[5], sqlPlaceholders[6], sqlPlaceholders[7], sqlPlaceholders[8], sqlPlaceholders[9], sqlPlaceholders[10], sqlPlaceholders[11]) diff --git a/dataprovider/user.go b/dataprovider/user.go index 25c4c4db..6d93056e 100644 --- a/dataprovider/user.go +++ b/dataprovider/user.go @@ -34,7 +34,7 @@ type User struct { Permissions []string `json:"permissions"` UsedQuotaSize int64 `json:"used_quota_size"` UsedQuotaFiles int `json:"used_quota_files"` - LastQuotaScan int64 `json:"last_quota_scan"` + LastQuotaUpdate int64 `json:"last_quota_update"` UploadBandwidth int64 `json:"upload_bandwidth"` DownloadBandwidth int64 `json:"download_bandwidth"` } diff --git a/sql/mysql/20190728.sql b/sql/mysql/20190728.sql new file mode 100644 index 00000000..5c318bed --- /dev/null +++ b/sql/mysql/20190728.sql @@ -0,0 +1,6 @@ +BEGIN; +-- +-- Rename field last_quota_scan on user to last_quota_update +-- +ALTER TABLE `users` CHANGE `last_quota_scan` `last_quota_update` bigint NOT NULL; +COMMIT; \ No newline at end of file diff --git a/sql/pgsql/20190728.sql b/sql/pgsql/20190728.sql new file mode 100644 index 00000000..86b99a34 --- /dev/null +++ b/sql/pgsql/20190728.sql @@ -0,0 +1,6 @@ +BEGIN; +-- +-- Rename field last_quota_scan on user to last_quota_update +-- +ALTER TABLE "users" RENAME COLUMN "last_quota_scan" TO "last_quota_update"; +COMMIT; \ No newline at end of file diff --git a/sql/sqlite/20190728.sql b/sql/sqlite/20190728.sql new file mode 100644 index 00000000..86b99a34 --- /dev/null +++ b/sql/sqlite/20190728.sql @@ -0,0 +1,6 @@ +BEGIN; +-- +-- Rename field last_quota_scan on user to last_quota_update +-- +ALTER TABLE "users" RENAME COLUMN "last_quota_scan" TO "last_quota_update"; +COMMIT; \ No newline at end of file