This repository has been archived by the owner on Jan 31, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcreate_packages_table.sql
60 lines (45 loc) · 1.73 KB
/
create_packages_table.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
-- Table: public.packages
-- Drafted: https://github.com/confused-Techie/atom-community-server-backend-JS/issues/39
-- Credit: @Digitalone1
CREATE EXTENSION pgcrypto;
CREATE TABLE packages (
pointer UUID DEFAULT GEN_RANDOM_UUID() PRIMARY KEY,
name VARCHAR(128) NOT NULL UNIQUE,
created TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
creation_method VARCHAR(128),
downloads BIGINT NOT NULL DEFAULT 0,
stargazers_count BIGINT NOT NULL DEFAULT 0,
original_stargazers BIGINT NOT NULL DEFAULT 0,
data JSONB
);
-- Lowercase constraint
-- https://github.com/confused-Techie/atom-backend/issues/90
ALTER TABLE packages ADD CONSTRAINT lowercase_names CHECK (name = LOWER(name));
-- Add packageType column
ALTER TABLE packages ADD COLUMN package_type VARCHAR(20);
UPDATE packages
SET package_type = 'theme'
WHERE LOWER(data ->'metadata'->> 'theme') = 'syntax' OR LOWER(data ->'metadata'->>'theme') = 'ui';
UPDATE packages
SET package_type = 'package'
WHERE package_type IS DISTINCT FROM 'theme';
-- Create packageType enum type
CREATE TYPE packageType AS ENUM('package', 'theme');
-- Cast existing values of package_type in packageType
ALTER TABLE packages
ALTER COLUMN package_type TYPE packageType USING (package_type::packageType),
ALTER COLUMN package_type SET NOT NULL;
-- Create a function and a trigger to set the current timestamp
-- in the `updated` column of the updated row
CREATE FUNCTION now_on_updated_package()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated = NOW();
RETURN NEW;
END;
$$ language 'plpgsql';
CREATE TRIGGER trigger_now_on_updated
BEFORE UPDATE ON packages
FOR EACH ROW
EXECUTE PROCEDURE now_on_updated_package();