LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
LSST Data Management Base Package
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.registryNameregistryName = registryName
25  data = PgsqlRegistry.readYaml(registryName)
26  self.connconnconn = pgsql.connect(host=data["host"], port=data["port"], user=data["user"],
27  password=data["password"], database=data["database"])
28  cur = self.connconnconn.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.connconnconn)
42 
43  def __exit__(self, excType, excValue, traceback):
44  self.connconnconn.commit()
45  self.connconnconn.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.createTablecreateTablecreateTable, 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.configconfig.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.configconfig.columns.items()])
89  if len(self.configconfig.unique) > 0:
90  cmd += ", UNIQUE(" + ",".join(self.configconfig.unique) + ")"
91  cmd += ")"
92  cur.execute(cmd)
93 
94  cmd = "CREATE TABLE %s_visit (" % self.configconfig.table
95  cmd += ",".join(["%s %s" % (col, typeMap.get(self.configconfig.columns[col].lower(), 'TEXT')) for
96  col in self.configconfig.visit])
97  cmd += ", UNIQUE(" + ",".join(set(self.configconfig.visit).intersection(set(self.configconfig.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 createTable(self, conn, table=None, forceCreateTables=False)
Definition: ingest.py:302
def createTable(self, conn, table=None)
Definition: ingestPgsql.py:65
def openRegistry(self, directory, create=False, dryrun=False)
Definition: ingestPgsql.py:52
def __init__(self, registryName, createTableFunc, forceCreateTables)
Definition: ingestPgsql.py:17
def __exit__(self, excType, excValue, traceback)
Definition: ingestPgsql.py:43
daf::base::PropertySet * set
Definition: fits.cc:912