diff --git a/Dockerfile b/Dockerfile index 225e394903bb02d687bfbaf7027b4f51edf97871..19414c0a69056168cef97ae98274530d507a326e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM gitlab-registry.cern.ch/push-notifications/notifications-routing/notifications-routing-base:afc24a62 +FROM gitlab-registry.cern.ch/push-notifications/notifications-routing/notifications-routing-base:77c9480e ARG build_env COPY ./ ./ diff --git a/Dockerfile-base b/Dockerfile-base index e3a10016c9f51e88d4310f94ef6facd676f9d3f4..3517ce1e3739da82f5494f673002b5f252b5cf77 100644 --- a/Dockerfile-base +++ b/Dockerfile-base @@ -17,6 +17,9 @@ RUN dnf install -y python38 \ RUN ln -nsf /usr/bin/python3.6 /usr/bin/python && ln -nsf /usr/bin/pip3.6 /usr/bin/pip && \ ln -nsf /usr/bin/python3.6 /usr/bin/python3 && ln -nsf /usr/bin/pip3.6 /usr/bin/pip3 +# upgrade pip +RUN pip install --upgrade pip + WORKDIR /app/ ENV POETRY_HOME=/opt/poetry POETRY_VIRTUALENVS_CREATE=false PYTHONPATH=/app diff --git a/notifications_routing/data_source/postgres/postgres_data_source.py b/notifications_routing/data_source/postgres/postgres_data_source.py index 1108630b138c9b8e65af3d4d9f464d3a5dc374ad..0f39d18ecef87dd740319cb7132d33c346722343 100644 --- a/notifications_routing/data_source/postgres/postgres_data_source.py +++ b/notifications_routing/data_source/postgres/postgres_data_source.py @@ -5,7 +5,7 @@ from contextlib import contextmanager from datetime import date, time from typing import Dict, List -from sqlalchemy import Boolean, Column, Date, ForeignKey, String, Table, Time, create_engine, or_ +from sqlalchemy import Boolean, Column, Date, ForeignKey, MetaData, String, Table, Time, create_engine, or_ from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.ext.automap import automap_base from sqlalchemy.orm import relationship, sessionmaker @@ -20,14 +20,18 @@ from notifications_routing.exceptions import MultipleResultsFoundError, NotFound class PostgresDataSource(DataSource): """Implements methods from DataSource interface.""" - Base = automap_base() + Base = automap_base( + metadata=MetaData( + schema=Config.DB_SCHEMA, + ) + ) def __init__(self): """Initialize Data Source.""" logging.debug("Init PostgresDataSource") self.__engine = create_engine(Config.SQLALCHEMY_DATABASE_URI) self.__session = sessionmaker(self.__engine) - self.Base.prepare(self.__engine, reflect=True) + self.Base.prepare(self.__engine) @contextmanager def session(self): @@ -195,7 +199,6 @@ class User(PostgresDataSource.Base): """User Model.""" __tablename__ = "Users" - __table_args__ = {"schema": Config.DB_SCHEMA, "extend_existing": True} id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) username = Column(String) @@ -210,33 +213,29 @@ class User(PostgresDataSource.Base): channel_groups = Table( "channels_groups__groups", PostgresDataSource.Base.metadata, - Column("channelsId", UUID(as_uuid=True), ForeignKey(f"{Config.DB_SCHEMA}.Channels.id")), - Column("groupsId", UUID(as_uuid=True), ForeignKey(f"{Config.DB_SCHEMA}.Groups.id")), - schema=Config.DB_SCHEMA, + Column("channelsId", UUID(as_uuid=True), ForeignKey("Channels.id")), + Column("groupsId", UUID(as_uuid=True), ForeignKey("Groups.id")), ) channel_members = Table( "channels_members__users", PostgresDataSource.Base.metadata, - Column("channelsId", UUID(as_uuid=True), ForeignKey(f"{Config.DB_SCHEMA}.Channels.id")), - Column("usersId", UUID(as_uuid=True), ForeignKey(f"{Config.DB_SCHEMA}.Users.id")), - schema=Config.DB_SCHEMA, + Column("channelsId", UUID(as_uuid=True), ForeignKey("Channels.id")), + Column("usersId", UUID(as_uuid=True), ForeignKey("Users.id")), ) preferences_devices = Table( "preferences_devices__devices", PostgresDataSource.Base.metadata, - Column("preferencesId", UUID(as_uuid=True), ForeignKey(f"{Config.DB_SCHEMA}.Preferences.id")), - Column("devicesId", UUID(as_uuid=True), ForeignKey(f"{Config.DB_SCHEMA}.Devices.id")), - schema=Config.DB_SCHEMA, + Column("preferencesId", UUID(as_uuid=True), ForeignKey("Preferences.id")), + Column("devicesId", UUID(as_uuid=True), ForeignKey("Devices.id")), ) preferences_disabled_channels = Table( "preferences_disabled_channels__channels", PostgresDataSource.Base.metadata, - Column("preferencesId", UUID(as_uuid=True), ForeignKey(f"{Config.DB_SCHEMA}.Preferences.id")), - Column("channelsId", UUID(as_uuid=True), ForeignKey(f"{Config.DB_SCHEMA}.Channels.id")), - schema=Config.DB_SCHEMA, + Column("preferencesId", UUID(as_uuid=True), ForeignKey("Preferences.id")), + Column("channelsId", UUID(as_uuid=True), ForeignKey("Channels.id")), ) @@ -244,10 +243,10 @@ class Device(PostgresDataSource.Base): """Device Model.""" __tablename__ = "Devices" - __table_args__ = {"schema": Config.DB_SCHEMA, "extend_existing": True} + id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) name = Column(String) - userId = Column(UUID(as_uuid=True), ForeignKey(f"{Config.DB_SCHEMA}.Users.id")) + userId = Column(UUID(as_uuid=True), ForeignKey("Users.id")) info = Column(String) type = Column(String) subType = Column(String) @@ -258,10 +257,11 @@ class Preference(PostgresDataSource.Base): """Preference Model.""" __tablename__ = "Preferences" - __table_args__ = {"schema": Config.DB_SCHEMA, "extend_existing": True} + id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) type = Column(String) - userId = Column(UUID(as_uuid=True), ForeignKey(f"{Config.DB_SCHEMA}.Users.id")) + name = Column(String) + userId = Column(UUID(as_uuid=True), ForeignKey("Users.id")) targetId = Column(String) notificationPriority = Column(String) rangeStart = Column(Time) @@ -274,7 +274,7 @@ class Channel(PostgresDataSource.Base): """Channel Model.""" __tablename__ = "Channels" - __table_args__ = {"schema": Config.DB_SCHEMA, "extend_existing": True} + id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) slug = Column(String) name = Column(String) @@ -286,7 +286,6 @@ class Group(PostgresDataSource.Base): """Group Model.""" __tablename__ = "Groups" - __table_args__ = {"schema": Config.DB_SCHEMA, "extend_existing": True} id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) groupIdentifier = Column(String) @@ -296,12 +295,17 @@ class UserDailyNotification(PostgresDataSource.Base): """UserDailyNotification Model.""" __tablename__ = "UserDailyNotifications" - __table_args__ = {"schema": Config.DB_SCHEMA, "extend_existing": True} - userId = Column(UUID(as_uuid=True), primary_key=True) - notificationId = Column(UUID(as_uuid=True)) - date = Column(Date, primary_key=True) - time = Column(Time, primary_key=True) + userId = Column(UUID(as_uuid=True), ForeignKey("Users.id"), index=True) + notificationId = Column(UUID(as_uuid=True), ForeignKey("Users.id"), index=True) + date = Column( + Date, + primary_key=True, + ) + time = Column( + Time, + primary_key=True, + ) def __init__(self, user_id, notification_id, notification_date, notification_time) -> None: """Initialize UserDailyNotification.""" diff --git a/poetry.lock b/poetry.lock index 0a9541dbbab4c41c633bada1216800b5942d48e4..adef2d8d645df4bc7dfddd6807f691bbfa319fcb 100644 --- a/poetry.lock +++ b/poetry.lock @@ -415,13 +415,14 @@ python-versions = "*" [[package]] name = "sqlalchemy" -version = "1.2.19" +version = "1.3.20" description = "Database Abstraction Library" category = "main" optional = false -python-versions = "*" +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" [package.extras] +mssql = ["pyodbc"] mssql_pymssql = ["pymssql"] mssql_pyodbc = ["pyodbc"] mysql = ["mysqlclient"] @@ -507,7 +508,7 @@ testing = ["pytest (>=3.5,!=3.7.3)", "pytest-checkdocs (>=1.2.3)", "pytest-flake [metadata] lock-version = "1.1" python-versions = "^3.6.1" -content-hash = "03577ea896d4c5ea4d53152b47a85a9f9db92ea00b2a360ac6520473ae46ae2a" +content-hash = "420e3cd5a465a3442b37d3b785fb51e33c57ba52860835712509f6df57051108" [metadata.files] appdirs = [ @@ -721,7 +722,44 @@ snowballstemmer = [ {file = "snowballstemmer-2.1.0.tar.gz", hash = "sha256:e997baa4f2e9139951b6f4c631bad912dfd3c792467e2f03d7239464af90e914"}, ] sqlalchemy = [ - {file = "SQLAlchemy-1.2.19.tar.gz", hash = "sha256:5bb2c4fc2bcc3447ad45716c66581eab982c007dcf925482498d8733f86f17c7"}, + {file = "SQLAlchemy-1.3.20-cp27-cp27m-macosx_10_14_x86_64.whl", hash = "sha256:bad73f9888d30f9e1d57ac8829f8a12091bdee4949b91db279569774a866a18e"}, + {file = "SQLAlchemy-1.3.20-cp27-cp27m-manylinux1_x86_64.whl", hash = "sha256:e32e3455db14602b6117f0f422f46bc297a3853ae2c322ecd1e2c4c04daf6ed5"}, + {file = "SQLAlchemy-1.3.20-cp27-cp27m-manylinux2010_x86_64.whl", hash = "sha256:5cdfe54c1e37279dc70d92815464b77cd8ee30725adc9350f06074f91dbfeed2"}, + {file = "SQLAlchemy-1.3.20-cp27-cp27m-win32.whl", hash = "sha256:2e9bd5b23bba8ae8ce4219c9333974ff5e103c857d9ff0e4b73dc4cb244c7d86"}, + {file = "SQLAlchemy-1.3.20-cp27-cp27m-win_amd64.whl", hash = "sha256:5d92c18458a4aa27497a986038d5d797b5279268a2de303cd00910658e8d149c"}, + {file = "SQLAlchemy-1.3.20-cp27-cp27mu-manylinux1_x86_64.whl", hash = "sha256:53fd857c6c8ffc0aa6a5a3a2619f6a74247e42ec9e46b836a8ffa4abe7aab327"}, + {file = "SQLAlchemy-1.3.20-cp27-cp27mu-manylinux2010_x86_64.whl", hash = "sha256:0a92745bb1ebbcb3985ed7bda379b94627f0edbc6c82e9e4bac4fb5647ae609a"}, + {file = "SQLAlchemy-1.3.20-cp35-cp35m-macosx_10_14_x86_64.whl", hash = "sha256:b6f036ecc017ec2e2cc2a40615b41850dc7aaaea6a932628c0afc73ab98ba3fb"}, + {file = "SQLAlchemy-1.3.20-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:3aa6d45e149a16aa1f0c46816397e12313d5e37f22205c26e06975e150ffcf2a"}, + {file = "SQLAlchemy-1.3.20-cp35-cp35m-manylinux2010_x86_64.whl", hash = "sha256:ed53209b5f0f383acb49a927179fa51a6e2259878e164273ebc6815f3a752465"}, + {file = "SQLAlchemy-1.3.20-cp35-cp35m-manylinux2014_aarch64.whl", hash = "sha256:d3b709d64b5cf064972b3763b47139e4a0dc4ae28a36437757f7663f67b99710"}, + {file = "SQLAlchemy-1.3.20-cp35-cp35m-win32.whl", hash = "sha256:950f0e17ffba7a7ceb0dd056567bc5ade22a11a75920b0e8298865dc28c0eff6"}, + {file = "SQLAlchemy-1.3.20-cp35-cp35m-win_amd64.whl", hash = "sha256:8dcbf377529a9af167cbfc5b8acec0fadd7c2357fc282a1494c222d3abfc9629"}, + {file = "SQLAlchemy-1.3.20-cp36-cp36m-macosx_10_14_x86_64.whl", hash = "sha256:0157c269701d88f5faf1fa0e4560e4d814f210c01a5b55df3cab95e9346a8bcc"}, + {file = "SQLAlchemy-1.3.20-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:7cd40cb4bc50d9e87b3540b23df6e6b24821ba7e1f305c1492b0806c33dbdbec"}, + {file = "SQLAlchemy-1.3.20-cp36-cp36m-manylinux2010_x86_64.whl", hash = "sha256:c092fe282de83d48e64d306b4bce03114859cdbfe19bf8a978a78a0d44ddadb1"}, + {file = "SQLAlchemy-1.3.20-cp36-cp36m-manylinux2014_aarch64.whl", hash = "sha256:166917a729b9226decff29416f212c516227c2eb8a9c9f920d69ced24e30109f"}, + {file = "SQLAlchemy-1.3.20-cp36-cp36m-win32.whl", hash = "sha256:632b32183c0cb0053194a4085c304bc2320e5299f77e3024556fa2aa395c2a8b"}, + {file = "SQLAlchemy-1.3.20-cp36-cp36m-win_amd64.whl", hash = "sha256:bbc58fca72ce45a64bb02b87f73df58e29848b693869e58bd890b2ddbb42d83b"}, + {file = "SQLAlchemy-1.3.20-cp37-cp37m-macosx_10_14_x86_64.whl", hash = "sha256:b15002b9788ffe84e42baffc334739d3b68008a973d65fad0a410ca5d0531980"}, + {file = "SQLAlchemy-1.3.20-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:9e379674728f43a0cd95c423ac0e95262500f9bfd81d33b999daa8ea1756d162"}, + {file = "SQLAlchemy-1.3.20-cp37-cp37m-manylinux2010_x86_64.whl", hash = "sha256:2b5dafed97f778e9901b79cc01b88d39c605e0545b4541f2551a2fd785adc15b"}, + {file = "SQLAlchemy-1.3.20-cp37-cp37m-manylinux2014_aarch64.whl", hash = "sha256:fcdb3755a7c355bc29df1b5e6fb8226d5c8b90551d202d69d0076a8a5649d68b"}, + {file = "SQLAlchemy-1.3.20-cp37-cp37m-win32.whl", hash = "sha256:bca4d367a725694dae3dfdc86cf1d1622b9f414e70bd19651f5ac4fb3aa96d61"}, + {file = "SQLAlchemy-1.3.20-cp37-cp37m-win_amd64.whl", hash = "sha256:f605f348f4e6a2ba00acb3399c71d213b92f27f2383fc4abebf7a37368c12142"}, + {file = "SQLAlchemy-1.3.20-cp38-cp38-macosx_10_14_x86_64.whl", hash = "sha256:84f0ac4a09971536b38cc5d515d6add7926a7e13baa25135a1dbb6afa351a376"}, + {file = "SQLAlchemy-1.3.20-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:2909dffe5c9a615b7e6c92d1ac2d31e3026dc436440a4f750f4749d114d88ceb"}, + {file = "SQLAlchemy-1.3.20-cp38-cp38-manylinux2010_x86_64.whl", hash = "sha256:c3ab23ee9674336654bf9cac30eb75ac6acb9150dc4b1391bec533a7a4126471"}, + {file = "SQLAlchemy-1.3.20-cp38-cp38-manylinux2014_aarch64.whl", hash = "sha256:009e8388d4d551a2107632921320886650b46332f61dc935e70c8bcf37d8e0d6"}, + {file = "SQLAlchemy-1.3.20-cp38-cp38-win32.whl", hash = "sha256:bf53d8dddfc3e53a5bda65f7f4aa40fae306843641e3e8e701c18a5609471edf"}, + {file = "SQLAlchemy-1.3.20-cp38-cp38-win_amd64.whl", hash = "sha256:7c735c7a6db8ee9554a3935e741cf288f7dcbe8706320251eb38c412e6a4281d"}, + {file = "SQLAlchemy-1.3.20-cp39-cp39-macosx_10_14_x86_64.whl", hash = "sha256:4bdbdb8ca577c6c366d15791747c1de6ab14529115a2eb52774240c412a7b403"}, + {file = "SQLAlchemy-1.3.20-cp39-cp39-manylinux1_x86_64.whl", hash = "sha256:ce64a44c867d128ab8e675f587aae7f61bd2db836a3c4ba522d884cd7c298a77"}, + {file = "SQLAlchemy-1.3.20-cp39-cp39-manylinux2010_x86_64.whl", hash = "sha256:be41d5de7a8e241864189b7530ca4aaf56a5204332caa70555c2d96379e18079"}, + {file = "SQLAlchemy-1.3.20-cp39-cp39-manylinux2014_aarch64.whl", hash = "sha256:1f5f369202912be72fdf9a8f25067a5ece31a2b38507bb869306f173336348da"}, + {file = "SQLAlchemy-1.3.20-cp39-cp39-win32.whl", hash = "sha256:0cca1844ba870e81c03633a99aa3dc62256fb96323431a5dec7d4e503c26372d"}, + {file = "SQLAlchemy-1.3.20-cp39-cp39-win_amd64.whl", hash = "sha256:d05cef4a164b44ffda58200efcb22355350979e000828479971ebca49b82ddb1"}, + {file = "SQLAlchemy-1.3.20.tar.gz", hash = "sha256:d2f25c7f410338d31666d7ddedfa67570900e248b940d186b48461bd4e5569a1"}, ] "stomp.py" = [ {file = "stomp.py-6.1.0-py3-none-any.whl", hash = "sha256:8a1ed68cd8b12f41ba56a8dfeff995e7866d1d47ed7f53aaa78da3bea44696b8"}, diff --git a/pyproject.toml b/pyproject.toml index a42dde8a35bd488ddee9f9ddc355bee274aca0a9..35ca0b21ab5b2a580d2ff8da70adb2156954e1b5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,11 +26,11 @@ license = "MIT" python = "^3.6.1" six = "==1.15.0" "stomp.py" = "==6.1.0" -sqlalchemy = "1.2.19" psycopg2-binary = "^2.8.6" requests = "^2.24.0" pyyaml = "^5.3.1" sentry-sdk = "^0.19.2" +SQLAlchemy = "1.3.20" [tool.poetry.dev-dependencies] pre-commit = "~2.9.2"