Skip to content

Commit

Permalink
Sync with beanie 2023.10 (#16)
Browse files Browse the repository at this point in the history
* Sunced | 14.10.2023 | 1

* skip migration break test

* fix fastapi tests

* remove redundant gh actions

* sync with beanie | 14.10.2023 | 2

* changelog
  • Loading branch information
roman-right authored Oct 14, 2023
1 parent a6bb240 commit b63db0a
Show file tree
Hide file tree
Showing 109 changed files with 4,404 additions and 1,333 deletions.
19 changes: 0 additions & 19 deletions .github/workflows/github-actions-lint.yml

This file was deleted.

17 changes: 0 additions & 17 deletions .github/workflows/github-actions-mypy.yml

This file was deleted.

8 changes: 3 additions & 5 deletions .github/workflows/github-actions-publish-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ jobs:
publish_docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: actions/setup-python@v2
with:
python-version: 3.8
- name: install poetry
run: pip install poetry
python-version: 3.10.9
- name: install dependencies
run: poetry install
run: pip3 install .[doc]
- name: publish docs
run: bash scripts/publish_docs.sh
16 changes: 7 additions & 9 deletions .github/workflows/github-actions-publish-project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ jobs:
publish_project:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: 3.10.9
- name: install poetry
run: pip install poetry
- name: install dependencies
run: poetry install
- uses: actions/checkout@v3
- name: install flit
run: pip3 install flit
- name: publish project
run: poetry publish --build --username __token__ --password ${{ secrets.PYPI_TOKEN }}
env:
FLIT_USERNAME: __token__
FLIT_PASSWORD: ${{ secrets.FLIT_PASSWORD }}
run: flit publish
30 changes: 19 additions & 11 deletions .github/workflows/github-actions-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,37 @@ name: Tests
on: [ pull_request ]

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: 3.11
- uses: pre-commit/[email protected]
run-tests:
strategy:
fail-fast: false
matrix:
python-version: [ 3.7, 3.8, 3.9, 3.10.9, 3.11 ]
python-version: [ 3.7, 3.8, 3.9, 3.10.6, 3.11 ]
mongodb-version: [ 4.4, 5.0 ]
pydantic-version: [ 1.10.0 ]
pydantic-version: [ 1.10.12, 2.3 ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: pip
cache-dependency-path: pyproject.toml
- name: Start MongoDB
uses: supercharge/mongodb-github-action@1.3.0
uses: supercharge/mongodb-github-action@1.8.0
with:
mongodb-version: ${{ matrix.mongodb-version }}
mongodb-replica-set: test-rs
- name: install poetry
run: pip install poetry
- name: install dependencies
run: poetry install
- name: specify pydantic
run: poetry add pydantic==${{ matrix.pydantic-version }}
run: pip install .[test]
- name: install pydantic
run: pip install pydantic==${{ matrix.pydantic-version }}
- name: run tests
run: poetry run pytest
run: pytest -v
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,5 @@ docker-compose-aws.yml
tilt_modules

# Poetry stuff
poetry.lock
poetry.lock
.pdm-python
10 changes: 4 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
repos:
- repo: https://github.com/ambv/black
rev: 22.3.0
- repo: https://github.com/psf/black
rev: 23.9.1
hooks:
- id: black
language_version: python3.10
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.220
rev: v0.0.290
hooks:
- id: ruff
args: [ "--force-exclude" ]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.910
rev: v1.5.1
hooks:
- id: mypy
additional_dependencies:
- types-click
- types-toml
exclude: ^tests/

42 changes: 27 additions & 15 deletions bunnet/__init__.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
from bunnet.migrations.controllers.free_fall import free_fall_migration
from bunnet.migrations.controllers.iterative import iterative_migration
from bunnet.odm.actions import (
before_event,
after_event,
After,
Before,
Delete,
Insert,
Replace,
Save,
SaveChanges,
ValidateOnSave,
Before,
After,
Delete,
Update,
ValidateOnSave,
after_event,
before_event,
)
from bunnet.odm.bulk import BulkWriter
from bunnet.odm.custom_types import DecimalAnnotation
from bunnet.odm.custom_types.bson.binary import BsonBinary
from bunnet.odm.documents import Document
from bunnet.odm.fields import (
PydanticObjectId,
BackLink,
DeleteRules,
Indexed,
Link,
PydanticObjectId,
WriteRules,
DeleteRules,
)
from bunnet.odm.settings.timeseries import TimeSeriesConfig, Granularity
from bunnet.odm.documents import Document
from bunnet.odm.queries.update import UpdateResponse
from bunnet.odm.settings.timeseries import Granularity, TimeSeriesConfig
from bunnet.odm.union_doc import UnionDoc
from bunnet.odm.utils.init import init_bunnet
from bunnet.odm.views import View
from bunnet.odm.union_doc import UnionDoc

__version__ = "1.1.0"
__version__ = "1.2.0"
__all__ = [
# ODM
"Document",
Expand All @@ -42,6 +47,7 @@
"after_event",
"Insert",
"Replace",
"Save",
"SaveChanges",
"ValidateOnSave",
"Delete",
Expand All @@ -50,11 +56,17 @@
"Update",
# Bulk Write
"BulkWriter",
# Migrations
"iterative_migration",
"free_fall_migration",
# Relations
"Link",
"BackLink",
"WriteRules",
"DeleteRules",
# Migrations
"iterative_migration",
"free_fall_migration",
# Custom Types
"DecimalAnnotation",
"BsonBinary",
# UpdateResponse
"UpdateResponse",
]
130 changes: 81 additions & 49 deletions bunnet/executors/migrate.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,101 @@
import logging
import os
import shutil
from datetime import datetime
from pathlib import Path
from typing import Dict, Any
from typing import Any

import click
import toml
from pydantic import BaseSettings

from bunnet.migrations import template
from bunnet.migrations.database import DBHandler
from bunnet.migrations.models import RunningMode, RunningDirections
from bunnet.migrations.models import RunningDirections, RunningMode
from bunnet.migrations.runner import MigrationNode

logging.basicConfig(format="%(message)s", level=logging.INFO)


def toml_config_settings_source(settings: BaseSettings) -> Dict[str, Any]:
path = Path("pyproject.toml")
if path.is_file():
return (
toml.load(path)
.get("tool", {})
.get("bunnet", {})
.get("migrations", {})
class MigrationSettings:
def __init__(self, **kwargs):
self.direction = (
kwargs.get("direction")
or self.get_env_value("direction")
or self.get_from_toml("direction")
or RunningDirections.FORWARD
)
return {}


class MigrationSettings(BaseSettings):
direction: RunningDirections = RunningDirections.FORWARD
distance: int = 0
connection_uri: str
database_name: str
path: Path
allow_index_dropping: bool = False

class Config:
env_prefix = "bunnet_"
fields = {
"connection_uri": {
"env": [
"uri",
"connection_uri",
"connection_string",
"mongodb_dsn",
"mongodb_uri",
]
},
"db": {"env": ["db", "db_name", "database_name"]},
}

@classmethod
def customise_sources(
cls,
init_settings,
env_settings,
file_secret_settings,
):
return (
init_settings,
toml_config_settings_source,
env_settings,
file_secret_settings,

self.distance = int(
kwargs.get("distance")
or self.get_env_value("distance")
or self.get_from_toml("distance")
or 0
)
self.connection_uri = str(
kwargs.get("connection_uri")
or self.get_env_value("connection_uri")
or self.get_from_toml("connection_uri")
)
self.database_name = str(
kwargs.get("database_name")
or self.get_env_value("database_name")
or self.get_from_toml("database_name")
)
self.path = Path(
kwargs.get("path")
or self.get_env_value("path")
or self.get_from_toml("path")
)
self.allow_index_dropping = bool(
kwargs.get("allow_index_dropping")
or self.get_env_value("allow_index_dropping")
or self.get_from_toml("allow_index_dropping")
or False
)

@staticmethod
def get_env_value(field_name) -> Any:
if field_name == "connection_uri":
value = (
os.environ.get("BUNNET_URI")
or os.environ.get("BUNNET_CONNECTION_URI")
or os.environ.get("BUNNET_CONNECTION_STRING")
or os.environ.get("BUNNET_MONGODB_DSN")
or os.environ.get("BUNNET_MONGODB_URI")
or os.environ.get("bunnet_uri")
or os.environ.get("bunnet_connection_uri")
or os.environ.get("bunnet_connection_string")
or os.environ.get("bunnet_mongodb_dsn")
or os.environ.get("bunnet_mongodb_uri")
)
elif field_name == "database_name":
value = (
os.environ.get("BUNNET_DB")
or os.environ.get("BUNNET_DB_NAME")
or os.environ.get("BUNNET_DATABASE_NAME")
or os.environ.get("bunnet_db")
or os.environ.get("bunnet_db_name")
or os.environ.get("bunnet_database_name")
)
else:
value = os.environ.get(
f"BUNNET_{field_name.upper()}"
) or os.environ.get(f"bunnet_{field_name.lower()}")
return value

@staticmethod
def get_from_toml(field_name) -> Any:
path = Path("pyproject.toml")
if path.is_file():
val = (
toml.load(path)
.get("tool", {})
.get("bunnet", {})
.get("migrations", {})
)
else:
val = {}
return val.get(field_name)


@click.group()
Expand Down
Loading

0 comments on commit b63db0a

Please sign in to comment.