From 287e1ebfa32b755c6bdf749df04a8eec5d346551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20Zi=C4=99tkiewicz?= Date: Tue, 25 Feb 2025 09:24:42 +0100 Subject: [PATCH 1/8] Update migrations --- lib/kanta/migrations/postgresql/v01.ex | 2 +- lib/kanta/migrations/postgresql/v02.ex | 4 +-- lib/kanta/migrations/postgresql/v03.ex | 10 +++----- lib/kanta/migrations/postgresql/v04.ex | 30 ++++++++++++++++++++++ lib/kanta/migrations/sqlite3/v01.ex | 4 ++- lib/kanta/migrations/sqlite3/v02.ex | 2 +- lib/kanta/migrations/sqlite3/v03.ex | 35 ++++++++++++++++++++++++++ 7 files changed, 74 insertions(+), 13 deletions(-) create mode 100644 lib/kanta/migrations/postgresql/v04.ex create mode 100644 lib/kanta/migrations/sqlite3/v03.ex diff --git a/lib/kanta/migrations/postgresql/v01.ex b/lib/kanta/migrations/postgresql/v01.ex index bd70682..0ca3658 100644 --- a/lib/kanta/migrations/postgresql/v01.ex +++ b/lib/kanta/migrations/postgresql/v01.ex @@ -1,6 +1,6 @@ defmodule Kanta.Migrations.Postgresql.V01 do @moduledoc """ - Kanta V1 Migrations + Kanta PostgreSQL V1 Migrations """ use Ecto.Migration diff --git a/lib/kanta/migrations/postgresql/v02.ex b/lib/kanta/migrations/postgresql/v02.ex index a545584..518922d 100644 --- a/lib/kanta/migrations/postgresql/v02.ex +++ b/lib/kanta/migrations/postgresql/v02.ex @@ -1,6 +1,6 @@ defmodule Kanta.Migrations.Postgresql.V02 do @moduledoc """ - Kanta V2 Migrations + Kanta PostgreSQL V2 Migrations """ use Ecto.Migration @@ -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 diff --git a/lib/kanta/migrations/postgresql/v03.ex b/lib/kanta/migrations/postgresql/v03.ex index 33ab2f5..79c4644 100644 --- a/lib/kanta/migrations/postgresql/v03.ex +++ b/lib/kanta/migrations/postgresql/v03.ex @@ -1,6 +1,6 @@ defmodule Kanta.Migrations.Postgresql.V03 do @moduledoc """ - Kanta V3 Migrations + Kanta PostgreSQL V3 Migrations """ use Ecto.Migration @@ -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 @@ -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 diff --git a/lib/kanta/migrations/postgresql/v04.ex b/lib/kanta/migrations/postgresql/v04.ex new file mode 100644 index 0000000..c387bb8 --- /dev/null +++ b/lib/kanta/migrations/postgresql/v04.ex @@ -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 diff --git a/lib/kanta/migrations/sqlite3/v01.ex b/lib/kanta/migrations/sqlite3/v01.ex index 14f12ea..1416170 100644 --- a/lib/kanta/migrations/sqlite3/v01.ex +++ b/lib/kanta/migrations/sqlite3/v01.ex @@ -1,5 +1,7 @@ defmodule Kanta.Migrations.SQLite3.V01 do - @moduledoc false + @moduledoc """ + Kanta SQLite3 V1 Migrations + """ use Ecto.Migration diff --git a/lib/kanta/migrations/sqlite3/v02.ex b/lib/kanta/migrations/sqlite3/v02.ex index 797c0c1..1b7f0fd 100644 --- a/lib/kanta/migrations/sqlite3/v02.ex +++ b/lib/kanta/migrations/sqlite3/v02.ex @@ -1,6 +1,6 @@ defmodule Kanta.Migrations.SQLite3.V02 do @moduledoc """ - Kanta V2 Migrations + Kanta SQLite3 V2 Migrations """ use Ecto.Migration diff --git a/lib/kanta/migrations/sqlite3/v03.ex b/lib/kanta/migrations/sqlite3/v03.ex new file mode 100644 index 0000000..1afb28e --- /dev/null +++ b/lib/kanta/migrations/sqlite3/v03.ex @@ -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 From 82bbc06a5f88f35671fe817f63894d2a8fe7f9d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20Zi=C4=99tkiewicz?= Date: Tue, 25 Feb 2025 09:24:54 +0100 Subject: [PATCH 2/8] Update Postgres migration version --- lib/kanta/migrations/postgresql.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kanta/migrations/postgresql.ex b/lib/kanta/migrations/postgresql.ex index 44d5d59..7399757 100644 --- a/lib/kanta/migrations/postgresql.ex +++ b/lib/kanta/migrations/postgresql.ex @@ -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 From bd97e7fb0e1d8ac382bba3e6ddd3123be560210f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20Zi=C4=99tkiewicz?= Date: Tue, 25 Feb 2025 09:25:00 +0100 Subject: [PATCH 3/8] Update SQLite migration version --- lib/kanta/migrations/sqlite3.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kanta/migrations/sqlite3.ex b/lib/kanta/migrations/sqlite3.ex index 6848fd7..e3ec507 100644 --- a/lib/kanta/migrations/sqlite3.ex +++ b/lib/kanta/migrations/sqlite3.ex @@ -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 From df73cf8383239cc898c004757046efebcd537486 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20Zi=C4=99tkiewicz?= Date: Tue, 25 Feb 2025 09:25:13 +0100 Subject: [PATCH 4/8] Update migration module docs --- lib/kanta/migration.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/kanta/migration.ex b/lib/kanta/migration.ex index 8c117d5..6bed45c 100644 --- a/lib/kanta/migration.ex +++ b/lib/kanta/migration.ex @@ -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 ``` @@ -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: From 6e3d673a51f5f488e6bbe68795796ee643ac7314 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20Zi=C4=99tkiewicz?= Date: Tue, 25 Feb 2025 09:25:22 +0100 Subject: [PATCH 5/8] Divide DB setup into Postgres and SQLite sections in README --- README.md | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f163eb4..9e22407 100644 --- a/README.md +++ b/README.md @@ -136,7 +136,32 @@ 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. + def down do + Kanta.Migration.down(version: 1) + end +end +``` + +If you are using multitenancy with i.e. [triplex](https://github.com/ateliware/triplex), you can pass the schema prefix to the migration. + +```elixir +Kanta.Migration.up(version: ..., prefix: prefix()) +``` + +### SQLite3 ```elixir defmodule MyApp.Repo.Migrations.AddKantaTranslationsTable do @@ -146,8 +171,9 @@ defmodule MyApp.Repo.Migrations.AddKantaTranslationsTable do Kanta.Migration.up(version: 3, prefix: prefix()) # Prefix is needed if you are using multitenancy with i.e. triplex end + # We specify `version: 1` because we want to rollback all the way down. 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, prefix: prefix()) # Prefix is needed if you are using multitenancy with i.e. triplex end end ``` From fa1c400ffb16c7e29e531b8b885db2f5cc6c2ff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20Zi=C4=99tkiewicz?= Date: Tue, 25 Feb 2025 09:47:21 +0100 Subject: [PATCH 6/8] Remove V04.ex --- lib/kanta/migrations/postgresql/V04.ex | 23 -------------------- lib/kanta/migrations/postgresql/v04.ex | 30 -------------------------- 2 files changed, 53 deletions(-) delete mode 100644 lib/kanta/migrations/postgresql/V04.ex delete mode 100644 lib/kanta/migrations/postgresql/v04.ex diff --git a/lib/kanta/migrations/postgresql/V04.ex b/lib/kanta/migrations/postgresql/V04.ex deleted file mode 100644 index 5de52a1..0000000 --- a/lib/kanta/migrations/postgresql/V04.ex +++ /dev/null @@ -1,23 +0,0 @@ -defmodule Kanta.Migrations.Postgresql.V04 do - @moduledoc """ - Kanta V4 Migrations - """ - - use Ecto.Migration - - def up do - Kanta.Migration.up(version: 3) - - execute( - "INSERT INTO kanta_contexts (name,inserted_at,updated_at) VALUES('default', NOW(), NOW()) ON CONFLICT (name) DO NOTHING;" - ) - - execute("UPDATE kanta_messages SET context_id=1 WHERE context_id IS NULL;") - end - - def down do - execute("UPDATE kanta_messages SET context_id=NULL WHERE context_id=1;") - - execute("DELETE FROM kanta_contexts WHERE name='default';") - end -end diff --git a/lib/kanta/migrations/postgresql/v04.ex b/lib/kanta/migrations/postgresql/v04.ex deleted file mode 100644 index c387bb8..0000000 --- a/lib/kanta/migrations/postgresql/v04.ex +++ /dev/null @@ -1,30 +0,0 @@ -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 From 4e49c9a9bf86a570ce469dbf357947d96a92bb82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20Zi=C4=99tkiewicz?= Date: Tue, 25 Feb 2025 09:47:55 +0100 Subject: [PATCH 7/8] Replaced V04.ex with v04.ex --- lib/kanta/migrations/postgresql/v04.ex | 30 ++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 lib/kanta/migrations/postgresql/v04.ex diff --git a/lib/kanta/migrations/postgresql/v04.ex b/lib/kanta/migrations/postgresql/v04.ex new file mode 100644 index 0000000..c387bb8 --- /dev/null +++ b/lib/kanta/migrations/postgresql/v04.ex @@ -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 From 77e4d33722d534175d4237ff0ca63e0ce4eef9e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20Zi=C4=99tkiewicz?= Date: Tue, 25 Feb 2025 11:29:55 +0100 Subject: [PATCH 8/8] Remove note about multi-tenancy from README --- README.md | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 9e22407..bd5ac54 100644 --- a/README.md +++ b/README.md @@ -148,19 +148,13 @@ defmodule MyApp.Repo.Migrations.AddKantaTranslationsTable do Kanta.Migration.up(version: 4) end - # We specify `version: 1` because we want to rollback all the way down. + # 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 ``` -If you are using multitenancy with i.e. [triplex](https://github.com/ateliware/triplex), you can pass the schema prefix to the migration. - -```elixir -Kanta.Migration.up(version: ..., prefix: prefix()) -``` - ### SQLite3 ```elixir @@ -168,12 +162,12 @@ 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. + # 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, prefix: prefix()) # Prefix is needed if you are using multitenancy with i.e. triplex + Kanta.Migration.down(version: 1) end end ```