]> scripts.mit.edu Git - wizard.git/blob - wizard/sql.py
Rewrite parametrize to use new parametrizeWithVars
[wizard.git] / wizard / sql.py
1 import sqlalchemy
2 import os
3
4 from wizard import shell
5
6 # We're going to use sqlalchemy.engine.url.URL as our database
7 # info intermediate object
8
9 def connect(url):
10     """Convenience method for connecting to a MySQL database."""
11     engine = sqlalchemy.create_engine(url)
12     meta = sqlalchemy.MetaData()
13     meta.bind = engine
14     meta.reflect()
15     return meta
16
17 def fill_url(url):
18     """
19     If the URL has a database name but no other values, it will
20     use the global configuration, and then try the database name.
21     """
22     if not url:
23         return None
24     if not url.database:
25         # it's hopeless
26         return url
27     # omitted port and query
28     if any((url.host, url.username, url.password)):
29         # don't try for defaults if a few of these were set
30         return url
31     # this is hook stuff
32     if url.driver == "mysql":
33         try:
34             url.host, url.username, url.password = shell.Shell().eval("/mit/scripts/sql/bin/get-password").split()
35             return url
36         except shell.CallError:
37             pass
38     dsn = os.getenv("WIZARD_DSN")
39     old_url = url
40     url = sqlalchemy.engine.url.make_url(dsn)
41     url.database = old_url.database
42     return url