Skip to content

Commit

Permalink
refactor to suport SQLAlchemy 2.0 migration (#82)
Browse files Browse the repository at this point in the history
* Add pyicu to Docker image

* Update PyPI GHA

* Refactor servicelayer to work with SQLAlchemy 2.0

* Extract dependencies to requirements.txt and pin versions

* Add semver to setup.py, edit GHA

* Upgrade SQLAlchemy version from requrements.txt
  • Loading branch information
catileptic authored Apr 25, 2023
1 parent 2bf1fef commit e35a185
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 14 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ jobs:
python setup.py sdist bdist_wheel
- name: Publish a Python distribution to PyPI
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
uses: pypa/gh-action-pypi-publish@master
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.pypi_password }}
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ ENV DEBIAN_FRONTEND noninteractive

RUN apt-get -qq -y update \
&& apt-get -qq -y install python3-pip \
pkg-config libicu-dev \
&& apt-get -qq -y autoremove \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN pip3 install --no-binary=:pyicu: pyicu

ENV LANG='en_US.UTF-8'

COPY . /opt/servicelayer
RUN pip3 install -q --no-cache-dir -e /opt/servicelayer[dev]
WORKDIR /opt/servicelayer
RUN pip3 install -q --no-cache-dir -e /opt/servicelayer[dev]
RUN pip3 install -r requirements.txt

CMD /bin/bash
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

all: clean install test

build-docker:
docker-compose build --no-rm --parallel

install:
pip install -q -e .
pip install -q twine coverage nose moto boto3
Expand Down
7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
banal==1.0.6
normality==2.4.0
fakeredis==1.10.1
sqlalchemy==2.0.4
structlog==22.3.0
colorama==0.4.6
pika==1.3.1
16 changes: 10 additions & 6 deletions servicelayer/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, name, uri=settings.TAGS_DATABASE_URI, **config):
self.is_postgres = self.engine.dialect.name == "postgresql"
self.table = Table(
name,
MetaData(self.engine),
MetaData(),
Column("key", String, primary_key=True), # noqa
Column("value", JSONB if self.is_postgres else JSON),
Column("timestamp", DateTime),
Expand All @@ -36,27 +36,31 @@ def delete(self, key=None, prefix=None):
stmt = stmt.where(self.table.c.key == key)
if prefix is not None:
stmt = stmt.where(self.table.c.key.startswith(prefix))
self.engine.execute(stmt)
with self.engine.connect() as conn:
conn.execute(stmt)
conn.commit()

def close(self):
self.engine.dispose()

def get(self, key, since=None):
stmt = select([self.table.c.value])
stmt = select(self.table.c.value)
stmt = stmt.where(self.table.c.key == key)
if since is not None:
stmt = stmt.where(self.table.c.timestamp >= since)
rp = self.engine.execute(stmt)
with self.engine.connect() as conn:
rp = conn.execute(stmt)
row = rp.fetchone()
if row is not None:
return row.value

def exists(self, key, since=None):
stmt = select([func.count()])
stmt = select(func.count())
stmt = stmt.where(self.table.c.key == key)
if since is not None:
stmt = stmt.where(self.table.c.timestamp >= since)
rp = self.engine.execute(stmt)
with self.engine.connect() as conn:
rp = conn.execute(stmt)
count = rp.scalar()
return count > 0

Expand Down
10 changes: 5 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
include_package_data=True,
zip_safe=True,
install_requires=[
"banal >= 1.0.1, <2.0.0",
"normality >= 2.1.1, <3.0.0",
"fakeredis == 1.10.1",
"sqlalchemy >= 1.3",
"banal >= 1.0.6, < 2.0.0",
"normality >= 2.4.0, < 3.0.0",
"fakeredis == 1.10.1, < 2.0.0",
"sqlalchemy >= 2.0.4, < 3.0.0",
"structlog >= 20.2.0, < 23.0.0",
"colorama >= 0.4.4, < 1.0.0",
"pika == 1.3.1",
"pika >= 1.3.1, < 2.0.0"
],
extras_require={
"amazon": ["boto3 >= 1.11.9, <2.0.0"],
Expand Down

0 comments on commit e35a185

Please sign in to comment.