Skip to content
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

Ecto 3 support #61

Merged
merged 18 commits into from
Mar 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions .credo.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# This file contains the configuration for Credo and you are probably reading
# this after creating it with `mix credo.gen.config`.
#
# If you find anything wrong or unclear in this file, please report an
# issue on GitHub: https://github.com/rrrene/credo/issues
#
%{
#
# You can have as many configs as you like in the `configs:` field.
configs: [
%{
#
# Run any exec using `mix credo -C <name>`. If no exec name is given
# "default" is used.
#
name: "default",
#
# These are the files included in the analysis:
files: %{
#
# You can give explicit globs or simply directories.
# In the latter case `**/*.{ex,exs}` will be used.
#
included: ["lib/", "src/", "web/", "apps/"],
excluded: [~r"/_build/", ~r"/deps/"]
},
#
# If you create your own checks, you must specify the source files for
# them here, so they can be loaded by Credo before running the analysis.
#
requires: [],
#
# If you want to enforce a style guide and need a more traditional linting
# experience, you can change `strict` to `true` below:
#
strict: true,
#
# If you want to use uncolored output by default, you can change `color`
# to `false` below:
#
color: true,
#
# You can customize the parameters of any check by adding a second element
# to the tuple.
#
# To disable a check put `false` as second element:
#
# {Credo.Check.Design.DuplicatedCode, false}
#
checks: [
{Credo.Check.Consistency.ExceptionNames},
{Credo.Check.Consistency.LineEndings},
{Credo.Check.Consistency.ParameterPatternMatching},
{Credo.Check.Consistency.SpaceAroundOperators},
{Credo.Check.Consistency.SpaceInParentheses},
{Credo.Check.Consistency.TabsOrSpaces},

# You can customize the priority of any check
# Priority values are: `low, normal, high, higher`
#
{Credo.Check.Design.AliasUsage, priority: :low},

# For some checks, you can also set other parameters
#
# If you don't want the `setup` and `test` macro calls in ExUnit tests
# or the `schema` macro in Ecto schemas to trigger DuplicatedCode, just
# set the `excluded_macros` parameter to `[:schema, :setup, :test]`.
#
{Credo.Check.Design.DuplicatedCode, excluded_macros: []},

# You can also customize the exit_status of each check.
# If you don't want these comments to cause `mix credo` to fail, just
# set this value to 0 (zero).
#
{Credo.Check.Design.TagTODO, exit_status: 2},
{Credo.Check.Design.TagFIXME},

{Credo.Check.Readability.FunctionNames},
{Credo.Check.Readability.LargeNumbers},
{Credo.Check.Readability.MaxLineLength, priority: :low, max_length: 98},
{Credo.Check.Readability.ModuleAttributeNames},
{Credo.Check.Readability.ModuleDoc},
{Credo.Check.Readability.ModuleNames},
{Credo.Check.Readability.ParenthesesOnZeroArityDefs},
{Credo.Check.Readability.ParenthesesInCondition},
{Credo.Check.Readability.PredicateFunctionNames},
{Credo.Check.Readability.PreferImplicitTry},
{Credo.Check.Readability.RedundantBlankLines},
{Credo.Check.Readability.StringSigils},
{Credo.Check.Readability.TrailingBlankLine},
{Credo.Check.Readability.TrailingWhiteSpace},
{Credo.Check.Readability.VariableNames},
{Credo.Check.Readability.Semicolons},
{Credo.Check.Readability.SpaceAfterCommas},

{Credo.Check.Refactor.DoubleBooleanNegation},
{Credo.Check.Refactor.CondStatements},
{Credo.Check.Refactor.CyclomaticComplexity},
{Credo.Check.Refactor.FunctionArity},
{Credo.Check.Refactor.LongQuoteBlocks},
{Credo.Check.Refactor.MatchInCondition},
{Credo.Check.Refactor.NegatedConditionsInUnless},
{Credo.Check.Refactor.NegatedConditionsWithElse},
{Credo.Check.Refactor.Nesting},
{Credo.Check.Refactor.PipeChainStart},
{Credo.Check.Refactor.UnlessWithElse},

{Credo.Check.Warning.BoolOperationOnSameValues},
{Credo.Check.Warning.ExpensiveEmptyEnumCheck},
{Credo.Check.Warning.IExPry},
{Credo.Check.Warning.IoInspect},
{Credo.Check.Warning.LazyLogging},
{Credo.Check.Warning.OperationOnSameValues},
{Credo.Check.Warning.OperationWithConstantResult},
{Credo.Check.Warning.UnusedEnumOperation},
{Credo.Check.Warning.UnusedFileOperation},
{Credo.Check.Warning.UnusedKeywordOperation},
{Credo.Check.Warning.UnusedListOperation},
{Credo.Check.Warning.UnusedPathOperation},
{Credo.Check.Warning.UnusedRegexOperation},
{Credo.Check.Warning.UnusedStringOperation},
{Credo.Check.Warning.UnusedTupleOperation},
{Credo.Check.Warning.RaiseInsideRescue},

# Controversial and experimental checks (opt-in, just remove `, false`)
#
{Credo.Check.Refactor.ABCSize, false},
{Credo.Check.Refactor.AppendSingleItem, false},
{Credo.Check.Refactor.VariableRebinding, false},
{Credo.Check.Warning.MapGetUnsafePass, false},
{Credo.Check.Consistency.MultiAliasImportRequireUse, false},

# Deprecated checks (these will be deleted after a grace period)
#
{Credo.Check.Readability.Specs, false},
{Credo.Check.Warning.NameRedeclarationByAssignment, false},
{Credo.Check.Warning.NameRedeclarationByCase, false},
{Credo.Check.Warning.NameRedeclarationByDef, false},
{Credo.Check.Warning.NameRedeclarationByFn, false},

# Custom checks can be created using `mix credo.gen.check`.
#
]
}
]
}
3 changes: 3 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
elixir 1.6.6
10 changes: 6 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
language: elixir
elixir:
- 1.4.2
- 1.6.6
otp_release:
- '19.0'
- '20.0'
addons:
postgresql: '9.4'
mysql: '5.6'

install:
- mix local.rebar --force
Expand All @@ -14,12 +15,13 @@ install:
env:
global:
- MIX_ENV=test
- DATABASE_NAME=travis_ci_test

before_script:
- psql -c 'create database travis_ci_test;' -U postgres
- mix test.reset
script:
- mix compile --all-warnings --warnings-as-errors
- mix format --check-formatted --dry-run
- mix credo
- mix coveralls.travis
after_script:
- MIX_ENV=docs mix deps.get
Expand Down
53 changes: 53 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Changelog

## 1.3.0

### Added

- Support to MySQL :dolphin:

### Changed

- `Triplex.create/1,2` now rolls back the prefix creation if the `func` fails with error tuple
- Now we support to Ecto 3! :tada: But be aware that this new version does not support
the old versions of Ecto, only 3.0 and up

### Breaking changes

It's not our fault, but there is a breaking change if you upgrade it because migration on
Ecto 3 are ran on a different process.

The problem you may find is basically with this kind of code:

```elixir
Repo.transaction(fn ->
{:ok, _} = Triplex.create("tenant")
User.insert!(%{name: "Demo user 1"})
User.insert!(%{name: "Demo user 2"})
end)
```

As `Triplex.create/1` runs the tenant migrations, and they will run on different processes,
you will get an error from your db saying that the given tenant prefix (schema or database
depending on the db) does not exist.

That occurs because the new processes will checkout a new connection to db, making them
not aware of the current transaction, since it is on another db connection. But don't panic,
we have a solution for you!

Here is how you could achieve the same results on success or fail:

```elixir
Triplex.create_schema("tenant", Repo, fn(tenant, repo) ->
Repo.transaction(fn ->
{:ok, _} = Triplex.migrate(tenant, repo)
User.insert!(%{name: "Demo user 1"})
User.insert!(%{name: "Demo user 2"})

tenant
end)
end)
```

For more details about these function check the online documentation for `Triplex.create/1,2`
and `Triplex.create_schema/1,2,3`.
35 changes: 35 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Contributing

Here is a step by step for contributing to the project:

- Fork the project
- Create a branch for your feature/fix
- Make it happen
- Create tests for it
- Make sure to run `mix format`
- Also, give `mix credo` a check, we enforce no warning on CI build
- Don't forget to add the changes you made to `CHANGELOG.md`
- Open a PR
- Wait for review!

## Releasing

Here is the step by step to release the project (team members only):

- Change the `version` key on `mix.exs` to the version you'll release
- Commit it to `master`
- Make sure CI passes and docs are good
- Prepare the version branch:
- If you're releasing a major or minor, open a branch for it, with a naming like `1.0`
- If you're releasing a patch with all the things on master, checkout the minor branch and
`git rebase master` on it
- If you're releasing a patch, but can't get everything of master, `git cherry-pick <sha>` of
every commit you need, adapt if needed and push it
- Create a draft for your release on Github
- From branch: `1.0`
- Tag: `v1.0.0`
- Release name: `1.0.0`
- Description: exactly the same as changelog
- Run `mix hex.publish`
- Submit your release draft
- Update `README.md` version
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ config :triplex, ecto_repos: []
# by uncommenting the line below and defining dev.exs, test.exs and such.
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).
import_config "#{Mix.env}.exs"
import_config "#{Mix.env()}.exs"
1 change: 0 additions & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
use Mix.Config

1 change: 0 additions & 1 deletion config/docs.exs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
use Mix.Config

1 change: 0 additions & 1 deletion config/prod.exs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
use Mix.Config

21 changes: 17 additions & 4 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,30 @@ use Mix.Config
# Configure triplex
config :triplex,
reserved_tenants: [
"www", "api", "admin", "security", "app", "staging", ~r/^db\d+$/
"www",
"api",
"admin",
"security",
"app",
"staging",
~r/^db\d+$/
]

# Configure your database
config :triplex, ecto_repos: [Triplex.TestRepo]
config :triplex, Triplex.TestRepo,
adapter: Ecto.Adapters.Postgres,
config :triplex, ecto_repos: [Triplex.PGTestRepo, Triplex.MSTestRepo]

config :triplex, Triplex.PGTestRepo,
username: System.get_env("PG_USERNAME") || "postgres",
password: System.get_env("PG_PASSWORD") || "postgres",
hostname: System.get_env("PG_HOST") || "localhost",
database: "triplex_test",
pool: Ecto.Adapters.SQL.Sandbox

config :triplex, Triplex.MSTestRepo,
username: System.get_env("MS_USERNAME") || "root",
password: System.get_env("MS_PASSWORD") || "",
hostname: System.get_env("MS_HOST") || "localhost",
database: "triplex_test",
pool: Ecto.Adapters.SQL.Sandbox

config :logger, level: :warn
Loading