Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Support SQLAlchemy default in-memory SQLite URL
SQLAlchemy's documentation states if you want to create an in-memory
database you can provide a URL of `sqlite://` and that `:memory` is
implied. The `database_exists` and `create_database` function did not
handle this use-case.
  • Loading branch information
RobertDeRose committed Mar 28, 2016
1 parent 204ff74 commit 2380151
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 5 additions & 0 deletions conftest.py
Expand Up @@ -67,6 +67,11 @@ def sqlite_memory_dsn():
return 'sqlite:///:memory:'


@pytest.fixture
def sqlite_none_database_dsn():
return 'sqlite://'


@pytest.fixture
def sqlite_file_dsn():
return 'sqlite:///{0}.db'.format(db_name)
Expand Down
10 changes: 8 additions & 2 deletions sqlalchemy_utils/functions/database.py
Expand Up @@ -466,7 +466,12 @@ def database_exists(url):
return bool(engine.execute(text).scalar())

elif engine.dialect.name == 'sqlite':
return database == ':memory:' or os.path.exists(database)
if database:
return database == ':memory:' or os.path.exists(database)
else:
# The default SQLAlchemy database is in memory,
# and :memory is not required, thus we should support that use-case
return True

else:
text = 'SELECT 1'
Expand Down Expand Up @@ -538,7 +543,8 @@ def create_database(url, encoding='utf8', template=None):
engine.execute(text)

elif engine.dialect.name == 'sqlite' and database != ':memory:':
open(database, 'w').close()
if database:
open(database, 'w').close()

else:
text = 'CREATE DATABASE {0}'.format(quote(engine, database))
Expand Down
6 changes: 6 additions & 0 deletions tests/functions/test_database.py
Expand Up @@ -27,6 +27,12 @@ def test_exists_memory(self, dsn):
assert database_exists(dsn)


@pytest.mark.usefixture('sqlite_none_database_dsn')
class TestDatabaseSQLiteMemoryNoDatabaseString(object):
def test_exists_memory_none_database(self, sqlite_none_database_dsn):
assert database_exists(sqlite_none_database_dsn)


@pytest.mark.usefixtures('sqlite_file_dsn')
class TestDatabaseSQLiteFile(DatabaseTest):
pass
Expand Down

0 comments on commit 2380151

Please sign in to comment.