-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
20240502115822_alias_api_prop_names.exs
81 lines (70 loc) · 2.14 KB
/
20240502115822_alias_api_prop_names.exs
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
defmodule Plausible.IngestRepo.Migrations.AliasApiPropNames do
@moduledoc """
Migration adds a ALIAS columns needed to keep DB schema and api
property naming in sync to reduce overhead in code.
"""
use Ecto.Migration
@sessions_prop_names %{
"source" => "referrer_source",
"device" => "screen_size",
"screen" => "screen_size",
"os" => "operating_system",
"os_version" => "operating_system_version",
"country" => "country_code",
"region" => "subdivision1_code",
"city" => "city_geoname_id",
"entry_page_hostname" => "hostname"
}
@events_prop_names %{
"source" => "referrer_source",
"device" => "screen_size",
"screen" => "screen_size",
"os" => "operating_system",
"os_version" => "operating_system_version",
"country" => "country_code",
"region" => "subdivision1_code",
"city" => "city_geoname_id"
}
def up do
column_types = get_column_types()
on_cluster = Plausible.MigrationUtils.on_cluster_statement("sessions_v2")
for {alias_name, column_name} <- @sessions_prop_names do
execute """
ALTER TABLE sessions_v2
#{on_cluster}
ADD COLUMN #{alias_name} #{Map.fetch!(column_types, column_name)} ALIAS #{column_name}
"""
end
for {alias_name, column_name} <- @events_prop_names do
execute """
ALTER TABLE events_v2
#{on_cluster}
ADD COLUMN #{alias_name} #{Map.fetch!(column_types, column_name)} ALIAS #{column_name}
"""
end
end
def down do
on_cluster = Plausible.MigrationUtils.on_cluster_statement("sessions_v2")
for {alias_name, _column_name} <- @sessions_prop_names do
execute """
ALTER TABLE sessions_v2
#{on_cluster}
DROP COLUMN #{alias_name}
"""
end
for {alias_name, _column_name} <- @events_prop_names do
execute """
ALTER TABLE events_v2
#{on_cluster}
DROP COLUMN #{alias_name}
"""
end
end
def get_column_types() do
%{rows: rows} =
Plausible.IngestRepo.query!("""
SELECT name, type FROM system.columns WHERE table = 'sessions_v2'
""")
Map.new(rows, fn [name, type] -> {name, type} end)
end
end