Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Page Scroll Goals: migration step 2 #5034

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/plausible/goal/schema.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Plausible.Goal do

Check failure on line 1 in lib/plausible/goal/schema.ex

View workflow job for this annotation

GitHub Actions / App code does not change at the same time

Code and migrations shouldn't be changed at the same time
use Plausible
use Ecto.Schema
import Ecto.Changeset
Expand All @@ -8,6 +8,7 @@
schema "goals" do
field :event_name, :string
field :page_path, :string
field :scroll_threshold, :integer, default: -1
field :display_name, :string

on_ee do
Expand All @@ -23,7 +24,7 @@
timestamps()
end

@fields [:id, :site_id, :event_name, :page_path, :display_name] ++
@fields [:id, :site_id, :event_name, :page_path, :scroll_threshold, :display_name] ++
on_ee(do: [:currency], else: [])

@max_event_name_length 120
Expand All @@ -40,6 +41,9 @@
|> maybe_put_display_name()
|> unique_constraint(:event_name, name: :goals_event_name_unique)
|> unique_constraint(:page_path, name: :goals_page_path_unique)
|> unique_constraint([:page_path, :scroll_threshold],
name: :goals_page_path_and_scroll_threshold_unique
)
|> unique_constraint(:display_name, name: :goals_site_id_display_name_index)
|> validate_length(:event_name, max: @max_event_name_length)
|> check_constraint(:event_name,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule Plausible.Repo.Migrations.AddScrollThresholdToGoals do
use Ecto.Migration

@disable_ddl_transaction true
@disable_migration_lock true

@new_index unique_index(
:goals,
[:site_id, :page_path, :scroll_threshold],
where: "page_path IS NOT NULL",
name: :goals_page_path_and_scroll_threshold_unique
)

def up do
alter table(:goals) do
add :scroll_threshold, :smallint, null: false, default: -1
end

create(@new_index)
end

def down do
drop(@new_index)

alter table(:goals) do
remove :scroll_threshold
end
end
end
7 changes: 5 additions & 2 deletions test/plausible/goals_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ defmodule Plausible.GoalsTest do

test "create/2 fails to create the same pageview goal twice" do
site = new_site()
{:ok, _} = Goals.create(site, %{"page_path" => "foo bar"})
assert {:error, changeset} = Goals.create(site, %{"page_path" => "foo bar"})
{:ok, _} = Goals.create(site, %{"page_path" => "foo bar", "display_name" => "one"})

assert {:error, changeset} =
Goals.create(site, %{"page_path" => "foo bar", "display_name" => "two"})

assert {"has already been taken", _} = changeset.errors[:page_path]
end

Expand Down
Loading