forked from freecontent/My-File-Store-Bot-AllVersion
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.py
More file actions
56 lines (45 loc) · 1.54 KB
/
database.py
File metadata and controls
56 lines (45 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, scoped_session
import os
import threading
import asyncio
from sqlalchemy import Column, Integer, Boolean, String, ForeignKey, UniqueConstraint, func
DATABASE_URL = os.environ.get("DATABASE_URL", "")
def start() -> scoped_session:
engine = create_engine(DATABASE_URL, client_encoding="utf8")
BASE.metadata.bind = engine
BASE.metadata.create_all(engine)
return scoped_session(sessionmaker(bind=engine, autoflush=False))
BASE = declarative_base()
SESSION = start()
INSERTION_LOCK = threading.RLock()
class Database(BASE):
__tablename__ = "database"
id = Column(String, primary_key=True)
up_name = Column(Boolean)
def __init__(self, id, up_name):
self.id = str(id)
self.up_name = up_name
Database.__table__.create(checkfirst=True)
async def update_as_name(id, mode):
with INSERTION_LOCK:
msg = SESSION.query(Database).get(str(id))
if not msg:
msg = Database(str(id), False)
else:
msg.up_name = mode
SESSION.delete(msg)
SESSION.add(msg)
SESSION.commit()
async def get_data(id):
try:
user_data = SESSION.query(Database).get(str(id))
if not user_data:
new_user = Database(str(id), False)
SESSION.add(new_user)
SESSION.commit()
user_data = SESSION.query(Database).get(str(id))
return user_data
finally:
SESSION.close()