PhoenixKitWebAnalytics.Migrations (PhoenixKitWebAnalytics v0.2.0)

Copy Markdown View Source

Versioned migration coordinator for phoenix_kit_web_analytics — the module returned from PhoenixKitWebAnalytics.migration_module/0.

mix phoenix_kit.update discovers this, compares migrated_version_runtime/1 (what's installed) with current_version/0 (what the code needs), and — when behind — generates a host migration whose up/0 calls up/1 here. So the host installs/updates this module's tables with no hand-written migration, and it honors the host's --prefix (named-schema installs).

Version is tracked via a COMMENT ON TABLE on phoenix_kit_web_analytics_events (mirroring core's own PhoenixKit.Migrations.Postgres and PhoenixKitBoards.Migrations) — not just a boolean "does the table exist", so a future V2 can tell "not installed" apart from "installed at V1".

Versions:

  • 0 — tables absent (not installed)
  • 1phoenix_kit_web_analytics_events + phoenix_kit_web_analytics_daily_stats, UUIDv7 primary keys

Prefix safety

Every statement passes prefix: through, index names are bare (Postgres scopes an index to its table's schema), and existence checks are anchored to the target schema — so a --prefix "analytics" install lands entirely inside that schema.

Summary

Types

Migration options: :prefix and :version.

Functions

The version this code expects the schema to be at.

Roll back. Migration-context only.

The version currently installed in the database (0 if the table is absent). Migration-context only — reads via Ecto.Migration.repo/0.

Runtime-safe version of migrated_version/1 — uses PhoenixKit's configured repo instead of the Ecto.Migration repo() helper, so it can be called from Mix tasks and other non-migration contexts (mix phoenix_kit.update).

Run migrations up to (and including) the target version. Migration-context only.

Types

opts()

@type opts() :: keyword() | map()

Migration options: :prefix and :version.

Core passes a keyword list; the internal helpers normalize it to a map and pass that back through the same public functions, so both are accepted.

Functions

current_version()

@spec current_version() :: pos_integer()

The version this code expects the schema to be at.

down(opts \\ [])

@spec down(opts()) :: :ok

Roll back. Migration-context only.

migrated_version(opts \\ [])

@spec migrated_version(opts()) :: non_neg_integer()

The version currently installed in the database (0 if the table is absent). Migration-context only — reads via Ecto.Migration.repo/0.

migrated_version_runtime(opts \\ [])

@spec migrated_version_runtime(opts()) :: non_neg_integer()

Runtime-safe version of migrated_version/1 — uses PhoenixKit's configured repo instead of the Ecto.Migration repo() helper, so it can be called from Mix tasks and other non-migration contexts (mix phoenix_kit.update).

up(opts \\ [])

@spec up(opts()) :: :ok

Run migrations up to (and including) the target version. Migration-context only.