-
Notifications
You must be signed in to change notification settings - Fork 1
/
batch-update-script.sql
89 lines (69 loc) · 1.8 KB
/
batch-update-script.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
-- Create Employee table
CREATE TABLE IF NOT EXISTS "Employee"."Employee"
(
id integer NOT NULL,
name character varying(200) NOT NULL,
authentication_key character varying(100),
CONSTRAINT "Employee_pkey" PRIMARY KEY (id)
);
-- Inserting data into table
Insert into "Employee"."Employee"(id,name,authentication_key) Values
(1,'A',null),
(2,'B',null),
(3,'C',null),
(4,'D',null),
(5,'E',null),
(6,'F',null),
(7,'G',null),
(8,'H',null),
(9,'I',null),
(10,'J',null),
(11,'K',null),
(12,'L',null),
(13,'M',null),
(14,'N',null),
(15,'O',null),
(16,'P',null),
(17,'Q',null),
(18,'R',null),
(19,'S',null),
(20,'T',null),
(21,'U',null),
(22,'V',null),
(23,'W',null),
(24,'X',null),
(25,'Y',null),
(26,'Z',null);
-- Temp table creation
CREATE TEMP TABLE emp_auth_to_be_updated AS
SELECT ROW_NUMBER() OVER(ORDER BY id) row_id, id
FROM "Employee"."Employee"
WHERE authentication_key is null ;
-- create index on row id
CREATE INDEX ON emp_auth_to_be_updated(row_id);
-- Procedure
CREATE OR REPLACE PROCEDURE "Employee".update_auth_token()
LANGUAGE plpgsql
AS $$
DECLARE
-- variable declaration
total_records int;
batch_size int:=5;
counter int:=0;
BEGIN
SELECT INTO total_records COUNT(*) FROM "Employee"."Employee" e WHERE authentication_key is NULL;
RAISE INFO 'Total records to be updated %', total_records ;
WHILE counter <= total_records LOOP
UPDATE "Employee"."Employee" emp
SET authentication_key = encode(gen_random_bytes(32), 'base64')
FROM emp_auth_to_be_updated eatbu
WHERE eatbu.id = emp.id
AND eatbu.row_id > counter AND eatbu.row_id <= counter+batch_size;
COMMIT;
counter := counter+batch_size;
END LOOP ;
END;
$$;
-- Drop temp table and procedure if no longer reuired
drop procedure "Employee".update_auth_token();
drop table emp_auth_to_be_updated;