import sqlalchemy
+import os
-def mysql_connect(options):
+from wizard import shell
+
+# We're going to use sqlalchemy.engine.url.URL as our database
+# info intermediate object
+
+def connect(url):
"""Convenience method for connecting to a MySQL database."""
- engine = sqlalchemy.create_engine(sqlalchemy.engine.url.URL(
- "mysql",
- username=options.mysql_user,
- password=options.mysql_password,
- host=options.mysql_host,
- database=options.mysql_db,
- ))
+ engine = sqlalchemy.create_engine(url)
meta = sqlalchemy.MetaData()
meta.bind = engine
meta.reflect()
return meta
+
+def fill_url(url):
+ """
+ If the URL has a database name but no other values, it will
+ use the global configuration, and then try the database name.
+ """
+ if not url:
+ return None
+ if not url.database:
+ # it's hopeless
+ return url
+ # omitted port and query
+ if any((url.host, url.username, url.password)):
+ # don't try for defaults if a few of these were set
+ return url
+ # this is hook stuff
+ if url.driver == "mysql":
+ try:
+ url.host, url.username, url.password = shell.Shell().eval("/mit/scripts/sql/bin/get-password").split()
+ return url
+ except shell.CallError:
+ pass
+ dsn = os.getenv("WIZARD_DSN")
+ old_url = url
+ url = sqlalchemy.engine.url.make_url(dsn)
+ url.database = old_url.database
+ return url