From 288c7790d035959fe73f273dc2b086693e1cf9c9 Mon Sep 17 00:00:00 2001 From: wang yan Date: Fri, 6 Mar 2020 03:03:26 +0800 Subject: [PATCH] add sql for migrating access log 1, loop each access log, change to resource/resource_type, and insert into audit log 2, loop each first push operation, change it to create repository and insert into audit log. Signed-off-by: wang yan --- .../postgresql/0030_2.0.0_schema.up.sql | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/make/migrations/postgresql/0030_2.0.0_schema.up.sql b/make/migrations/postgresql/0030_2.0.0_schema.up.sql index 8ce0d9439..eced4de21 100644 --- a/make/migrations/postgresql/0030_2.0.0_schema.up.sql +++ b/make/migrations/postgresql/0030_2.0.0_schema.up.sql @@ -184,3 +184,24 @@ CREATE TABLE audit_log username varchar(255) NOT NULL, op_time timestamp default CURRENT_TIMESTAMP ); + +/*migrate access log to audit log*/ +/*TODO drop table access_log?*/ +DO $$ +DECLARE + access RECORD; +BEGIN + FOR access IN SELECT * FROM access_log + LOOP + /*insert project create and delete*/ + IF (access.operation = 'create' AND access.repo_tag = 'N/A') OR (access.operation = 'delete' AND access.repo_tag = 'N/A') THEN + INSERT INTO audit_log (project_id, operation, resource_type, resource, username, op_time) VALUES (access.project_id, access.operation, 'project', access.repo_name, access.username, access.op_time); + ELSIF access.operation = 'delete' AND access.repo_tag != 'N/A' THEN + INSERT INTO audit_log (project_id, operation, resource_type, resource, username, op_time) VALUES (access.project_id, 'delete', 'artifact', CONCAT(access.repo_name,':',access.repo_tag), access.username, access.op_time); + ELSIF access.operation = 'push' THEN + INSERT INTO audit_log (project_id, operation, resource_type, resource, username, op_time) VALUES (access.project_id, 'create', 'artifact', CONCAT(access.repo_name,':',access.repo_tag), access.username, access.op_time); + ELSIF access.operation = 'pull' THEN + INSERT INTO audit_log (project_id, operation, resource_type, resource, username, op_time) VALUES (access.project_id, 'pull', 'artifact', CONCAT(access.repo_name,':',access.repo_tag), access.username, access.op_time); + END IF; + END LOOP; +END $$;