Skip to content

Commit

Permalink
Merge branch 'main' into impl-count
Browse files Browse the repository at this point in the history
  • Loading branch information
wiseaidev authored Aug 10, 2022
2 parents fe1dd1b + a00a68b commit 6907c4b
Show file tree
Hide file tree
Showing 18 changed files with 593 additions and 769 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Python 3.9
uses: actions/setup-python@v4.0.0
uses: actions/setup-python@v4.2.0
with:
python-version: 3.9
#----------------------------------------------
Expand Down Expand Up @@ -97,7 +97,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Python ${{ matrix.pyver }}
uses: actions/setup-python@v4.0.0
uses: actions/setup-python@v4.2.0
with:
python-version: ${{ matrix.pyver }}
#----------------------------------------------
Expand Down Expand Up @@ -158,7 +158,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Python 3.9
uses: actions/setup-python@v4.0.0
uses: actions/setup-python@v4.2.0
with:
python-version: 3.9
- name: Install Poetry
Expand Down
37 changes: 37 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: "CodeQL"

on:
push:
branches: [ "main" ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ "main" ]
schedule:
- cron: '40 4 * * 4'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'python' ]

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
2 changes: 1 addition & 1 deletion .github/workflows/spellcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v3
- name: Check Spelling
uses: rojopolis/spellcheck-github-actions@0.23.0
uses: rojopolis/spellcheck-github-actions@0.26.0
with:
config_path: .github/spellcheck-settings.yml
task_name: Markdown
1 change: 1 addition & 0 deletions aredis_om/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .async_redis import redis # isort:skip
from .checks import has_redis_json, has_redisearch
from .connections import get_redis_connection
from .model.migrations.migrator import MigrationError, Migrator
Expand Down
1 change: 1 addition & 0 deletions aredis_om/async_redis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from redis import asyncio as redis
8 changes: 4 additions & 4 deletions aredis_om/connections.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import os

import aioredis
from . import redis


URL = os.environ.get("REDIS_OM_URL", None)


def get_redis_connection(**kwargs) -> aioredis.Redis:
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 url:
return aioredis.Redis.from_url(url, **kwargs)
return redis.Redis.from_url(url, **kwargs)

# Decode from UTF-8 by default
if "decode_responses" not in kwargs:
kwargs["decode_responses"] = True
return aioredis.Redis(**kwargs)
return redis.Redis(**kwargs)
39 changes: 20 additions & 19 deletions aredis_om/model/migrations/migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from enum import Enum
from typing import List, Optional

from aioredis import Redis, ResponseError
from ... import redis


log = logging.getLogger(__name__)
Expand Down Expand Up @@ -39,18 +39,19 @@ def schema_hash_key(index_name):
return f"{index_name}:hash"


async def create_index(redis: Redis, index_name, schema, current_hash):
db_number = redis.connection_pool.connection_kwargs.get("db")
async def create_index(conn: redis.Redis, index_name, schema, current_hash):
db_number = conn.connection_pool.connection_kwargs.get("db")
if db_number and db_number > 0:
raise MigrationError(
"Creating search indexes is only supported in database 0. "
f"You attempted to create an index in database {db_number}"
)
try:
await redis.execute_command(f"ft.info {index_name}")
except ResponseError:
await redis.execute_command(f"ft.create {index_name} {schema}")
await redis.set(schema_hash_key(index_name), current_hash)
await conn.execute_command(f"ft.info {index_name}")
except redis.ResponseError:
await conn.execute_command(f"ft.create {index_name} {schema}")
# TODO: remove "type: ignore" when type stubs will be fixed
await conn.set(schema_hash_key(index_name), current_hash) # type: ignore
else:
log.info("Index already exists, skipping. Index hash: %s", index_name)

Expand All @@ -67,7 +68,7 @@ class IndexMigration:
schema: str
hash: str
action: MigrationAction
redis: Redis
conn: redis.Redis
previous_hash: Optional[str] = None

async def run(self):
Expand All @@ -78,14 +79,14 @@ async def run(self):

async def create(self):
try:
await create_index(self.redis, self.index_name, self.schema, self.hash)
except ResponseError:
await create_index(self.conn, self.index_name, self.schema, self.hash)
except redis.ResponseError:
log.info("Index already exists: %s", self.index_name)

async def drop(self):
try:
await self.redis.execute_command(f"FT.DROPINDEX {self.index_name}")
except ResponseError:
await self.conn.execute_command(f"FT.DROPINDEX {self.index_name}")
except redis.ResponseError:
log.info("Index does not exist: %s", self.index_name)


Expand All @@ -105,7 +106,7 @@ async def detect_migrations(self):

for name, cls in model_registry.items():
hash_key = schema_hash_key(cls.Meta.index_name)
redis = cls.db()
conn = cls.db()
try:
schema = cls.redisearch_schema()
except NotImplementedError:
Expand All @@ -114,21 +115,21 @@ async def detect_migrations(self):
current_hash = hashlib.sha1(schema.encode("utf-8")).hexdigest() # nosec

try:
await redis.execute_command("ft.info", cls.Meta.index_name)
except ResponseError:
await conn.execute_command("ft.info", cls.Meta.index_name)
except redis.ResponseError:
self.migrations.append(
IndexMigration(
name,
cls.Meta.index_name,
schema,
current_hash,
MigrationAction.CREATE,
redis,
conn,
)
)
continue

stored_hash = await redis.get(hash_key)
stored_hash = await conn.get(hash_key)
schema_out_of_date = current_hash != stored_hash

if schema_out_of_date:
Expand All @@ -140,7 +141,7 @@ async def detect_migrations(self):
schema,
current_hash,
MigrationAction.DROP,
redis,
conn,
stored_hash,
)
)
Expand All @@ -151,7 +152,7 @@ async def detect_migrations(self):
schema,
current_hash,
MigrationAction.CREATE,
redis,
conn,
stored_hash,
)
)
Expand Down
Loading

0 comments on commit 6907c4b

Please sign in to comment.