Skip to content

Commit

Permalink
Add database models for basic entities
Browse files Browse the repository at this point in the history
Signed-off-by: Akashdeep Dhar <[email protected]>
  • Loading branch information
gridhead committed Jul 1, 2024
1 parent 3673be8 commit 00fde81
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
21 changes: 21 additions & 0 deletions webhook_to_fedora_messaging/models/apikey.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# SPDX-FileCopyrightText: Contributors to the Fedora Project
#
# SPDX-License-Identifier: GPL-3.0-or-later

from sqlalchemy import Column, Integer, UnicodeText, Boolean, ForeignKey, DateTime
from webhook_to_fedora_messaging.models.util import UUIDCreatableMixin, CreatableMixin

from webhook_to_fedora_messaging.database import Base

from uuid import uuid4


class APIKey(Base, UUIDCreatableMixin, CreatableMixin):
__tablename__ = "apikeys"

id = Column(Integer, primary_key=True, nullable=False)
user_id = Column(Integer, ForeignKey("user.id", ondelete="CASCADE"), unique=False, nullable=False)
name = Column(UnicodeText, nullable=False)
token = Column(UnicodeText, unique=True, nullable=False, default=uuid4().hex)
expiry_date = Column(DateTime, nullable=True)
disabled = Column(Boolean, nullable=False, default=False)
19 changes: 19 additions & 0 deletions webhook_to_fedora_messaging/models/service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SPDX-FileCopyrightText: Contributors to the Fedora Project
#
# SPDX-License-Identifier: GPL-3.0-or-later

from sqlalchemy import Column, Integer, UnicodeText, Boolean, ForeignKey
from webhook_to_fedora_messaging.models.util import UUIDCreatableMixin, CreatableMixin

from webhook_to_fedora_messaging.database import Base


class Service(Base, UUIDCreatableMixin, CreatableMixin):
__tablename__ = "services"

id = Column(Integer, primary_key=True, nullable=False)
user_id = Column(Integer, ForeignKey("user.id", ondelete="CASCADE"), unique=False, nullable=False)
name = Column(UnicodeText, nullable=False)
type = Column(UnicodeText, nullable=False)
desc = Column(UnicodeText, nullable=False)
disabled = Column(Boolean, nullable=False, default=False)
16 changes: 16 additions & 0 deletions webhook_to_fedora_messaging/models/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# SPDX-FileCopyrightText: Contributors to the Fedora Project
#
# SPDX-License-Identifier: GPL-3.0-or-later

from sqlalchemy import Column, Integer, UnicodeText, Boolean
from webhook_to_fedora_messaging.models.util import UUIDCreatableMixin, CreatableMixin

from webhook_to_fedora_messaging.database import Base


class User(Base, UUIDCreatableMixin, CreatableMixin):
__tablename__ = "users"

id = Column(Integer, primary_key=True, nullable=False)
username = Column(UnicodeText, unique=True, nullable=False)
is_admin = Column(Boolean, nullable=False, default=False)
40 changes: 40 additions & 0 deletions webhook_to_fedora_messaging/models/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# SPDX-FileCopyrightText: Contributors to the Fedora Project
#
# SPDX-License-Identifier: GPL-3.0-or-later


from sqlalchemy import Column, UnicodeText
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql.expression import FunctionElement
from sqlalchemy.types import DateTime as SQLDateTime

from uuid import uuid4


class utcnow(FunctionElement):
"""
Current timestamp in UTC for SQL expressions
"""
type = SQLDateTime
inherit_cache = True


@compiles(utcnow, "postgresql")
def _postgresql_utcnow(element, compiler, **kwargs):
return "(NOW() AT TIME ZONE 'utc')"


class UUIDCreatableMixin:
"""
An SQLAlchemy mixin to automatically generate a custom 8-digit UUID string
"""

uuid = Column("uuid", UnicodeText, unique=True, nullable=False, default=uuid4().hex[0:8])


class CreatableMixin:
"""
An SQLAlchemy mixin to store the time when an entity was created
"""

creation_date = Column("creation_date", SQLDateTime, nullable=False, server_default=utcnow())

0 comments on commit 00fde81

Please sign in to comment.