Skip to content

Commit e339cfe

Browse files
committed
Hacky test thing
1 parent 41bbe8c commit e339cfe

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

cloudbot/bot.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from sqlalchemy import create_engine
99

1010
from sqlalchemy.orm import scoped_session, sessionmaker
11+
from sqlalchemy.ext.declarative import declarative_base
1112
from sqlalchemy.schema import MetaData
1213

1314
import cloudbot
@@ -89,9 +90,11 @@ def __init__(self, loop=asyncio.get_event_loop()):
8990
self.db_factory = sessionmaker(bind=self.db_engine)
9091
self.db_session = scoped_session(self.db_factory)
9192
self.db_metadata = MetaData()
93+
self.db_base = declarative_base(metadata=self.db_metadata, bind=self.db_engine)
9294

9395
# set botvars so plugins can access when loading
9496
botvars.metadata = self.db_metadata
97+
botvars.base = self.db_base
9598
botvars.user_agent = self.user_agent
9699

97100
logger.debug("Database system initialised.")

cloudbot/util/botvars.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
# this is assigned in the CloudBot so that its recreated when the bot restarts
66
metadata = None
77
user_agent = None
8+
base = None

plugins/orm_test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from sqlalchemy import Column, Integer, String, Sequence
2+
3+
from cloudbot import hook
4+
from cloudbot.util.botvars import base
5+
6+
7+
class User(base):
8+
__tablename__ = 'users_'
9+
__table_args__ = {'extend_existing': True}
10+
id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
11+
name = Column(String(50))
12+
data = Column(String(100))
13+
14+
base.metadata.create_all()
15+
16+
print(User)
17+
print(User.__table__ )
18+
19+
@hook.command
20+
def test(text, db):
21+
User.__table__.create()
22+
name = text.split(" ", 1)[0]
23+
data = text.split(" ", 1)[1]
24+
25+
ed_user = User(name=name, data=data)
26+
db.add(ed_user)
27+
db.commit()
28+
return "id: {}, name: {}, data: {}".format(ed_user.id, ed_user.name, ed_user.data)
29+
30+
31+
32+

0 commit comments

Comments
 (0)