-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from curiosum-dev/fix/align-with-main
Fix/align with main
- Loading branch information
Showing
24 changed files
with
469 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
defmodule Kanta.Migrations.SQLite3 do | ||
@moduledoc false | ||
|
||
@behaviour Kanta.Migration | ||
|
||
use Ecto.Migration | ||
|
||
@initial_version 1 | ||
@current_version 1 | ||
|
||
@doc false | ||
def initial_version, do: @initial_version | ||
|
||
@doc false | ||
def current_version, do: @current_version | ||
|
||
@impl Kanta.Migration | ||
def up(opts) do | ||
opts = with_defaults(opts, @current_version) | ||
initial = migrated_version(opts) | ||
|
||
cond do | ||
initial == 0 -> | ||
change(@initial_version..opts.version, :up, opts) | ||
|
||
initial < opts.version -> | ||
change((initial + 1)..opts.version, :up, opts) | ||
|
||
true -> | ||
:ok | ||
end | ||
end | ||
|
||
@impl Kanta.Migration | ||
def down(opts) do | ||
opts = with_defaults(opts, @initial_version) | ||
initial = max(migrated_version(opts), @initial_version) | ||
|
||
if initial >= opts.version do | ||
change(initial..opts.version, :down, opts) | ||
end | ||
end | ||
|
||
@impl Kanta.Migration | ||
def migrated_version(opts) do | ||
opts = with_defaults(opts, @initial_version) | ||
|
||
repo = Map.get_lazy(opts, :repo, fn -> repo() end) | ||
query = "PRAGMA user_version" | ||
|
||
case repo.query(query, [], log: false) do | ||
{:ok, %{rows: [[version]]}} when is_integer(version) -> version | ||
_ -> 0 | ||
end | ||
end | ||
|
||
defp change(range, direction, opts) do | ||
for index <- range do | ||
pad_idx = String.pad_leading(to_string(index), 2, "0") | ||
|
||
[__MODULE__, "V#{pad_idx}"] | ||
|> Module.concat() | ||
|> apply(direction, [opts]) | ||
end | ||
|
||
case direction do | ||
:up -> record_version(opts, Enum.max(range)) | ||
:down -> record_version(opts, Enum.min(range) - 1) | ||
end | ||
end | ||
|
||
defp record_version(_opts, 0), do: :ok | ||
|
||
defp record_version(_opts, version) do | ||
execute "PRAGMA user_version = #{version}" | ||
end | ||
|
||
defp with_defaults(opts, version) do | ||
Enum.into(opts, %{version: version}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
defmodule Kanta.Migrations.SQLite3.V01 do | ||
@moduledoc false | ||
|
||
use Ecto.Migration | ||
|
||
@kanta_locales "kanta_locales" | ||
@kanta_domains "kanta_domains" | ||
@kanta_contexts "kanta_contexts" | ||
@kanta_messages "kanta_messages" | ||
@kanta_singular_translations "kanta_singular_translations" | ||
@kanta_plural_translations "kanta_plural_translations" | ||
|
||
def up(opts) do | ||
[ | ||
&up_locales/1, | ||
&up_contexts/1, | ||
&up_domains/1, | ||
&up_messages/1, | ||
&up_singular_translations/1, | ||
&up_plural_translations/1 | ||
] | ||
|> Enum.each(&apply(&1, [opts])) | ||
end | ||
|
||
def down(opts) do | ||
[ | ||
&down_plural_translations/1, | ||
&down_singular_translations/1, | ||
&down_messages/1, | ||
&down_domains/1, | ||
&down_contexts/1, | ||
&down_locales/1 | ||
] | ||
|> Enum.each(&apply(&1, [opts])) | ||
end | ||
|
||
defp up_locales(_opts) do | ||
create_if_not_exists table(@kanta_locales) do | ||
add(:iso639_code, :string) | ||
add(:name, :string) | ||
add(:native_name, :string) | ||
add(:family, :string) | ||
add(:wiki_url, :string) | ||
add(:colors, {:array, :string}) | ||
add(:plurals_header, :string) | ||
timestamps() | ||
end | ||
|
||
create_if_not_exists unique_index(@kanta_locales, [:iso639_code]) | ||
end | ||
|
||
defp up_domains(_opts) do | ||
create_if_not_exists table(@kanta_domains) do | ||
add(:name, :string) | ||
add(:description, :text) | ||
add(:color, :string, null: false, default: "#7E37D8") | ||
timestamps() | ||
end | ||
|
||
create_if_not_exists unique_index(@kanta_domains, [:name]) | ||
end | ||
|
||
defp up_contexts(_opts) do | ||
create_if_not_exists table(@kanta_contexts) do | ||
add(:name, :string) | ||
add(:description, :text) | ||
add(:color, :string, null: false, default: "#7E37D8") | ||
timestamps() | ||
end | ||
|
||
create_if_not_exists unique_index(@kanta_contexts, [:name]) | ||
end | ||
|
||
defp up_messages(_opts) do | ||
create_if_not_exists table(@kanta_messages) do | ||
add(:msgid, :text) | ||
|
||
add(:message_type, :string, | ||
null: false, | ||
check: %{name: "message_type_check", expr: "message_type IN ('singular', 'plural')"} | ||
) | ||
|
||
add(:domain_id, references(@kanta_domains), null: true) | ||
add(:context_id, references(@kanta_contexts), null: true) | ||
timestamps() | ||
end | ||
|
||
execute "ALTER TABLE #{@kanta_messages} ADD COLUMN searchable TEXT AS (msgid) VIRTUAL" | ||
|
||
create_if_not_exists unique_index(@kanta_messages, [:context_id, :domain_id, :msgid]) | ||
end | ||
|
||
defp up_singular_translations(_opts) do | ||
create_if_not_exists table(@kanta_singular_translations) do | ||
add(:original_text, :text) | ||
add(:translated_text, :text, null: true) | ||
add(:locale_id, references(@kanta_locales)) | ||
add(:message_id, references(@kanta_messages)) | ||
timestamps() | ||
end | ||
|
||
create_if_not_exists unique_index(@kanta_singular_translations, [:locale_id, :message_id]) | ||
|
||
execute "ALTER TABLE #{@kanta_singular_translations} ADD COLUMN searchable TEXT AS (translated_text) VIRTUAL" | ||
end | ||
|
||
defp up_plural_translations(_opts) do | ||
create_if_not_exists table(@kanta_plural_translations) do | ||
add(:nplural_index, :integer) | ||
add(:original_text, :text) | ||
add(:translated_text, :text, null: true) | ||
add(:locale_id, references(@kanta_locales)) | ||
add(:message_id, references(@kanta_messages)) | ||
timestamps() | ||
end | ||
|
||
create_if_not_exists unique_index(@kanta_plural_translations, [ | ||
:locale_id, | ||
:message_id, | ||
:nplural_index | ||
]) | ||
|
||
execute "ALTER TABLE #{@kanta_plural_translations} ADD COLUMN searchable TEXT AS (translated_text) VIRTUAL" | ||
end | ||
|
||
defp down_locales(_opts) do | ||
drop table(@kanta_locales) | ||
end | ||
|
||
defp down_domains(_opts) do | ||
drop table(@kanta_domains) | ||
end | ||
|
||
defp down_contexts(_opts) do | ||
drop table(@kanta_contexts) | ||
end | ||
|
||
defp down_messages(_opts) do | ||
drop table(@kanta_messages) | ||
end | ||
|
||
defp down_singular_translations(_opts) do | ||
drop table(@kanta_singular_translations) | ||
end | ||
|
||
defp down_plural_translations(_opts) do | ||
drop table(@kanta_plural_translations) | ||
end | ||
end |
Oops, something went wrong.