From 71981a58618f5f8d9866ccdf5832165798a4f443 Mon Sep 17 00:00:00 2001 From: Elia el Lazkani Date: Mon, 2 Dec 2024 00:17:27 +0100 Subject: [PATCH] chore(#5): Fixes the timestamp into native SQL --- shortenit/models/base.py | 1 + shortenit/models/main.py | 13 ++++++------- shortenit/models/objects.py | 19 +++++++++++-------- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/shortenit/models/base.py b/shortenit/models/base.py index 81bd762..57dcd91 100644 --- a/shortenit/models/base.py +++ b/shortenit/models/base.py @@ -1,4 +1,5 @@ from sqlalchemy.orm import DeclarativeBase, MappedAsDataclass + class Base(MappedAsDataclass, DeclarativeBase): pass diff --git a/shortenit/models/main.py b/shortenit/models/main.py index 78827ef..8e07ac3 100644 --- a/shortenit/models/main.py +++ b/shortenit/models/main.py @@ -1,17 +1,16 @@ -from shortenit.models.objects import Link, Pointer +from datetime import datetime -import time - -from sqlalchemy import create_engine +from sqlalchemy import create_engine, text from sqlalchemy.orm import Session -from sqlalchemy import text + +from shortenit.models.objects import Link, Pointer engine = create_engine("sqlite+pysqlite:///:memory:", echo=True) Link.metadata.create_all(engine) Pointer.metadata.create_all(engine) -link = Link("00001", "https://duckduckgo.com", time.time(), []) -pointer = Pointer("00001", "duckduckgo!", "30d", time.time(), link.id, link) +link = Link("00001", "https://duckduckgo.com", []) +pointer = Pointer("00001", "duckduckgo!", "30d", link.id, link) link.pointers.append(pointer) with Session(engine) as session: diff --git a/shortenit/models/objects.py b/shortenit/models/objects.py index bfd163a..57cff0a 100644 --- a/shortenit/models/objects.py +++ b/shortenit/models/objects.py @@ -1,19 +1,22 @@ -from shortenit.models.base import Base +from datetime import datetime +from typing import List -from typing import List, Optional -from sqlalchemy import Integer, String, ForeignKey -from sqlalchemy.orm import Mapped -from sqlalchemy.orm import relationship, mapped_column from pydantic import AnyHttpUrl +from sqlalchemy import DateTime, ForeignKey, String +from sqlalchemy.orm import Mapped, mapped_column, relationship +from sqlalchemy.sql import func + +from shortenit.models.base import Base +from shortenit.models.shortener import Shortener class Link(Base): __tablename__ = "links" - id: Mapped[int] = mapped_column(primary_key=True, index=True, init=False) + id: Mapped[int] = mapped_column(primary_key=True, index=True, init=False) identifier: Mapped[str] = mapped_column(index=True) data: Mapped[AnyHttpUrl] = mapped_column(String) - timestamp: Mapped[str] pointers: Mapped[List["Pointer"]] = relationship(back_populates="link") + timestamp: Mapped[datetime] = mapped_column(default=func.now()) class Pointer(Base): @@ -22,6 +25,6 @@ class Pointer(Base): identifier: Mapped[str] = mapped_column(index=True) data: Mapped[str] ttl: Mapped[str] - timestamp: Mapped[str] link_id: Mapped[int] = mapped_column(ForeignKey("links.id")) link: Mapped["Link"] = relationship(back_populates="pointers") + timestamp: Mapped[datetime] = mapped_column(default=func.now())