server/db: allow full DSN; use memdb in tests

The earlier commit is still relevant as it allows to integrate real
database when needed.
This commit is contained in:
rr- 2016-08-15 19:55:03 +02:00
parent e688f39887
commit 6c29377f6b
6 changed files with 15 additions and 48 deletions

View file

@ -89,7 +89,7 @@ user@host:szuru/server$ source python_modules/bin/activate # enters the sandbox
- API URL, - API URL,
- data directory, - data directory,
- data URL, - data URL,
- the `database` section, - database,
- the `smtp` section. - the `smtp` section.
2. Compile the frontend: 2. Compile the frontend:

View file

@ -11,29 +11,20 @@ base_url: # used to form links to frontend, example: http://example.com/
data_url: # used to form links to posts and avatars, example: http://example.com/data/ data_url: # used to form links to posts and avatars, example: http://example.com/data/
data_dir: # absolute path for posts and avatars storage, example: /srv/www/booru/client/public/data/ data_dir: # absolute path for posts and avatars storage, example: /srv/www/booru/client/public/data/
# usage: schema://user:password@host:port/database_name
# example: postgres://szuru:dog@localhost:5432/szuru_test
# example (useful for tests): sqlite:///:memory:
database:
test_database: 'sqlite:///:memory:' # required for runing the test suite
thumbnails: thumbnails:
avatar_width: 300 avatar_width: 300
avatar_height: 300 avatar_height: 300
post_width: 300 post_width: 300
post_height: 300 post_height: 300
database:
schema: postgres
host: # example: localhost
port: # example: 5432
user: # example: szuru
pass: # example: dog
name: # example: szuru
# required for runing the test suite
test_database:
schema: postgres
host: # example: localhost
port: # example: 5432
user: # example: szuru
pass: # example: dog
name: # example: szuru_test
# used to send password reminders # used to send password reminders
smtp: smtp:
host: # example: localhost host: # example: localhost

View file

@ -19,14 +19,7 @@ class QueryCounter(object):
def create_session(): def create_session():
_engine = sqlalchemy.create_engine( _engine = sqlalchemy.create_engine(config.config['database'])
'{schema}://{user}:{password}@{host}:{port}/{name}'.format(
schema=config.config['database']['schema'],
user=config.config['database']['user'],
password=config.config['database']['pass'],
host=config.config['database']['host'],
port=config.config['database']['port'],
name=config.config['database']['name']))
sqlalchemy.event.listen( sqlalchemy.event.listen(
_engine, 'after_execute', lambda *args: QueryCounter.bump()) _engine, 'after_execute', lambda *args: QueryCounter.bump())
_session_maker = sqlalchemy.orm.sessionmaker(bind=_engine) _session_maker = sqlalchemy.orm.sessionmaker(bind=_engine)

View file

@ -68,10 +68,8 @@ def validate_config():
raise errors.ConfigError( raise errors.ConfigError(
'data_dir must be an absolute path') 'data_dir must be an absolute path')
for key in ['schema', 'host', 'port', 'user', 'pass', 'name']: if not config.config['database']:
if not config.config['database'][key]: raise errors.ConfigError('Database is not configured')
raise errors.ConfigError(
'Database is not configured: %r is missing' % key)
def create_app(): def create_app():

View file

@ -16,15 +16,7 @@ alembic_config = alembic.context.config
logging.config.fileConfig(alembic_config.config_file_name) logging.config.fileConfig(alembic_config.config_file_name)
szuru_config = szurubooru.config.config szuru_config = szurubooru.config.config
alembic_config.set_main_option( alembic_config.set_main_option('sqlalchemy.url', szuru_config['database'])
'sqlalchemy.url',
'{schema}://{user}:{password}@{host}:{port}/{name}'.format(
schema=szuru_config['database']['schema'],
user=szuru_config['database']['user'],
password=szuru_config['database']['pass'],
host=szuru_config['database']['host'],
port=szuru_config['database']['port'],
name=szuru_config['database']['name']))
target_metadata = szurubooru.db.Base.metadata target_metadata = szurubooru.db.Base.metadata

View file

@ -31,18 +31,11 @@ class QueryCounter(object):
return self._statements return self._statements
if not config.config['test_database']['host']: if not config.config['test_database']:
raise RuntimeError('Test database not configured.') raise RuntimeError('Test database not configured.')
_query_counter = QueryCounter() _query_counter = QueryCounter()
_engine = sqlalchemy.create_engine( _engine = sqlalchemy.create_engine(config.config['test_database'])
'{schema}://{user}:{password}@{host}:{port}/{name}'.format(
schema=config.config['test_database']['schema'],
user=config.config['test_database']['user'],
password=config.config['test_database']['pass'],
host=config.config['test_database']['host'],
port=config.config['test_database']['port'],
name=config.config['test_database']['name']))
db.Base.metadata.drop_all(bind=_engine) db.Base.metadata.drop_all(bind=_engine)
db.Base.metadata.create_all(bind=_engine) db.Base.metadata.create_all(bind=_engine)
sqlalchemy.event.listen( sqlalchemy.event.listen(