analytics/priv/repo/migrations/20230914071245_goals_unique.exs
hq1 082ec91c63
OpenAPI: first pass on Plugins API - Shared Links (#3378)
* Update depenedencies: OpenAPISpex + cursor based pagination

* Update formatter config

* Add internal server error implementation

* Test errors

* Implement pagination interface

* Implement Plugins API module macros

* Implement Public API base URI

(to be used with path helpers once called from within
forwarded router's scope)

* Implement OpenAPI specs + schemas

* Implement Shared Links context module

* Add pagination and error views

* Add Shared Link view

* Implement Shared Link controller

* Expose SharedLink.t() spec

* Implement separate router for the Plugins API

* Update moduledocs

* Always wrap resource objects with `data`

* Update moduledoc

* Use https://github.com/open-api-spex/open_api_spex/pull/425

due to https://github.com/open-api-spex/open_api_spex/issues/92

* Rely on BASE_URL for swagger-ui server definition

* Fixup goals migration

* Migrate broken goals before deleting dupes

* Remove bypassing test rate limiting for which there's none anyway

* Move the context module under `Plausible.` namespace

* Bring back conn assignment to PluginsAPICase template

* Update test/plausible_web/plugins/api/controllers/shared_links_test.exs

Co-authored-by: Uku Taht <Uku.taht@gmail.com>

* Update renamed aliases

* Seed static token for development purposes

* Delegate Plugins API 500s to a familiar shape

* Simplify with statement

---------

Co-authored-by: Uku Taht <Uku.taht@gmail.com>
2023-10-02 11:18:49 +02:00

51 lines
1.1 KiB
Elixir

defmodule Plausible.Repo.Migrations.GoalsUnique do
use Ecto.Migration
@disable_ddl_transaction true
@disable_migration_lock true
def up do
execute """
DELETE
FROM
goals g
WHERE
g.id NOT IN (
SELECT
min(g2.id)
FROM
goals g2
GROUP BY
(g2.site_id,
CASE
WHEN g2.page_path IS NOT NULL THEN 'Page: ' || g2.page_path
WHEN g2.event_name IS NOT NULL THEN 'Event: ' || g2.event_name
END )
)
AND g.id NOT IN (
SELECT fs.goal_id
FROM funnel_steps fs
);
"""
create(
unique_index(:goals, [:site_id, :event_name],
where: "event_name IS NOT NULL",
name: :goals_event_name_unique
)
)
create(
unique_index(:goals, [:site_id, :page_path],
where: "page_path IS NOT NULL",
name: :goals_page_path_unique
)
)
end
def down do
drop(unique_index(:goals, [:user_id, :event_name], name: :goals_event_name_unique))
drop(unique_index(:goals, [:user_id, :page_path], name: :goals_page_path_unique))
end
end