From 6037c588163b6c88beb57ef7765afb2c8bbe20d8 Mon Sep 17 00:00:00 2001 From: datafoo <34766150+datafoo@users.noreply.github.com> Date: Tue, 5 Feb 2019 15:51:13 +0100 Subject: [PATCH] Use `timestamp with time zone` for timestamp fields in `projection_versions`. Fixes #20. --- CHANGELOG.md | 10 +- README.md | 98 +++++++++---------- lib/projections/ecto.ex | 2 +- ...70609113553_create_projection_versions.exs | 2 +- ..._create_projection_version_with_prefix.exs | 2 +- 5 files changed, 61 insertions(+), 53 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4c1421..85f3f54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,15 @@ - Add `.formatter.exs` to Hex package ([#19](https://github.com/commanded/commanded-ecto-projections/pull/19)). - Add microseconds to timestamp fields in `projection_versions` ([#22](https://github.com/commanded/commanded-ecto-projections/pull/22)). +### Upgrading + +- Upgrade your existing `projection_versions` table by running: + + ```sql + ALTER TABLE projection_versions ALTER COLUMN inserted_at TYPE timestamp with time zone USING inserted_at AT TIME ZONE 'UTC'; + ALTER TABLE projection_versions ALTER COLUMN updated_at TYPE timestamp with time zone USING updated_at AT TIME ZONE 'UTC'; + ``` + ## v0.8.0 ### Enhancements @@ -55,7 +64,6 @@ - Allow an Ecto schema prefix to be defined in config or per handler ([#4](https://github.com/commanded/commanded-ecto-projections/pull/4)). - ## v0.4.0 ### Enhancements diff --git a/README.md b/README.md index 8a12e89..80d6cb8 100644 --- a/README.md +++ b/README.md @@ -30,61 +30,61 @@ You should already have [Ecto](https://github.com/elixir-ecto/ecto) installed an 1. Add `commanded_ecto_projections` to your list of dependencies in `mix.exs`: - ```elixir - def deps do - [ - {:commanded_ecto_projections, "~> 0.8"}, - ] - end - ``` + ```elixir + def deps do + [ + {:commanded_ecto_projections, "~> 0.8"}, + ] + end + ``` 2. Configure `commanded_ecto_projections` with the Ecto repo used by your application: - ```elixir - config :commanded_ecto_projections, - repo: MyApp.Projections.Repo - ``` + ```elixir + config :commanded_ecto_projections, + repo: MyApp.Projections.Repo + ``` - Or alternatively in case of umbrella application define it later per projection: + Or alternatively in case of umbrella application define it later per projection: - ```elixir - defmodule MyApp.ExampleProjector do - use Commanded.Projections.Ecto, - name: "example_projection", - repo: MyApp.Projections.Repo + ```elixir + defmodule MyApp.ExampleProjector do + use Commanded.Projections.Ecto, + name: "example_projection", + repo: MyApp.Projections.Repo - ... - end - ``` + ... + end + ``` 3. Generate an Ecto migration in your app: - ```console - $ mix ecto.gen.migration create_projection_versions - ``` + ```console + $ mix ecto.gen.migration create_projection_versions + ``` 4. Modify the generated migration, in `priv/repo/migrations`, to create the `projection_versions` table: - ```elixir - defmodule CreateProjectionVersions do - use Ecto.Migration + ```elixir + defmodule CreateProjectionVersions do + use Ecto.Migration - def change do - create table(:projection_versions, primary_key: false) do - add :projection_name, :text, primary_key: true - add :last_seen_event_number, :bigint + def change do + create table(:projection_versions, primary_key: false) do + add :projection_name, :text, primary_key: true + add :last_seen_event_number, :bigint - timestamps() - end - end - end - ``` + timestamps(type: :timestamptz) + end + end + end + ``` -4. Run the Ecto migration: +5. Run the Ecto migration: - ```console - $ mix ecto.migrate - ``` + ```console + $ mix ecto.migrate + ``` ### Schema Prefix @@ -250,19 +250,19 @@ To rebuild a projection you will need to: 1. Delete the row containing the last seen event for the projection name: - ```SQL - delete from projection_versions - where projection_name = 'example_projection'; - ``` + ```SQL + delete from projection_versions + where projection_name = 'example_projection'; + ``` 2. Truncate the tables that are being populated by the projection, and restart their identity: - ```SQL - truncate table - example_projections, - other_projections - restart identity; - ``` + ```SQL + truncate table + example_projections, + other_projections + restart identity; + ``` You will also need to reset the event store subscription for the commanded event handler. This is specific to whichever event store you are using. diff --git a/lib/projections/ecto.ex b/lib/projections/ecto.ex index 03a3806..f1ce999 100644 --- a/lib/projections/ecto.ex +++ b/lib/projections/ecto.ex @@ -130,7 +130,7 @@ defmodule Commanded.Projections.Ecto do schema "projection_versions" do field(:last_seen_event_number, :integer) - timestamps(type: :naive_datetime_usec) + timestamps(type: :utc_datetime_usec) end @required_fields ~w(last_seen_event_number)a diff --git a/priv/repo/migrations/20170609113553_create_projection_versions.exs b/priv/repo/migrations/20170609113553_create_projection_versions.exs index ab6c652..ecdcbeb 100644 --- a/priv/repo/migrations/20170609113553_create_projection_versions.exs +++ b/priv/repo/migrations/20170609113553_create_projection_versions.exs @@ -6,7 +6,7 @@ defmodule Commanded.Projections.Repo.Migrations.CreateProjectionVersions do add :projection_name, :text, primary_key: true add :last_seen_event_number, :bigint - timestamps() + timestamps(type: :timestamptz) end end end diff --git a/priv/repo/migrations/20170929080308_create_projection_version_with_prefix.exs b/priv/repo/migrations/20170929080308_create_projection_version_with_prefix.exs index d4b965b..dc0efa8 100644 --- a/priv/repo/migrations/20170929080308_create_projection_version_with_prefix.exs +++ b/priv/repo/migrations/20170929080308_create_projection_version_with_prefix.exs @@ -8,7 +8,7 @@ defmodule Commanded.Projections.Repo.Migrations.CreateProjectionVersionWithPrefi add :projection_name, :text, primary_key: true add :last_seen_event_number, :bigint - timestamps() + timestamps(type: :timestamptz) end end