LSSTApplications  19.0.0-14-gb0260a2+72efe9b372,20.0.0+7927753e06,20.0.0+8829bf0056,20.0.0+995114c5d2,20.0.0+b6f4b2abd1,20.0.0+bddc4f4cbe,20.0.0-1-g253301a+8829bf0056,20.0.0-1-g2b7511a+0d71a2d77f,20.0.0-1-g5b95a8c+7461dd0434,20.0.0-12-g321c96ea+23efe4bbff,20.0.0-16-gfab17e72e+fdf35455f6,20.0.0-2-g0070d88+ba3ffc8f0b,20.0.0-2-g4dae9ad+ee58a624b3,20.0.0-2-g61b8584+5d3db074ba,20.0.0-2-gb780d76+d529cf1a41,20.0.0-2-ged6426c+226a441f5f,20.0.0-2-gf072044+8829bf0056,20.0.0-2-gf1f7952+ee58a624b3,20.0.0-20-geae50cf+e37fec0aee,20.0.0-25-g3dcad98+544a109665,20.0.0-25-g5eafb0f+ee58a624b3,20.0.0-27-g64178ef+f1f297b00a,20.0.0-3-g4cc78c6+e0676b0dc8,20.0.0-3-g8f21e14+4fd2c12c9a,20.0.0-3-gbd60e8c+187b78b4b8,20.0.0-3-gbecbe05+48431fa087,20.0.0-38-ge4adf513+a12e1f8e37,20.0.0-4-g97dc21a+544a109665,20.0.0-4-gb4befbc+087873070b,20.0.0-4-gf910f65+5d3db074ba,20.0.0-5-gdfe0fee+199202a608,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-g64f541c+d529cf1a41,20.0.0-6-g9a5b7a1+a1cd37312e,20.0.0-68-ga3f3dda+5fca18c6a4,20.0.0-9-g4aef684+e18322736b,w.2020.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
lsst.pipe.tasks.ingest.IngestConfig
Definition: ingest.py:371
lsst.pipe.tasks.ingestPgsql.PgsqlRegistryContext.__exit__
def __exit__(self, excType, excValue, traceback)
Definition: ingestPgsql.py:43
lsst.pipe.tasks.ingest.RegistryContext
Definition: ingest.py:225
lsst.pipe.tasks.ingestPgsql.PgsqlIngestConfig
Definition: ingestPgsql.py:104
lsst.pipe.tasks.ingestPgsql.PgsqlIngestTask
Definition: ingestPgsql.py:108
lsst.pipe.tasks.ingest.fakeContext
def fakeContext()
Definition: ingest.py:252
lsst.pipe.tasks.ingestPgsql.PgsqlRegistryContext.registryName
registryName
Definition: ingestPgsql.py:24
lsst.pex.config.configurableField.ConfigurableField
Definition: configurableField.py:170
lsst.pipe.tasks.ingest.IngestTask
Definition: ingest.py:386
lsst::daf::persistence.registries
Definition: registries.py:1
lsst.pipe.tasks.ingestPgsql.PgsqlRegistryContext
Definition: ingestPgsql.py:14
lsst.pipe.tasks.ingestPgsql.PgsqlRegisterTask.openRegistry
def openRegistry(self, directory, create=False, dryrun=False)
Definition: ingestPgsql.py:52
lsst.pipe.tasks.ingest.RegisterTask.createTable
def createTable(self, conn, table=None, forceCreateTables=False)
Definition: ingest.py:282
lsst.pipe.tasks.ingestPgsql.PgsqlRegisterTask.createTable
def createTable(self, conn, table=None)
Definition: ingestPgsql.py:65
lsst.pipe.tasks.ingest
Definition: ingest.py:1
lsst.pex.config
Definition: __init__.py:1
lsst.pipe.tasks.ingest.RegistryContext.conn
conn
Definition: ingest.py:237
lsst.pipe.base.task.Task.config
config
Definition: task.py:162
lsst.pipe.tasks.ingest.RegisterTask
Definition: ingest.py:260
lsst.pipe.tasks.ingestPgsql.PgsqlRegisterTask
Definition: ingestPgsql.py:49
lsst.pipe.tasks.ingestPgsql.PgsqlRegistryContext.__init__
def __init__(self, registryName, createTableFunc, forceCreateTables)
Definition: ingestPgsql.py:17
set
daf::base::PropertySet * set
Definition: fits.cc:912