2024-03-25 16:24:48: merge [main:72a62d1996eb71f9a1ab21643bf294148b87ae99];

This commit is contained in:
txix committed 2024-03-25 16:24:48 +03:00
1 parent 0669c6ec0a
commit ed8a4eef5d
85 files changed
+7671 -93

No files matched your search

@@ -0,0 +1,34 @@
-- +goose Up
-- +goose StatementBegin
CREATE OR REPLACE FUNCTION update_created_modified_column_date()
RETURNS TRIGGER AS
$body$
BEGIN
IF TG_OP = 'UPDATE' THEN
NEW.created_at = OLD.created_at;
NEW.updated_at = (now() at time zone 'utc');
ELSIF TG_OP = 'INSERT' THEN
NEW.updated_at = (now() at time zone 'utc');
END IF;
RETURN NEW;
END;
$body$ LANGUAGE plpgsql;
-- +goose StatementEnd
-- +goose StatementBegin
CREATE OR REPLACE FUNCTION update_created_column_date()
RETURNS TRIGGER AS
$body$
BEGIN
IF TG_OP = 'UPDATE' THEN
NEW.created_at = OLD.created_at;
END IF;
RETURN NEW;
END;
$body$ LANGUAGE plpgsql;
-- +goose StatementEnd
-- +goose Down
DROP FUNCTION update_created_column_date;
DROP FUNCTION update_created_modified_column_date;
@@ -0,0 +1,15 @@
-- +goose Up
CREATE TABLE IF NOT EXISTS roles (
id serial4 PRIMARY KEY NOT NULL,
name varchar(64) NOT NULL,
rights jsonb NOT NULL DEFAULT '{}',
description text,
created_at timestamp DEFAULT (now() at time zone 'utc') NOT NULL,
updated_at timestamp DEFAULT (now() at time zone 'utc') NOT NULL
);
CREATE TRIGGER update_create_update_time BEFORE UPDATE ON roles
FOR EACH ROW EXECUTE PROCEDURE update_created_modified_column_date();
-- +goose Down
DROP TABLE roles;
@@ -0,0 +1,26 @@
-- +goose Up
CREATE TABLE IF NOT EXISTS users (
id serial8 PRIMARY KEY NOT NULL,
role_id integer,
image varchar(255),
first_name varchar(255),
last_name varchar(255),
email varchar(255) UNIQUE NOT NULL,
password varchar(255),
phone varchar(255) UNIQUE,
created_at timestamp DEFAULT (now() at time zone 'utc') NOT NULL,
updated_at timestamp DEFAULT (now() at time zone 'utc') NOT NULL
);
CREATE TRIGGER update_create_update_time BEFORE UPDATE ON users
FOR EACH ROW EXECUTE PROCEDURE update_created_modified_column_date();
ALTER TABLE users
ADD CONSTRAINT "FK_users_roleId_roles_id"
FOREIGN KEY ("role_id") REFERENCES roles ("id")
ON DELETE SET NULL ON UPDATE SET NULL;
CREATE INDEX IX_users_roleId ON users USING hash (role_id);
-- +goose Down
DROP TABLE users;
@@ -0,0 +1,22 @@
-- +goose Up
CREATE TABLE IF NOT EXISTS tokens (
id serial8 NOT NULL PRIMARY KEY,
user_id int8 NOT NULL UNIQUE,
token text NOT NULL,
expired_at timestamp DEFAULT (now() at time zone 'utc') + '30 days'::interval NOT NULL,
created_at timestamp DEFAULT (now() at time zone 'utc') NOT NULL
);
CREATE TRIGGER update_create_update_time BEFORE UPDATE ON tokens
FOR EACH ROW EXECUTE PROCEDURE update_created_column_date();
ALTER TABLE tokens
ADD CONSTRAINT "FK_users_userId_users_id"
FOREIGN KEY ("user_id") REFERENCES users ("id")
ON DELETE CASCADE ON UPDATE CASCADE;
CREATE INDEX IX_tokens_userId ON tokens USING hash (user_id);
CREATE INDEX IX_tokens_token ON tokens USING btree (token);
-- +goose Down
DROP TABLE tokens;
@@ -0,0 +1,13 @@
-- +goose Up
INSERT INTO roles (id, name) VALUES (1, 'admin');
-- password: Xnp5kRZh
INSERT INTO users (id, role_id, first_name, last_name, email, password)
VALUES (1, 1, 'admin', 'admin', 'admin@ex.com', '$2y$12$6KXNZ8VXwiOu91.ZixqYpeYnRhGxmKuhEI0YB44.z8v0rhBVUywdu');
INSERT INTO users (id, role_id, first_name, last_name, email, password)
VALUES (2, 1, 'admin_second', 'admin_second', 'admin2@ex.com', '$2y$12$6KXNZ8VXwiOu91.ZixqYpeYnRhGxmKuhEI0YB44.z8v0rhBVUywdu');
SELECT setval(pg_get_serial_sequence('roles', 'id'), 2, FALSE);
SELECT setval(pg_get_serial_sequence('users', 'id'), 3, FALSE);
-- +goose Down
DELETE FROM users WHERE id IN (1, 2);
DELETE FROM roles WHERE id = 1;
@@ -0,0 +1,2 @@
-- +goose Up
DROP TABLE IF EXISTS tokens;
@@ -0,0 +1,7 @@
-- +goose Up
ALTER TABLE users
ADD COLUMN sudir_user_id TEXT UNIQUE NULL;
-- +goose Down
ALTER TABLE users
DROP COLUMN sudir_user_id;
@@ -0,0 +1,7 @@
-- +goose Up
INSERT INTO roles (id, name) VALUES (2, 'read_only_admin');
SELECT setval(pg_get_serial_sequence('roles', 'id'), 3, FALSE);
-- +goose Down
DELETE FROM roles WHERE id = 2;
SELECT setval(pg_get_serial_sequence('roles', 'id'), 2, FALSE);
@@ -0,0 +1,27 @@
-- +goose Up
ALTER TABLE users
DROP CONSTRAINT users_phone_key,
DROP COLUMN phone,
DROP COLUMN image;
ALTER TABLE users ALTER COLUMN first_name SET DEFAULT '';
UPDATE users SET first_name=DEFAULT WHERE first_name IS NULL;
ALTER TABLE users ALTER COLUMN first_name SET NOT NULL;
ALTER TABLE users ALTER COLUMN last_name SET DEFAULT '';
UPDATE users SET last_name=DEFAULT WHERE last_name IS NULL;
ALTER TABLE users ALTER COLUMN last_name SET NOT NULL;
ALTER TABLE users ALTER COLUMN password SET DEFAULT '';
UPDATE users SET password=DEFAULT WHERE password IS NULL;
ALTER TABLE users ALTER COLUMN password SET NOT NULL;
-- +goose Down
ALTER TABLE users
ADD COLUMN phone varchar(255) UNIQUE,
ADD COLUMN image varchar(255),
ALTER COLUMN first_name DROP NOT NULL,
ALTER COLUMN last_name DROP NOT NULL,
ALTER COLUMN password DROP NOT NULL;
@@ -0,0 +1,14 @@
-- +goose Up
CREATE TABLE tokens
(
token TEXT PRIMARY KEY,
user_id INT8 NOT NULL,
status TEXT NOT NULL,
expired_at TIMESTAMP NOT NULL,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL
);
-- +goose Down
DROP TABLE tokens;
@@ -0,0 +1,31 @@
-- +goose Up
alter table roles
add unique (name);
alter table tokens
add column id serial8 not null;
alter table tokens
add constraint fk_user_id__user_id
foreign key (user_id) references users (id)
on update cascade on delete cascade;
create index ix_tokens__user_id on tokens (user_id);
create table audit
(
id serial8 primary key,
user_id int8 not null,
message text not null,
created_at timestamp not null
);
create index ix_audit__created_at on audit (created_at DESC);
alter table users
add column blocked boolean not null default false;
alter table users
add unique (sudir_user_id);
-- +goose Down
@@ -0,0 +1,24 @@
-- +goose Up
-- user_roles
CREATE TABLE user_roles (
user_id integer,
role_id integer,
PRIMARY KEY (user_id, role_id),
CONSTRAINT FK_roles_id__role_id FOREIGN KEY (role_id) REFERENCES roles (id),
CONSTRAINT FK_users_id__user_id FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
);
INSERT INTO user_roles SELECT id as user_id, role_id FROM users;
-- alter users
ALTER TABLE users DROP COLUMN role_id;
ALTER TABLE users ADD COLUMN description TEXT NOT NULL DEFAULT ('');
-- alter roles
ALTER TABLE roles DROP COLUMN description;
ALTER TABLE roles DROP COLUMN rights;
ALTER TABLE roles ADD COLUMN permissions JSONB NOT NULL DEFAULT '[]'::jsonb;
ALTER TABLE roles ADD COLUMN external_group TEXT NOT NULL DEFAULT '';
-- +goose Down
@@ -0,0 +1,19 @@
-- +goose Up
CREATE TABLE audit_event
(
event text NOT NULL PRIMARY KEY,
enable bool NOT NULL DEFAULT false
);
INSERT INTO audit_event (event, enable)
VALUES ('success_login', true),
('error_login', true),
('success_logout', true),
('role_changed', true),
('user_changed', true);
ALTER TABLE audit
ADD COLUMN event text not null default '';
-- +goose Down
+32
View File
@@ -0,0 +1,32 @@
-- +goose Up
create table bgjob_job
(
id text primary key,
queue text not null,
type text not null,
arg bytea,
attempt int4 not null,
last_error text,
next_run_at int8 not null,
created_at timestamp not null,
updated_at timestamp not null
);
create index ix_bgjob_job__queue_next_run_at_created_at on bgjob_job (queue, next_run_at, created_at);
create table bgjob_dead_job
(
id serial8 primary key,
job_id text not null,
queue text not null,
type text not null,
arg bytea,
attempt int4 not null,
last_error text not null,
next_run_at int8 not null,
job_created_at timestamp not null,
job_updated_at timestamp not null,
moved_at timestamp not null
);
-- +goose Down
@@ -0,0 +1,6 @@
-- +goose Up
ALTER TABLE roles
ADD COLUMN immutable BOOL NOT NULL DEFAULT false,
ADD COLUMN exclusive BOOL NOT NULL DEFAULT false;
-- +goose Down
@@ -0,0 +1,27 @@
-- +goose Up
update roles
set permissions = '[
"user_create",
"user_update",
"user_delete",
"user_block",
"role_view",
"role_add",
"role_update",
"role_delete",
"session_view",
"session_revoke",
"security_log_view",
"audit_management_view"
]'
where name = 'admin';
update roles
set permissions = '[
"role_view",
"session_view",
"security_log_view"
]'
where name = 'read_only_admin';
-- +goose Down