From 6fb7b527426b564302d6fd8ca11dfa7b78cc2e7a Mon Sep 17 00:00:00 2001 From: Gord Thompson Date: Sun, 12 Jan 2020 20:08:22 -0500 Subject: Refactor test provisioning to dialect-level files Fixes: #5085 Move dialect-specific provisioning code to dialect-level copies of provision.py. This pull request is: - [ ] A documentation / typographical error fix - Good to go, no issue or tests are needed - [x] A short code fix - please include the issue number, and create an issue if none exists, which must include a complete example of the issue. one line code fixes without an issue and demonstration will not be accepted. - Please include: `Fixes: #` in the commit message - please include tests. one line code fixes without tests will not be accepted. - [ ] A new feature implementation - please include the issue number, and create an issue if none exists, which must include a complete example of how the feature would look. - Please include: `Fixes: #` in the commit message - please include tests. **Have a nice day!** Closes: #5092 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/5092 Pull-request-sha: 25b9b7a9800549fb823576af8674e8d33ff4b2c1 Change-Id: Ie0b4a69aa472a60bdbd825e04c8595382bcc98e1 --- lib/sqlalchemy/dialects/sqlite/__init__.py | 1 + lib/sqlalchemy/dialects/sqlite/provision.py | 54 +++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 lib/sqlalchemy/dialects/sqlite/provision.py (limited to 'lib/sqlalchemy/dialects/sqlite') diff --git a/lib/sqlalchemy/dialects/sqlite/__init__.py b/lib/sqlalchemy/dialects/sqlite/__init__.py index 142131f63..c35cb9251 100644 --- a/lib/sqlalchemy/dialects/sqlite/__init__.py +++ b/lib/sqlalchemy/dialects/sqlite/__init__.py @@ -6,6 +6,7 @@ # the MIT License: http://www.opensource.org/licenses/mit-license.php from . import base # noqa +from . import provision # noqa from . import pysqlcipher # noqa from . import pysqlite # noqa from .base import BLOB diff --git a/lib/sqlalchemy/dialects/sqlite/provision.py b/lib/sqlalchemy/dialects/sqlite/provision.py new file mode 100644 index 000000000..d4a5ae93f --- /dev/null +++ b/lib/sqlalchemy/dialects/sqlite/provision.py @@ -0,0 +1,54 @@ +import os + +from ...engine import url as sa_url +from ...testing.provision import create_db +from ...testing.provision import drop_db +from ...testing.provision import follower_url_from_main +from ...testing.provision import post_configure_engine +from ...testing.provision import temp_table_keyword_args + + +@follower_url_from_main.for_db("sqlite") +def _sqlite_follower_url_from_main(url, ident): + url = sa_url.make_url(url) + if not url.database or url.database == ":memory:": + return url + else: + return sa_url.make_url("sqlite:///%s.db" % ident) + + +@post_configure_engine.for_db("sqlite") +def _sqlite_post_configure_engine(url, engine, follower_ident): + from sqlalchemy import event + + @event.listens_for(engine, "connect") + def connect(dbapi_connection, connection_record): + # use file DBs in all cases, memory acts kind of strangely + # as an attached + if not follower_ident: + dbapi_connection.execute( + 'ATTACH DATABASE "test_schema.db" AS test_schema' + ) + else: + dbapi_connection.execute( + 'ATTACH DATABASE "%s_test_schema.db" AS test_schema' + % follower_ident + ) + + +@create_db.for_db("sqlite") +def _sqlite_create_db(cfg, eng, ident): + pass + + +@drop_db.for_db("sqlite") +def _sqlite_drop_db(cfg, eng, ident): + if ident: + os.remove("%s_test_schema.db" % ident) + else: + os.remove("%s.db" % ident) + + +@temp_table_keyword_args.for_db("sqlite") +def _sqlite_temp_table_keyword_args(cfg, eng): + return {"prefixes": ["TEMPORARY"]} -- cgit v1.2.1