Skip to content

Commit

Permalink
Hacky test thing
Browse files Browse the repository at this point in the history
  • Loading branch information
dmptrluke committed Mar 29, 2015
1 parent 41bbe8c commit e339cfe
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cloudbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from sqlalchemy import create_engine

from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.schema import MetaData

import cloudbot
Expand Down Expand Up @@ -89,9 +90,11 @@ def __init__(self, loop=asyncio.get_event_loop()):
self.db_factory = sessionmaker(bind=self.db_engine)
self.db_session = scoped_session(self.db_factory)
self.db_metadata = MetaData()
self.db_base = declarative_base(metadata=self.db_metadata, bind=self.db_engine)

# set botvars so plugins can access when loading
botvars.metadata = self.db_metadata
botvars.base = self.db_base
botvars.user_agent = self.user_agent

logger.debug("Database system initialised.")
Expand Down
1 change: 1 addition & 0 deletions cloudbot/util/botvars.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
# this is assigned in the CloudBot so that its recreated when the bot restarts
metadata = None
user_agent = None
base = None
32 changes: 32 additions & 0 deletions plugins/orm_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from sqlalchemy import Column, Integer, String, Sequence

from cloudbot import hook
from cloudbot.util.botvars import base


class User(base):
__tablename__ = 'users_'
__table_args__ = {'extend_existing': True}
id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
name = Column(String(50))
data = Column(String(100))

base.metadata.create_all()

print(User)
print(User.__table__ )

@hook.command
def test(text, db):
User.__table__.create()
name = text.split(" ", 1)[0]
data = text.split(" ", 1)[1]

ed_user = User(name=name, data=data)
db.add(ed_user)
db.commit()
return "id: {}, name: {}, data: {}".format(ed_user.id, ed_user.name, ed_user.data)




0 comments on commit e339cfe

Please sign in to comment.