LSSTApplications  17.0+124,17.0+14,17.0+73,18.0.0+37,18.0.0+80,18.0.0-4-g68ffd23+4,18.1.0-1-g0001055+12,18.1.0-1-g03d53ef+5,18.1.0-1-g1349e88+55,18.1.0-1-g2505f39+44,18.1.0-1-g5315e5e+4,18.1.0-1-g5e4b7ea+14,18.1.0-1-g7e8fceb+4,18.1.0-1-g85f8cd4+48,18.1.0-1-g8ff0b9f+4,18.1.0-1-ga2c679d+1,18.1.0-1-gd55f500+35,18.1.0-10-gb58edde+2,18.1.0-11-g0997b02+4,18.1.0-13-gfe4edf0b+12,18.1.0-14-g259bd21+21,18.1.0-19-gdb69f3f+2,18.1.0-2-g5f9922c+24,18.1.0-2-gd3b74e5+11,18.1.0-2-gfbf3545+32,18.1.0-26-g728bddb4+5,18.1.0-27-g6ff7ca9+2,18.1.0-3-g52aa583+25,18.1.0-3-g8ea57af+9,18.1.0-3-gb69f684+42,18.1.0-3-gfcaddf3+6,18.1.0-32-gd8786685a,18.1.0-4-gf3f9b77+6,18.1.0-5-g1dd662b+2,18.1.0-5-g6dbcb01+41,18.1.0-6-gae77429+3,18.1.0-7-g9d75d83+9,18.1.0-7-gae09a6d+30,18.1.0-9-gc381ef5+4,w.2019.45
LSSTDataManagementBasePackage
ingestPgsql.py
Go to the documentation of this file.
1 import os
2 
3 from lsst.pex.config import ConfigurableField
4 from lsst.pipe.tasks.ingest import IngestTask, IngestConfig, RegisterTask, RegistryContext, fakeContext
5 from lsst.daf.persistence.registries import PgsqlRegistry
6 
7 try:
8  import psycopg2 as pgsql
9  havePgSql = True
10 except ImportError:
11  havePgSql = False
12 
13 
15  """Context manager to provide a pgsql registry
16  """
17  def __init__(self, registryName, createTableFunc, forceCreateTables):
18  """Construct a context manager
19 
20  @param registryName: Name of registry file
21  @param createTableFunc: Function to create tables
22  @param forceCreateTables: Force the (re-)creation of tables?
23  """
24  self.registryName = registryName
25  data = PgsqlRegistry.readYaml(registryName)
26  self.conn = pgsql.connect(host=data["host"], port=data["port"], user=data["user"],
27  password=data["password"], database=data["database"])
28  cur = self.conn.cursor()
29 
30  # Check for existence of tables
31  cur.execute("SELECT relname FROM pg_class WHERE relkind='r' AND relname='raw'")
32  rows = cur.fetchall()
33 
34  if forceCreateTables or len(rows) == 0:
35  # Delete all tables and start over.
36  # Not simply doing "DROP SCHEMA" and "CREATE SCHEMA" because of permissions.
37  cur.execute("SELECT tablename FROM pg_tables WHERE schemaname = 'public'")
38  tables = cur.fetchall()
39  for tt in tables:
40  cur.execute("DROP TABLE %s CASCADE" % tt)
41  createTableFunc(self.conn)
42 
43  def __exit__(self, excType, excValue, traceback):
44  self.conn.commit()
45  self.conn.close()
46  return False # Don't suppress any exceptions
47 
48 
50  placeHolder = "%s"
51 
52  def openRegistry(self, directory, create=False, dryrun=False):
53  """Open the registry and return the connection handle.
54 
55  @param directory Directory in which the registry file will be placed
56  @param create Clobber any existing registry and create a new one?
57  @param dryrun Don't do anything permanent?
58  @return Database connection
59  """
60  if dryrun:
61  return fakeContext()
62  registryName = os.path.join(directory, "registry.pgsql")
63  return PgsqlRegistryContext(registryName, self.createTable, create)
64 
65  def createTable(self, conn, table=None):
66  """Create the registry tables
67 
68  One table (typically 'raw') contains information on all files, and the
69  other (typically 'raw_visit') contains information on all visits.
70 
71  This method is required because there's a slightly different syntax
72  compared to SQLite (FLOAT instead of DOUBLE, SERIAL instead of
73  AUTOINCREMENT).
74 
75  @param conn Database connection
76  @param table Name of table to create in database
77  """
78  if table is None:
79  table = self.config.table
80 
81  typeMap = {'int': 'INT',
82  'double': 'FLOAT', # Defaults to double precision
83  }
84 
85  cur = conn.cursor()
86  cmd = "CREATE TABLE %s (id SERIAL NOT NULL PRIMARY KEY, " % table
87  cmd += ",".join(["%s %s" % (col, typeMap.get(colType.lower(), 'text')) for
88  col, colType in self.config.columns.items()])
89  if len(self.config.unique) > 0:
90  cmd += ", UNIQUE(" + ",".join(self.config.unique) + ")"
91  cmd += ")"
92  cur.execute(cmd)
93 
94  cmd = "CREATE TABLE %s_visit (" % self.config.table
95  cmd += ",".join(["%s %s" % (col, typeMap.get(self.config.columns[col].lower(), 'TEXT')) for
96  col in self.config.visit])
97  cmd += ", UNIQUE(" + ",".join(set(self.config.visit).intersection(set(self.config.unique))) + ")"
98  cmd += ")"
99  cur.execute(cmd)
100  del cur
101  conn.commit()
102 
103 
105  register = ConfigurableField(target=PgsqlRegisterTask, doc="Registry entry")
106 
107 
109  ConfigClass = PgsqlIngestConfig
def __init__(self, registryName, createTableFunc, forceCreateTables)
Definition: ingestPgsql.py:17
daf::base::PropertySet * set
Definition: fits.cc:902
def openRegistry(self, directory, create=False, dryrun=False)
Definition: ingestPgsql.py:52
def createTable(self, conn, table=None, forceCreateTables=False)
Definition: ingest.py:298
def __exit__(self, excType, excValue, traceback)
Definition: ingestPgsql.py:43
def createTable(self, conn, table=None)
Definition: ingestPgsql.py:65