Skip to content

Commit

Permalink
Switch underlying driver to mongodb_driver for MongoDB 6.0+ support (#…
Browse files Browse the repository at this point in the history
…190)

* Upgrade docker-compose mongo image to 6.0.6

* Switch underlying driver to mongodb_driver for MongoDB 6.0+ support

* Interpret matched_count != 1 as staleness error in Connection.update_one/3

Basing staleness on the modified_count is incorrect, since updates that match a document but leave it as-is is a successful no-op. The action should only be considered stale if the update query did not match any documents.

* Use appropriate Hex release of mongodb_driver

* CI: same Mongo versions as elixir-mongodb-driver

* CI: don't restore old deps cache

* remove unused deps

* update mongo versions in readme

---------

Co-authored-by: Matthew Weidner <17693586+mweidner037@users.noreply.github.com>
  • Loading branch information
brennana and mweidner037 authored Oct 15, 2024
1 parent 6793f9b commit dfd21c5
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 112 deletions.
22 changes: 10 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ on:
- pull_request

jobs:

codeQuality:
name: "Tests & Code Quality"
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
otpElixir: [
{otp: "24.3.4.10", elixir: "1.14.3"}
]
otpElixir: [{ otp: "24.3.4.10", elixir: "1.14.3" }]
steps:
- name: repo checkout
uses: actions/checkout@v2
Expand All @@ -27,7 +24,7 @@ jobs:
_build
key: ${{ runner.os }}-${{matrix.otpElixir.otp}}-${{matrix.otpElixir.elixir}}-mix-${{ hashFiles('**/mix.lock') }}
restore-keys: |
${{ runner.os }}-${{matrix.otpElixir.otp}}-${{matrix.otpElixir.elixir}}-mix-
${{ runner.os }}-${{matrix.otpElixir.otp}}-${{matrix.otpElixir.elixir}}-mix-${{ hashFiles('**/mix.lock') }}
- run: mix deps.get
- run: mix credo --strict
continue-on-error: true
Expand All @@ -39,12 +36,13 @@ jobs:
strategy:
fail-fast: false
matrix:
mongodb: ["4.4", "5.0"]
otpElixir: [
{otp: "22.3.4.26", elixir: "1.12.3"},
{otp: "23.3.4.18", elixir: "1.13.4"},
{otp: "24.3.4.10", elixir: "1.14.3"}
]
mongodb: ["5.0", "6.0", "7.0"]
otpElixir:
[
{ otp: "22.3.4.26", elixir: "1.12.3" },
{ otp: "23.3.4.18", elixir: "1.13.4" },
{ otp: "24.3.4.10", elixir: "1.14.3" },
]
topology: [replica_set, server]
steps:
- name: repo checkout
Expand All @@ -66,7 +64,7 @@ jobs:
_build
key: ${{ runner.os }}-${{matrix.otpElixir.otp}}-${{matrix.otpElixir.elixir}}-mix-${{ hashFiles('**/mix.lock') }}
restore-keys: |
${{ runner.os }}-${{matrix.otpElixir.otp}}-${{matrix.otpElixir.elixir}}-mix-
${{ runner.os }}-${{matrix.otpElixir.otp}}-${{matrix.otpElixir.elixir}}-mix-${{ hashFiles('**/mix.lock') }}
- run: mix deps.get
- run: mix test
env:
Expand Down
50 changes: 24 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
[![License](https://img.shields.io/hexpm/l/mongodb_ecto.svg)](https://github.com/elixir-mongo/mongodb_ecto/blob/master/LICENSE)
[![Last Updated](https://img.shields.io/github/last-commit/elixir-mongo/mongodb_ecto.svg)](https://github.com/elixir-mongo/mongodb_ecto/commits/master)


`Mongo.Ecto` is a MongoDB adapter for Ecto.

For detailed information read the documentation for the `Mongo.Ecto` module,
or check out examples below.

## Example

```elixir
# In your config/config.exs file
config :my_app, Repo,
Expand Down Expand Up @@ -87,23 +87,23 @@ For additional information on usage please see the documentation for [Ecto](http

## Data Type Mapping

| BSON |Ecto|
| ---------- |------|
| double |`:float`|
| string  |`:string`|
| object |`:map`|
| array |`{:array, subtype}`|
| binary data |`:binary`|
| binary data (uuid) |`Ecto.UUID`|
| object id |`:binary_id`|
| boolean |`:boolean`|
| date |`Ecto.DateTime`|
| regular expression |`Mongo.Ecto.Regex`|
| JavaScript |`Mongo.Ecto.JavaScript`|
| symbol |(see below)|
| 32-bit integer |`:integer`|
| timestamp |`BSON.Timestamp`|
| 64-bit integer |`:integer`|
| BSON | Ecto |
| ------------------ | ----------------------- |
| double | `:float` |
| string  | `:string` |
| object | `:map` |
| array | `{:array, subtype}` |
| binary data | `:binary` |
| binary data (uuid) | `Ecto.UUID` |
| object id | `:binary_id` |
| boolean | `:boolean` |
| date | `Ecto.DateTime` |
| regular expression | `Mongo.Ecto.Regex` |
| JavaScript | `Mongo.Ecto.JavaScript` |
| symbol | (see below) |
| 32-bit integer | `:integer` |
| timestamp | `BSON.Timestamp` |
| 64-bit integer | `:integer` |

Symbols are deprecated by the
[BSON specification](http://bsonspec.org/spec.html). They will be converted
Expand All @@ -112,17 +112,15 @@ the database.

Additionally special values are translated as follows:

| BSON | Ecto|
| ---------- | ------|
| null | `nil`|
| min key | `:BSON_min`|
| max key | `:BSON_max`|

| BSON | Ecto |
| ------- | ----------- |
| null | `nil` |
| min key | `:BSON_min` |
| max key | `:BSON_max` |

## Supported Mongo versions

The adapter and the driver are tested against most recent versions from 2
branches: 4.4 and 5.0
The adapter and the driver are tested against most recent versions from 5.0, 6.0, and 7.0.

## Contributing

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
version: "3.9"
services:
mongo:
image: mongo:4.4.4
image: mongo:6.0.6
ports:
- 27017:27017
40 changes: 12 additions & 28 deletions lib/mongo_ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -397,24 +397,6 @@ defmodule Mongo.Ecto do
defmacro __before_compile__(_env) do
end

@pool_timeout 5_000
@timeout 15_000

defp normalize_config(config) do
config
|> Keyword.delete(:name)
|> Keyword.put_new(:timeout, @timeout)
|> Keyword.put_new(:pool_timeout, @pool_timeout)
end

defp pool_name(config) do
Keyword.get(config, :pool_name, default_pool_name(config))
end

defp default_pool_name(config) do
Module.concat(Keyword.get(config, :name, config[:repo]), Pool)
end

@doc false
def application, do: :mongodb_ecto

Expand All @@ -426,7 +408,7 @@ defmodule Mongo.Ecto do
connection = Connection

unless Code.ensure_loaded?(connection) do
driver = :mongodb
driver = :mongodb_driver

raise """
Could not find #{inspect(connection)}.
Expand All @@ -439,21 +421,18 @@ defmodule Mongo.Ecto do
"""
end

pool_name = pool_name(config)
norm_config = normalize_config(config)

log = Keyword.get(config, :log, :debug)
telemetry_prefix = Keyword.fetch!(config, :telemetry_prefix)
telemetry = {config[:repo], log, telemetry_prefix ++ [:query]}

opts = Keyword.take(config, @pool_opts)
meta = %{telemetry: telemetry, opts: opts, pool: {pool_name, norm_config}}
meta = %{telemetry: telemetry, opts: opts}
{:ok, connection.child_spec(config), meta}
end

@impl true
def ensure_all_started(_repo, type) do
{:ok, _mongo} = Application.ensure_all_started(:mongodb, type)
{:ok, _mongo} = Application.ensure_all_started(:mongodb_driver, type)
end

@impl true
Expand Down Expand Up @@ -533,7 +512,12 @@ defmodule Mongo.Ecto do
end

defp dump_date(%Date{} = date) do
{:ok, date}
dt =
{Date.to_erl(date), {0, 0, 0}}
|> NaiveDateTime.from_erl!()
|> DateTime.from_naive!("Etc/UTC")

{:ok, dt}
end

defp dump_date(nil), do: {:ok, nil}
Expand Down Expand Up @@ -806,7 +790,7 @@ defmodule Mongo.Ecto do
if major_version > 3 || (major_version == 3 && minor_version >= 4) do
_all_collection_names =
repo
|> command(%{listCollections: 1}, opts)
|> command([listCollections: 1], opts)
|> get_in(["cursor", "firstBatch"])
# exclude mongo views which were introduced in version 3.4
|> Enum.filter(&(&1["type"] == "collection"))
Expand Down Expand Up @@ -852,7 +836,7 @@ defmodule Mongo.Ecto do
end

defp list_collections([major_version | _], repo, opts) when major_version >= 3 do
colls = command(repo, %{listCollections: 1}, opts)["cursor"]["firstBatch"]
colls = command(repo, [listCollections: 1], opts)["cursor"]["firstBatch"]

_all_collections =
colls
Expand Down Expand Up @@ -880,7 +864,7 @@ defmodule Mongo.Ecto do
end

defp db_version(repo) do
command(repo, %{buildinfo: 1}, [])["versionArray"]
command(repo, [buildinfo: 1], [])["versionArray"]
end

@doc """
Expand Down
Loading

0 comments on commit dfd21c5

Please sign in to comment.