diff --git a/createdb.py b/createdb.py new file mode 100755 index 00000000..919a619f --- /dev/null +++ b/createdb.py @@ -0,0 +1,8 @@ +#!/usr/bin/python + +import rida.config +import rida.database + +config = rida.config.from_file("rida.conf") + +rida.database.Database.create_tables(config.db, True) diff --git a/rida/database.py b/rida/database.py index c73f0358..758aa514 100644 --- a/rida/database.py +++ b/rida/database.py @@ -34,11 +34,11 @@ Base = declarative_base() class Database(object): """Class for handling database connections.""" - def __init__(self, rdburl=None): + def __init__(self, rdburl=None, debug=False): """Initialize the database object.""" if not isinstance(rdburl, str): rdburl = "sqlite:///rida.db" - engine = create_engine(rdburl) + engine = create_engine(rdburl, echo=debug) Session = sessionmaker(bind=engine) self._session = Session() @@ -47,6 +47,23 @@ class Database(object): """Database session object.""" return self._session + @classmethod + def create_tables(cls, db_url, debug=False): + """ Creates our tables in the database. + + :arg db_url, URL used to connect to the database. The URL contains + information with regards to the database engine, the host to connect + to, the user and password and the database name. + ie: ://:@/ + :kwarg debug, a boolean specifying wether we should have the verbose + output of sqlalchemy or not. + :return a Database connection that can be used to query to db. + """ + engine = create_engine(db_url, echo=debug) + Base.metadata.create_all(engine) + return cls(db_url, debug=debug) + + class Module(Base): __tablename__ = "modules" id = Column(Integer, primary_key=True)