From 2ffefbeb3398b364a00abe0098f10fb533f13267 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Mon, 12 Apr 2021 20:00:49 +0200 Subject: [PATCH] add sql_tables_prefix also to indexes and constraints This allows you to reuse the same database for multiple SFTPGo instances Fixes #372 --- dataprovider/dataprovider.go | 6 +++--- dataprovider/mysql.go | 7 ++++--- dataprovider/pgsql.go | 11 ++++++----- dataprovider/sqlite.go | 7 ++++--- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/dataprovider/dataprovider.go b/dataprovider/dataprovider.go index 73c3914b..cf07f549 100644 --- a/dataprovider/dataprovider.go +++ b/dataprovider/dataprovider.go @@ -80,7 +80,7 @@ const ( operationAdd = "add" operationUpdate = "update" operationDelete = "delete" - sqlPrefixValidChars = "abcdefghijklmnopqrstuvwxyz_" + sqlPrefixValidChars = "abcdefghijklmnopqrstuvwxyz_0123456789" ) // ordering constants @@ -511,10 +511,10 @@ func validateHooks() error { } func validateSQLTablesPrefix() error { - if len(config.SQLTablesPrefix) > 0 { + if config.SQLTablesPrefix != "" { for _, char := range config.SQLTablesPrefix { if !strings.Contains(sqlPrefixValidChars, strings.ToLower(string(char))) { - return errors.New("invalid sql_tables_prefix only chars in range 'a..z', 'A..Z' and '_' are allowed") + return errors.New("invalid sql_tables_prefix only chars in range 'a..z', 'A..Z', '0-9' and '_' are allowed") } } sqlTableUsers = config.SQLTablesPrefix + sqlTableUsers diff --git a/dataprovider/mysql.go b/dataprovider/mysql.go index c79a0b50..8379c37b 100644 --- a/dataprovider/mysql.go +++ b/dataprovider/mysql.go @@ -36,9 +36,9 @@ const ( "`last_login` bigint NOT NULL, `filters` longtext NULL, `filesystem` longtext NULL, `additional_info` longtext NULL);" + "CREATE TABLE `{{folders_mapping}}` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `virtual_path` varchar(512) NOT NULL, " + "`quota_size` bigint NOT NULL, `quota_files` integer NOT NULL, `folder_id` integer NOT NULL, `user_id` integer NOT NULL);" + - "ALTER TABLE `{{folders_mapping}}` ADD CONSTRAINT `unique_mapping` UNIQUE (`user_id`, `folder_id`);" + - "ALTER TABLE `{{folders_mapping}}` ADD CONSTRAINT `folders_mapping_folder_id_fk_folders_id` FOREIGN KEY (`folder_id`) REFERENCES `{{folders}}` (`id`) ON DELETE CASCADE;" + - "ALTER TABLE `{{folders_mapping}}` ADD CONSTRAINT `folders_mapping_user_id_fk_users_id` FOREIGN KEY (`user_id`) REFERENCES `{{users}}` (`id`) ON DELETE CASCADE;" + + "ALTER TABLE `{{folders_mapping}}` ADD CONSTRAINT `{{prefix}}unique_mapping` UNIQUE (`user_id`, `folder_id`);" + + "ALTER TABLE `{{folders_mapping}}` ADD CONSTRAINT `{{prefix}}folders_mapping_folder_id_fk_folders_id` FOREIGN KEY (`folder_id`) REFERENCES `{{folders}}` (`id`) ON DELETE CASCADE;" + + "ALTER TABLE `{{folders_mapping}}` ADD CONSTRAINT `{{prefix}}folders_mapping_user_id_fk_users_id` FOREIGN KEY (`user_id`) REFERENCES `{{users}}` (`id`) ON DELETE CASCADE;" + "INSERT INTO {{schema_version}} (version) VALUES (8);" mysqlV9SQL = "ALTER TABLE `{{admins}}` ADD COLUMN `description` varchar(512) NULL;" + "ALTER TABLE `{{folders}}` ADD COLUMN `description` varchar(512) NULL;" + @@ -228,6 +228,7 @@ func (p *MySQLProvider) initializeDatabase() error { initialSQL = strings.ReplaceAll(initialSQL, "{{folders}}", sqlTableFolders) initialSQL = strings.ReplaceAll(initialSQL, "{{users}}", sqlTableUsers) initialSQL = strings.ReplaceAll(initialSQL, "{{folders_mapping}}", sqlTableFoldersMapping) + initialSQL = strings.ReplaceAll(initialSQL, "{{prefix}}", config.SQLTablesPrefix) return sqlCommonExecSQLAndUpdateDBVersion(p.dbHandle, strings.Split(initialSQL, ";"), 8) } diff --git a/dataprovider/pgsql.go b/dataprovider/pgsql.go index df490fad..7192586a 100644 --- a/dataprovider/pgsql.go +++ b/dataprovider/pgsql.go @@ -36,13 +36,13 @@ CREATE TABLE "{{users}}" ("id" serial NOT NULL PRIMARY KEY, "status" integer NOT "additional_info" text NULL); CREATE TABLE "{{folders_mapping}}" ("id" serial NOT NULL PRIMARY KEY, "virtual_path" varchar(512) NOT NULL, "quota_size" bigint NOT NULL, "quota_files" integer NOT NULL, "folder_id" integer NOT NULL, "user_id" integer NOT NULL); -ALTER TABLE "{{folders_mapping}}" ADD CONSTRAINT "unique_mapping" UNIQUE ("user_id", "folder_id"); -ALTER TABLE "{{folders_mapping}}" ADD CONSTRAINT "folders_mapping_folder_id_fk_folders_id" +ALTER TABLE "{{folders_mapping}}" ADD CONSTRAINT "{{prefix}}unique_mapping" UNIQUE ("user_id", "folder_id"); +ALTER TABLE "{{folders_mapping}}" ADD CONSTRAINT "{{prefix}}folders_mapping_folder_id_fk_folders_id" FOREIGN KEY ("folder_id") REFERENCES "{{folders}}" ("id") MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED; -ALTER TABLE "{{folders_mapping}}" ADD CONSTRAINT "folders_mapping_user_id_fk_users_id" +ALTER TABLE "{{folders_mapping}}" ADD CONSTRAINT "{{prefix}}folders_mapping_user_id_fk_users_id" FOREIGN KEY ("user_id") REFERENCES "{{users}}" ("id") MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED; -CREATE INDEX "folders_mapping_folder_id_idx" ON "{{folders_mapping}}" ("folder_id"); -CREATE INDEX "folders_mapping_user_id_idx" ON "{{folders_mapping}}" ("user_id"); +CREATE INDEX "{{prefix}}folders_mapping_folder_id_idx" ON "{{folders_mapping}}" ("folder_id"); +CREATE INDEX "{{prefix}}folders_mapping_user_id_idx" ON "{{folders_mapping}}" ("user_id"); INSERT INTO {{schema_version}} (version) VALUES (8); ` pgsqlV9SQL = `ALTER TABLE "{{admins}}" ADD COLUMN "description" varchar(512) NULL; @@ -235,6 +235,7 @@ func (p *PGSQLProvider) initializeDatabase() error { initialSQL = strings.ReplaceAll(initialSQL, "{{folders}}", sqlTableFolders) initialSQL = strings.ReplaceAll(initialSQL, "{{users}}", sqlTableUsers) initialSQL = strings.ReplaceAll(initialSQL, "{{folders_mapping}}", sqlTableFoldersMapping) + initialSQL = strings.ReplaceAll(initialSQL, "{{prefix}}", config.SQLTablesPrefix) if config.Driver == CockroachDataProviderName { // Cockroach does not support deferrable constraint validation, we don't need it, // we keep these definitions for the PostgreSQL driver to avoid changes for users diff --git a/dataprovider/sqlite.go b/dataprovider/sqlite.go index 396d89f0..68a38af9 100644 --- a/dataprovider/sqlite.go +++ b/dataprovider/sqlite.go @@ -38,9 +38,9 @@ CREATE TABLE "{{users}}" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "user CREATE TABLE "{{folders_mapping}}" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "virtual_path" varchar(512) NOT NULL, "quota_size" bigint NOT NULL, "quota_files" integer NOT NULL, "folder_id" integer NOT NULL REFERENCES "{{folders}}" ("id") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, "user_id" integer NOT NULL REFERENCES "{{users}}" ("id") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, -CONSTRAINT "unique_mapping" UNIQUE ("user_id", "folder_id")); -CREATE INDEX "folders_mapping_folder_id_idx" ON "{{folders_mapping}}" ("folder_id"); -CREATE INDEX "folders_mapping_user_id_idx" ON "{{folders_mapping}}" ("user_id"); +CONSTRAINT "{{prefix}}unique_mapping" UNIQUE ("user_id", "folder_id")); +CREATE INDEX "{{prefix}}folders_mapping_folder_id_idx" ON "{{folders_mapping}}" ("folder_id"); +CREATE INDEX "{{prefix}}folders_mapping_user_id_idx" ON "{{folders_mapping}}" ("user_id"); INSERT INTO {{schema_version}} (version) VALUES (8); ` sqliteV9SQL = `ALTER TABLE "{{admins}}" ADD COLUMN "description" varchar(512) NULL; @@ -249,6 +249,7 @@ func (p *SQLiteProvider) initializeDatabase() error { initialSQL = strings.ReplaceAll(initialSQL, "{{folders}}", sqlTableFolders) initialSQL = strings.ReplaceAll(initialSQL, "{{users}}", sqlTableUsers) initialSQL = strings.ReplaceAll(initialSQL, "{{folders_mapping}}", sqlTableFoldersMapping) + initialSQL = strings.ReplaceAll(initialSQL, "{{prefix}}", config.SQLTablesPrefix) return sqlCommonExecSQLAndUpdateDBVersion(p.dbHandle, []string{initialSQL}, 8) }