migrate from supabase to postgres

This commit is contained in:
2025-09-09 16:01:58 +07:00
parent a836ebb2a6
commit aa62667d89
21 changed files with 327 additions and 902 deletions
+12
View File
@@ -0,0 +1,12 @@
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, DateTime, BigInteger
from sqlalchemy.sql import func
from datetime import datetime
# Tạo Base class cho tất cả ORM models
Base = declarative_base()
class TimestampMixin:
"""Mixin để tự động thêm created_at và updated_at"""
created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False)
+61 -36
View File
@@ -1,51 +1,76 @@
import os
from dotenv import load_dotenv
from supabase import create_client, Client
from typing import Optional
import ssl
import certifi
from dotenv import load_dotenv
from sqlalchemy.ext.asyncio import (
AsyncEngine,
AsyncSession,
async_sessionmaker,
create_async_engine,
)
# Import Base để có thể tạo tables
from infra.db.base import Base
load_dotenv()
def _normalize_asyncpg_url(url: str) -> str:
if url.startswith("postgresql+asyncpg://"):
return url
if url.startswith("postgresql://"):
return url.replace("postgresql://", "postgresql+asyncpg://", 1)
if url.startswith("postgres://"):
return url.replace("postgres://", "postgresql+asyncpg://", 1)
return url
class PostgresConnection:
_instance = None
_instance: Optional["PostgresConnection"] = None
def __new__(cls):
if cls._instance is None:
cls._instance = super(PostgresConnection, cls).__new__(cls)
cls._instance._initialize_client()
cls._instance._initialize()
return cls._instance
def _initialize_client(self):
url = os.getenv('SUPABASE_URL')
key = os.getenv('SUPABASE_KEY')
if not url or not key:
raise ValueError("Missing Supabase credentials. Please check your .env file")
# Đảm bảo URL có định dạng đúng
if not url.startswith('https://'):
url = f'https://{url}'
# Nếu URL chứa 'db.', thay thế bằng project URL
if 'db.' in url:
project_id = url.split('db.')[1].split('.')[0]
url = f'https://{project_id}.supabase.co'
try:
# Tạo SSL context với certifi
ssl_context = ssl.create_default_context(cafile=certifi.where())
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
def _initialize(self) -> None:
# Ưu tiên POSTGRES_URL, fallback từ các biến rời
raw_url = os.getenv("POSTGRES_URL")
if not raw_url:
host = os.getenv("POSTGRES_HOST", "localhost")
port = os.getenv("POSTGRES_PORT", "5432")
user = os.getenv("POSTGRES_USER", "postgres")
password = os.getenv("POSTGRES_PASSWORD", "")
database = os.getenv("POSTGRES_DB", "postgres")
auth = f"{user}:{password}@" if password else f"{user}@"
raw_url = f"postgresql+asyncpg://{auth}{host}:{port}/{database}"
# Tạo client với SSL context
self.supabase: Client = create_client(url, key)
print("Successfully connected to Supabase!")
except Exception as e:
raise Exception(f"Failed to connect to Supabase: {str(e)}")
url = _normalize_asyncpg_url(raw_url)
def get_table(self, table_name: str):
return self.supabase.table(table_name)
self.engine: AsyncEngine = create_async_engine(
url,
echo=os.getenv("POSTGRES_ECHO", "false").lower() == "true",
pool_size=int(os.getenv("POSTGRES_POOL_SIZE", "5")),
max_overflow=int(os.getenv("POSTGRES_MAX_OVERFLOW", "10")),
pool_pre_ping=True,
)
self.session_maker: async_sessionmaker[AsyncSession] = async_sessionmaker(
self.engine, expire_on_commit=False
)
# Singleton instance
postgres = PostgresConnection()
async def create_tables(self):
"""Tạo tất cả tables từ Base metadata"""
async with self.engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
def get_engine(self) -> AsyncEngine:
return self.engine
def get_sessionmaker(self) -> async_sessionmaker[AsyncSession]:
return self.session_maker
# Singleton export cho dùng chung
postgres = PostgresConnection()