Browse Source

#3 トランザクションの追加

main
みてるぞ 3 weeks ago
parent
commit
2c9d91cb9a
3 changed files with 10 additions and 5 deletions
  1. +5
    -0
      config.py
  2. +2
    -0
      models.py
  3. +3
    -5
      queries_to_answers.py

+ 5
- 0
config.py View File

@@ -3,6 +3,8 @@ from __future__ import annotations
import os
from typing import TypedDict

from eloquent import DatabaseManager, Model # type: ignore

CONFIG: dict[str, DbConfig] = { 'mysql': { 'driver': 'mysql',
'host': 'localhost',
'database': 'nizika_ai',
@@ -10,6 +12,9 @@ CONFIG: dict[str, DbConfig] = { 'mysql': { 'driver': 'mysql',
'password': os.environ['MYSQL_PASS'],
'prefix': '' } }

DB = DatabaseManager (CONFIG)
Model.set_connection_resolver (DB)


class DbConfig (TypedDict):
driver: str


+ 2
- 0
models.py View File

@@ -4,6 +4,8 @@ from datetime import datetime

from eloquent import Model # type: ignore

from config import DB


class AnsweredFlag (Model):
id: int


+ 3
- 5
queries_to_answers.py View File

@@ -4,9 +4,7 @@ import random
from datetime import datetime
from typing import cast

from eloquent import DatabaseManager, Model # type: ignore

from config import CONFIG
from config import DB
from consts import Character
from models import Answer, Query, User
from talk import Talk
@@ -14,8 +12,7 @@ from talk import Talk

def main (
) -> None:
db = DatabaseManager (CONFIG)
Model.set_connection_resolver (db)
DB.begin_transaction ()
queries: list[Query] = Query.where ('answered', False).get ()
if not queries:
return
@@ -53,6 +50,7 @@ def main (
answer.save ()
query.answered = True
query.save ()
DB.commit ()


if __name__ == '__main__':


Loading…
Cancel
Save