Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for SQLite header in database_exists #351

Merged
merged 1 commit into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def sqlite_none_database_dsn():


@pytest.fixture
def sqlite_file_dsn():
def sqlite_file_dsn(db_name):
return 'sqlite:///{0}.db'.format(db_name)


Expand Down
14 changes: 12 additions & 2 deletions sqlalchemy_utils/functions/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,15 @@ def get_scalar_result(engine, sql):
engine.dispose()
return result

def sqlite_file_exists(database):
if not os.path.isfile(database) or os.path.getsize(database) < 100:
return False

with open(database, 'rb') as f:
header = f.read(100)

return header[:16] == b'SQLite format 3\x00'

url = copy(make_url(url))
database = url.database
if url.drivername.startswith('postgres'):
Expand All @@ -478,7 +487,7 @@ def get_scalar_result(engine, sql):

elif engine.dialect.name == 'sqlite':
if database:
return database == ':memory:' or os.path.exists(database)
return database == ':memory:' or sqlite_file_exists(database)
else:
# The default SQLAlchemy database is in memory,
# and :memory is not required, thus we should support that use-case
Expand Down Expand Up @@ -567,7 +576,8 @@ def create_database(url, encoding='utf8', template=None):

elif engine.dialect.name == 'sqlite' and database != ':memory:':
if database:
open(database, 'w').close()
engine.execute("CREATE TABLE DB(id int);")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the line below seem strange. What is their purpose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is so an actual SQLite file is created instead of just an empty file.

engine.execute("DROP TABLE DB;")

else:
text = 'CREATE DATABASE {0}'.format(quote(engine, database))
Expand Down
5 changes: 4 additions & 1 deletion tests/functions/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ def test_exists_memory_none_database(self, sqlite_none_database_dsn):

@pytest.mark.usefixtures('sqlite_file_dsn')
class TestDatabaseSQLiteFile(DatabaseTest):
pass
def test_existing_non_sqlite_file(self, dsn):
database = sa.engine.url.make_url(dsn).database
open(database, 'w').close()
self.test_create_and_drop(dsn)


@pytest.mark.skipif('pymysql is None')
Expand Down