Skip to content

Commit ea4d4fc

Browse files
author
Michael Blanton
committed
Merge branch 'main' of https://github.com/sdss/sdssdb
2 parents 703974c + 240640f commit ea4d4fc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+4062
-180
lines changed

.github/workflows/pythonapp.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- uses: actions/checkout@v4
2222

2323
- name: Set up Python
24-
uses: actions/setup-python@v4
24+
uses: actions/setup-python@v5
2525
with:
2626
python-version: ${{ matrix.python-version }}
2727

.github/workflows/release.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ jobs:
2323
uses: actions/checkout@v4
2424

2525
- name: Create release
26-
uses: softprops/action-gh-release@v1
26+
uses: softprops/action-gh-release@v2
2727
with:
2828
name: sdssdb ${{ github.ref_name }}
2929

3030
- name: Set up Python
31-
uses: actions/setup-python@v4
31+
uses: actions/setup-python@v5
3232
with:
3333
python-version: ${{ matrix.python-version }}
3434

CHANGELOG.rst

+29
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,35 @@ Changelog
55

66
This document records the main changes to the ``sdssdb`` code.
77

8+
* Add method ``get_database_uri()`` to ``DatabaseConnection``.
9+
10+
* :release:`0.12.0 <2024-06-28>`
11+
* Updated ``sdss_id_to_catalog`` view models to use double underscore separating table name from primary key.
12+
* Very significantly speed up the reflection of the database by caching the schema metadata.
13+
* Add ``targetdb`` ``targeting_generation`` and ``targeting_generation_to_target`` tables and models.
14+
* Allow passing additional connection parameters to ``set_profile()`` which will override the profile defaults.
15+
16+
* :release:`0.11.4 <2024-04-30>`
17+
* Add columns ``too_metadata.last_modified_date``, ``too_target.observe_from_mjd``, and ``too_target.added_date``.
18+
* Rename column ``too_target.expiration_date`` to ``too_target.observe_until_mjd``.
19+
20+
* :release:`0.11.3 <2024-04-28>`
21+
* Add columns ``fiber_type`` and ``assigned`` to ``opsdb.assignment_to_focal``.
22+
23+
* :release:`0.11.2 <2024-04-24>`
24+
* Add column ``can_offset`` to ``too_metadata``.
25+
26+
* :release:`0.11.1 <2024-04-24>`
27+
* Fix typo in `CatalogToToO_Target` model name.
28+
29+
* :release:`0.11.0 <2024-04-24>`
30+
* Explicitly define ``CatalogToXXX`` models instead of loading them dynamically.
31+
32+
* :release:`0.10.0 <2024-04-23>`
33+
* Add ``catalogdb.too_target`` and ``catalogdb.too_metadata`` tables and models.
34+
* Add new cadences.
35+
* Add ``disabled`` flag to tile.
36+
837
* :release:`0.9.0 <2024-03-08>`
938
* Add new schema for ``astra`` to ``sdss5db``.
1039
* Added new peewee and sqlalchemy ORMs for ``astra`` schema.

python/sdssdb/connection.py

+46-9
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,29 @@
66
# @Filename: database.py
77
# @License: BSD 3-clause (http://www.opensource.org/licenses/BSD-3-Clause)
88

9+
from __future__ import annotations
10+
911
import abc
1012
import importlib
1113
import os
1214
import re
1315
import socket
1416

17+
import peewee
1518
import pgpasslib
1619
import six
17-
20+
from peewee import OperationalError, PostgresqlDatabase
21+
from playhouse.postgres_ext import ArrayField
22+
from playhouse.reflection import Introspector, UnknownField
1823
from sqlalchemy import MetaData, create_engine
1924
from sqlalchemy.engine import url
2025
from sqlalchemy.exc import OperationalError as OpError
2126
from sqlalchemy.orm import scoped_session, sessionmaker
2227

23-
import peewee
24-
from peewee import OperationalError, PostgresqlDatabase
25-
from playhouse.postgres_ext import ArrayField
26-
from playhouse.reflection import Introspector, UnknownField
27-
2828
import sdssdb
2929
from sdssdb import config, log
3030
from sdssdb.utils.internals import get_database_columns
3131

32-
3332
__all__ = ['DatabaseConnection', 'PeeweeDatabaseConnection', 'SQLADatabaseConnection']
3433

3534

@@ -44,6 +43,30 @@ def _should_autoconnect():
4443
return sdssdb.autoconnect
4544

4645

46+
def get_database_uri(
47+
dbname: str,
48+
host: str | None = None,
49+
port: int | None = None,
50+
user: str | None = None,
51+
password: str | None = None,
52+
):
53+
"""Returns the URI to the database."""
54+
55+
if user is None and password is None:
56+
auth: str = ""
57+
elif password is None:
58+
auth: str = f"{user}@"
59+
else:
60+
auth: str = f"{user}:{password}@"
61+
62+
host_port: str = f"{host or ''}" if port is None else f"{host or ''}:{port}"
63+
64+
if auth == "" and host_port == "":
65+
return f"postgresql://{dbname}"
66+
67+
return f"postgresql://{auth}{host_port}/{dbname}"
68+
69+
4770
class DatabaseConnection(six.with_metaclass(abc.ABCMeta)):
4871
"""A PostgreSQL database connection with profile and autoconnect features.
4972
@@ -109,7 +132,7 @@ def __repr__(self):
109132
return '<{} (dbname={!r}, profile={!r}, connected={})>'.format(
110133
self.__class__.__name__, self.dbname, self.profile, self.connected)
111134

112-
def set_profile(self, profile=None, connect=True):
135+
def set_profile(self, profile=None, connect=True, **params):
113136
"""Sets the profile from the configuration file.
114137
115138
Parameters
@@ -119,6 +142,9 @@ def set_profile(self, profile=None, connect=True):
119142
determine the profile.
120143
connect : bool
121144
If True, tries to connect to the database using the new profile.
145+
params
146+
Connection parameters (``user``, ``host``, ``port``, ``password``)
147+
that will override the profile values.
122148
123149
Returns
124150
-------
@@ -157,6 +183,8 @@ def set_profile(self, profile=None, connect=True):
157183
self._config['host'] = None
158184
break
159185

186+
self._config.update(params)
187+
160188
if connect:
161189
if self.connected and self.profile == previous_profile:
162190
pass
@@ -276,7 +304,16 @@ def list_profiles(profile=None):
276304

277305
return config[profile]
278306

279-
@abc.abstractproperty
307+
def get_connection_uri(self):
308+
"""Returns the URI to the database connection."""
309+
310+
params = self.connection_params
311+
if not self.connected or params is None:
312+
raise RuntimeError('The database is not connected.')
313+
314+
return get_database_uri(self.dbname, **params)
315+
316+
@abc.abstractmethod
280317
def connection_params(self):
281318
"""Returns a dictionary with the connection parameters.
282319

python/sdssdb/etc/sdssdb.yml

+4
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,7 @@ tunnel_operations:
9090
admin: sdss
9191
host: localhost
9292
port: 7502
93+
94+
tunnel_pipelines:
95+
host: localhost
96+
port: 7602

python/sdssdb/peewee/__init__.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,14 @@ def reflect(self):
148148

149149
# Lists tables in the schema. This is a bit of a hack but
150150
# faster than using database.table_exists because it's cached.
151-
database.get_fields(table_name, schema)
152-
schema_tables = database._metadata[schema].keys()
151+
metadata = database._metadata
152+
if schema not in metadata or len(metadata[schema]) == 0:
153+
database.get_fields(table_name, schema, cache=False)
154+
155+
schema_tables = metadata[schema].keys()
153156

154157
if table_name not in schema_tables:
155-
# Give it another try without caching. This is sometimes necessary
156-
# for tables things like catalog_to_X table that are created
157-
# dynamically.
158-
database.get_fields(table_name, schema, cache=False)
159-
schema_tables = database._metadata[schema].keys()
160-
if table_name not in schema_tables:
161-
return
158+
return
162159

163160
for index in meta.indexes:
164161
if hasattr(index, 'reflected') and index.reflected:

python/sdssdb/peewee/sdss5db/astradb.py

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class Source(AstraBase):
3333
gaia_dr3_source_id = BigIntegerField(null=True)
3434
tic_v8_id = BigIntegerField(null=True)
3535
healpix = IntegerField(null=True)
36-
carton_0 = TextField()
3736
lead = TextField(null=True)
3837
version_id = IntegerField(null=True)
3938
catalogid = BigIntegerField(null=True)

0 commit comments

Comments
 (0)