-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Sergio Castaño Arteaga <tegioz@icloud.com> Signed-off-by: Cintia Sanchez Garcia <cynthiasg@icloud.com> Co-authored-by: Sergio Castaño Arteaga <tegioz@icloud.com> Co-authored-by: Cintia Sanchez Garcia <cynthiasg@icloud.com>
- Loading branch information
1 parent
fecbe5a
commit 11fadc3
Showing
23 changed files
with
604 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
database/migrations/functions/packages/update_packages_views.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
-- update_packages_views updates the views of the packages provided. | ||
create or replace function update_packages_views(p_lock_key bigint, p_data jsonb) | ||
returns void as $$ | ||
-- Make sure only one batch of updates is processed at a time | ||
select pg_advisory_xact_lock(p_lock_key); | ||
|
||
-- Insert or update the corresponding views counters as needed | ||
insert into package_views (package_id, version, day, total) | ||
select | ||
(value->>0)::uuid as package_id, | ||
(value->>1)::text as version, | ||
(value->>2)::date as day, | ||
(value->>3)::integer as total | ||
from jsonb_array_elements(p_data) | ||
on conflict (package_id, version, day) do | ||
update set total = package_views.total + excluded.total; | ||
$$ language sql; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
create extension pg_partman; | ||
|
||
create table if not exists package_views ( | ||
package_id uuid not null references package on delete cascade, | ||
version text not null, | ||
day date not null, | ||
total integer not null, | ||
unique (package_id, version, day) | ||
) partition by range (day); | ||
|
||
select create_parent('public.package_views', 'day', 'native', 'monthly', p_start_partition := current_date::text); | ||
|
||
---- create above / drop below ---- | ||
|
||
drop table if exists package_views; | ||
drop table template_public_package_views; | ||
delete from part_config where parent_table = 'public.package_views'; |
7 changes: 5 additions & 2 deletions
7
database/tests/Dockerfile-postgres-pgtap → database/tests/Dockerfile-postgres
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
# Build pgtap extension | ||
# Build extensions | ||
FROM postgres:13 AS builder | ||
RUN apt-get update | ||
RUN apt-get install -y git make postgresql-server-dev-13 | ||
RUN git clone https://github.com/theory/pgtap | ||
RUN cd pgtap && make && make install | ||
RUN cd pgtap && make && make install && cd .. | ||
RUN git clone https://github.com/pgpartman/pg_partman | ||
RUN cd pg_partman && make NO_BGW=1 install | ||
|
||
# Build final image | ||
FROM postgres:13 | ||
COPY --from=builder /usr/share/postgresql/13/extension/pgtap* /usr/share/postgresql/13/extension/ | ||
COPY --from=builder /usr/share/postgresql/13/extension/pg_partman* /usr/share/postgresql/13/extension/ |
81 changes: 81 additions & 0 deletions
81
database/tests/functions/packages/update_packages_views.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
-- Start transaction and plan tests | ||
begin; | ||
select plan(3); | ||
|
||
-- Declare some variables | ||
\set lockKey 1 | ||
\set org1ID '00000000-0000-0000-0000-000000000001' | ||
\set repo1ID '00000000-0000-0000-0000-000000000001' | ||
\set package1ID '00000000-0000-0000-0000-000000000001' | ||
\set package2ID '00000000-0000-0000-0000-000000000002' | ||
|
||
-- Seed some data | ||
insert into organization (organization_id, name, display_name, description, home_url) | ||
values (:'org1ID', 'org1', 'Organization 1', 'Description 1', 'https://org1.com'); | ||
insert into repository (repository_id, name, display_name, url, repository_kind_id, organization_id) | ||
values (:'repo1ID', 'repo1', 'Repo 1', 'https://repo1.com', 0, :'org1ID'); | ||
insert into package ( | ||
package_id, | ||
name, | ||
latest_version, | ||
repository_id | ||
) values ( | ||
:'package1ID', | ||
'pkg1', | ||
'1.0.0', | ||
:'repo1ID' | ||
); | ||
insert into package ( | ||
package_id, | ||
name, | ||
latest_version, | ||
repository_id | ||
) values ( | ||
:'package2ID', | ||
'pkg2', | ||
'1.0.0', | ||
:'repo1ID' | ||
); | ||
|
||
-- Run some tests | ||
select update_packages_views(:lockKey, '[ | ||
["00000000-0000-0000-0000-000000000001", "1.0.0", "2021-12-3", 10] | ||
]'); | ||
select results_eq( | ||
'select * from package_views', | ||
$$ values | ||
('00000000-0000-0000-0000-000000000001'::uuid, '1.0.0', '2021-12-3'::date, 10) | ||
$$, | ||
'First run: one insert' | ||
); | ||
select update_packages_views(:lockKey, '[ | ||
["00000000-0000-0000-0000-000000000001", "1.0.0", "2021-12-3", 10] | ||
]'); | ||
select results_eq( | ||
'select * from package_views', | ||
$$ values | ||
('00000000-0000-0000-0000-000000000001'::uuid, '1.0.0', '2021-12-3'::date, 20) | ||
$$, | ||
'Second run: one update' | ||
); | ||
select update_packages_views(:lockKey, '[ | ||
["00000000-0000-0000-0000-000000000001", "1.0.0", "2021-12-5", 10], | ||
["00000000-0000-0000-0000-000000000001", "1.0.1", "2021-12-5", 10], | ||
["00000000-0000-0000-0000-000000000002", "1.0.0", "2021-12-5", 10], | ||
["00000000-0000-0000-0000-000000000002", "1.0.0", "2021-12-6", 5] | ||
]'); | ||
select results_eq( | ||
'select * from package_views', | ||
$$ values | ||
('00000000-0000-0000-0000-000000000001'::uuid, '1.0.0', '2021-12-3'::date, 20), | ||
('00000000-0000-0000-0000-000000000001'::uuid, '1.0.0', '2021-12-5'::date, 10), | ||
('00000000-0000-0000-0000-000000000001'::uuid, '1.0.1', '2021-12-5'::date, 10), | ||
('00000000-0000-0000-0000-000000000002'::uuid, '1.0.0', '2021-12-5'::date, 10), | ||
('00000000-0000-0000-0000-000000000002'::uuid, '1.0.0', '2021-12-6'::date, 5) | ||
$$, | ||
'Third run: one update and four inserts' | ||
); | ||
|
||
-- Finish tests and rollback transaction | ||
select * from finish(); | ||
rollback; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.