Skip to content

Commit

Permalink
Merge pull request #4748 from dev-hato/update_psycopg_3
Browse files Browse the repository at this point in the history
psycopgアップデート
  • Loading branch information
massongit authored Jan 31, 2025
2 parents 98d027d + 8ad1bf5 commit 5aec2d5
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 107 deletions.
6 changes: 3 additions & 3 deletions create_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
初回動かす必要のあるスクリプト
"""

from library.database import Database
from library.database import execute_sql


def create_table() -> None:
"""テーブルを作成する"""

with Database() as _db, open(
with open(
"postgres/docker-entrypoint-initdb.d/02_init.sql", encoding="UTF-8"
) as init_sql:
sql = ""
for line in init_sql.readlines():
sql += line
if ";" in line:
_db.execute_sql(sql)
execute_sql(sql)
sql = ""


Expand Down
25 changes: 5 additions & 20 deletions library/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,14 @@
import slackbot_settings as conf


class Database:
"""DBを操作するためのベースクラス"""
def execute_sql(sql: str) -> None:
"""SQLを実行する"""

def __init__(self):
try:
self.conn = psycopg.connect(conf.DB_URL)
except psycopg.Error as _e:
print("Can not connect to database.")
raise _e

def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, traceback):
self.conn.close()

def execute_sql(self, sql: str) -> None:
"""SQLを実行する"""

with self.conn.cursor() as cursor:
with psycopg.connect(conf.DB_URL) as conn:
with conn.cursor() as cursor:
try:
cursor.execute(sql)
self.conn.commit()
conn.commit()
print(f"Execute: {sql}")
except Exception as _e:
print("Can not execute sql(create_table).")
Expand Down
77 changes: 38 additions & 39 deletions library/vocabularydb.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,64 @@

import psycopg

from library.database import Database
import slackbot_settings as conf


class VocabularyDatabase(Database):
"""パワーワードを扱うDBを操作するためのクラス"""

def get_word_list(self):
"""パワーワードの一覧をDBから取得する"""
with self.conn.cursor() as cursor:
def get_word_list():
"""パワーワードの一覧をDBから取得する"""
with psycopg.connect(conf.DB_URL) as conn:
with conn.cursor() as cursor:
try:
cursor.execute("SELECT no, word FROM vocabulary ORDER BY no;")
results = cursor.fetchall()
except psycopg.Error:
print("Can not execute sql(select_list).")

return results
return results


def get_random_word(self):
"""パワーワードをDBからランダムで取得する"""
def get_random_word():
"""パワーワードをDBからランダムで取得する"""

with self.conn.cursor() as cursor:
with psycopg.connect(conf.DB_URL) as conn:
with conn.cursor() as cursor:
try:
cursor.execute("SELECT word FROM vocabulary ORDER BY random() LIMIT 1;")
results = cursor.fetchone()
except psycopg.Error:
print("Can not execute sql(select_random).")

return results
return results


def add_word(self, word: str) -> None:
"""パワーワードをDBに登録する"""
def add_word(word: str) -> None:
"""パワーワードをDBに登録する"""

with self.conn.cursor() as cursor:
with psycopg.connect(conf.DB_URL) as conn:
with conn.cursor() as cursor:
try:
cursor.execute("INSERT INTO vocabulary(word) VALUES(%s);", (word,))
self.conn.commit()
conn.commit()
except psycopg.Error:
print("Can not execute sql(add).")

def delete_word(self, word_id: int) -> None:
"""指定したidのパワーワードをDBから削除する"""

with self.conn.cursor() as cursor:
def delete_word(word_id: int) -> None:
"""指定したidのパワーワードをDBから削除する"""

with psycopg.connect(conf.DB_URL) as conn:
with conn.cursor() as cursor:
try:
cursor.execute("DELETE FROM vocabulary WHERE no = %s;", (word_id,))
self.conn.commit()
conn.commit()
except psycopg.Error:
print("Can not execute sql(delete).")


def get_vocabularys():
"""一覧を表示する"""

with VocabularyDatabase() as v_d:
result = v_d.get_word_list()
result = get_word_list()

if len(result) > 0:
slack_msg = "```"
Expand All @@ -79,17 +82,15 @@ def get_vocabularys():
def add_vocabulary(msg: str) -> None:
"""追加する"""

with VocabularyDatabase() as v_d:
v_d.add_word(msg)
add_word(msg)


def show_vocabulary(word_id: int) -> str:
"""指定したものを表示する"""

slack_msg = "該当する番号は見つからなかったっぽ!"

with VocabularyDatabase() as v_d:
result = v_d.get_word_list()
result = get_word_list()

cnt = 1
for row in result:
Expand All @@ -106,8 +107,7 @@ def show_random_vocabulary() -> str:

slack_msg = "鳩は唐揚げ!!"

with VocabularyDatabase() as v_d:
result = v_d.get_random_word()
result = get_random_word()

if result is not None and len(result) > 0:
slack_msg = result[0]
Expand All @@ -120,16 +120,15 @@ def delete_vocabulary(word_id: int) -> str:

slack_msg = "該当する番号は見つからなかったっぽ!"

with VocabularyDatabase() as v_d:
result = v_d.get_word_list()
cnt = 1
for row in result:
row_id, _ = row
if cnt == word_id:
delete_id = row_id
v_d.delete_word(delete_id)
slack_msg = "忘れたっぽ!"
break
cnt += 1
result = get_word_list()
cnt = 1
for row in result:
row_id, _ = row
if cnt == word_id:
delete_id = row_id
delete_word(delete_id)
slack_msg = "忘れたっぽ!"
break
cnt += 1

return slack_msg
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "hato-bot"
version = "3.0.5"
description = "愛嬌のあるBot"
requires-python = "==3.13.1"
dependencies = [ "python-dotenv==1.0.1", "requests==2.32.3", "Pillow>=7.1.2", "opencv-python==4.11.0.86", "slack-bolt==1.22.0", "slack-sdk==3.34.0", "gitpython==3.1.44", "pandas==2.2.3", "matplotlib==3.10.0", "openai==1.60.2", "discord.py==2.4.0", "misskey.py==4.1.0", "websockets==14.2", "flask==3.1.0", "markupsafe==3.0.2", "numpy==2.2.2", "emoji==2.14.1", "puremagic==1.28", "audioop-lts==0.2.1", "psycopg[binary,pool]==3.2.3", "sudden-death==0.0.1",]
dependencies = [ "python-dotenv==1.0.1", "requests==2.32.3", "Pillow>=7.1.2", "opencv-python==4.11.0.86", "slack-bolt==1.22.0", "slack-sdk==3.34.0", "gitpython==3.1.44", "pandas==2.2.3", "matplotlib==3.10.0", "openai==1.60.2", "discord.py==2.4.0", "misskey.py==4.1.0", "websockets==14.2", "flask==3.1.0", "markupsafe==3.0.2", "numpy==2.2.2", "emoji==2.14.1", "puremagic==1.28", "audioop-lts==0.2.1", "psycopg[binary,pool]==3.2.4", "sudden-death==0.0.1",]

[dependency-groups]
dev = [ "autopep8==2.3.2", "requests-mock==1.12.1", "pylint==3.3.4", "sqlfluff==3.3.0", "mypy==1.14.1", "flake8==7.1.1", "isort==6.0.0", "pre-commit==4.1.0", "importlib-metadata==8.6.1", "toml==0.10.2", "types-toml==0.10.8.20240310", "pyink==24.10.0",]
Expand Down
46 changes: 24 additions & 22 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from concurrent.futures import ThreadPoolExecutor

import discord
import psycopg
import slack_bolt
import websockets
from flask import Flask, jsonify, request
Expand All @@ -27,7 +28,6 @@
MisskeyClient,
SlackClient,
)
from library.database import Database
from plugins import analyze

app = Flask(__name__)
Expand All @@ -52,27 +52,29 @@ def on_app_mention(body):
print(f"authed_users: {authed_users}")
print(f"client_msg_id: {client_msg_id}")

with Database() as _db, _db.conn.cursor() as cursor:
cursor.execute(
"SELECT client_msg_id FROM slack_client_msg_id WHERE client_msg_id = %s LIMIT 1",
(client_msg_id,),
)

if cursor.fetchone():
print("skip")
return

cursor.execute(
"DELETE FROM slack_client_msg_id "
"WHERE created_at < CURRENT_TIMESTAMP - interval '10 minutes'",
(client_msg_id,),
)
cursor.execute(
"INSERT INTO slack_client_msg_id(client_msg_id, created_at) "
"VALUES(%s, CURRENT_TIMESTAMP)",
(client_msg_id,),
)
_db.conn.commit()
with psycopg.connect(conf.DB_URL) as conn:
with conn.cursor() as cursor:
cursor.execute(
"SELECT client_msg_id FROM slack_client_msg_id WHERE client_msg_id = %s LIMIT 1",
(client_msg_id,),
)

if cursor.fetchone():
print("skip")
return

cursor.execute(
"DELETE FROM slack_client_msg_id "
"WHERE created_at < CURRENT_TIMESTAMP - interval '10 minutes'",
(client_msg_id,),
)
cursor.execute(
"INSERT INTO slack_client_msg_id(client_msg_id, created_at) "
"VALUES(%s, CURRENT_TIMESTAMP)",
(client_msg_id,),
)

conn.commit()

with ThreadPoolExecutor(max_workers=3) as tpe:
for block in blocks:
Expand Down
36 changes: 18 additions & 18 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions wait_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import psycopg

from library.database import Database
from library.database import execute_sql


def wait_db() -> None:
Expand All @@ -16,9 +16,8 @@ def wait_db() -> None:

for i in range(max_attempt):
try:
with Database() as _db:
_db.execute_sql("SELECT 1")
break
execute_sql("SELECT 1")
break
except psycopg.OperationalError as _e:
if i == max_attempt - 1:
raise _e
Expand Down

0 comments on commit 5aec2d5

Please sign in to comment.