C0lla commited on
Commit
d09ec67
1 Parent(s): 8097574

Delete database/migrations

Browse files
database/migrations/20230817172319_init.sql DELETED
@@ -1,66 +0,0 @@
1
- -- +goose Up
2
- create extension if not exists pgcrypto;
3
-
4
- create extension if not exists btree_gin;
5
-
6
- create schema if not exists teldrive;
7
-
8
- create collation if not exists numeric (provider = icu, locale = 'en@colnumeric=yes');
9
-
10
- -- +goose StatementBegin
11
- create or replace
12
- function teldrive.generate_uid(size int) returns text language plpgsql as $$
13
- declare characters text := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
14
-
15
- bytes bytea := gen_random_bytes(size);
16
-
17
- l int := length(characters);
18
-
19
- i int := 0;
20
-
21
- output text := '';
22
-
23
- begin while i < size loop output := output || substr(characters,
24
- get_byte(bytes,
25
- i) % l + 1,
26
- 1);
27
-
28
- i := i + 1;
29
- end loop;
30
-
31
- return output;
32
- end;
33
-
34
- $$;
35
- -- +goose StatementEnd
36
-
37
- -- +goose StatementBegin
38
- create or replace
39
- function teldrive.get_tsvector(t text) returns tsvector language plpgsql immutable as $$
40
- declare res tsvector := to_tsvector(regexp_replace(t,
41
- '[^A-Za-z0-9 ]',
42
- ' ',
43
- 'g'));
44
-
45
- begin return res;
46
- end;
47
-
48
- $$;
49
- -- +goose StatementEnd
50
-
51
- -- +goose StatementBegin
52
- create or replace
53
- function teldrive.get_tsquery(t text) returns tsquery language plpgsql immutable as $$
54
- declare res tsquery = concat(
55
- plainto_tsquery(regexp_replace(t,
56
- '[^A-Za-z0-9 ]',
57
- ' ',
58
- 'g')),
59
- ':*'
60
- )::tsquery;
61
-
62
- begin return res;
63
- end;
64
-
65
- $$;
66
- -- +goose StatementEnd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
database/migrations/20230817172325_tables.sql DELETED
@@ -1,88 +0,0 @@
1
- -- +goose Up
2
- create table teldrive.files (
3
- id text primary key not null default teldrive.generate_uid(16),
4
- name text not null,
5
- type text not null,
6
- mime_type text not null,
7
- path text null,
8
- size bigint null,
9
- starred bool not null,
10
- depth integer null,
11
- user_id bigint not null,
12
- parent_id text null,
13
- status text default 'active'::text,
14
- channel_id bigint null,
15
- parts jsonb null,
16
- created_at timestamp not null default timezone('utc'::text,
17
- now()),
18
- updated_at timestamp not null default timezone('utc'::text,
19
- now()),
20
- constraint unique_file unique (name,
21
- parent_id,user_id)
22
- );
23
-
24
- create table teldrive.uploads (
25
- id text not null primary key default teldrive.generate_uid(16),
26
- upload_id text not null,
27
- name text not null,
28
- part_no int4 not null,
29
- part_id int4 not null,
30
- total_parts int4 not null,
31
- channel_id int8 not null,
32
- size int8 not null,
33
- created_at timestamp null default timezone('utc'::text,
34
- now())
35
- );
36
-
37
- create table teldrive.users (
38
- user_id bigint not null primary key,
39
- name text null,
40
- user_name text null,
41
- is_premium bool not null,
42
- tg_session text not null,
43
- settings jsonb null,
44
- created_at timestamptz not null default timezone('utc'::text,
45
- now()),
46
- updated_at timestamptz not null default timezone('utc'::text,
47
- now())
48
- );
49
-
50
- create collation if not exists numeric (provider = icu, locale = 'en@colnumeric=yes');
51
- create index name_search_idx on
52
- teldrive.files
53
- using gin (teldrive.get_tsvector(name),
54
- updated_at);
55
-
56
- create index name_numeric_idx on
57
- teldrive.files(name collate numeric nulls first);
58
-
59
- create index parent_name_numeric_idx on
60
- teldrive.files (parent_id,
61
- name collate numeric desc);
62
-
63
- create index path_idx on
64
- teldrive.files (path);
65
-
66
- create index parent_idx on
67
- teldrive.files (parent_id);
68
-
69
- create index starred_updated_at_idx on
70
- teldrive.files (starred,
71
- updated_at desc);
72
-
73
- create index status_idx on teldrive.files (status);
74
-
75
- create index user_id_idx on teldrive.files (user_id);
76
-
77
- -- +goose Down
78
- drop table if exists teldrive.files;
79
- drop table if exists teldrive.uploads;
80
- drop table if exists teldrive.users;
81
- drop index if exists teldrive.name_search_idx;
82
- drop index if exists teldrive.name_numeric_idx;
83
- drop index if exists teldrive.parent_name_numeric_idx;
84
- drop index if exists teldrive.path_idx;
85
- drop index if exists teldrive.parent_idx;
86
- drop index if exists teldrive.starred_updated_at_idx;
87
- drop index if exists teldrive.status_idx;
88
- drop index if exists teldrive.user_id_idx;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
database/migrations/20230817172329_functions.sql DELETED
@@ -1,188 +0,0 @@
1
- -- +goose Up
2
-
3
- -- +goose StatementBegin
4
- create procedure teldrive.update_size() language plpgsql as $$
5
- declare rec record;
6
-
7
- total_size bigint;
8
-
9
- begin
10
-
11
- for rec in
12
- select
13
- id
14
- from
15
- files
16
- where
17
- type = 'folder'
18
- order by
19
- depth desc loop total_size := (
20
- select
21
- sum(size) as total_size
22
- from
23
- teldrive.files
24
- where
25
- parent_id = rec.id
26
- );
27
-
28
- update
29
- teldrive.files
30
- set
31
- size = total_size
32
- where
33
- id = rec.id;
34
- end loop;
35
- end;
36
-
37
- $$;
38
- -- +goose StatementEnd
39
-
40
-
41
- -- +goose StatementBegin
42
- create function teldrive.update_folder(folder_id text,
43
- new_name text default null,
44
- new_path text default null)
45
- returns setof teldrive.files
46
- language plpgsql
47
- as $$
48
- declare folder record;
49
-
50
- path_items text [];
51
-
52
- begin
53
-
54
- if new_path is null then
55
- select
56
- *
57
- into
58
- folder
59
- from
60
- teldrive.files
61
- where
62
- id = folder_id;
63
-
64
- path_items := string_to_array(folder.path,
65
- '/');
66
-
67
- path_items [array_length(path_items,
68
- 1)] := new_name;
69
-
70
- new_path := array_to_string(path_items,
71
- '/');
72
- end if;
73
-
74
- update
75
- teldrive.files
76
- set
77
- path = new_path,
78
- name = new_name
79
- where
80
- id = folder_id;
81
-
82
- for folder in
83
- select
84
- *
85
- from
86
- teldrive.files
87
- where
88
- type = 'folder'
89
- and parent_id = folder_id loop call teldrive.update_folder(
90
- folder.id,
91
- folder.name,
92
- concat(new_path,
93
- '/',
94
- folder.name)
95
- );
96
- end loop;
97
-
98
- return query
99
- select
100
- *
101
- from
102
- teldrive.files
103
- where
104
- id = folder_id;
105
- end;
106
-
107
- $$
108
- ;
109
- -- +goose StatementEnd
110
-
111
- -- +goose StatementBegin
112
- create procedure teldrive.delete_files(in file_ids text[],
113
- in op text default 'bulk')
114
- language plpgsql
115
- as $$
116
- declare
117
- rec record;
118
-
119
- begin
120
- if op = 'bulk' then
121
- for rec in
122
- select
123
- id,
124
- type
125
- from
126
- teldrive.files
127
- where
128
- id = any (file_ids)
129
- loop
130
- if rec.type = 'folder' then
131
- call teldrive.delete_files(array [rec.id],
132
- 'single');
133
-
134
- delete
135
- from
136
- teldrive.files
137
- where
138
- id = rec.id;
139
- else
140
- update
141
- teldrive.files
142
- set
143
- status = 'pending_deletion'
144
- where
145
- id = rec.id;
146
- end if;
147
- end loop;
148
- else
149
-
150
- for rec in
151
- select
152
- id,
153
- type
154
- from
155
- teldrive.files
156
- where
157
- parent_id = file_ids[1]
158
- loop
159
- if rec.type = 'folder' then
160
- call teldrive.delete_files(array [rec.id],
161
- 'single');
162
-
163
- delete
164
- from
165
- teldrive.files
166
- where
167
- id = rec.id;
168
- else
169
- update
170
- teldrive.files
171
- set
172
- status = 'pending_deletion'
173
- where
174
- id = rec.id;
175
- end if;
176
- end loop;
177
- end if;
178
- end;
179
-
180
- $$
181
- ;
182
- -- +goose StatementEnd
183
-
184
-
185
- -- +goose Down
186
- drop procedure if exists teldrive.update_size;
187
- drop function if exists teldrive.update_folder;
188
- drop procedure if exists teldrive.delete_files;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
database/migrations/20230820014560_create_dirs_from_path.sql DELETED
@@ -1,48 +0,0 @@
1
- -- +goose Up
2
- -- +goose StatementBegin
3
-
4
- CREATE OR REPLACE FUNCTION teldrive.create_directories(
5
- IN tg_id BIGINT,
6
- IN long_path TEXT
7
- ) RETURNS SETOF teldrive.files AS $$
8
- DECLARE
9
- path_parts TEXT[];
10
- current_directory_id TEXT;
11
- new_directory_id TEXT;
12
- directory_name TEXT;
13
- path_so_far TEXT;
14
- depth_dir integer;
15
- begin
16
-
17
- path_parts := string_to_array(regexp_replace(long_path, '^/+', ''), '/');
18
-
19
- path_so_far := '';
20
- depth_dir := 0;
21
-
22
- SELECT id into current_directory_id FROM teldrive.files WHERE parent_id='root';
23
-
24
-
25
- FOR directory_name IN SELECT unnest(path_parts) LOOP
26
- path_so_far := CONCAT(path_so_far,'/', directory_name);
27
- depth_dir := depth_dir +1;
28
- SELECT id INTO new_directory_id
29
- FROM teldrive.files
30
- WHERE parent_id = current_directory_id
31
- AND "name" = directory_name AND "user_id"=tg_id;
32
-
33
- IF new_directory_id IS NULL THEN
34
- INSERT INTO teldrive.files ("name", "type", mime_type, parent_id, "user_id",starred,"depth","path")
35
- VALUES (directory_name, 'folder', 'drive/folder', current_directory_id, tg_id,false,depth_dir,path_so_far)
36
- RETURNING id INTO new_directory_id;
37
- END IF;
38
-
39
- current_directory_id := new_directory_id;
40
- END LOOP;
41
-
42
- RETURN QUERY SELECT * FROM teldrive.files WHERE id = current_directory_id;
43
- END;
44
- $$ LANGUAGE plpgsql;
45
- -- +goose StatementEnd
46
-
47
- -- +goose Down
48
- DROP FUNCTION IF EXISTS teldrive.create_directories;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
database/migrations/20230911113332_alter_table.sql DELETED
@@ -1,5 +0,0 @@
1
- -- +goose Up
2
-
3
- ALTER TABLE teldrive.files DROP CONSTRAINT IF EXISTS unique_file;
4
-
5
- CREATE UNIQUE INDEX unique_file ON teldrive.files (name, parent_id, user_id) WHERE (status= 'active');
 
 
 
 
 
 
database/migrations/20230916012407_alter_user.sql DELETED
@@ -1,3 +0,0 @@
1
- -- +goose Up
2
-
3
- ALTER TABLE teldrive.users DROP COLUMN settings;
 
 
 
 
database/migrations/20230916012507_tables.sql DELETED
@@ -1,27 +0,0 @@
1
- -- +goose Up
2
- -- +goose StatementBegin
3
-
4
- CREATE TABLE teldrive.bots (
5
- user_id bigint NOT NULL,
6
- token text NOT NULL,
7
- bot_user_name text NOT NULL,
8
- bot_id bigint NOT NULL,
9
- FOREIGN KEY (user_id) REFERENCES teldrive.users(user_id),
10
- CONSTRAINT btoken_user_un UNIQUE (user_id,token)
11
- );
12
-
13
- CREATE TABLE teldrive.channels (
14
- channel_id bigint NOT NULL PRIMARY KEY,
15
- channel_name text NOT NULL,
16
- user_id bigint NOT NULL,
17
- selected boolean DEFAULT false,
18
- FOREIGN KEY (user_id) REFERENCES teldrive.users(user_id)
19
- );
20
-
21
- -- +goose StatementEnd
22
-
23
- -- +goose Down
24
- -- +goose StatementBegin
25
- DROP TABLE IF EXISTS teldrive.bots;
26
- DROP TABLE IF EXISTS teldrive.channels;
27
- -- +goose StatementEnd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
database/migrations/20230918234353_account_stats.sql DELETED
@@ -1,22 +0,0 @@
1
- -- +goose Up
2
- -- +goose StatementBegin
3
- CREATE OR REPLACE FUNCTION teldrive.account_stats(
4
- IN u_id BIGINT
5
- ) RETURNS TABLE (total_size BIGINT, total_files BIGINT, ch_id BIGINT,ch_name TEXT ) AS $$
6
- DECLARE
7
- total_size BIGINT;
8
- total_files BIGINT;
9
- ch_id BIGINT;
10
- ch_name TEXT;
11
- BEGIN
12
- SELECT COUNT(*), SUM(size) into total_files,total_size FROM teldrive.files WHERE user_id=u_id AND type= 'file' and status='active';
13
- SELECT channel_id ,channel_name into ch_id,ch_name FROM teldrive.channels WHERE selected=TRUE AND user_id=u_id;
14
- RETURN QUERY SELECT total_size,total_files,ch_id,ch_name;
15
- END;
16
- $$ LANGUAGE plpgsql;
17
- -- +goose StatementEnd
18
-
19
- -- +goose Down
20
- -- +goose StatementBegin
21
- DROP FUNCTION IF EXISTS teldrive.account_stats;
22
- -- +goose StatementEnd