Skip to content

Commit

Permalink
quick&dirty database migration
Browse files Browse the repository at this point in the history
  • Loading branch information
traxo-xx committed Mar 12, 2014
1 parent de22a0d commit 9b51387
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
16 changes: 16 additions & 0 deletions plugins/core/player_manager/manager.py
Expand Up @@ -4,6 +4,7 @@
import inspect
import logging
import json
import sqlite3

from enum import Enum
from sqlalchemy.ext.mutable import Mutable
Expand Down Expand Up @@ -44,6 +45,20 @@ def process_result_value(self, value, dialect):
return value


def migrate_db(config):
dbcon = sqlite3.connect(path.preauthChild(config.player_db).path)
dbcur = dbcon.cursor()

try:
dbcur.execute('SELECT org_name FROM players;')
except sqlite3.OperationalError, e:
if "column" in str(e):
dbcur.execute('ALTER TABLE `players` ADD COLUMN `org_name`;')
dbcur.execute('UPDATE `players` SET `org_name`=`name`;')
dbcon.commit()
dbcon.close()


logger = logging.getLogger("starrypy.player_manager.manager")

declarative_base = lambda cls: sqla_declarative_base(cls=cls)
Expand Down Expand Up @@ -206,6 +221,7 @@ class Ban(Base):
class PlayerManager(object):
def __init__(self, config):
self.config = config
migrate_db(self.config)
self.engine = create_engine('sqlite:///%s' % path.preauthChild(self.config.player_db).path)
Base.metadata.create_all(self.engine)
self.sessionmaker = sessionmaker(bind=self.engine, autoflush=True)
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Expand Up @@ -2,4 +2,5 @@ sqlalchemy
twisted
enum34
construct
nose
nose
sqlite3

0 comments on commit 9b51387

Please sign in to comment.