db replica base

This commit is contained in:
2025-09-22 14:22:46 +03:00
parent d5003b5dcb
commit 79568954bc
6 changed files with 65 additions and 20 deletions

View File

@@ -11,7 +11,7 @@ db_folder.mkdir(parents=True, exist_ok=True)
db_path = db_folder / "example.db"
async_engine = create_async_engine(f"sqlite+aiosqlite:///{db_path}", echo=True)
#sqlite+aiosqlite — тип БД + async-драйвер ///example.db — путь к файлу (три слэша, если путь относительный; четыре, если абсолютный
#async_engine = create_async_engine( "postgresql+asyncpg://user:pass@host:5432/mydb", echo=True) #Можно указать Pgpool-II для psql или proxysql для mysql mariadb
from passlib.context import CryptContext
#Hash password
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
@@ -37,13 +37,13 @@ class User(Base):
async def init_db():
async with async_engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
async def CreateUser(user_info):
async def create_user(user_info):
async with AsyncSessionLocal() as session:
new_user = User(email=user_info.email, description=user_info.description, activated=user_info.activated, password=hash_password(user_info.password))
session.add(new_user)
await session.commit()
await session.refresh(new_user)
async def GetUserbyEmail(email):
async def get_user_by_email(email):
async with AsyncSessionLocal() as session:
result = await session.execute(select(User).where(User.email==email))
user = result.scalar_one_or_none()
@@ -53,7 +53,7 @@ async def get_all_rows():
result = await session.execute(select(User))
users = result.scalars().all()
return users
async def UpdateUser(user_info):
async def update_user(user_info):
async with AsyncSessionLocal() as session:
result = await session.execute(select(User).where(User.id==user_info.id))
user = result.scalar_one_or_none()
@@ -63,14 +63,14 @@ async def UpdateUser(user_info):
user.activated = user_info.activated
user.password = hash_password(user_info.password)
await session.commit()
async def DeleteUser(email):
async def delete_user(email):
async with AsyncSessionLocal() as session:
result = await session.execute(select(User).where(User.email==email))
user = result.scalar_one_or_none()
if user:
await session.delete(user)
await session.commit()
async def LoginUser(user_info):
async def login_user(user_info):
async with AsyncSessionLocal() as session:
result = await session.execute(select(User).where(User.email == user_info.email))
user = result.scalar_one_or_none()

View File

@@ -0,0 +1,12 @@
services:
postgres-master:
image: bitnami/postgresql:16
environment:
- POSTGRESQL_REPLICATION_MODE=master
- POSTGRESQL_REPLICATION_USER=repl_user
- POSTGRESQL_REPLICATION_PASSWORD=repl_pass
- POSTGRESQL_USERNAME=admin
- POSTGRESQL_PASSWORD=admin
- POSTGRESQL_DATABASE=mydb
ports:
- '5432:5432'

View File

@@ -0,0 +1,20 @@
#Это если вдруг мне когда нибудь приспичит сделать sql balancer с распределенными бд через pgpool-II
backend_hostname0 = 'pg-master'
backend_port0 = 5432
backend_weight0 = 1
backend_flag0 = 'ALWAYS_PRIMARY'
backend_hostname1 = 'pg-replica1'
backend_port1 = 5432
backend_weight1 = 1
backend_flag1 = 'ALLOW_TO_FAILOVER'
backend_hostname2 = 'pg-replica2'
backend_port2 = 5432
backend_weight2 = 1 #сколько запросов куда отправлять 1/1 например = 50/50
backend_flag2 = 'ALLOW_TO_FAILOVER'
load_balance_mode = on
replication_mode = off #не юзают, тк устарело и глючно
master_slave_mode = on

View File

@@ -0,0 +1,11 @@
services:
postgres-replica:
image: bitnami/postgresql:16
environment:
- POSTGRESQL_REPLICATION_MODE=slave
- POSTGRESQL_REPLICATION_USER=repl_user
- POSTGRESQL_REPLICATION_PASSWORD=repl_pass
- POSTGRESQL_MASTER_HOST=MASTER_IP_ИЛИ_DNS
- POSTGRESQL_PASSWORD=admin
ports:
- "5432:5432"