-
Notifications
You must be signed in to change notification settings - Fork 16
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
Prevent message extractor from crash and log warning on obsolete migration #101
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ecaf50d
Mention Postgres 15+ requirement in README
arturz 2717878
Create function for returning DB adapter name
arturz 56ce895
Create migration version checker
arturz 1aea437
Do not run messages extractor on obsolete migrations
arturz d7556b3
Add minimum SQLite version to README
arturz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
defmodule Kanta.MigrationVersionChecker do | ||
@moduledoc """ | ||
GenServer responsible for checking if a new migration version is available for Kanta. | ||
|
||
This module runs a version check when started to compare the current database migration | ||
version against the latest available version. If a newer version is available, it displays | ||
a formatted warning message in the console with: | ||
|
||
- Current and latest version numbers | ||
- Step-by-step instructions for updating | ||
- Commands to generate and run the required migrations | ||
|
||
The checker supports both PostgreSQL and SQLite3 databases and automatically detects | ||
which adapter is being used. | ||
""" | ||
|
||
use GenServer | ||
|
||
@colors [ | ||
warning: :yellow, | ||
highlight: :cyan, | ||
brand: :magenta, | ||
reset: :reset | ||
] | ||
|
||
def start_link(_opts) do | ||
GenServer.start_link(__MODULE__, [], name: __MODULE__) | ||
end | ||
|
||
@impl true | ||
def init(_) do | ||
check_version() | ||
|
||
{:ok, %{}} | ||
end | ||
|
||
defp check_version do | ||
migrator = | ||
case Kanta.Repo.get_adapter_name() do | ||
:postgres -> Kanta.Migrations.Postgresql | ||
:sqlite -> Kanta.Migrations.SQLite3 | ||
end | ||
|
||
latest_version = migrator.current_version() | ||
migrated = migrator.migrated_version(%{repo: Kanta.Repo.get_repo()}) | ||
|
||
if migrated < latest_version do | ||
warning_message = """ | ||
#{colorize("⚠️ [Kanta Migration Alert]", @colors[:warning])} | ||
#{colorize("━━━━━━━━━━━━━━━━━━━━━━━━━━", @colors[:brand])} | ||
|
||
A new version of Kanta migrations is available for your database! | ||
|
||
Current version: #{colorize(to_string(migrated), @colors[:highlight])} | ||
Latest version: #{colorize(to_string(latest_version), @colors[:highlight])} | ||
|
||
To ensure optimal performance and functionality, please update your database schema. | ||
|
||
📝 Here's what you need to do: | ||
|
||
1. Generate a new migration: | ||
#{colorize("$ mix ecto.gen.migration update_kanta_migrations", @colors[:brand])} | ||
|
||
2. Add the following to your migration file: | ||
#{colorize("def up do", @colors[:highlight])} | ||
#{colorize("Kanta.Migration.up(version: #{latest_version})", @colors[:highlight])} | ||
#{colorize("end", @colors[:highlight])} | ||
|
||
#{colorize("def down do", @colors[:highlight])} | ||
#{colorize("Kanta.Migration.down(version: #{latest_version})", @colors[:highlight])} | ||
#{colorize("end", @colors[:highlight])} | ||
|
||
3. Run the migration: | ||
#{colorize("$ mix ecto.migrate", @colors[:brand])} | ||
|
||
📚 For more details, visit the Kanta documentation. | ||
#{colorize("━━━━━━━━━━━━━━━━━━━━━━━━━━", @colors[:brand])} | ||
""" | ||
|
||
IO.puts(warning_message) | ||
end | ||
end | ||
|
||
defp colorize(text, color) do | ||
IO.ANSI.format([color, text, @colors[:reset]]) | ||
|> IO.chardata_to_string() | ||
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there also a requirement regarding the SQLite version?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, good catch