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

[WIP] Ecto migrations #2

Open
wants to merge 5 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
7 changes: 7 additions & 0 deletions lib/mix/tasks/event_store.migrate.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule Mix.Tasks.EventStore.Migrate do
use Mix.Task

def run(_args) do
Mix.Tasks.Ecto.Migrate.run([])
end
end
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ defmodule EventStore.Mixfile do
{:poolboy, "~> 1.5"},
{:postgrex, "~> 0.13"},
{:uuid, "~> 1.1", only: [:bench, :test]},
{:ecto, "~> 2.2"},
]
end

Expand Down
27 changes: 27 additions & 0 deletions priv/repo/migrations/20170830085420_create_event_counter_table.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule EventStore.StorageAdapters.Ecto.Repo.Migrations.CreateEventCounterTable do
use Ecto.Migration

def change do
create table(:event_counter, primary_key: false) do
add :event_id, :bigint, primary_key: true, null: false
end

execute """
INSERT INTO event_counter (event_id) VALUES (0);
"""

Application.get_env(:eventstore, :repo).__adapter__()
|> create_rules()
end

defp create_rules(Ecto.Adapters.Postgres) do
execute """
CREATE RULE no_insert_event_counter AS ON INSERT TO event_counter DO INSTEAD NOTHING;
"""

execute """
CREATE RULE no_delete_event_counter AS ON DELETE TO event_counter DO INSTEAD NOTHING;
"""
end
defp create_rules(_), do: :ok
end
13 changes: 13 additions & 0 deletions priv/repo/migrations/20170830085844_create_streams_table.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule EventStore.StorageAdapters.Ecto.Repo.Migrations.CreateStreamsTable do
use Ecto.Migration

def change do
create table(:streams, primary_key: false) do
add :stream_id, :bigserial, primary_key: true, null: false
add :stream_uuid, :text, null: false
add :created_at, :naive_datetime, default: fragment("(now() at time zone 'utc')"), null: false
end

create unique_index(:streams, [:stream_uuid], name: :ix_streams_stream_uuid)
end
end
34 changes: 34 additions & 0 deletions priv/repo/migrations/20170830090352_create_events_table.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
defmodule EventStore.StorageAdapters.Ecto.Repo.Migrations.CreateEventsTable do
use Ecto.Migration

def change do
create table(:events, primary_key: false) do
add :event_id, :bigint, primary_key: true, null: false
add :stream_id, references(:streams, type: :bigint, column: :stream_id), null: false
add :stream_version, :bigint, null: false
add :event_type, :text, null: false
add :correlation_id, :text
add :causation_id, :text
add :data, :bytea, null: false
add :metadata, :bytea, null: true
add :created_at, :naive_datetime, default: fragment("(now() at time zone 'utc')"), null: false
end

create index(:events, [:stream_id], name: :ix_events_stream_id)
create unique_index(:events, [:stream_id, "stream_version DESC"], name: :ix_events_stream_id_stream_version)

Application.get_env(:eventstore, :repo).__adapter__()
|> create_rules()
end

defp create_rules(Ecto.Adapters.Postgres) do
execute """
CREATE RULE no_update_events AS ON UPDATE TO events DO INSTEAD NOTHING;
"""

execute """
CREATE RULE no_delete_events AS ON DELETE TO events DO INSTEAD NOTHING;
"""
end
defp create_rules(_), do: :ok
end
16 changes: 16 additions & 0 deletions priv/repo/migrations/20170830091334_create_subscriptions_table.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule EventStore.StorageAdapters.Ecto.Repo.Migrations.CreateSubscriptionsTable do
use Ecto.Migration

def change do
create table(:subscriptions, primary_key: false) do
add :subscription_id, :bigserial, primary_key: true, null: false
add :stream_uuid, :text, null: false
add :subscription_name, :text, null: false
add :last_seen_event_id, :bigint, null: true
add :last_seen_stream_version, :bigint, null: true
add :created_at, :naive_datetime, default: fragment("(now() at time zone 'utc')"), null: false
end

create unique_index(:subscriptions, [:stream_uuid, :subscription_name], name: :ix_subscriptions_stream_uuid_subscription_name)
end
end
14 changes: 14 additions & 0 deletions priv/repo/migrations/20170830091636_create_snapshots_table.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule EventStore.StorageAdapters.Ecto.Repo.Migrations.CreateSnapshotsTable do
use Ecto.Migration

def change do
create table(:snapshots, primary_key: false) do
add :source_uuid, :text, primary_key: true, null: false
add :source_version, :bigint, null: false
add :source_type, :text, null: false
add :data, :bytea, null: false
add :metadata, :bytea, null: true
add :created_at, :naive_datetime, default: fragment("(now() at time zone 'utc')"), null: false
end
end
end