Skip to content

Instantly share code, notes, and snippets.

@sprin
Last active November 30, 2022 04:57
Show Gist options
  • Save sprin/5846464 to your computer and use it in GitHub Desktop.
Save sprin/5846464 to your computer and use it in GitHub Desktop.
A demo of creating a new database via SQL Alchemy. This module takes the form of a nosetest with three steps: - Set up the new database. - Create a table in the new database. - Teardown the new database.
"""
A demo of creating a new database via SQL Alchemy.
Under MIT License from sprin (https://gist.github.com/sprin/5846464/)
This module takes the form of a nosetest with three steps:
- Set up the new database.
- Create a table in the new database.
- Teardown the new database.
"""
from sqlalchemy import (
create_engine,
MetaData,
Table,
Column,
Integer,
)
from sqlalchemy.pool import NullPool
# XXX: It is advised to use another user that can connect to a default database,
# and has CREATE DATABASE permissions, rather than use a superuser.
DB_CONFIG_DICT = {
'user': 'postgres',
'password': 'postgres',
'host': 'localhost',
'port': 5432,
}
DB_CONN_FORMAT = "postgresql://{user}:{password}@{host}:{port}/{database}"
DB_CONN_URI_DEFAULT = (DB_CONN_FORMAT.format(
database='postgres',
**DB_CONFIG_DICT))
engine_default = create_engine(DB_CONN_URI_DEFAULT)
NEW_DB_NAME = 'test'
DB_CONN_URI_NEW = (DB_CONN_FORMAT.format(
database=NEW_DB_NAME,
**DB_CONFIG_DICT))
metadata = MetaData()
proj = Table('test', metadata, Column('id', Integer))
def setup_module():
conn = engine_default.connect()
conn.execute("COMMIT")
# Do not substitute user-supplied database names here.
conn.execute("CREATE DATABASE %s" % NEW_DB_NAME)
conn.close()
def test_create_table():
# Get a new engine for the just-created database and create a table.
engine_new = create_engine(DB_CONN_URI_NEW, poolclass=NullPool)
conn = engine_new.connect()
metadata.create_all(conn)
conn.close()
def teardown_module():
conn = engine_default.connect()
conn.execute("COMMIT")
# Do not substitute user-supplied database names here.
conn.execute("DROP DATABASE %s" % NEW_DB_NAME)
conn.close()
@sprin
Copy link
Author

sprin commented Jul 19, 2013

MIT LICENSE

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

@stoic1979
Copy link

Can I create for custom owner and grant all permissions ? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment