Skip to content

Mysql compatibility#53

Merged
mgiacomini merged 14 commits intoateliware:masterfrom
maxmarcon:mysql-compatibility-upstream
Jun 19, 2018
Merged

Mysql compatibility#53
mgiacomini merged 14 commits intoateliware:masterfrom
maxmarcon:mysql-compatibility-upstream

Conversation

@maxmarcon
Copy link
Contributor

Here's the PR for issue #52

Looking forward to your comments.

@coveralls
Copy link

coveralls commented Feb 6, 2018

Coverage Status

Coverage decreased (-11.06%) to 88.945% when pulling 2e90ee4 on maxmarcon:mysql-compatibility-upstream into 65b84a8 on ateliware:master.

mix.exs Outdated
{:ecto, "~> 2.1"},
{:postgrex, ">= 0.11.0"},

{:mariaex, "~> 0.8.2"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, let's make this one and :postgrex dependencies optional, since the user will probably choose between one and another.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept only mariaex optional, because otherwise your test suite fails. It's weird, I would have expected the "optional" requirement to have an effect only if the project is included by another one: https://hexdocs.pm/mix/Mix.Tasks.Deps.html but I must be missing something.

lib/triplex.ex Outdated
case SQL.query(repo, "DELETE FROM #{Triplex.config().tenant_table} WHERE NAME = ?", [tenant]) do
{:error, msg} -> {:error, msg}
_ -> {:ok, tenant}
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could refactor this nested cases to use a with

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

lib/triplex.ex Outdated
{:error, reserved_message(new_tenant)}
else
repo.__adapter__ == Ecto.Adapters.MySQL ->
{:error, "you cannot rename tenants in a MySQL database. You will have to drop the tenant and recreate it with a different name"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, to drop the tenant and create another is not a good option, since the user will lose all his tenant data. Let's remove this part and stay only with: "you cannot rename tenants in a MySQL database"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, changed.

lib/triplex.ex Outdated
sql = case repo.__adapter__ do
Ecto.Adapters.MySQL ->
"SELECT name FROM #{config().tenant_table}"
_ ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's "let it fail" and put Ecto.Adapters.Postgres on this case clause ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup

lib/triplex.ex Outdated
sql = case repo.__adapter__ do
Ecto.Adapters.MySQL ->
"SELECT COUNT(*) FROM #{config().tenant_table} WHERE name = ?"
_ ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's "let it fail" and put Ecto.Adapters.Postgres on this case clause ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup

@maxmarcon
Copy link
Contributor Author

maxmarcon commented Feb 11, 2018

@kelvinst thanks for your comments. I addressed them the best I could.

I also did 2 additional things:

  1. I adapted the tests to work under MySQL. There's some ugliness caused by DDL operations in MySQL automatically issuing a commit of the transaction. Avoiding this required changing the mode of the sandbox. I documented it in triplex_test.exs.

  2. I modified the triplex.migrations task to list the state of the migrations for each tenant. So far, it wasn't really working because it was only using Ecto.Migrator.migrations, which gives you the list of migrations with their status but is not "tenant aware" (you can't pass it a prefix). Therefore, it was always showing the migrations as being down. I changed it to combine that information with the result of Ecto.Migrator.migrated_versions, which accepts a prefix and you can use to obtain the real status of the migrations in a tenant. Suggestion: it would be cool to be able to run/rollback migrations for only a specific tenant, too.

@kelvinst
Copy link
Contributor

Hey @maxmarcon cool! Sorry for the delay, new job. 😬

I'll review it soon, I promise. 😄

@@ -0,0 +1,58 @@
defmodule Mix.Tasks.Triplex.Init.Tenant do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about renaming this task to triplex.mysql.install?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure thing

@mgiacomini mgiacomini merged commit bc80229 into ateliware:master Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants