chore(#5): Fixes the timestamp into native SQL

This commit is contained in:
Elia el Lazkani 2024-12-02 00:17:27 +01:00
parent dd37cdb6a3
commit 71981a5861
3 changed files with 18 additions and 15 deletions

View file

@ -1,4 +1,5 @@
from sqlalchemy.orm import DeclarativeBase, MappedAsDataclass from sqlalchemy.orm import DeclarativeBase, MappedAsDataclass
class Base(MappedAsDataclass, DeclarativeBase): class Base(MappedAsDataclass, DeclarativeBase):
pass pass

View file

@ -1,17 +1,16 @@
from shortenit.models.objects import Link, Pointer from datetime import datetime
import time from sqlalchemy import create_engine, text
from sqlalchemy import create_engine
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from sqlalchemy import text
from shortenit.models.objects import Link, Pointer
engine = create_engine("sqlite+pysqlite:///:memory:", echo=True) engine = create_engine("sqlite+pysqlite:///:memory:", echo=True)
Link.metadata.create_all(engine) Link.metadata.create_all(engine)
Pointer.metadata.create_all(engine) Pointer.metadata.create_all(engine)
link = Link("00001", "https://duckduckgo.com", time.time(), []) link = Link("00001", "https://duckduckgo.com", [])
pointer = Pointer("00001", "duckduckgo!", "30d", time.time(), link.id, link) pointer = Pointer("00001", "duckduckgo!", "30d", link.id, link)
link.pointers.append(pointer) link.pointers.append(pointer)
with Session(engine) as session: with Session(engine) as session:

View file

@ -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 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): class Link(Base):
__tablename__ = "links" __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) identifier: Mapped[str] = mapped_column(index=True)
data: Mapped[AnyHttpUrl] = mapped_column(String) data: Mapped[AnyHttpUrl] = mapped_column(String)
timestamp: Mapped[str]
pointers: Mapped[List["Pointer"]] = relationship(back_populates="link") pointers: Mapped[List["Pointer"]] = relationship(back_populates="link")
timestamp: Mapped[datetime] = mapped_column(default=func.now())
class Pointer(Base): class Pointer(Base):
@ -22,6 +25,6 @@ class Pointer(Base):
identifier: Mapped[str] = mapped_column(index=True) identifier: Mapped[str] = mapped_column(index=True)
data: Mapped[str] data: Mapped[str]
ttl: Mapped[str] ttl: Mapped[str]
timestamp: Mapped[str]
link_id: Mapped[int] = mapped_column(ForeignKey("links.id")) link_id: Mapped[int] = mapped_column(ForeignKey("links.id"))
link: Mapped["Link"] = relationship(back_populates="pointers") link: Mapped["Link"] = relationship(back_populates="pointers")
timestamp: Mapped[datetime] = mapped_column(default=func.now())