Refine connection strings (dotenv), Makefile targets

This commit is contained in:
Andrew Brookins 2021-10-20 14:29:31 -07:00
parent d2fa4c586f
commit 49654eeede
7 changed files with 49 additions and 7 deletions

View file

@ -14,6 +14,7 @@ help:
@echo " format reformat code"
@echo " test run all the tests"
@echo " shell open a Poetry shell"
@echo " redis start a Redis instance with Docker"
@echo ""
@echo "Check the Makefile to know exactly what each target is doing."
@ -44,7 +45,7 @@ format: $(INSTALL_STAMP)
.PHONY: test
test: $(INSTALL_STAMP)
#$(POETRY) run pytest ./tests/ --cov-report term-missing --cov-fail-under 100 --cov $(NAME)
$(POETRY) run pytest ./tests/
$(POETRY) run pytest -s -vv ./tests/
.PHONY: shell
shell: $(INSTALL_STAMP)
@ -53,3 +54,7 @@ shell: $(INSTALL_STAMP)
.PHONY: redis
redis:
docker-compose up -d
.PHONY: all
all: redis $(INSTALL_STAMP) lint test

View file

@ -3,7 +3,7 @@ version: "3.8"
services:
redis:
image: "redislabs/redismod:edge"
entrypoint: ["redis-server", "--appendonly", "yes", "--loadmodule", "/usr/lib/redis/modules/rejson.so"]
entrypoint: ["redis-server", "--appendonly", "yes", "--loadmodule", "/usr/lib/redis/modules/rejson.so", "--loadmodule", "/usr/lib/redis/modules/redisearch.so"]
restart: always
ports:
- "6380:6379"

17
poetry.lock generated
View file

@ -513,6 +513,17 @@ toml = "*"
[package.extras]
testing = ["argcomplete", "hypothesis (>=3.56)", "mock", "nose", "requests", "xmlschema"]
[[package]]
name = "python-dotenv"
version = "0.19.1"
description = "Read key-value pairs from a .env file and set them as environment variables"
category = "main"
optional = false
python-versions = ">=3.5"
[package.extras]
cli = ["click (>=5.0)"]
[[package]]
name = "python-ulid"
version = "1.0.3"
@ -645,7 +656,7 @@ python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,>=2.7"
[metadata]
lock-version = "1.1"
python-versions = "^3.8"
content-hash = "f1ccd73314f307ce41497d093ddce99cfb96ebf1814e854a94e37d5156647967"
content-hash = "1bc742aa620496b8823ab47f485c0e7a24c64ba1e54521d8485ec79d7e453016"
[metadata.files]
aioredis = [
@ -883,6 +894,10 @@ pytest = [
{file = "pytest-6.2.5-py3-none-any.whl", hash = "sha256:7310f8d27bc79ced999e760ca304d69f6ba6c6649c0b60fb0e04a4a77cacc134"},
{file = "pytest-6.2.5.tar.gz", hash = "sha256:131b36680866a76e6781d13f101efb86cf674ebb9762eb70d3082b6f29889e89"},
]
python-dotenv = [
{file = "python-dotenv-0.19.1.tar.gz", hash = "sha256:14f8185cc8d494662683e6914addcb7e95374771e707601dfc70166946b4c4b8"},
{file = "python_dotenv-0.19.1-py2.py3-none-any.whl", hash = "sha256:bbd3da593fc49c249397cbfbcc449cf36cb02e75afc8157fcc6a81df6fb7750a"},
]
python-ulid = [
{file = "python-ulid-1.0.3.tar.gz", hash = "sha256:5dd8b969312a40e2212cec9c1ad63f25d4b6eafd92ee3195883e0287b6e9d19e"},
{file = "python_ulid-1.0.3-py3-none-any.whl", hash = "sha256:8704dc20f547f531fe3a41d4369842d737a0f275403b909d0872e7ea0fe8d6f2"},

View file

@ -16,6 +16,7 @@ pptree = "^3.1"
types-redis = "^3.5.9"
types-six = "^1.16.1"
python-ulid = "^1.0.3"
python-dotenv = "^0.19.1"
[tool.poetry.dev-dependencies]
mypy = "^0.910"

View file

@ -1,5 +1,26 @@
import os
import dotenv
import redis
def get_redis_connection() -> redis.Redis:
return redis.Redis(decode_responses=True)
dotenv.load_dotenv()
URL = os.environ.get("REDIS_OM_URL", None)
def get_redis_connection(**kwargs) -> redis.Redis:
# If someone passed in a 'url' parameter, or specified a REDIS_OM_URL
# environment variable, we'll create the Redis client from the URL.
url = kwargs.pop("url", URL)
if not url:
import ipdb
ipdb.set_trace()
if url:
return redis.from_url(url, **kwargs)
# Decode from UTF-8 by default
if "decode_responses" not in kwargs:
kwargs["decode_responses"] = True
return redis.Redis(**kwargs)

View file

@ -37,6 +37,7 @@ from pydantic.utils import Representation
from redis.client import Pipeline
from ulid import ULID
from ..connections import get_redis_connection
from .encoders import jsonable_encoder
from .render_tree import render_tree
from .token_escaper import TokenEscaper
@ -997,7 +998,7 @@ class ModelMeta(ModelMetaclass):
)
if not getattr(new_class._meta, "database", None):
new_class._meta.database = getattr(
base_meta, "database", redis.Redis(decode_responses=True)
base_meta, "database", get_redis_connection()
)
if not getattr(new_class._meta, "primary_key_creator_cls", None):
new_class._meta.primary_key_creator_cls = getattr(

View file

@ -17,7 +17,6 @@ def redis():
@pytest.fixture
def key_prefix():
# TODO
yield "redis-developer"