Skip to content

Commit da91964

Browse files
committed
helpers for getting resources lists from database
1 parent ddd1655 commit da91964

2 files changed

Lines changed: 99 additions & 0 deletions

File tree

apps/transport/lib/db/irve_valid_file.ex

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule DB.IRVEValidFile do
55
"""
66
use Ecto.Schema
77
use TypedEctoSchema
8+
import Ecto.Query
89

910
typed_schema "irve_valid_file" do
1011
field(:datagouv_dataset_id, :string, null: false)
@@ -16,4 +17,36 @@ defmodule DB.IRVEValidFile do
1617
has_many(:irve_valid_pdcs, DB.IRVEValidPDC, foreign_key: :irve_valid_file_id)
1718
timestamps(type: :utc_datetime_usec)
1819
end
20+
21+
@doc """
22+
Set of `datagouv_resource_id`s currently present in the database. Meant to be snapshotted
23+
before a consolidation run, to tell whether a datagouv resource already had a version imported.
24+
"""
25+
def existing_datagouv_resource_ids do
26+
DB.IRVEValidFile
27+
|> distinct(true)
28+
|> select([f], f.datagouv_resource_id)
29+
|> DB.Repo.all()
30+
|> MapSet.new()
31+
end
32+
33+
@doc """
34+
File-level metadata and stored PDC count, for the given (typically orphan) `datagouv_resource_id`s.
35+
Used to report resources still in the database but no longer listed on data.gouv.fr.
36+
"""
37+
def orphan_files(datagouv_resource_ids) do
38+
DB.IRVEValidFile
39+
|> where([f], f.datagouv_resource_id in ^datagouv_resource_ids)
40+
|> join(:left, [f], p in DB.IRVEValidPDC, on: p.irve_valid_file_id == f.id)
41+
|> group_by([f], f.id)
42+
|> select([f, p], %{
43+
datagouv_dataset_id: f.datagouv_dataset_id,
44+
datagouv_resource_id: f.datagouv_resource_id,
45+
dataset_title: f.dataset_title,
46+
datagouv_organization_or_owner: f.datagouv_organization_or_owner,
47+
datagouv_last_modified: f.datagouv_last_modified,
48+
pdc_count: count(p.id)
49+
})
50+
|> DB.Repo.all()
51+
end
1952
end
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
defmodule DB.IRVEValidFileTest do
2+
use ExUnit.Case, async: true
3+
4+
setup do
5+
:ok = Ecto.Adapters.SQL.Sandbox.checkout(DB.Repo)
6+
end
7+
8+
defp insert_file!(resource_id, attrs \\ %{}) do
9+
DB.Repo.insert!(%DB.IRVEValidFile{
10+
datagouv_dataset_id: Map.get(attrs, :dataset_id, "dataset-#{resource_id}"),
11+
datagouv_resource_id: resource_id,
12+
checksum: Map.get(attrs, :checksum, "checksum-#{resource_id}"),
13+
dataset_title: Map.get(attrs, :dataset_title, "title-#{resource_id}"),
14+
datagouv_organization_or_owner: Map.get(attrs, :org, "org-#{resource_id}")
15+
})
16+
end
17+
18+
describe "existing_datagouv_resource_ids/0" do
19+
test "returns the distinct set of datagouv_resource_ids" do
20+
insert_file!("resource-a")
21+
insert_file!("resource-b")
22+
# same resource, another version: must be deduplicated
23+
insert_file!("resource-a", %{checksum: "checksum-a-2"})
24+
25+
assert DB.IRVEValidFile.existing_datagouv_resource_ids() ==
26+
MapSet.new(["resource-a", "resource-b"])
27+
end
28+
end
29+
30+
describe "orphan_files/1" do
31+
test "returns metadata and PDC count for the given resource ids only" do
32+
path = System.tmp_dir!() |> Path.join("irve_orphan_#{Ecto.UUID.generate()}.csv")
33+
34+
File.write!(
35+
path,
36+
DB.Factory.IRVE.to_csv_body([
37+
DB.Factory.IRVE.generate_row(%{"id_pdc_itinerance" => "FRPAN99E00000001"}),
38+
DB.Factory.IRVE.generate_row(%{"id_pdc_itinerance" => "FRPAN99E00000002"})
39+
])
40+
)
41+
42+
Transport.IRVE.DatabaseImporter.write_to_db(
43+
path,
44+
"orphan-dataset-id",
45+
"orphan-resource-id",
46+
"orphan-dataset-title",
47+
"orphan-org",
48+
"2024-01-01T10:00:00+00:00"
49+
)
50+
51+
insert_file!("kept-resource-id")
52+
53+
assert [orphan] = DB.IRVEValidFile.orphan_files(["orphan-resource-id"])
54+
55+
assert %{
56+
datagouv_dataset_id: "orphan-dataset-id",
57+
datagouv_resource_id: "orphan-resource-id",
58+
dataset_title: "orphan-dataset-title",
59+
datagouv_organization_or_owner: "orphan-org",
60+
pdc_count: 2
61+
} = orphan
62+
63+
assert DB.IRVEValidFile.orphan_files([]) == []
64+
end
65+
end
66+
end

0 commit comments

Comments
 (0)