mirror of
https://github.com/plausible/analytics.git
synced 2024-12-27 03:21:37 +03:00
Add migration to add ALIAS columns to common session/visit properties (#4058)
This allows for simplifications in the API code
This commit is contained in:
parent
972dd5d150
commit
850a843d82
@ -0,0 +1,81 @@
|
||||
defmodule Plausible.IngestRepo.Migrations.AliasApiPropNames do
|
||||
"""
|
||||
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
|
Loading…
Reference in New Issue
Block a user