mirror of
https://gitlab.com/akasroua/covot
synced 2025-12-24 16:11:55 +01:00
Migrate database to sqlite
This commit is contained in:
9
database/__init__.py
Normal file
9
database/__init__.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from sqlalchemy import MetaData, create_engine
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from constants import DATABASE
|
||||
|
||||
engine = create_engine(DATABASE, connect_args={"check_same_thread": False})
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
Base = declarative_base()
|
||||
27
database/crud.py
Normal file
27
database/crud.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from sqlalchemy import or_
|
||||
|
||||
from database import SessionLocal, engine, models
|
||||
from database.models import Users
|
||||
|
||||
db = SessionLocal()
|
||||
|
||||
|
||||
def search_database(id):
|
||||
return db.query(Users).filter(
|
||||
or_(Users.correo_institucional == id, Users.numero_de_documento == id)
|
||||
)
|
||||
|
||||
|
||||
def insert_data(data):
|
||||
item = Users(**data)
|
||||
db.add(item)
|
||||
db.commit()
|
||||
db.refresh(item)
|
||||
|
||||
|
||||
def create_database(data):
|
||||
models.Base.metadata.create_all(bind=engine)
|
||||
existing_row = search_database(data["correo_institucional"])
|
||||
if existing_row:
|
||||
return
|
||||
insert_data(data)
|
||||
17
database/models.py
Normal file
17
database/models.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from sqlalchemy import Column
|
||||
from sqlalchemy.types import Integer, String
|
||||
|
||||
from database import Base
|
||||
|
||||
|
||||
class Users(Base):
|
||||
__tablename__ = "users"
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
primer_apellido = Column(String, nullable=False)
|
||||
segundo_apellido = Column(String, nullable=False)
|
||||
nombre = Column(String, nullable=False)
|
||||
tipo_de_documento = Column(String, nullable=False)
|
||||
numero_de_documento = Column(String, nullable=False, unique=True)
|
||||
centro_academico = Column(String, nullable=False)
|
||||
correo_institucional = Column(String, nullable=False, unique=True)
|
||||
Reference in New Issue
Block a user