-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pg.pm
220 lines (191 loc) · 9.04 KB
/
Pg.pm
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package CPANMetaBrowser::Plugin::Backend::Pg;
# This software is Copyright (c) 2017 by Dan Book <[email protected]>.
# This is free software, licensed under:
# The Artistic License 2.0 (GPL Compatible)
use 5.020;
use Mojo::Base 'Mojolicious::Plugin', -signatures;
use Mojo::JSON qw(true false);
use Mojo::Pg 4.08;
sub register ($self, $app, $config) {
{
my $pg_url = $ENV{CPAN_META_BROWSER_PG_URL} // $app->config->{pg_url} // die "'pg_url' config or 'CPAN_META_BROWSER_PG_URL' env required for pg backend\n";
my $pg = Mojo::Pg->new($pg_url);
$pg->migrations->from_data->migrate;
$app->helper(pg => sub { $pg });
}
$app->helper(get_packages => sub ($c, $module, $as_prefix = 0, $as_infix = 0) {
return [] unless length $module;
my ($where, @params);
if (ref $module eq 'ARRAY') {
$where = 'lower("p"."package") = ANY(?)';
@params = [map { lc } @$module];
} elsif ($as_infix) {
$where = 'lower("p"."package") LIKE lower(?)';
@params = ('%' . $c->_sql_pattern_escape($module) . '%');
} elsif ($as_prefix) {
$where = 'lower("p"."package") LIKE lower(?)';
@params = ($c->_sql_pattern_escape($module) . '%');
} else {
$where = 'lower("p"."package") = lower(?)';
@params = ($module);
}
my $query = 'SELECT "p"."package" AS "module", "p"."version", "p"."path",
(SELECT "userid" FROM "perms" WHERE lower("package") = lower("p"."package") AND "best_permission"=? ORDER BY lower("userid") LIMIT 1) AS "owner"
FROM "packages" AS "p" WHERE ' . $where . ' ORDER BY lower("p"."package")';
my $details = $c->pg->db->query($query, 'f', @params)->hashes;
($_->{uploader}) = $_->{path} =~ m{^[^/]+/[^/]+/([a-z]+)}i for @$details;
return $details;
});
$app->helper(existing_packages => sub ($c, $db) {
return $db->select('packages', ['package'])->arrays->map(sub { $_->[0] });
});
$app->helper(update_package => sub ($c, $db, $data) {
my $current = $db->select('packages', ['*'], {package => $data->{package}})->hashes->first;
return 1 if $c->_keys_equal($data, $current, [qw(version path)]);
return $db->insert('packages', {%$data{qw(package version path)}},
{on_conflict => ['package', {map {($_ => \qq{EXCLUDED."$_"})} qw(version path)}]});
});
$app->helper(delete_package => sub ($c, $db, $package) {
return $db->delete('packages', {package => $package});
});
$app->helper(get_perms => sub ($c, $author, $module = '', $as_prefix = 0, $as_infix = 0, $other_authors = 0) {
return [] unless length $author or length $module;
my (@where, @params);
if (length $author) {
if ($other_authors) {
push @where, '"p"."package" IN (SELECT "package" FROM "perms" WHERE lower("userid") = lower(?))';
push @params, $author;
} else {
push @where, 'lower("p"."userid") = lower(?)';
push @params, $author;
}
}
if (length $module) {
if ($as_infix) {
push @where, 'lower("p"."package") LIKE lower(?)';
push @params, '%' . $c->_sql_pattern_escape($module) . '%';
} elsif ($as_prefix) {
push @where, 'lower("p"."package") LIKE lower(?)';
push @params, $c->_sql_pattern_escape($module) . '%';
} else {
push @where, 'lower("p"."package") = lower(?)';
push @params, $module;
}
}
my $where = join ' AND ', @where;
my $query = 'SELECT "p"."package" AS "module", "p"."userid" AS "author", "p"."best_permission",
(SELECT "userid" FROM "perms" WHERE lower("package") = lower("p"."package") AND "best_permission"=? ORDER BY lower("userid") LIMIT 1) AS "owner"
FROM "perms" AS "p" WHERE ' . $where . ' ORDER BY lower("p"."package"), lower("p"."userid")';
my $perms = $c->pg->db->query($query, 'f', @params)->hashes;
return $perms;
});
$app->helper(existing_perms => sub ($c, $db) {
return $db->select('perms', ['userid','package'])->hashes;
});
$app->helper(update_perms => sub ($c, $db, $data) {
my $current = $db->select('perms', ['*'], {%$data{qw(package userid)}})->hashes->first;
return 1 if $c->_keys_equal($data, $current, ['best_permission']);
return $db->insert('perms', {%$data{qw(package userid best_permission)}},
{on_conflict => \'("package","userid") DO UPDATE SET "best_permission"=EXCLUDED."best_permission"'});
});
$app->helper(delete_perms => sub ($c, $db, $userid, $packages) {
return $db->delete('perms', {userid => $userid, package => \['= ANY (?)', $packages]});
});
$app->helper(get_authors => sub ($c, $author, $as_prefix = 0, $as_infix = 0) {
return [] unless length $author;
my ($where, @params);
if ($as_infix) {
$where = 'lower("cpanid") LIKE lower(?)';
@params = ('%' . $c->_sql_pattern_escape($author) . '%');
} elsif ($as_prefix) {
$where = 'lower("cpanid") LIKE lower(?)';
@params = ($c->_sql_pattern_escape($author) . '%');
} else {
$where = 'lower("cpanid") = lower(?)';
@params = ($author);
}
my $query = 'SELECT "cpanid" AS "author", "type", "fullname", "asciiname", "email", "homepage", "info", "introduced", "has_cpandir"
FROM "authors" WHERE ' . $where . ' ORDER BY lower("cpanid")';
my $details = $c->pg->db->query($query, @params)->hashes;
$_->{has_cpandir} = $_->{has_cpandir} ? true : false for @$details;
return $details;
});
$app->helper(existing_authors => sub ($c, $db) {
return $db->select('authors', ['cpanid'])->arrays->map(sub { $_->[0] });
});
$app->helper(update_author => sub ($c, $db, $data) {
my $current = $db->select('authors', ['*'], {cpanid => $data->{cpanid}})->hashes->first;
return 1 if $c->_keys_equal($data, $current, [qw(type fullname asciiname email homepage info introduced has_cpandir)]);
return $db->insert('authors', {%$data{qw(cpanid type fullname asciiname email homepage info introduced has_cpandir)}},
{on_conflict => ['cpanid', {map {($_ => \qq{EXCLUDED."$_"})} qw(type fullname asciiname email homepage info introduced has_cpandir)}]});
});
$app->helper(delete_author => sub ($c, $db, $cpanid) {
return $db->delete('authors', {cpanid => $cpanid});
});
$app->helper(get_refreshed => sub ($c, $type) {
my $refreshed = $c->pg->db->select('refreshed', [\'extract(epoch from "last_updated")'], {type => $type});
return +($refreshed->arrays->first // [])->[0];
});
$app->helper(update_refreshed => sub ($c, $db, $type, $time) {
return $db->insert('refreshed', {type => $type, last_updated => \['to_timestamp(?)', $time]},
{on_conflict => ['type', {last_updated => \'EXCLUDED."last_updated"'}]});
});
}
1;
__DATA__
@@ migrations
-- 1 up
CREATE TABLE IF NOT EXISTS "packages" (
"package" CHARACTER VARYING NOT NULL PRIMARY KEY,
"version" CHARACTER VARYING NULL,
"path" CHARACTER VARYING NOT NULL
);
CREATE INDEX IF NOT EXISTS "packages_package_idx" ON "packages" (lower("package") text_pattern_ops);
CREATE TYPE "cpan_permission" AS ENUM ('m','f','a','c');
CREATE TABLE IF NOT EXISTS "perms" (
"package" CHARACTER VARYING NOT NULL,
"userid" CHARACTER VARYING NOT NULL,
"best_permission" cpan_permission NOT NULL,
PRIMARY KEY ("package","userid")
);
CREATE INDEX IF NOT EXISTS "perms_userid_best_permission_idx" ON "perms" (lower("userid") text_pattern_ops,"best_permission");
CREATE INDEX IF NOT EXISTS "perms_package_best_permission_idx" ON "perms" (lower("package") text_pattern_ops,"best_permission");
CREATE TABLE IF NOT EXISTS "authors" (
"cpanid" CHARACTER VARYING NOT NULL PRIMARY KEY,
"fullname" CHARACTER VARYING NULL,
"asciiname" CHARACTER VARYING NULL,
"email" CHARACTER VARYING NULL,
"homepage" CHARACTER VARYING NULL,
"introduced" BIGINT NULL,
"has_cpandir" BOOLEAN NULL
);
CREATE INDEX IF NOT EXISTS "authors_cpanid_idx" ON "authors" (lower("cpanid") text_pattern_ops);
-- 1 down
DROP TABLE IF EXISTS "packages";
DROP TABLE IF EXISTS "perms";
DROP TABLE IF EXISTS "authors";
DROP TYPE IF EXISTS "cpan_permission";
--2 up
CREATE TABLE IF NOT EXISTS "refreshed" (
"type" CHARACTER VARYING NOT NULL PRIMARY KEY,
"last_updated" TIMESTAMP WITH TIME ZONE NOT NULL
);
--2 down
DROP TABLE IF EXISTS "refreshed";
--3 up
CREATE INDEX IF NOT EXISTS "packages_package_trgm_idx" ON "packages" USING GIN (lower("package") gin_trgm_ops);
CREATE INDEX IF NOT EXISTS "perms_userid_trgm_idx" ON "perms" USING GIN (lower("userid") gin_trgm_ops);
CREATE INDEX IF NOT EXISTS "perms_package_trgm_idx" ON "perms" USING GIN (lower("package") gin_trgm_ops);
CREATE INDEX IF NOT EXISTS "authors_cpanid_trgm_idx" ON "authors" USING GIN (lower("cpanid") gin_trgm_ops);
--4 up
ALTER TYPE "cpan_permission" RENAME TO "cpan_permission_old";
CREATE TYPE "cpan_permission" AS ENUM ('m','f','p','c');
ALTER TABLE "perms" ALTER COLUMN "best_permission" TYPE cpan_permission USING best_permission::text::cpan_permission;
DROP TYPE "cpan_permission_old";
CREATE TYPE "cpanid_type" AS ENUM ('author','list');
ALTER TABLE "authors"
ADD COLUMN "type" cpanid_type NOT NULL DEFAULT 'author',
ADD COLUMN "info" CHARACTER VARYING NULL;
--4 down
ALTER TABLE "authors" DROP COLUMN "type", DROP COLUMN "info";
DROP TYPE IF EXISTS "cpanid_type";