From 1dd62e1ae017adf8e8fd4b617e4f96128e0b9a08 Mon Sep 17 00:00:00 2001 From: coolneng Date: Fri, 3 Jul 2020 02:03:02 +0200 Subject: [PATCH] Migrate DB to sqlite --- app/routes.py | 2 +- database/__init__.py | 8 +++++--- database/crud.py | 1 - database/models.py | 44 ++++++++++++++++++++++++++++++++++-------- shell.nix | 17 ++-------------- tests/requests_test.py | 4 +--- 6 files changed, 45 insertions(+), 31 deletions(-) diff --git a/app/routes.py b/app/routes.py index 3ab0e23..57b21ec 100644 --- a/app/routes.py +++ b/app/routes.py @@ -3,7 +3,7 @@ from fastapi import Response, status from app import app from app.schemas import * from app.twilio import send_otp -from database.crud import insert_data, verify_login, verify_otp +from database.crud import insert_data, verify_otp @app.post("/register", status_code=status.HTTP_200_OK) diff --git a/database/__init__.py b/database/__init__.py index f6f5b6c..6d11c8b 100644 --- a/database/__init__.py +++ b/database/__init__.py @@ -1,12 +1,14 @@ -from constants import CONNECTION_URI +from constants import DB from sqlalchemy import MetaData, create_engine from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker from databases import Database metadata = MetaData() -engine = create_engine(CONNECTION_URI) +engine = create_engine(DB) metadata.create_all(engine) +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) Base = declarative_base() -database = Database(CONNECTION_URI) +database = Database(DB) from database import models diff --git a/database/crud.py b/database/crud.py index aa473f2..9abf618 100644 --- a/database/crud.py +++ b/database/crud.py @@ -3,7 +3,6 @@ from datetime import datetime from app import app from app.schemas import * from database import database -from werkzeug.security import check_password_hash from pydoc import locate diff --git a/database/models.py b/database/models.py index a0cebb1..289d657 100644 --- a/database/models.py +++ b/database/models.py @@ -1,9 +1,11 @@ -from app import Base -from sqlalchemy import Column, Integer, String, DateTime, Text, Enum +from database import Base +from sqlalchemy import Column, Integer, String, DateTime, Text, Enum, text from sqlalchemy.sql import func class Users(Base): + __tablename__ = "users" + id = Column(Integer, primary_key=True, autoincrement=True) social_id = Column(Text) type = Column(Integer) @@ -20,8 +22,8 @@ class Users(Base): access_key = Column(Text) lang_type = Column(Integer) badge = Column(Integer) - status = Column(Integer, server_default=Text("0")) - admin_status = Column(Integer, server_default=Text("0")) + status = Column(Integer, server_default=text("0")) + admin_status = Column(Integer, server_default=text("0")) device_id = Column(Text) device_type = Column(Integer) created = Column(DateTime, nullable=False, server_default=func.now()) @@ -62,6 +64,8 @@ class Users(Base): class Cities(Base): + __tablename__ = "cities" + id = Column(Integer, primary_key=True, autoincrement=True) name = Column(String(255)) image = Column(String(255)) @@ -78,6 +82,8 @@ class Cities(Base): class Games(Base): + __tablename__ = "games" + id = Column(Integer, primary_key=True, autoincrement=True) name = Column(String(255)) image = Column(String(255)) @@ -95,12 +101,12 @@ class Games(Base): no_of_already_player = Column(Integer) payment_mode = Column(Integer) card_id = Column(Integer) - status = Column(Integer, server_default=Text("1")) - game_status = Column(Integer, server_default=Text("0")) + status = Column(Integer, server_default=text("1")) + game_status = Column(Integer, server_default=text("0")) cancel_status = Column(Integer) cancel_date = Column(DateTime) - noti_status = Column(Integer, server_default=Text("0")) - conduct_status = Column(Integer, server_default=Text("1")) + noti_status = Column(Integer, server_default=text("0")) + conduct_status = Column(Integer, server_default=text("1")) created = Column(DateTime, nullable=False, server_default=func.now()) modified = Column(DateTime, nullable=False, onupdate=func.now()) @@ -140,6 +146,8 @@ class Games(Base): class Payments(Base): + __tablename__ = "payments" + id = Column(Integer, primary_key=True, autoincrement=True) user_id = Column(Integer) game_id = Column(Integer) @@ -183,6 +191,8 @@ class Payments(Base): class PlayerAvailabilities(Base): + __tablename__ = "player_availabilities" + id = Column(Integer, primary_key=True, autoincrement=True) game_id = Column(Integer) player_id = Column(Integer) @@ -199,6 +209,8 @@ class PlayerAvailabilities(Base): class PlayerCancelGames(Base): + __tablename__ = "player_cancel_games" + id = Column(Integer, primary_key=True, autoincrement=True) player_id = Column(Integer) game_id = Column(Integer) @@ -213,6 +225,8 @@ class PlayerCancelGames(Base): class PurchaseGames(Base): + __tablename__ = "purchase_games" + id = Column(Integer, primary_key=True, autoincrement=True) game_id = Column(Integer) user_id = Column(Integer) @@ -229,6 +243,8 @@ class PurchaseGames(Base): class Sports(Base): + __tablename__ = "sports" + id = Column(Integer, primary_key=True, autoincrement=True) name = Column(String(255)) spanish_name = Column(String(100)) @@ -245,6 +261,8 @@ class Sports(Base): class Teams(Base): + __tablename__ = "teams" + id = Column(Integer, primary_key=True, autoincrement=True) user_id = Column(Integer) team_id = Column(Enum("1", "2")) @@ -263,6 +281,8 @@ class Teams(Base): class UserRatings(Base): + __tablename__ = "user_ratings" + id = Column(Integer, primary_key=True, autoincrement=True) game_id = Column(Integer) user_id = Column(Integer) @@ -281,6 +301,8 @@ class UserRatings(Base): class VenueImages(Base): + __tablename__ = "venue_images" + id = Column(Integer, primary_key=True, autoincrement=True) venue_id = Column(Integer) user_id = Column(Integer) @@ -297,6 +319,8 @@ class VenueImages(Base): class Venues(Base): + __tablename__ = "venues" + id = Column(Integer, primary_key=True, autoincrement=True) user_id = Column(Integer) address = Column(Text) @@ -319,6 +343,8 @@ class Venues(Base): class ViewNews(Base): + __tablename__ = "view_news" + id = Column(Integer, primary_key=True, autoincrement=True) news_id = Column(Integer) user_id = Column(Integer) @@ -333,6 +359,8 @@ class ViewNews(Base): class WebBookings(Base): + __tablename__ = "web_bookings" + id = Column(Integer, primary_key=True, autoincrement=True) name = Column(String(255)) email = Column(String(255)) diff --git a/shell.nix b/shell.nix index ca55b7d..788b57a 100644 --- a/shell.nix +++ b/shell.nix @@ -10,29 +10,16 @@ mkShell { python38Packages.pydantic python38Packages.email_validator python38Packages.sqlalchemy - python38Packages.pymysql + python38Packages.aiosqlite python38Packages.databases - python38Packages.aiomysql python38Packages.alembic python38Packages.pytest python38Packages.twilio - mysql57 + sqlite # Development tools python38Packages.black python38Packages.isort python38Packages.pyflakes python-language-server ]; - - shellHook = '' - pkill mysql - rm -rf .mysql && mkdir .mysql - - mysqld --datadir="$(pwd)/.mysql" --socket="$(pwd)/.mysql/mysql.sock" --initialize-insecure - mysqld --datadir="$(pwd)/.mysql" --socket="$(pwd)/.mysql/mysql.sock" --skip-networking & sleep 1 - - mysql --socket="$(pwd)/.mysql/mysql.sock" -u root < $(pwd)/assets/test_db.sql - - alias mysql='mysql --socket="$(pwd)/.mysql/mysql.sock" -u root' - ''; } diff --git a/tests/requests_test.py b/tests/requests_test.py index 6d8cfe5..db28002 100644 --- a/tests/requests_test.py +++ b/tests/requests_test.py @@ -1,8 +1,6 @@ -from app.models import * -from config import Config +from database.models import * from pytest import fixture from secrets import token_hex -from werkzeug.security import generate_password_hash def test_registration(client):