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:
		
							parent
							
								
									e688f39887
								
							
						
					
					
						commit
						6c29377f6b
					
				@ -89,7 +89,7 @@ user@host:szuru/server$ source python_modules/bin/activate # enters the sandbox
 | 
			
		||||
    - API URL,
 | 
			
		||||
    - data directory,
 | 
			
		||||
    - data URL,
 | 
			
		||||
    - the `database` section,
 | 
			
		||||
    - database,
 | 
			
		||||
    - the `smtp` section.
 | 
			
		||||
 | 
			
		||||
2. Compile the frontend:
 | 
			
		||||
 | 
			
		||||
@ -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_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:
 | 
			
		||||
    avatar_width: 300
 | 
			
		||||
    avatar_height: 300
 | 
			
		||||
    post_width: 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
 | 
			
		||||
smtp:
 | 
			
		||||
    host: # example: localhost
 | 
			
		||||
 | 
			
		||||
@ -19,14 +19,7 @@ class QueryCounter(object):
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def create_session():
 | 
			
		||||
    _engine = sqlalchemy.create_engine(
 | 
			
		||||
        '{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']))
 | 
			
		||||
    _engine = sqlalchemy.create_engine(config.config['database'])
 | 
			
		||||
    sqlalchemy.event.listen(
 | 
			
		||||
        _engine, 'after_execute', lambda *args: QueryCounter.bump())
 | 
			
		||||
    _session_maker = sqlalchemy.orm.sessionmaker(bind=_engine)
 | 
			
		||||
 | 
			
		||||
@ -68,10 +68,8 @@ def validate_config():
 | 
			
		||||
        raise errors.ConfigError(
 | 
			
		||||
            'data_dir must be an absolute path')
 | 
			
		||||
 | 
			
		||||
    for key in ['schema', 'host', 'port', 'user', 'pass', 'name']:
 | 
			
		||||
        if not config.config['database'][key]:
 | 
			
		||||
            raise errors.ConfigError(
 | 
			
		||||
                'Database is not configured: %r is missing' % key)
 | 
			
		||||
    if not config.config['database']:
 | 
			
		||||
        raise errors.ConfigError('Database is not configured')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def create_app():
 | 
			
		||||
 | 
			
		||||
@ -16,15 +16,7 @@ alembic_config = alembic.context.config
 | 
			
		||||
logging.config.fileConfig(alembic_config.config_file_name)
 | 
			
		||||
 | 
			
		||||
szuru_config = szurubooru.config.config
 | 
			
		||||
alembic_config.set_main_option(
 | 
			
		||||
    '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']))
 | 
			
		||||
alembic_config.set_main_option('sqlalchemy.url', szuru_config['database'])
 | 
			
		||||
 | 
			
		||||
target_metadata = szurubooru.db.Base.metadata
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -31,18 +31,11 @@ class QueryCounter(object):
 | 
			
		||||
        return self._statements
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if not config.config['test_database']['host']:
 | 
			
		||||
if not config.config['test_database']:
 | 
			
		||||
    raise RuntimeError('Test database not configured.')
 | 
			
		||||
 | 
			
		||||
_query_counter = QueryCounter()
 | 
			
		||||
_engine = sqlalchemy.create_engine(
 | 
			
		||||
    '{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']))
 | 
			
		||||
_engine = sqlalchemy.create_engine(config.config['test_database'])
 | 
			
		||||
db.Base.metadata.drop_all(bind=_engine)
 | 
			
		||||
db.Base.metadata.create_all(bind=_engine)
 | 
			
		||||
sqlalchemy.event.listen(
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user