loomchild / icoin

GNU Affero General Public License v3.0
0 stars 0 forks source link

Create database #13

Closed loomchild closed 9 years ago

loomchild commented 9 years ago

Create main and test database if it does not exist. Requires superuser.

Prototype:

def create():
    "Create configured database if it does not exist"
    name = app.config["DB_NAME"]
    url = get_url(app.config["DB_USER"], app.config["DB_PASSWORD"], 
        app.config["DB_HOST"], app.config["DB_PORT"], "postgres")
    engine = sqlalchemy.create_engine(url)
    with engine.connect() as conn:
        result = conn.execute(
            "SELECT 1 FROM pg_catalog.pg_database WHERE datname='{}'"
            .format(name))
        if not result.first():
            conn.execute("COMMIT")
            conn.execute("CREATE DATABASE {}".format(name))
loomchild commented 9 years ago

Is this really required or better use psql directly