]> scripts.mit.edu Git - wizard.git/blobdiff - wizard/sql.py
Rewrite parametrize to use new parametrizeWithVars
[wizard.git] / wizard / sql.py
index a0a6387cf325e0b6173142a04e0ebdc7c0123b78..5780dccd6bcb3e0c53bfce912dbde9f8773c3297 100644 (file)
@@ -1,15 +1,42 @@
 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