Merge pull request #207 from RobertDeRose/handle-default-sqlite-database
Handle default sqlite database
This commit is contained in:
@@ -67,6 +67,11 @@ def sqlite_memory_dsn():
|
|||||||
return 'sqlite:///:memory:'
|
return 'sqlite:///:memory:'
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def sqlite_none_database_dsn():
|
||||||
|
return 'sqlite://'
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def sqlite_file_dsn():
|
def sqlite_file_dsn():
|
||||||
return 'sqlite:///{0}.db'.format(db_name)
|
return 'sqlite:///{0}.db'.format(db_name)
|
||||||
|
@@ -466,7 +466,12 @@ def database_exists(url):
|
|||||||
return bool(engine.execute(text).scalar())
|
return bool(engine.execute(text).scalar())
|
||||||
|
|
||||||
elif engine.dialect.name == 'sqlite':
|
elif engine.dialect.name == 'sqlite':
|
||||||
|
if database:
|
||||||
return database == ':memory:' or os.path.exists(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:
|
else:
|
||||||
text = 'SELECT 1'
|
text = 'SELECT 1'
|
||||||
@@ -538,6 +543,7 @@ def create_database(url, encoding='utf8', template=None):
|
|||||||
engine.execute(text)
|
engine.execute(text)
|
||||||
|
|
||||||
elif engine.dialect.name == 'sqlite' and database != ':memory:':
|
elif engine.dialect.name == 'sqlite' and database != ':memory:':
|
||||||
|
if database:
|
||||||
open(database, 'w').close()
|
open(database, 'w').close()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
@@ -569,8 +575,9 @@ def drop_database(url):
|
|||||||
|
|
||||||
engine = sa.create_engine(url)
|
engine = sa.create_engine(url)
|
||||||
|
|
||||||
if engine.dialect.name == 'sqlite' and url.database != ':memory:':
|
if engine.dialect.name == 'sqlite' and database != ':memory:':
|
||||||
os.remove(url.database)
|
if database:
|
||||||
|
os.remove(database)
|
||||||
|
|
||||||
elif engine.dialect.name == 'postgresql' and engine.driver == 'psycopg2':
|
elif engine.dialect.name == 'postgresql' and engine.driver == 'psycopg2':
|
||||||
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
|
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
|
||||||
|
@@ -27,6 +27,12 @@ class TestDatabaseSQLiteMemory(object):
|
|||||||
assert database_exists(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')
|
@pytest.mark.usefixtures('sqlite_file_dsn')
|
||||||
class TestDatabaseSQLiteFile(DatabaseTest):
|
class TestDatabaseSQLiteFile(DatabaseTest):
|
||||||
pass
|
pass
|
||||||
|
Reference in New Issue
Block a user