diff --git a/.github/workflows/CI-mariadb.yml b/.github/workflows/CI-mariadb.yml index c5ca0f359032..1afd041fe671 100644 --- a/.github/workflows/CI-mariadb.yml +++ b/.github/workflows/CI-mariadb.yml @@ -1,16 +1,11 @@ name: CI env: DATABASE_TYPE: mariadb - MYSQL_HOST: localhost - MYSQL_PORT: 3306 - MYSQL_USERNAME: root - MYSQL_PASSWORD: root123 - MYSQL_DATABASE: registry - POSTGRESQL_HOST: localhost - POSTGRESQL_PORT: 5432 - POSTGRESQL_USR: postgres - POSTGRESQL_PWD: root123 - POSTGRESQL_DATABASE: registry + DB_HOST: localhost + DB_PORT: 3306 + DB_USERNAME: root + DB_PASSWORD: root123 + DB_DATABASE: registry DOCKER_COMPOSE_VERSION: 1.23.0 HARBOR_ADMIN: admin HARBOR_ADMIN_PASSWD: Harbor12345 diff --git a/.github/workflows/CI-mysql.yml b/.github/workflows/CI-mysql.yml index 2e31a2fd7bda..e8a90e522532 100644 --- a/.github/workflows/CI-mysql.yml +++ b/.github/workflows/CI-mysql.yml @@ -1,16 +1,11 @@ name: CI env: DATABASE_TYPE: mysql - MYSQL_HOST: localhost - MYSQL_PORT: 3308 - MYSQL_USERNAME: root - MYSQL_PASSWORD: root123 - MYSQL_DATABASE: registry - POSTGRESQL_HOST: localhost - POSTGRESQL_PORT: 5432 - POSTGRESQL_USR: postgres - POSTGRESQL_PWD: root123 - POSTGRESQL_DATABASE: registry + DB_HOST: localhost + DB_PORT: 3308 + DB_USERNAME: root + DB_PASSWORD: root123 + DB_DATABASE: registry DOCKER_COMPOSE_VERSION: 1.23.0 HARBOR_ADMIN: admin HARBOR_ADMIN_PASSWD: Harbor12345 @@ -27,7 +22,7 @@ on: - 'docs/**' jobs: - UTTEST4MARIADB: + UTTEST4MYSQL: env: UTTEST: true runs-on: diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index ce7191fe8287..051eab767a88 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -1,16 +1,11 @@ name: CI env: DATABASE_TYPE: postgresql - POSTGRESQL_HOST: localhost - POSTGRESQL_PORT: 5432 - POSTGRESQL_USR: postgres - POSTGRESQL_PWD: root123 - POSTGRESQL_DATABASE: registry - MYSQL_HOST: localhost - MYSQL_PORT: 3306 - MYSQL_USERNAME: root - MYSQL_PASSWORD: root123 - MYSQL_DATABASE: registry + DB_HOST: localhost + DB_PORT: 5432 + DB_USERNAME: postgres + DB_PASSWORD: root123 + DB_DATABASE: registry DOCKER_COMPOSE_VERSION: 1.23.0 HARBOR_ADMIN: admin HARBOR_ADMIN_PASSWD: Harbor12345 diff --git a/make/migrations/mysql/0001_initial_schema.up.sql b/make/migrations/mysql/0001_initial_schema.up.sql index 70c00a8210aa..51babe6092de 100644 --- a/make/migrations/mysql/0001_initial_schema.up.sql +++ b/make/migrations/mysql/0001_initial_schema.up.sql @@ -65,8 +65,9 @@ create table project ( ); -insert into project (owner_id, name, creation_time, update_time) values -(1, 'library', NOW(), NOW()); + +insert into project (owner_id, name, creation_time, update_time) +select user_id , 'library', NOW(), NOW() from harbor_user where username = 'admin'; create table project_member ( id SERIAL NOT NULL, diff --git a/make/migrations/mysql/0040_2.1.0_schema.up.sql b/make/migrations/mysql/0040_2.1.0_schema.up.sql index 65d339d1be9c..58f60890ab95 100644 --- a/make/migrations/mysql/0040_2.1.0_schema.up.sql +++ b/make/migrations/mysql/0040_2.1.0_schema.up.sql @@ -1,4 +1,19 @@ -ALTER TABLE project ADD COLUMN IF NOT EXISTS registry_id int; +CREATE PROCEDURE PROC_ADD_COLUMN_IF_NOT_EXISTS (in TB_NAME varchar(64), in CL_NAME varchar(64), in CL_TYPE varchar(64)) BEGIN +SELECT count(*) INTO @EXIST_CL +FROM INFORMATION_SCHEMA.COLUMNS +WHERE TABLE_SCHEMA = database() + AND TABLE_NAME = TB_NAME + AND COLUMN_NAME = CL_NAME LIMIT 1; + +SET @sql_cl = IF (@EXIST_CL <= 0, CONCAT('ALTER TABLE `', TB_NAME, '` ADD COLUMN `',CL_NAME, '` ', CL_TYPE), + 'select \' COLUMN EXISTS\' status'); +PREPARE stmt_cl FROM @sql_cl; +EXECUTE stmt_cl; +DEALLOCATE PREPARE stmt_cl; +END; + +CALL PROC_ADD_COLUMN_IF_NOT_EXISTS('project', 'registry_id', 'int'); + ALTER TABLE cve_whitelist RENAME TO cve_allowlist; UPDATE role SET name='maintainer' WHERE name='master'; UPDATE project_metadata SET name='reuse_sys_cve_allowlist' WHERE name='reuse_sys_cve_whitelist'; @@ -36,11 +51,32 @@ CREATE TABLE IF NOT EXISTS task ( CHECK (extra_attrs is null or JSON_VALID (extra_attrs)) ); -ALTER TABLE `blob` ADD COLUMN update_time timestamp default CURRENT_TIMESTAMP; -ALTER TABLE `blob` ADD COLUMN status varchar(255) default 'none'; -ALTER TABLE `blob` ADD COLUMN version BIGINT default 0; -CREATE INDEX IF NOT EXISTS idx_status ON `blob` (status); -CREATE INDEX IF NOT EXISTS idx_version ON `blob` (version); +CALL PROC_ADD_COLUMN_IF_NOT_EXISTS('blob', 'update_time', 'timestamp default CURRENT_TIMESTAMP'); +CALL PROC_ADD_COLUMN_IF_NOT_EXISTS('blob', 'status', 'varchar(255) default \'none\''); +CALL PROC_ADD_COLUMN_IF_NOT_EXISTS('blob', 'version', 'BIGINT default 0'); + +CREATE PROCEDURE PROC_CREATE_INDEX_IF_NOT_EXISTS (in TB_NAME varchar(64), in CL_NAME varchar(64), in IND_NAME varchar(64)) BEGIN +SELECT count(*) INTO @EXIST_IND +FROM INFORMATION_SCHEMA.STATISTICS +WHERE TABLE_SCHEMA = database() + AND TABLE_NAME = TB_NAME + AND INDEX_NAME = IND_NAME LIMIT 1; + +SET @sql_ind = IF (@EXIST_IND <= 0, CONCAT('CREATE INDEX `', IND_NAME, '` ON `', TB_NAME, '` (', CL_NAME, ')'), + 'select \' INDEX EXISTS\' status'); +PREPARE stmt_ind FROM @sql_ind; +EXECUTE stmt_ind; +DEALLOCATE PREPARE stmt_ind; +END; + +CALL PROC_CREATE_INDEX_IF_NOT_EXISTS('blob', 'status', 'idx_status'); +CALL PROC_CREATE_INDEX_IF_NOT_EXISTS('blob', 'version', 'idx_version'); + + +set @x := (select count(*) from information_schema.statistics where table_name = 'table' and index_name = 'IX_Table_XYZ' and table_schema = database()); +set @sql := if( @x > 0, 'select ''Index exists.''', 'Alter Table TableName ADD Index IX_Table_XYZ (column1);'); +PREPARE stmt FROM @sql; +EXECUTE stmt; CREATE TABLE IF NOT EXISTS p2p_preheat_instance ( id SERIAL PRIMARY KEY NOT NULL, @@ -71,11 +107,11 @@ CREATE TABLE IF NOT EXISTS p2p_preheat_policy ( UNIQUE (name, project_id) ); -ALTER TABLE schedule ADD COLUMN IF NOT EXISTS vendor_type varchar(16); -ALTER TABLE schedule ADD COLUMN IF NOT EXISTS vendor_id int; -ALTER TABLE schedule ADD COLUMN IF NOT EXISTS cron varchar(64); -ALTER TABLE schedule ADD COLUMN IF NOT EXISTS callback_func_name varchar(128); -ALTER TABLE schedule ADD COLUMN IF NOT EXISTS callback_func_param text; +CALL PROC_ADD_COLUMN_IF_NOT_EXISTS('schedule', 'vendor_type', 'varchar(16)'); +CALL PROC_ADD_COLUMN_IF_NOT_EXISTS('schedule', 'vendor_id', 'int'); +CALL PROC_ADD_COLUMN_IF_NOT_EXISTS('schedule', 'cron', 'varchar(64)'); +CALL PROC_ADD_COLUMN_IF_NOT_EXISTS('schedule', 'callback_func_name', 'varchar(128)'); +CALL PROC_ADD_COLUMN_IF_NOT_EXISTS('schedule', 'callback_func_param', 'text'); /*abstract the cron, callback function parameters from table retention_policy*/ UPDATE schedule, ( @@ -128,8 +164,7 @@ ALTER TABLE schedule DROP COLUMN IF EXISTS status; UPDATE registry SET type = 'quay' WHERE type = 'quay-io'; - -ALTER TABLE artifact ADD COLUMN IF NOT EXISTS icon varchar(255); +CALL PROC_ADD_COLUMN_IF_NOT_EXISTS('artifact', 'icon', 'varchar(255)'); /*remove the constraint for name in table 'notification_policy'*/ /*ALTER TABLE notification_policy DROP CONSTRAINT notification_policy_name_key;*/ @@ -149,7 +184,7 @@ INSERT INTO data_migrations (version) VALUES ( ELSE 0 END ); -ALTER TABLE schema_migrations DROP COLUMN IF EXISTS data_version; +ALTER TABLE schema_migrations DROP COLUMN data_version; UPDATE artifact SET icon=( diff --git a/make/migrations/mysql/0050_2.2.0_schema.up.sql b/make/migrations/mysql/0050_2.2.0_schema.up.sql index c476257da74f..b532e6adcd73 100644 --- a/make/migrations/mysql/0050_2.2.0_schema.up.sql +++ b/make/migrations/mysql/0050_2.2.0_schema.up.sql @@ -18,21 +18,21 @@ Clean the dirty data in quota/quota_usage DELETE FROM quota WHERE reference='project' AND reference_id NOT IN (SELECT project_id FROM project WHERE deleted=FALSE); DELETE FROM quota_usage WHERE reference='project' AND reference_id NOT IN (SELECT project_id FROM project WHERE deleted=FALSE); -ALTER TABLE schedule ADD COLUMN cron_type varchar(64); -ALTER TABLE robot ADD COLUMN secret varchar(2048); -ALTER TABLE robot ADD COLUMN salt varchar(64); +CALL PROC_ADD_COLUMN_IF_NOT_EXISTS('schedule', 'cron_type', 'varchar(64)'); +CALL PROC_ADD_COLUMN_IF_NOT_EXISTS('robot', 'secret', 'varchar(2048)'); +CALL PROC_ADD_COLUMN_IF_NOT_EXISTS('robot', 'salt', 'varchar(64)'); SET sql_mode = ''; -ALTER TABLE task ADD COLUMN vendor_type varchar(16); +CALL PROC_ADD_COLUMN_IF_NOT_EXISTS('task', 'vendor_type', 'varchar(16)'); UPDATE task, execution SET task.vendor_type = execution.vendor_type WHERE task.execution_id = execution.id; ALTER TABLE task MODIFY COLUMN vendor_type varchar(16) NOT NULL; -ALTER TABLE execution ADD COLUMN update_time timestamp; +CALL PROC_ADD_COLUMN_IF_NOT_EXISTS('execution', 'update_time', 'timestamp'); UPDATE artifact AS art SET size = ( SELECT sum( size ) FROM `blob` WHERE digest IN ( SELECT digest_blob FROM artifact_blob WHERE digest_af = art.digest ) ); -ALTER TABLE robot ADD COLUMN duration int; +CALL PROC_ADD_COLUMN_IF_NOT_EXISTS('robot', 'duration', 'int'); CREATE TABLE IF NOT EXISTS role_permission ( id SERIAL PRIMARY KEY NOT NULL, @@ -188,7 +188,7 @@ END; CALL PROC_UPDATE_REPLICATION_EXECUTION(); /*move the replication execution records into the new execution table*/ -ALTER TABLE replication_execution ADD COLUMN new_execution_id int; +CALL PROC_ADD_COLUMN_IF_NOT_EXISTS('replication_execution', 'new_execution_id', 'int'); INSERT INTO execution ( vendor_type, vendor_id, status, status_message, revision, `trigger`, start_time, end_time ) SELECT 'REPLICATION', rep_exec.policy_id, @@ -357,6 +357,7 @@ SET sql_mode = ''; ALTER TABLE execution MODIFY COLUMN vendor_type varchar(64) NOT NULL; ALTER TABLE `schedule` MODIFY COLUMN vendor_type varchar(64) DEFAULT NULL; ALTER TABLE `schedule` ADD COLUMN extra_attrs JSON; +CALL PROC_ADD_COLUMN_IF_NOT_EXISTS('schedule', 'extra_attrs', 'JSON'); ALTER TABLE task MODIFY COLUMN vendor_type varchar(64) NOT NULL; /* Remove these columns in scan_report because execution-task pattern will handle them */ @@ -783,7 +784,7 @@ DELETE rt FROM retention_task AS rt LEFT JOIN retention_execution re ON rt.execu WHERE re.id IS NULL; /*move the replication execution records into the new execution table*/ -ALTER TABLE retention_execution ADD COLUMN IF NOT EXISTS new_execution_id int; +CALL PROC_ADD_COLUMN_IF_NOT_EXISTS('retention_execution', 'new_execution_id', 'int'); CREATE PROCEDURE PROC_UPDATE_EXECUTION_AND_RETENTION_EXECUTION ( ) BEGIN DECLARE diff --git a/make/migrations/mysql/0052_2.2.2_schema.up.sql b/make/migrations/mysql/0052_2.2.2_schema.up.sql index 6d5b5201c42a..e716f4264d7d 100644 --- a/make/migrations/mysql/0052_2.2.2_schema.up.sql +++ b/make/migrations/mysql/0052_2.2.2_schema.up.sql @@ -1,2 +1,2 @@ -ALTER TABLE schedule ADD COLUMN revision integer; +CALL PROC_ADD_COLUMN_IF_NOT_EXISTS('schedule', 'revision', 'integer'); UPDATE schedule set revision = 0; \ No newline at end of file diff --git a/make/migrations/mysql/0060_2.3.0_schema.up.sql b/make/migrations/mysql/0060_2.3.0_schema.up.sql index 54ebddfa6c5c..75ebbb71396f 100644 --- a/make/migrations/mysql/0060_2.3.0_schema.up.sql +++ b/make/migrations/mysql/0060_2.3.0_schema.up.sql @@ -1,4 +1,4 @@ -ALTER TABLE replication_policy ADD COLUMN dest_namespace_replace_count int; +CALL PROC_ADD_COLUMN_IF_NOT_EXISTS('replication_policy', 'dest_namespace_replace_count', 'int'); UPDATE replication_policy SET dest_namespace_replace_count=-1 WHERE dest_namespace IS NOT NULL; CREATE INDEX idx_artifact_push_time ON artifact (push_time); diff --git a/make/migrations/mysql/0070_2.4.0_schema.up.sql b/make/migrations/mysql/0070_2.4.0_schema.up.sql index 3accb76f4345..5abf986b1512 100644 --- a/make/migrations/mysql/0070_2.4.0_schema.up.sql +++ b/make/migrations/mysql/0070_2.4.0_schema.up.sql @@ -1,11 +1,13 @@ /* cleanup deleted user project members */ DELETE FROM project_member WHERE project_member.entity_type = 'u' AND EXISTS (SELECT NULL FROM harbor_user WHERE project_member.entity_id = harbor_user.user_id AND harbor_user.deleted = true ); -ALTER TABLE replication_policy ADD COLUMN speed_kb int; +CALL PROC_ADD_COLUMN_IF_NOT_EXISTS('replication_policy', 'speed_kb', 'int'); /* add version fields for lock free quota */ ALTER TABLE quota ADD COLUMN version bigint DEFAULT 0; ALTER TABLE quota_usage ADD COLUMN version bigint DEFAULT 0; +CALL PROC_ADD_COLUMN_IF_NOT_EXISTS('quota', 'version', 'bigint DEFAULT 0'); +CALL PROC_ADD_COLUMN_IF_NOT_EXISTS('quota_usage', 'version', 'bigint DEFAULT 0'); /* convert Negligible to None for the severity of the vulnerability record */ UPDATE vulnerability_record SET severity='None' WHERE severity='Negligible'; diff --git a/make/photon/prepare/templates/core/env.jinja b/make/photon/prepare/templates/core/env.jinja index f9b93627dfa7..c9dea10d1a78 100644 --- a/make/photon/prepare/templates/core/env.jinja +++ b/make/photon/prepare/templates/core/env.jinja @@ -8,24 +8,14 @@ _REDIS_URL_REG={{redis_url_reg}} LOG_LEVEL={{log_level}} EXT_ENDPOINT={{public_url}} DATABASE_TYPE={{harbor_db_type}} -{% if ( harbor_db_type == "mysql" or harbor_db_type == "mariadb" ) %} -MYSQL_HOST={{harbor_db_host}} -MYSQL_PORT={{harbor_db_port}} -MYSQL_USERNAME={{harbor_db_username}} -MYSQL_PASSWORD={{harbor_db_password}} -MYSQL_DATABASE={{harbor_db_name}} -MYSQL_MAX_IDLE_CONNS={{harbor_db_max_idle_conns}} -MYSQL_MAX_OPEN_CONNS={{harbor_db_max_open_conns}} -{% else %} -POSTGRESQL_HOST={{harbor_db_host}} -POSTGRESQL_PORT={{harbor_db_port}} -POSTGRESQL_USERNAME={{harbor_db_username}} -POSTGRESQL_PASSWORD={{harbor_db_password}} -POSTGRESQL_DATABASE={{harbor_db_name}} -POSTGRESQL_SSLMODE={{harbor_db_sslmode}} -POSTGRESQL_MAX_IDLE_CONNS={{harbor_db_max_idle_conns}} -POSTGRESQL_MAX_OPEN_CONNS={{harbor_db_max_open_conns}} -{% endif %} +DB_HOST={{harbor_db_host}} +DB_PORT={{harbor_db_port}} +DB_USERNAME={{harbor_db_username}} +DB_PASSWORD={{harbor_db_password}} +DB_DATABASE={{harbor_db_name}} +DB_MAX_IDLE_CONNS={{harbor_db_max_idle_conns}} +DB_MAX_OPEN_CONNS={{harbor_db_max_open_conns}} +DB_SSLMODE={{harbor_db_sslmode}} REGISTRY_URL={{registry_url}} PORTAL_URL={{portal_url}} TOKEN_SERVICE_URL={{token_service_url}} diff --git a/src/common/const.go b/src/common/const.go index 83944d51d985..4be2bb4ee644 100755 --- a/src/common/const.go +++ b/src/common/const.go @@ -51,21 +51,14 @@ const ( ExtEndpoint = "ext_endpoint" AUTHMode = "auth_mode" DatabaseType = "database_type" - PostGreSQLHOST = "postgresql_host" - PostGreSQLPort = "postgresql_port" - PostGreSQLUsername = "postgresql_username" - PostGreSQLPassword = "postgresql_password" - PostGreSQLDatabase = "postgresql_database" - PostGreSQLSSLMode = "postgresql_sslmode" - PostGreSQLMaxIdleConns = "postgresql_max_idle_conns" - PostGreSQLMaxOpenConns = "postgresql_max_open_conns" - MySQLHOST = "mysql_host" - MySQLPort = "mysql_port" - MySQLUsername = "mysql_username" - MySQLPassword = "mysql_password" - MySQLDatabase = "mysql_database" - MySQLMaxIdleConns = "mysql_max_idle_conns" - MySQLMaxOpenConns = "mysql_max_open_conns" + DBHOST = "db_host" + DBPort = "db_port" + DBUsername = "db_username" + DBPassword = "db_password" + DBDatabase = "db_database" + DBSSLMode = "db_sslmode" + DBMaxIdleConns = "db_max_idle_conns" + DBMaxOpenConns = "db_max_open_conns" SelfRegistration = "self_registration" CoreURL = "core_url" CoreLocalURL = "core_local_url" diff --git a/src/common/dao/testutils.go b/src/common/dao/testutils.go index a9f28ab4faa0..03372a1b0483 100644 --- a/src/common/dao/testutils.go +++ b/src/common/dao/testutils.go @@ -17,37 +17,40 @@ package dao import ( "fmt" "github.com/goharbor/harbor/src/common/models" - "os" - "strconv" - "github.com/goharbor/harbor/src/lib/log" + "os" ) var defaultRegistered = false +func PrepareTestForDB() { + dbType := os.Getenv("DATABASE_TYPE") + switch dbType { + case "", "postgresql": + PrepareTestForPostgresSQL() + case "mysql", "mariadb": + PrepareTestForMySQL() + default: + PrepareTestForPostgresSQL() + } +} + // PrepareTestForMySQL is for test only. func PrepareTestForMySQL() { - dbHost := os.Getenv("MYSQL_HOST") + dbHost := os.Getenv("DB_HOST") if len(dbHost) == 0 { - log.Fatalf("environment variable MYSQL_HOST is not set") - } - dbUser := os.Getenv("MYSQL_USERNAME") - if len(dbUser) == 0 { - log.Fatalf("environment variable MYSQL_USERNAME is not set") + log.Fatalf("environment variable DB_HOST is not set") } - dbPortStr := os.Getenv("MYSQL_PORT") - if len(dbPortStr) == 0 { - log.Fatalf("environment variable MYSQL_PORT is not set") - } - dbPort, err := strconv.Atoi(dbPortStr) - if err != nil { - log.Fatalf("invalid POSTGRESQL_PORT: %v", err) + dbUser := "root" + dbPort := 3306 + if os.Getenv("DATABASE_TYPE") == "mysql" { + dbPort = 3308 } - dbPassword := os.Getenv("MYSQL_PASSWORD") - dbDatabase := os.Getenv("MYSQL_DATABASE") + dbPassword := os.Getenv("DB_PASSWORD") + dbDatabase := os.Getenv("DB_DATABASE") if len(dbDatabase) == 0 { - log.Fatalf("environment variable POSTGRESQL_DATABASE is not set") + log.Fatalf("environment variable DB_DATABASE is not set") } database := &models.Database{ @@ -63,7 +66,7 @@ func PrepareTestForMySQL() { }, } - log.Infof("MYSQL_HOST: %s, MYSQL_USERNAME: %s, MYSQL_PORT: %d, MYSQL_PASSWORD: %s\n", dbHost, dbUser, dbPort, dbPassword) + log.Infof("DB_HOST: %s, DB_USERNAME: %s, DB_PORT: %d, DB_PASSWORD: %s\n", dbHost, dbUser, dbPort, dbPassword) initDatabaseForTest(database) } @@ -73,27 +76,17 @@ func PrepareTestForSQLite() { // PrepareTestForPostgresSQL is for test only. func PrepareTestForPostgresSQL() { - dbHost := os.Getenv("POSTGRESQL_HOST") + dbHost := os.Getenv("DB_HOST") if len(dbHost) == 0 { - log.Fatalf("environment variable POSTGRESQL_HOST is not set") - } - dbUser := os.Getenv("POSTGRESQL_USR") - if len(dbUser) == 0 { - log.Fatalf("environment variable POSTGRESQL_USR is not set") - } - dbPortStr := os.Getenv("POSTGRESQL_PORT") - if len(dbPortStr) == 0 { - log.Fatalf("environment variable POSTGRESQL_PORT is not set") - } - dbPort, err := strconv.Atoi(dbPortStr) - if err != nil { - log.Fatalf("invalid POSTGRESQL_PORT: %v", err) + log.Fatalf("environment variable DB_HOST is not set") } + dbUser := "postgres" + dbPort := 5432 - dbPassword := os.Getenv("POSTGRESQL_PWD") - dbDatabase := os.Getenv("POSTGRESQL_DATABASE") + dbPassword := os.Getenv("DB_PASSWORD") + dbDatabase := os.Getenv("DB_DATABASE") if len(dbDatabase) == 0 { - log.Fatalf("environment variable POSTGRESQL_DATABASE is not set") + log.Fatalf("environment variable DB_DATABASE is not set") } database := &models.Database{ @@ -109,7 +102,7 @@ func PrepareTestForPostgresSQL() { }, } - log.Infof("POSTGRES_HOST: %s, POSTGRES_USR: %s, POSTGRES_PORT: %d, POSTGRES_PWD: %s\n", dbHost, dbUser, dbPort, dbPassword) + log.Infof("DB_HOST: %s, DB_USERNAME: %s, DB_PORT: %d, DB_PASSWORD: %s\n", dbHost, dbUser, dbPort, dbPassword) initDatabaseForTest(database) } diff --git a/src/common/utils/test/config.go b/src/common/utils/test/config.go index c9637a077571..ae6c683090d2 100644 --- a/src/common/utils/test/config.go +++ b/src/common/utils/test/config.go @@ -22,11 +22,11 @@ var defaultConfig = map[string]interface{}{ common.ExtEndpoint: "https://host01.com", common.AUTHMode: common.DBAuth, common.DatabaseType: "postgresql", - common.PostGreSQLHOST: "127.0.0.1", - common.PostGreSQLPort: 5432, - common.PostGreSQLUsername: "postgres", - common.PostGreSQLPassword: "root123", - common.PostGreSQLDatabase: "registry", + common.DBHOST: "127.0.0.1", + common.DBPort: 5432, + common.DBUsername: "postgres", + common.DBPassword: "root123", + common.DBDatabase: "registry", common.SelfRegistration: true, common.LDAPURL: "ldap://127.0.0.1", common.LDAPSearchDN: "uid=searchuser,ou=people,dc=mydomain,dc=com", diff --git a/src/common/utils/test/database.go b/src/common/utils/test/database.go index e9fbcccb59ac..8bcb5af53868 100644 --- a/src/common/utils/test/database.go +++ b/src/common/utils/test/database.go @@ -28,42 +28,60 @@ import ( // InitDatabaseFromEnv is used to initialize database for testing func InitDatabaseFromEnv() { - dbHost := os.Getenv("POSTGRESQL_HOST") + dbHost := os.Getenv("DB_HOST") if len(dbHost) == 0 { - log.Fatalf("environment variable POSTGRESQL_HOST is not set") + log.Fatalf("environment variable DB_HOST is not set") } - dbUser := os.Getenv("POSTGRESQL_USR") + dbUser := os.Getenv("DB_USERNAME") if len(dbUser) == 0 { - log.Fatalf("environment variable POSTGRESQL_USR is not set") + log.Fatalf("environment variable DB_USERNAME is not set") } - dbPortStr := os.Getenv("POSTGRESQL_PORT") + dbPortStr := os.Getenv("DB_PORT") if len(dbPortStr) == 0 { - log.Fatalf("environment variable POSTGRESQL_PORT is not set") + log.Fatalf("environment variable DB_PORT is not set") } dbPort, err := strconv.Atoi(dbPortStr) if err != nil { - log.Fatalf("invalid POSTGRESQL_PORT: %v", err) + log.Fatalf("invalid DB_PORT: %v", err) } - dbPassword := os.Getenv("POSTGRESQL_PWD") - dbDatabase := os.Getenv("POSTGRESQL_DATABASE") + dbPassword := os.Getenv("DB_PASSWORD") + dbDatabase := os.Getenv("DB_DATABASE") adminPwd := os.Getenv("HARBOR_ADMIN_PASSWD") if len(dbDatabase) == 0 { - log.Fatalf("environment variable POSTGRESQL_DATABASE is not set") + log.Fatalf("environment variable DB_DATABASE is not set") } - database := &models.Database{ - Type: "postgresql", - PostGreSQL: &models.PostGreSQL{ - Host: dbHost, - Port: dbPort, - Username: dbUser, - Password: dbPassword, - Database: dbDatabase, - }, + dbType := os.Getenv("DATABASE_TYPE") + database := &models.Database{} + switch dbType { + case "", "postgresql": + database = &models.Database{ + Type: "postgresql", + PostGreSQL: &models.PostGreSQL{ + Host: dbHost, + Port: dbPort, + Username: dbUser, + Password: dbPassword, + Database: dbDatabase, + }, + } + case "mysql", "mariadb": + database = &models.Database{ + Type: "mysql", + MySQL: &models.MySQL{ + Host: dbHost, + Port: dbPort, + Username: dbUser, + Password: dbPassword, + Database: dbDatabase, + }, + } + default: + log.Fatalf("invalid db type %s", dbType) } - log.Infof("POSTGRES_HOST: %s, POSTGRES_USR: %s, POSTGRES_PORT: %d, POSTGRES_PWD: %s\n", dbHost, dbUser, dbPort, dbPassword) + log.Infof("DB_HOST: %s, DB_USERNAME: %s, DB_PORT: %d, DB_PASSWORD: %s\n", dbHost, dbUser, dbPort, dbPassword) if err := dao.InitDatabase(database); err != nil { log.Fatalf("failed to init database : %v", err) diff --git a/src/common/utils/test/test.go b/src/common/utils/test/test.go index 17cdbfed9dc7..600876312b3f 100644 --- a/src/common/utils/test/test.go +++ b/src/common/utils/test/test.go @@ -100,11 +100,11 @@ func GetUnitTestConfig() map[string]interface{} { common.ExtEndpoint: fmt.Sprintf("https://%s", ipAddress), common.AUTHMode: "db_auth", common.DatabaseType: "postgresql", - common.PostGreSQLHOST: ipAddress, - common.PostGreSQLPort: 5432, - common.PostGreSQLUsername: "postgres", - common.PostGreSQLPassword: "root123", - common.PostGreSQLDatabase: "registry", + common.DBHOST: ipAddress, + common.DBPort: 5432, + common.DBUsername: "postgres", + common.DBPassword: "root123", + common.DBDatabase: "registry", common.LDAPURL: "ldap://ldap.vmware.com", common.LDAPSearchDN: "cn=admin,dc=example,dc=com", common.LDAPSearchPwd: "admin", diff --git a/src/controller/event/handler/auditlog/auditlog_test.go b/src/controller/event/handler/auditlog/auditlog_test.go index 24eb3a81a05e..a2ab16ebe3dd 100644 --- a/src/controller/event/handler/auditlog/auditlog_test.go +++ b/src/controller/event/handler/auditlog/auditlog_test.go @@ -65,7 +65,7 @@ type AuditLogHandlerTestSuite struct { } func (suite *AuditLogHandlerTestSuite) SetupSuite() { - common_dao.PrepareTestForPostgresSQL() + common_dao.PrepareTestForDB() suite.logMgr = &MockAuditLogManager{} suite.auditLogHandler = &Handler{} } diff --git a/src/controller/event/handler/internal/artifact_test.go b/src/controller/event/handler/internal/artifact_test.go index cdc499603fc4..bcc4862b1e5e 100644 --- a/src/controller/event/handler/internal/artifact_test.go +++ b/src/controller/event/handler/internal/artifact_test.go @@ -48,7 +48,7 @@ func TestArtifactHandler(t *testing.T) { // SetupSuite prepares for running ArtifactHandlerTestSuite. func (suite *ArtifactHandlerTestSuite) SetupSuite() { - common_dao.PrepareTestForPostgresSQL() + common_dao.PrepareTestForDB() config.Init() suite.handler = &Handler{} suite.ctx = orm.NewContext(context.TODO(), beegoorm.NewOrm()) diff --git a/src/controller/event/handler/webhook/artifact/replication_test.go b/src/controller/event/handler/webhook/artifact/replication_test.go index 1d0ec0453757..9615d0d84c1f 100644 --- a/src/controller/event/handler/webhook/artifact/replication_test.go +++ b/src/controller/event/handler/webhook/artifact/replication_test.go @@ -16,31 +16,31 @@ package artifact import ( "context" - proModels "github.com/goharbor/harbor/src/pkg/project/models" "testing" "time" - "github.com/goharbor/harbor/src/lib/config" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" common_dao "github.com/goharbor/harbor/src/common/dao" "github.com/goharbor/harbor/src/controller/event" "github.com/goharbor/harbor/src/controller/project" repctl "github.com/goharbor/harbor/src/controller/replication" repctlmodel "github.com/goharbor/harbor/src/controller/replication/model" + "github.com/goharbor/harbor/src/lib/config" _ "github.com/goharbor/harbor/src/pkg/config/db" _ "github.com/goharbor/harbor/src/pkg/config/inmemory" "github.com/goharbor/harbor/src/pkg/notification" policy_model "github.com/goharbor/harbor/src/pkg/notification/policy/model" + proModels "github.com/goharbor/harbor/src/pkg/project/models" projecttesting "github.com/goharbor/harbor/src/testing/controller/project" replicationtesting "github.com/goharbor/harbor/src/testing/controller/replication" "github.com/goharbor/harbor/src/testing/mock" testingnotification "github.com/goharbor/harbor/src/testing/pkg/notification/policy" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func TestReplicationHandler_Handle(t *testing.T) { - common_dao.PrepareTestForPostgresSQL() + common_dao.PrepareTestForDB() config.Init() PolicyMgr := notification.PolicyMgr diff --git a/src/controller/event/handler/webhook/artifact/retention_test.go b/src/controller/event/handler/webhook/artifact/retention_test.go index 1be73847b04a..4de78197e5a7 100644 --- a/src/controller/event/handler/webhook/artifact/retention_test.go +++ b/src/controller/event/handler/webhook/artifact/retention_test.go @@ -115,6 +115,6 @@ func TestRetentionHandler_IsStateful(t *testing.T) { } func TestMain(m *testing.M) { - dao.PrepareTestForPostgresSQL() + dao.PrepareTestForDB() os.Exit(m.Run()) } diff --git a/src/controller/event/handler/webhook/quota/quota_test.go b/src/controller/event/handler/webhook/quota/quota_test.go index 78a521ce82f2..8245db959ca2 100644 --- a/src/controller/event/handler/webhook/quota/quota_test.go +++ b/src/controller/event/handler/webhook/quota/quota_test.go @@ -50,7 +50,7 @@ func TestQuotaPreprocessHandler(t *testing.T) { // SetupSuite prepares env for test suite. func (suite *QuotaPreprocessHandlerSuite) SetupSuite() { - common_dao.PrepareTestForPostgresSQL() + common_dao.PrepareTestForDB() cfg := map[string]interface{}{ common.NotificationEnable: true, } diff --git a/src/controller/event/handler/webhook/scan/scan_test.go b/src/controller/event/handler/webhook/scan/scan_test.go index 8868088d3608..7cc7481061de 100644 --- a/src/controller/event/handler/webhook/scan/scan_test.go +++ b/src/controller/event/handler/webhook/scan/scan_test.go @@ -60,7 +60,7 @@ func TestScanImagePreprocessHandler(t *testing.T) { // SetupSuite prepares env for test suite. func (suite *ScanImagePreprocessHandlerSuite) SetupSuite() { - common_dao.PrepareTestForPostgresSQL() + common_dao.PrepareTestForDB() cfg := map[string]interface{}{ common.NotificationEnable: true, } diff --git a/src/controller/ldap/controller_test.go b/src/controller/ldap/controller_test.go index 384fb91a5582..9a9f7201a2f7 100644 --- a/src/controller/ldap/controller_test.go +++ b/src/controller/ldap/controller_test.go @@ -32,11 +32,11 @@ var defaultConfigWithVerifyCert = map[string]interface{}{ common.ExtEndpoint: "https://host01.com", common.AUTHMode: common.LDAPAuth, common.DatabaseType: "postgresql", - common.PostGreSQLHOST: "127.0.0.1", - common.PostGreSQLPort: 5432, - common.PostGreSQLUsername: "postgres", - common.PostGreSQLPassword: "root123", - common.PostGreSQLDatabase: "registry", + common.DBHOST: "127.0.0.1", + common.DBPort: 5432, + common.DBUsername: "postgres", + common.DBPassword: "root123", + common.DBDatabase: "registry", common.SelfRegistration: true, common.LDAPURL: "ldap://127.0.0.1:389", common.LDAPSearchDN: "cn=admin,dc=example,dc=com", diff --git a/src/controller/retention/controller_test.go b/src/controller/retention/controller_test.go index 0ad8c97b2f1a..08c4f8ae163f 100644 --- a/src/controller/retention/controller_test.go +++ b/src/controller/retention/controller_test.go @@ -49,7 +49,7 @@ func (s *ControllerTestSuite) SetupSuite() { } func TestMain(m *testing.M) { - dao.PrepareTestForPostgresSQL() + dao.PrepareTestForDB() os.Exit(m.Run()) } diff --git a/src/controller/usergroup/test/controller_test.go b/src/controller/usergroup/test/controller_test.go index b9a9b90a340d..1cb97829ffdd 100644 --- a/src/controller/usergroup/test/controller_test.go +++ b/src/controller/usergroup/test/controller_test.go @@ -40,11 +40,11 @@ var defaultConfigWithVerifyCert = map[string]interface{}{ common.ExtEndpoint: "https://host01.com", common.AUTHMode: common.LDAPAuth, common.DatabaseType: "postgresql", - common.PostGreSQLHOST: "127.0.0.1", - common.PostGreSQLPort: 5432, - common.PostGreSQLUsername: "postgres", - common.PostGreSQLPassword: "root123", - common.PostGreSQLDatabase: "registry", + common.DBHOST: "127.0.0.1", + common.DBPort: 5432, + common.DBUsername: "postgres", + common.DBPassword: "root123", + common.DBDatabase: "registry", common.SelfRegistration: true, common.LDAPURL: "ldap://127.0.0.1:389", common.LDAPSearchDN: "cn=admin,dc=example,dc=com", diff --git a/src/core/auth/authproxy/auth_test.go b/src/core/auth/authproxy/auth_test.go index 266937a678e3..e22d9c5e2fa5 100644 --- a/src/core/auth/authproxy/auth_test.go +++ b/src/core/auth/authproxy/auth_test.go @@ -57,12 +57,12 @@ func TestMain(m *testing.M) { common.HTTPAuthProxyEndpoint: a.Endpoint, common.HTTPAuthProxyTokenReviewEndpoint: a.TokenReviewEndpoint, common.HTTPAuthProxyVerifyCert: false, - common.PostGreSQLSSLMode: cfgMap[common.PostGreSQLSSLMode], - common.PostGreSQLUsername: cfgMap[common.PostGreSQLUsername], - common.PostGreSQLPort: cfgMap[common.PostGreSQLPort], - common.PostGreSQLHOST: cfgMap[common.PostGreSQLHOST], - common.PostGreSQLPassword: cfgMap[common.PostGreSQLPassword], - common.PostGreSQLDatabase: cfgMap[common.PostGreSQLDatabase], + common.DBSSLMode: cfgMap[common.DBSSLMode], + common.DBUsername: cfgMap[common.DBUsername], + common.DBPort: cfgMap[common.DBPort], + common.DBHOST: cfgMap[common.DBHOST], + common.DBPassword: cfgMap[common.DBPassword], + common.DBDatabase: cfgMap[common.DBDatabase], } config.InitWithSettings(conf) diff --git a/src/core/auth/ldap/ldap_test.go b/src/core/auth/ldap/ldap_test.go index d26493505afb..6555451a4c4b 100644 --- a/src/core/auth/ldap/ldap_test.go +++ b/src/core/auth/ldap/ldap_test.go @@ -38,14 +38,14 @@ import ( ) var ldapTestConfig = map[string]interface{}{ - common.ExtEndpoint: "host01.com", - common.AUTHMode: "ldap_auth", - common.DatabaseType: "postgresql", - common.PostGreSQLHOST: "127.0.0.1", - common.PostGreSQLPort: 5432, - common.PostGreSQLUsername: "postgres", - common.PostGreSQLPassword: "root123", - common.PostGreSQLDatabase: "registry", + common.ExtEndpoint: "host01.com", + common.AUTHMode: "ldap_auth", + common.DatabaseType: "postgresql", + common.DBHOST: "127.0.0.1", + common.DBPort: 5432, + common.DBUsername: "postgres", + common.DBPassword: "root123", + common.DBDatabase: "registry", // config.SelfRegistration: true, common.LDAPURL: "ldap://127.0.0.1", common.LDAPSearchDN: "cn=admin,dc=example,dc=com", diff --git a/src/jobservice/job/impl/context_test.go b/src/jobservice/job/impl/context_test.go index 7706a9f3c6cb..4bc883cb1f67 100644 --- a/src/jobservice/job/impl/context_test.go +++ b/src/jobservice/job/impl/context_test.go @@ -48,7 +48,7 @@ type ContextImplTestSuite struct { // TestContextImplTestSuite is entry of go test func TestContextImplTestSuite(t *testing.T) { - common_dao.PrepareTestForPostgresSQL() + common_dao.PrepareTestForDB() suite.Run(t, new(ContextImplTestSuite)) } diff --git a/src/jobservice/logger/backend/db_logger_test.go b/src/jobservice/logger/backend/db_logger_test.go index fbe000d06308..203c490082d4 100644 --- a/src/jobservice/logger/backend/db_logger_test.go +++ b/src/jobservice/logger/backend/db_logger_test.go @@ -13,7 +13,7 @@ import ( func TestMain(m *testing.M) { // databases := []string{"mysql", "sqlite"} - databases := []string{"postgresql"} + databases := []string{"postgresql", "mysql", "mariadb"} for _, database := range databases { log.Infof("run test cases for database: %s", database) @@ -21,6 +21,8 @@ func TestMain(m *testing.M) { switch database { case "postgresql": dao.PrepareTestForPostgresSQL() + case "mysql", "mariadb": + dao.PrepareTestForMySQL() default: log.Fatalf("invalid database: %s", database) } diff --git a/src/jobservice/logger/entry_test.go b/src/jobservice/logger/entry_test.go index 105f3e9a8f88..a084145eb111 100644 --- a/src/jobservice/logger/entry_test.go +++ b/src/jobservice/logger/entry_test.go @@ -13,7 +13,7 @@ import ( func TestMain(m *testing.M) { // databases := []string{"mysql", "sqlite"} - databases := []string{"postgresql"} + databases := []string{"postgresql", "mysql", "mariadb"} for _, database := range databases { log.Infof("run test cases for database: %s", database) @@ -21,6 +21,8 @@ func TestMain(m *testing.M) { switch database { case "postgresql": dao.PrepareTestForPostgresSQL() + case "mysql", "mariadb": + dao.PrepareTestForMySQL() default: log.Fatalf("invalid database: %s", database) } diff --git a/src/jobservice/logger/getter/db_getter_test.go b/src/jobservice/logger/getter/db_getter_test.go index bdd2ad85eea7..4fbc0c1531b3 100644 --- a/src/jobservice/logger/getter/db_getter_test.go +++ b/src/jobservice/logger/getter/db_getter_test.go @@ -11,7 +11,7 @@ import ( ) func TestMain(m *testing.M) { - databases := []string{"postgresql"} + databases := []string{"postgresql", "mysql", "mariadb"} for _, database := range databases { log.Infof("run test cases for database: %s", database) @@ -19,6 +19,8 @@ func TestMain(m *testing.M) { switch database { case "postgresql": dao.PrepareTestForPostgresSQL() + case "mysql", "mariadb": + dao.PrepareTestForMySQL() default: log.Fatalf("invalid database: %s", database) } diff --git a/src/jobservice/logger/sweeper/db_sweeper_test.go b/src/jobservice/logger/sweeper/db_sweeper_test.go index 631eed87ce1f..c0f41b7b2010 100644 --- a/src/jobservice/logger/sweeper/db_sweeper_test.go +++ b/src/jobservice/logger/sweeper/db_sweeper_test.go @@ -10,7 +10,7 @@ import ( ) func TestMain(m *testing.M) { - databases := []string{"postgresql"} + databases := []string{"postgresql", "mysql", "mariadb"} for _, database := range databases { log.Infof("run test cases for database: %s", database) @@ -18,6 +18,8 @@ func TestMain(m *testing.M) { switch database { case "postgresql": dao.PrepareTestForPostgresSQL() + case "mysql", "mariadb": + dao.PrepareTestForMySQL() default: log.Fatalf("invalid database: %s", database) } diff --git a/src/jobservice/runner/redis_test.go b/src/jobservice/runner/redis_test.go index b3a5fb1cd20f..1e00555704be 100644 --- a/src/jobservice/runner/redis_test.go +++ b/src/jobservice/runner/redis_test.go @@ -15,13 +15,13 @@ package runner import ( "context" + common_dao "github.com/goharbor/harbor/src/common/dao" "os" "sync" "testing" "time" "github.com/gocraft/work" - common_dao "github.com/goharbor/harbor/src/common/dao" "github.com/goharbor/harbor/src/jobservice/config" "github.com/goharbor/harbor/src/jobservice/env" "github.com/goharbor/harbor/src/jobservice/job" @@ -50,7 +50,7 @@ type RedisRunnerTestSuite struct { // TestRedisRunnerTestSuite is entry of go test func TestRedisRunnerTestSuite(t *testing.T) { - common_dao.PrepareTestForPostgresSQL() + common_dao.PrepareTestForDB() suite.Run(t, new(RedisRunnerTestSuite)) } diff --git a/src/jobservice/runtime/bootstrap_test.go b/src/jobservice/runtime/bootstrap_test.go index 94f641e51988..639ead72a1ea 100644 --- a/src/jobservice/runtime/bootstrap_test.go +++ b/src/jobservice/runtime/bootstrap_test.go @@ -40,7 +40,7 @@ type BootStrapTestSuite struct { // SetupSuite prepares test suite func (suite *BootStrapTestSuite) SetupSuite() { - dao.PrepareTestForPostgresSQL() + dao.PrepareTestForDB() // Load configurations err := config.DefaultConfig.Load("../config_test.yml", true) diff --git a/src/jobservice/sync/schedule_test.go b/src/jobservice/sync/schedule_test.go index f73714d71eec..90bfb8781eb4 100644 --- a/src/jobservice/sync/schedule_test.go +++ b/src/jobservice/sync/schedule_test.go @@ -48,7 +48,7 @@ func TestWorker(t *testing.T) { func (suite *WorkerTestSuite) SetupSuite() { sysContext := context.TODO() - dao.PrepareTestForPostgresSQL() + dao.PrepareTestForDB() getPolicies := func() ([]*period.Policy, error) { return []*period.Policy{ diff --git a/src/jobservice/worker/cworker/c_worker_test.go b/src/jobservice/worker/cworker/c_worker_test.go index 584315706f6d..88e1d9be39da 100644 --- a/src/jobservice/worker/cworker/c_worker_test.go +++ b/src/jobservice/worker/cworker/c_worker_test.go @@ -53,7 +53,7 @@ type CWorkerTestSuite struct { func (suite *CWorkerTestSuite) SetupSuite() { suite.namespace = tests.GiveMeTestNamespace() suite.pool = tests.GiveMeRedisPool() - common_dao.PrepareTestForPostgresSQL() + common_dao.PrepareTestForDB() // Append node ID vCtx := context.WithValue(context.Background(), utils.NodeID, utils.GenerateNodeID()) diff --git a/src/lib/config/metadata/metadatalist.go b/src/lib/config/metadata/metadatalist.go index 9e80e85c726c..1565b148f818 100644 --- a/src/lib/config/metadata/metadatalist.go +++ b/src/lib/config/metadata/metadatalist.go @@ -22,7 +22,7 @@ type Item struct { Scope string `json:"scope,omitempty"` // email, ldapbasic, ldapgroup, uaa settings, used to retieve configure items by group Group string `json:"group,omitempty"` - // environment key to retrieves this value when initialize, for example: POSTGRESQL_HOST, only used for system settings, for user settings no EnvKey + // environment key to retrieves this value when initialize, for example: DB_HOST, only used for system settings, for user settings no EnvKey EnvKey string `json:"environment_key,omitempty"` // The default string value for this key DefaultValue string `json:"default_value,omitempty"` @@ -105,22 +105,14 @@ var ( {Name: common.NotaryURL, Scope: SystemScope, Group: BasicGroup, EnvKey: "NOTARY_URL", DefaultValue: "http://notary-server:4443", ItemType: &StringType{}, Editable: false}, {Name: common.ScanAllPolicy, Scope: UserScope, Group: BasicGroup, EnvKey: "", DefaultValue: "", ItemType: &MapType{}, Editable: false, Description: `The policy to scan images`}, - {Name: common.PostGreSQLDatabase, Scope: SystemScope, Group: DatabaseGroup, EnvKey: "POSTGRESQL_DATABASE", DefaultValue: "registry", ItemType: &StringType{}, Editable: false}, - {Name: common.PostGreSQLHOST, Scope: SystemScope, Group: DatabaseGroup, EnvKey: "POSTGRESQL_HOST", DefaultValue: "postgresql", ItemType: &StringType{}, Editable: false}, - {Name: common.PostGreSQLPassword, Scope: SystemScope, Group: DatabaseGroup, EnvKey: "POSTGRESQL_PASSWORD", DefaultValue: "root123", ItemType: &PasswordType{}, Editable: false}, - {Name: common.PostGreSQLPort, Scope: SystemScope, Group: DatabaseGroup, EnvKey: "POSTGRESQL_PORT", DefaultValue: "5432", ItemType: &PortType{}, Editable: false}, - {Name: common.PostGreSQLSSLMode, Scope: SystemScope, Group: DatabaseGroup, EnvKey: "POSTGRESQL_SSLMODE", DefaultValue: "disable", ItemType: &StringType{}, Editable: false}, - {Name: common.PostGreSQLUsername, Scope: SystemScope, Group: DatabaseGroup, EnvKey: "POSTGRESQL_USERNAME", DefaultValue: "postgres", ItemType: &StringType{}, Editable: false}, - {Name: common.PostGreSQLMaxIdleConns, Scope: SystemScope, Group: DatabaseGroup, EnvKey: "POSTGRESQL_MAX_IDLE_CONNS", DefaultValue: "2", ItemType: &IntType{}, Editable: false}, - {Name: common.PostGreSQLMaxOpenConns, Scope: SystemScope, Group: DatabaseGroup, EnvKey: "POSTGRESQL_MAX_OPEN_CONNS", DefaultValue: "0", ItemType: &IntType{}, Editable: false}, - - {Name: common.MySQLDatabase, Scope: SystemScope, Group: DatabaseGroup, EnvKey: "MYSQL_DATABASE", DefaultValue: "registry", ItemType: &StringType{}, Editable: false}, - {Name: common.MySQLHOST, Scope: SystemScope, Group: DatabaseGroup, EnvKey: "MYSQL_HOST", DefaultValue: "mysql", ItemType: &StringType{}, Editable: false}, - {Name: common.MySQLPassword, Scope: SystemScope, Group: DatabaseGroup, EnvKey: "MYSQL_PASSWORD", DefaultValue: "root123", ItemType: &PasswordType{}, Editable: false}, - {Name: common.MySQLPort, Scope: SystemScope, Group: DatabaseGroup, EnvKey: "MYSQL_PORT", DefaultValue: "3306", ItemType: &PortType{}, Editable: false}, - {Name: common.MySQLUsername, Scope: SystemScope, Group: DatabaseGroup, EnvKey: "MYSQL_USERNAME", DefaultValue: "root", ItemType: &StringType{}, Editable: false}, - {Name: common.MySQLMaxIdleConns, Scope: SystemScope, Group: DatabaseGroup, EnvKey: "MYSQL_MAX_IDLE_CONNS", DefaultValue: "2", ItemType: &IntType{}, Editable: false}, - {Name: common.MySQLMaxOpenConns, Scope: SystemScope, Group: DatabaseGroup, EnvKey: "MYSQL_MAX_OPEN_CONNS", DefaultValue: "0", ItemType: &IntType{}, Editable: false}, + {Name: common.DBDatabase, Scope: SystemScope, Group: DatabaseGroup, EnvKey: "DB_DATABASE", DefaultValue: "registry", ItemType: &StringType{}, Editable: false}, + {Name: common.DBHOST, Scope: SystemScope, Group: DatabaseGroup, EnvKey: "DB_HOST", DefaultValue: "postgresql", ItemType: &StringType{}, Editable: false}, + {Name: common.DBPassword, Scope: SystemScope, Group: DatabaseGroup, EnvKey: "DB_PASSWORD", DefaultValue: "root123", ItemType: &PasswordType{}, Editable: false}, + {Name: common.DBPort, Scope: SystemScope, Group: DatabaseGroup, EnvKey: "DB_PORT", DefaultValue: "5432", ItemType: &PortType{}, Editable: false}, + {Name: common.DBSSLMode, Scope: SystemScope, Group: DatabaseGroup, EnvKey: "DB_SSLMODE", DefaultValue: "disable", ItemType: &StringType{}, Editable: false}, + {Name: common.DBUsername, Scope: SystemScope, Group: DatabaseGroup, EnvKey: "DB_USERNAME", DefaultValue: "postgres", ItemType: &StringType{}, Editable: false}, + {Name: common.DBMaxIdleConns, Scope: SystemScope, Group: DatabaseGroup, EnvKey: "DB_MAX_IDLE_CONNS", DefaultValue: "2", ItemType: &IntType{}, Editable: false}, + {Name: common.DBMaxOpenConns, Scope: SystemScope, Group: DatabaseGroup, EnvKey: "DB_MAX_OPEN_CONNS", DefaultValue: "0", ItemType: &IntType{}, Editable: false}, {Name: common.ProjectCreationRestriction, Scope: UserScope, Group: BasicGroup, EnvKey: "PROJECT_CREATION_RESTRICTION", DefaultValue: common.ProCrtRestrEveryone, ItemType: &ProjectCreationRestrictionType{}, Editable: false, Description: `Indicate who can create projects, it could be ''adminonly'' or ''everyone''.`}, {Name: common.ReadOnly, Scope: UserScope, Group: BasicGroup, EnvKey: "READ_ONLY", DefaultValue: "false", ItemType: &BoolType{}, Editable: false, Description: `The flag to indicate whether Harbor is in readonly mode.`}, diff --git a/src/lib/config/systemconfig.go b/src/lib/config/systemconfig.go index c0350b34e88c..207eaaa919f5 100644 --- a/src/lib/config/systemconfig.go +++ b/src/lib/config/systemconfig.go @@ -253,25 +253,25 @@ func Database() (*models.Database, error) { switch database.Type { case "", "postgresql": postgresql := &models.PostGreSQL{ - Host: DefaultMgr().Get(backgroundCtx, common.PostGreSQLHOST).GetString(), - Port: DefaultMgr().Get(backgroundCtx, common.PostGreSQLPort).GetInt(), - Username: DefaultMgr().Get(backgroundCtx, common.PostGreSQLUsername).GetString(), - Password: DefaultMgr().Get(backgroundCtx, common.PostGreSQLPassword).GetPassword(), - Database: DefaultMgr().Get(backgroundCtx, common.PostGreSQLDatabase).GetString(), - SSLMode: DefaultMgr().Get(backgroundCtx, common.PostGreSQLSSLMode).GetString(), - MaxIdleConns: DefaultMgr().Get(backgroundCtx, common.PostGreSQLMaxIdleConns).GetInt(), - MaxOpenConns: DefaultMgr().Get(backgroundCtx, common.PostGreSQLMaxOpenConns).GetInt(), + Host: DefaultMgr().Get(backgroundCtx, common.DBHOST).GetString(), + Port: DefaultMgr().Get(backgroundCtx, common.DBPort).GetInt(), + Username: DefaultMgr().Get(backgroundCtx, common.DBUsername).GetString(), + Password: DefaultMgr().Get(backgroundCtx, common.DBPassword).GetPassword(), + Database: DefaultMgr().Get(backgroundCtx, common.DBDatabase).GetString(), + SSLMode: DefaultMgr().Get(backgroundCtx, common.DBSSLMode).GetString(), + MaxIdleConns: DefaultMgr().Get(backgroundCtx, common.DBMaxIdleConns).GetInt(), + MaxOpenConns: DefaultMgr().Get(backgroundCtx, common.DBMaxOpenConns).GetInt(), } database.PostGreSQL = postgresql case "mariadb", "mysql": mysql := &models.MySQL{ - Host: DefaultMgr().Get(backgroundCtx, common.MySQLHOST).GetString(), - Port: DefaultMgr().Get(backgroundCtx, common.MySQLPort).GetInt(), - Username: DefaultMgr().Get(backgroundCtx, common.MySQLUsername).GetString(), - Password: DefaultMgr().Get(backgroundCtx, common.MySQLPassword).GetString(), - Database: DefaultMgr().Get(backgroundCtx, common.MySQLDatabase).GetString(), - MaxIdleConns: DefaultMgr().Get(backgroundCtx, common.MySQLMaxIdleConns).GetInt(), - MaxOpenConns: DefaultMgr().Get(backgroundCtx, common.MySQLMaxOpenConns).GetInt(), + Host: DefaultMgr().Get(backgroundCtx, common.DBHOST).GetString(), + Port: DefaultMgr().Get(backgroundCtx, common.DBPort).GetInt(), + Username: DefaultMgr().Get(backgroundCtx, common.DBUsername).GetString(), + Password: DefaultMgr().Get(backgroundCtx, common.DBPassword).GetString(), + Database: DefaultMgr().Get(backgroundCtx, common.DBDatabase).GetString(), + MaxIdleConns: DefaultMgr().Get(backgroundCtx, common.DBMaxIdleConns).GetInt(), + MaxOpenConns: DefaultMgr().Get(backgroundCtx, common.DBMaxOpenConns).GetInt(), } database.MySQL = mysql } diff --git a/src/lib/orm/test/orm_test.go b/src/lib/orm/test/orm_test.go index 1524f8a84a14..2d9b79459a1d 100644 --- a/src/lib/orm/test/orm_test.go +++ b/src/lib/orm/test/orm_test.go @@ -104,7 +104,7 @@ type OrmSuite struct { // SetupSuite ... func (suite *OrmSuite) SetupSuite() { RegisterModel(&Foo{}) - dao.PrepareTestForPostgresSQL() + dao.PrepareTestForDB() o, err := FromContext(Context()) if err != nil { diff --git a/src/pkg/accessory/dao/dao_test.go b/src/pkg/accessory/dao/dao_test.go index 12ecb3dc406a..4f499df1116a 100644 --- a/src/pkg/accessory/dao/dao_test.go +++ b/src/pkg/accessory/dao/dao_test.go @@ -40,7 +40,7 @@ type daoTestSuite struct { func (d *daoTestSuite) SetupSuite() { d.dao = New() - common_dao.PrepareTestForPostgresSQL() + common_dao.PrepareTestForDB() d.ctx = orm.NewContext(nil, beegoorm.NewOrm()) d.ClearTables = []string{"artifact", "artifact_accessory"} diff --git a/src/pkg/artifact/dao/dao_test.go b/src/pkg/artifact/dao/dao_test.go index c6f70c6f0b4a..49c0286ed182 100644 --- a/src/pkg/artifact/dao/dao_test.go +++ b/src/pkg/artifact/dao/dao_test.go @@ -46,7 +46,7 @@ type daoTestSuite struct { func (d *daoTestSuite) SetupSuite() { d.dao = New() d.tagDAO = tagdao.New() - common_dao.PrepareTestForPostgresSQL() + common_dao.PrepareTestForDB() d.ctx = orm.NewContext(nil, beegoorm.NewOrm()) } diff --git a/src/pkg/audit/dao/dao_test.go b/src/pkg/audit/dao/dao_test.go index cd3d853bf388..56d33b6aa745 100644 --- a/src/pkg/audit/dao/dao_test.go +++ b/src/pkg/audit/dao/dao_test.go @@ -35,7 +35,7 @@ type daoTestSuite struct { func (d *daoTestSuite) SetupSuite() { d.dao = New() - common_dao.PrepareTestForPostgresSQL() + common_dao.PrepareTestForDB() d.ctx = orm.NewContext(nil, beegoorm.NewOrm()) artifactID, err := d.dao.Create(d.ctx, &model.AuditLog{ Operation: "Create", diff --git a/src/pkg/authproxy/http_test.go b/src/pkg/authproxy/http_test.go index b2a876b4416c..e8bcf07ea7b8 100644 --- a/src/pkg/authproxy/http_test.go +++ b/src/pkg/authproxy/http_test.go @@ -14,7 +14,7 @@ import ( ) func TestMain(m *testing.M) { - dao.PrepareTestForPostgresSQL() + dao.PrepareTestForDB() result := m.Run() if result != 0 { os.Exit(result) diff --git a/src/pkg/blob/dao/mysql_dao.go b/src/pkg/blob/dao/mysql_dao.go index fa705cb61a2b..cec9499ebb81 100644 --- a/src/pkg/blob/dao/mysql_dao.go +++ b/src/pkg/blob/dao/mysql_dao.go @@ -26,7 +26,7 @@ import ( "github.com/goharbor/harbor/src/pkg/blob/models" ) -// NewMysqlDao returns an instance of the default DAO +// NewMysqlDao returns an instance of the mysql DAO func NewMysqlDao() DAO { return &mysqlDao{} } diff --git a/src/pkg/config/manager.go b/src/pkg/config/manager.go index ed199bf5a161..6c3077c93778 100644 --- a/src/pkg/config/manager.go +++ b/src/pkg/config/manager.go @@ -161,25 +161,25 @@ func (c *CfgManager) GetDatabaseCfg() *models.Database { switch database.Type { case "", "postgresql": postgresql := &models.PostGreSQL{ - Host: c.Get(ctx, common.PostGreSQLHOST).GetString(), - Port: c.Get(ctx, common.PostGreSQLPort).GetInt(), - Username: c.Get(ctx, common.PostGreSQLUsername).GetString(), - Password: c.Get(ctx, common.PostGreSQLPassword).GetString(), - Database: c.Get(ctx, common.PostGreSQLDatabase).GetString(), - SSLMode: c.Get(ctx, common.PostGreSQLSSLMode).GetString(), - MaxIdleConns: c.Get(ctx, common.PostGreSQLMaxIdleConns).GetInt(), - MaxOpenConns: c.Get(ctx, common.PostGreSQLMaxOpenConns).GetInt(), + Host: c.Get(ctx, common.DBHOST).GetString(), + Port: c.Get(ctx, common.DBPort).GetInt(), + Username: c.Get(ctx, common.DBUsername).GetString(), + Password: c.Get(ctx, common.DBPassword).GetString(), + Database: c.Get(ctx, common.DBDatabase).GetString(), + SSLMode: c.Get(ctx, common.DBSSLMode).GetString(), + MaxIdleConns: c.Get(ctx, common.DBMaxIdleConns).GetInt(), + MaxOpenConns: c.Get(ctx, common.DBMaxOpenConns).GetInt(), } database.PostGreSQL = postgresql case "mariadb", "mysql": mysql := &models.MySQL{ - Host: c.Get(ctx, common.MySQLHOST).GetString(), - Port: c.Get(ctx, common.MySQLPort).GetInt(), - Username: c.Get(ctx, common.MySQLUsername).GetString(), - Password: c.Get(ctx, common.MySQLPassword).GetString(), - Database: c.Get(ctx, common.MySQLDatabase).GetString(), - MaxIdleConns: c.Get(ctx, common.MySQLMaxIdleConns).GetInt(), - MaxOpenConns: c.Get(ctx, common.MySQLMaxOpenConns).GetInt(), + Host: c.Get(ctx, common.DBHOST).GetString(), + Port: c.Get(ctx, common.DBPort).GetInt(), + Username: c.Get(ctx, common.DBUsername).GetString(), + Password: c.Get(ctx, common.DBPassword).GetString(), + Database: c.Get(ctx, common.DBDatabase).GetString(), + MaxIdleConns: c.Get(ctx, common.DBMaxIdleConns).GetInt(), + MaxOpenConns: c.Get(ctx, common.DBMaxOpenConns).GetInt(), } database.MySQL = mysql } diff --git a/src/pkg/immutable/dao/dao_test.go b/src/pkg/immutable/dao/dao_test.go index d471c8ceb33b..edf8fbeeed7a 100644 --- a/src/pkg/immutable/dao/dao_test.go +++ b/src/pkg/immutable/dao/dao_test.go @@ -24,7 +24,7 @@ type immutableRuleDaoTestSuite struct { func (t *immutableRuleDaoTestSuite) SetupSuite() { t.require = require.New(t.T()) t.assert = assert.New(t.T()) - dao.PrepareTestForPostgresSQL() + dao.PrepareTestForDB() t.dao = New() } diff --git a/src/pkg/immutable/match/rule/match_test.go b/src/pkg/immutable/match/rule/match_test.go index afd58bb6d0d6..ad7e77d9b215 100644 --- a/src/pkg/immutable/match/rule/match_test.go +++ b/src/pkg/immutable/match/rule/match_test.go @@ -156,7 +156,7 @@ func (s *MatchTestSuite) TearDownSuite() { } func TestMain(m *testing.M) { - dao.PrepareTestForPostgresSQL() + dao.PrepareTestForDB() if result := m.Run(); result != 0 { os.Exit(result) diff --git a/src/pkg/label/dao/dao_test.go b/src/pkg/label/dao/dao_test.go index 0df550015096..a8900a5c347f 100644 --- a/src/pkg/label/dao/dao_test.go +++ b/src/pkg/label/dao/dao_test.go @@ -39,7 +39,7 @@ type labelDaoTestSuite struct { } func (l *labelDaoTestSuite) SetupSuite() { - common_dao.PrepareTestForPostgresSQL() + common_dao.PrepareTestForDB() l.dao = &defaultDAO{} l.artDAO = artdao.New() l.ctx = orm.NewContext(nil, beegoorm.NewOrm()) diff --git a/src/pkg/notifier/handler/notification/slack_handler_test.go b/src/pkg/notifier/handler/notification/slack_handler_test.go index c2d5e1899dc3..1a3f89fee57f 100644 --- a/src/pkg/notifier/handler/notification/slack_handler_test.go +++ b/src/pkg/notifier/handler/notification/slack_handler_test.go @@ -15,7 +15,7 @@ import ( ) func TestSlackHandler_Handle(t *testing.T) { - dao.PrepareTestForPostgresSQL() + dao.PrepareTestForDB() hookMgr := notification.HookManager defer func() { notification.HookManager = hookMgr diff --git a/src/pkg/notifier/notifier_test.go b/src/pkg/notifier/notifier_test.go index 451495144328..8b3647c3225e 100644 --- a/src/pkg/notifier/notifier_test.go +++ b/src/pkg/notifier/notifier_test.go @@ -118,7 +118,7 @@ func TestSubscribeAndUnSubscribe(t *testing.T) { } func TestPublish(t *testing.T) { - dao.PrepareTestForPostgresSQL() + dao.PrepareTestForDB() count := len(notificationWatcher.handlers) err := Subscribe("topic1", &fakeStatefulHandler{0}) if err != nil { @@ -160,7 +160,7 @@ func TestPublish(t *testing.T) { } func TestConcurrentPublish(t *testing.T) { - dao.PrepareTestForPostgresSQL() + dao.PrepareTestForDB() count := len(notificationWatcher.handlers) err := Subscribe("topic1", &fakeStatefulHandler{0}) if err != nil { diff --git a/src/pkg/p2p/preheat/dao/instance/dao_test.go b/src/pkg/p2p/preheat/dao/instance/dao_test.go index 8d36688c72e2..283b4f34784d 100644 --- a/src/pkg/p2p/preheat/dao/instance/dao_test.go +++ b/src/pkg/p2p/preheat/dao/instance/dao_test.go @@ -36,7 +36,7 @@ type instanceSuite struct { } func (is *instanceSuite) SetupSuite() { - common_dao.PrepareTestForPostgresSQL() + common_dao.PrepareTestForDB() is.ctx = orm.NewContext(nil, beego_orm.NewOrm()) is.dao = New() } diff --git a/src/pkg/p2p/preheat/dao/policy/dao_test.go b/src/pkg/p2p/preheat/dao/policy/dao_test.go index 32bd35dcbb1c..e5cd657324f6 100644 --- a/src/pkg/p2p/preheat/dao/policy/dao_test.go +++ b/src/pkg/p2p/preheat/dao/policy/dao_test.go @@ -43,7 +43,7 @@ func TestDaoTestSuite(t *testing.T) { // SetupSuite setups testing env. func (d *daoTestSuite) SetupSuite() { - common_dao.PrepareTestForPostgresSQL() + common_dao.PrepareTestForDB() d.dao = New() d.ctx = orm.NewContext(nil, beego_orm.NewOrm()) d.defaultPolicy = &policy.Schema{ diff --git a/src/pkg/reg/dao/dao_test.go b/src/pkg/reg/dao/dao_test.go index a8d3f7a0162a..b19d1f59c827 100644 --- a/src/pkg/reg/dao/dao_test.go +++ b/src/pkg/reg/dao/dao_test.go @@ -35,7 +35,7 @@ type daoTestSuite struct { func (d *daoTestSuite) SetupSuite() { d.dao = NewDAO() - common_dao.PrepareTestForPostgresSQL() + common_dao.PrepareTestForDB() d.ctx = orm.NewContext(nil, beegoorm.NewOrm()) } diff --git a/src/pkg/replication/dao/dao_test.go b/src/pkg/replication/dao/dao_test.go index ba5134a10c59..879e28d15211 100644 --- a/src/pkg/replication/dao/dao_test.go +++ b/src/pkg/replication/dao/dao_test.go @@ -36,7 +36,7 @@ type daoTestSuite struct { func (d *daoTestSuite) SetupSuite() { d.dao = NewDAO() - common_dao.PrepareTestForPostgresSQL() + common_dao.PrepareTestForDB() d.ctx = orm.NewContext(nil, beegoorm.NewOrm()) } diff --git a/src/pkg/repository/dao/dao_test.go b/src/pkg/repository/dao/dao_test.go index 1d7280544a59..c73144bb974a 100644 --- a/src/pkg/repository/dao/dao_test.go +++ b/src/pkg/repository/dao/dao_test.go @@ -50,7 +50,7 @@ func (d *daoTestSuite) SetupSuite() { d.dao = New() d.tagDao = tag_dao.New() d.afDao = af_dao.New() - common_dao.PrepareTestForPostgresSQL() + common_dao.PrepareTestForDB() d.ctx = orm.NewContext(nil, beegoorm.NewOrm()) } diff --git a/src/pkg/retention/dao/retention_test.go b/src/pkg/retention/dao/retention_test.go index e6b65f4344b4..87696a04e496 100644 --- a/src/pkg/retention/dao/retention_test.go +++ b/src/pkg/retention/dao/retention_test.go @@ -16,7 +16,7 @@ import ( ) func TestMain(m *testing.M) { - dao.PrepareTestForPostgresSQL() + dao.PrepareTestForDB() os.Exit(m.Run()) } diff --git a/src/pkg/retention/manager_test.go b/src/pkg/retention/manager_test.go index 32f538392239..36746087ea5a 100644 --- a/src/pkg/retention/manager_test.go +++ b/src/pkg/retention/manager_test.go @@ -12,7 +12,7 @@ import ( ) func TestMain(m *testing.M) { - dao.PrepareTestForPostgresSQL() + dao.PrepareTestForDB() os.Exit(m.Run()) } diff --git a/src/pkg/retention/policy/action/performer_test.go b/src/pkg/retention/policy/action/performer_test.go index dec39f2b6841..8656890625e2 100644 --- a/src/pkg/retention/policy/action/performer_test.go +++ b/src/pkg/retention/policy/action/performer_test.go @@ -68,7 +68,7 @@ func (suite *TestPerformerSuite) SetupSuite() { suite.oldClient = dep.DefaultClient dep.DefaultClient = &fakeRetentionClient{} - dao.PrepareTestForPostgresSQL() + dao.PrepareTestForDB() } // TearDownSuite ... diff --git a/src/pkg/retention/policy/alg/or/processor_test.go b/src/pkg/retention/policy/alg/or/processor_test.go index d11746e42277..70d5c6ccc8a0 100644 --- a/src/pkg/retention/policy/alg/or/processor_test.go +++ b/src/pkg/retention/policy/alg/or/processor_test.go @@ -52,7 +52,7 @@ func TestProcessor(t *testing.T) { // SetupSuite ... func (suite *ProcessorTestSuite) SetupSuite() { - dao.PrepareTestForPostgresSQL() + dao.PrepareTestForDB() suite.all = []*selector.Candidate{ { Namespace: "library", diff --git a/src/pkg/retention/policy/builder_test.go b/src/pkg/retention/policy/builder_test.go index 3fe00b77895e..b138664bdcdd 100644 --- a/src/pkg/retention/policy/builder_test.go +++ b/src/pkg/retention/policy/builder_test.go @@ -67,7 +67,7 @@ func TestBuilder(t *testing.T) { // SetupSuite prepares the testing content if needed func (suite *TestBuilderSuite) SetupSuite() { - dao.PrepareTestForPostgresSQL() + dao.PrepareTestForDB() suite.all = []*selector.Candidate{ { NamespaceID: 1, diff --git a/src/pkg/scan/report/manager_test.go b/src/pkg/scan/report/manager_test.go index 74ba14119404..bf277f2d8fed 100644 --- a/src/pkg/scan/report/manager_test.go +++ b/src/pkg/scan/report/manager_test.go @@ -40,7 +40,7 @@ func TestManager(t *testing.T) { // SetupSuite prepares test env for suite TestManagerSuite. func (suite *TestManagerSuite) SetupSuite() { - dao.PrepareTestForPostgresSQL() + dao.PrepareTestForDB() suite.m = NewManager() } diff --git a/src/pkg/scheduler/dao_test.go b/src/pkg/scheduler/dao_test.go index 4236805a0886..d110822bbdda 100644 --- a/src/pkg/scheduler/dao_test.go +++ b/src/pkg/scheduler/dao_test.go @@ -34,7 +34,7 @@ type daoTestSuite struct { func (d *daoTestSuite) SetupSuite() { d.dao = &dao{} - common_dao.PrepareTestForPostgresSQL() + common_dao.PrepareTestForDB() d.ctx = orm.Context() } diff --git a/src/pkg/tag/dao/dao_test.go b/src/pkg/tag/dao/dao_test.go index acb8494962db..08e6f47852c5 100644 --- a/src/pkg/tag/dao/dao_test.go +++ b/src/pkg/tag/dao/dao_test.go @@ -39,7 +39,7 @@ type daoTestSuite struct { func (d *daoTestSuite) SetupSuite() { d.dao = New() - common_dao.PrepareTestForPostgresSQL() + common_dao.PrepareTestForDB() d.ctx = orm.NewContext(nil, beegoorm.NewOrm()) d.artDAO = artdao.New() artifactID, err := d.artDAO.Create(d.ctx, &artdao.Artifact{ diff --git a/src/pkg/task/dao/execution_test.go b/src/pkg/task/dao/execution_test.go index 92fefb6385b7..6626f9ec1648 100644 --- a/src/pkg/task/dao/execution_test.go +++ b/src/pkg/task/dao/execution_test.go @@ -36,7 +36,7 @@ type executionDAOTestSuite struct { } func (e *executionDAOTestSuite) SetupSuite() { - dao.PrepareTestForPostgresSQL() + dao.PrepareTestForDB() e.ctx = orm.Context() e.taskDao = &taskDAO{} e.executionDAO = &executionDAO{ diff --git a/src/server/middleware/immutable/pushmf_test.go b/src/server/middleware/immutable/pushmf_test.go index 82788dcb5276..c4aa8aa7fc39 100644 --- a/src/server/middleware/immutable/pushmf_test.go +++ b/src/server/middleware/immutable/pushmf_test.go @@ -177,7 +177,7 @@ func (suite *HandlerSuite) TestPutDeleteManifestCreated() { } func TestMain(m *testing.M) { - dao.PrepareTestForPostgresSQL() + dao.PrepareTestForDB() if result := m.Run(); result != 0 { os.Exit(result) diff --git a/src/testing/suite.go b/src/testing/suite.go index dd1b5e09fbd5..2bbed1cbe289 100644 --- a/src/testing/suite.go +++ b/src/testing/suite.go @@ -24,7 +24,6 @@ import ( "math/rand" "net/http" "net/http/httptest" - "os" "sync" "time" @@ -55,16 +54,7 @@ type Suite struct { func (suite *Suite) SetupSuite() { once.Do(func() { config.Init() - dbType := os.Getenv("DATABASE_TYPE") - - switch dbType { - case "", "postgresql": - dao.PrepareTestForPostgresSQL() - case "mysql", "mariadb": - dao.PrepareTestForMySQL() - default: - dao.PrepareTestForPostgresSQL() - } + dao.PrepareTestForDB() }) } diff --git a/tests/ci/ut_run.sh b/tests/ci/ut_run.sh index 3c3ad3375306..cc437a0ee2c4 100755 --- a/tests/ci/ut_run.sh +++ b/tests/ci/ut_run.sh @@ -17,5 +17,5 @@ docker ps DIR="$(cd "$(dirname "$0")" && pwd)" go test -race -i ./src/core ./src/jobservice -sudo -E env "PATH=$PATH" "POSTGRES_MIGRATION_SCRIPTS_PATH=$DIR/../../make/migrations/postgresql/" ./tests/coverage4gotest.sh +sudo -E env "PATH=$PATH" "POSTGRES_MIGRATION_SCRIPTS_PATH=$DIR/../../make/migrations/postgresql/" "MYSQL_MIGRATION_SCRIPTS_PATH=$DIR/../../make/migrations/mysql/" ./tests/coverage4gotest.sh #goveralls -coverprofile=profile.cov -service=github || true \ No newline at end of file