Skip to content

Commit

Permalink
Merge pull request #106 from curiosum-dev/102-develop-make-migrations…
Browse files Browse the repository at this point in the history
…-rollbackable

Make migrations rollbackable
  • Loading branch information
arturz authored Feb 25, 2025
2 parents 1baa935 + 77e4d33 commit 41bba78
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 43 deletions.
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,38 @@ Migrations is heavily inspired by the Oban approach. To add to the project table
mix ecto.gen.migration add_kanta_translations_table
```

Open the generated migration file and set up `up` and `down` functions:
Open the generated migration file and set up `up` and `down` functions.

### PostgreSQL

```elixir
defmodule MyApp.Repo.Migrations.AddKantaTranslationsTable do
use Ecto.Migration

def up do
Kanta.Migration.up(version: 4)
end

# We specify `version: 1` because we want to rollback all the way down including the first migration.
def down do
Kanta.Migration.down(version: 1)
end
end
```

### SQLite3

```elixir
defmodule MyApp.Repo.Migrations.AddKantaTranslationsTable do
use Ecto.Migration

def up do
Kanta.Migration.up(version: 3, prefix: prefix()) # Prefix is needed if you are using multitenancy with i.e. triplex
Kanta.Migration.up(version: 3)
end

# We specify `version: 1` because we want to rollback all the way down including the first migration.
def down do
Kanta.Migration.down(version: 3, prefix: prefix()) # Prefix is needed if you are using multitenancy with i.e. triplex
Kanta.Migration.down(version: 1)
end
end
```
Expand Down
4 changes: 2 additions & 2 deletions lib/kanta/migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ defmodule Kanta.Migration do
def up, do: Kanta.Migration.up(version: 11)
def down, do: Kanta.Migration.down(version: 11)
def down, do: Kanta.Migration.down(version: 1)
end
```
Expand Down Expand Up @@ -169,7 +169,7 @@ defmodule Kanta.Migration do
Run migrations down to and including a specified version:
Kanta.Migration.down(version: 5)
Kanta.Migration.down(version: 1)
Run migrations in an alternate prefix:
Expand Down
2 changes: 1 addition & 1 deletion lib/kanta/migrations/postgresql.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Kanta.Migrations.Postgresql do
use Ecto.Migration

@initial_version 1
@current_version 3
@current_version 4
@default_prefix "public"

@doc false
Expand Down
23 changes: 0 additions & 23 deletions lib/kanta/migrations/postgresql/V04.ex

This file was deleted.

2 changes: 1 addition & 1 deletion lib/kanta/migrations/postgresql/v01.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Kanta.Migrations.Postgresql.V01 do
@moduledoc """
Kanta V1 Migrations
Kanta PostgreSQL V1 Migrations
"""

use Ecto.Migration
Expand Down
4 changes: 1 addition & 3 deletions lib/kanta/migrations/postgresql/v02.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Kanta.Migrations.Postgresql.V02 do
@moduledoc """
Kanta V2 Migrations
Kanta PostgreSQL V2 Migrations
"""

use Ecto.Migration
Expand All @@ -10,13 +10,11 @@ defmodule Kanta.Migrations.Postgresql.V02 do
@kanta_plural_translations "kanta_plural_translations"

def up(opts) do
Kanta.Migration.up(version: 1)
up_fuzzy_search(opts)
end

def down(opts) do
down_fuzzy_search(opts)
Kanta.Migration.down(version: 1)
end

def up_fuzzy_search(opts) do
Expand Down
10 changes: 3 additions & 7 deletions lib/kanta/migrations/postgresql/v03.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Kanta.Migrations.Postgresql.V03 do
@moduledoc """
Kanta V3 Migrations
Kanta PostgreSQL V3 Migrations
"""

use Ecto.Migration
Expand All @@ -10,8 +10,6 @@ defmodule Kanta.Migrations.Postgresql.V03 do
@kanta_messages "kanta_messages"

def up(opts) do
Kanta.Migration.up(version: 2)

[
&up_application_sources/1,
&up_kanta_messages/1
Expand All @@ -21,12 +19,10 @@ defmodule Kanta.Migrations.Postgresql.V03 do

def down(opts) do
[
&down_application_sources/1,
&down_kanta_messages/1
&down_kanta_messages/1,
&down_application_sources/1
]
|> Enum.each(&apply(&1, [opts]))

Kanta.Migration.down(version: 2)
end

def up_application_sources(_opts) do
Expand Down
30 changes: 30 additions & 0 deletions lib/kanta/migrations/postgresql/v04.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule Kanta.Migrations.Postgresql.V04 do
@moduledoc """
Kanta PostgreSQL V4 Migrations
"""

use Ecto.Migration

@doc """
Ensure that the `default` context exists.
"""
def up(_opts) do
# Insert the 'default' context if it doesn't exist
execute("""
INSERT INTO kanta_contexts (name, inserted_at, updated_at)
VALUES ('default', NOW(), NOW())
ON CONFLICT (name) DO NOTHING;
""")

flush()

# Update messages with null context_id to use the 'default' context's ID
execute("""
UPDATE kanta_messages
SET context_id = (SELECT id FROM kanta_contexts WHERE name = 'default')
WHERE context_id IS NULL;
""")
end

def down(_opts), do: nil
end
2 changes: 1 addition & 1 deletion lib/kanta/migrations/sqlite3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Kanta.Migrations.SQLite3 do
use Ecto.Migration

@initial_version 1
@current_version 2
@current_version 3

@doc false
def initial_version, do: @initial_version
Expand Down
4 changes: 3 additions & 1 deletion lib/kanta/migrations/sqlite3/v01.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
defmodule Kanta.Migrations.SQLite3.V01 do
@moduledoc false
@moduledoc """
Kanta SQLite3 V1 Migrations
"""

use Ecto.Migration

Expand Down
2 changes: 1 addition & 1 deletion lib/kanta/migrations/sqlite3/v02.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Kanta.Migrations.SQLite3.V02 do
@moduledoc """
Kanta V2 Migrations
Kanta SQLite3 V2 Migrations
"""

use Ecto.Migration
Expand Down
35 changes: 35 additions & 0 deletions lib/kanta/migrations/sqlite3/v03.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
defmodule Kanta.Migrations.SQLite3.V03 do
@moduledoc """
Kanta SQLite3 V3 Migrations
"""

use Ecto.Migration

@doc """
Ensure that the `default` context exists.
"""
def up(_opts) do
# Insert the 'default' context if it doesn't exist
execute("""
INSERT INTO kanta_contexts (name, inserted_at, updated_at)
VALUES ('default', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
ON CONFLICT (name) DO NOTHING;
""")

flush()

# Update messages with null context_id to use the 'default' context's ID
execute("""
UPDATE kanta_messages
SET context_id = (
SELECT id
FROM kanta_contexts
WHERE name = 'default'
LIMIT 1
)
WHERE context_id IS NULL;
""")
end

def down(_opts), do: nil
end

0 comments on commit 41bba78

Please sign in to comment.