migrate from supabase to postgres
This commit is contained in:
+61
-36
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user