Skip to content

Commit

Permalink
Further refactoring of apdbSqlSchema.
Browse files Browse the repository at this point in the history
Move parts of ApdbSqlSchema class responsible for conversion of
schema model into SQLAlchemy schema into a separate class which can be
reused by dax_ppdb.
  • Loading branch information
andy-slac committed Apr 15, 2024
1 parent c4a6682 commit 7c07e7a
Show file tree
Hide file tree
Showing 4 changed files with 452 additions and 233 deletions.
29 changes: 27 additions & 2 deletions python/lsst/dax/apdb/schema_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ def from_felis(cls, dm_column: felis.datamodel.Column) -> Column:
)
return column

def clone(self) -> Column:
"""Make a clone of self."""
return dataclasses.replace(self, table=None)


@dataclasses.dataclass
class Index:
Expand Down Expand Up @@ -301,6 +305,24 @@ class ForeignKeyConstraint(Constraint):
which is different from the table of this constraint.
"""

onupdate: str | None = None
"""What to do when parent table columns are updated. Typical values are
CASCADE, DELETE and RESTRICT.
"""

ondelete: str | None = None
"""What to do when parent table columns are deleted. Typical values are
CASCADE, DELETE and RESTRICT.
"""

@property
def referenced_table(self) -> Table:
"""Table referenced by this constraint."""
assert len(self.referenced_columns) > 0, "column list cannot be empty"
ref_table = self.referenced_columns[0].table
assert ref_table is not None, "foreign key column must have table defined"
return ref_table


@dataclasses.dataclass
class CheckConstraint(Constraint):
Expand Down Expand Up @@ -338,6 +360,11 @@ class Table:
annotations: Mapping[str, Any] = dataclasses.field(default_factory=dict)
"""Additional annotations for this table."""

def __post_init__(self) -> None:
"""Update all columns to point to this table."""
for column in self.columns:
column.table = self

@classmethod
def from_felis(cls, dm_table: felis.datamodel.Table, columns: Mapping[str, Column]) -> Table:
"""Convert Felis table definition into instance of this class.
Expand Down Expand Up @@ -374,8 +401,6 @@ def from_felis(cls, dm_table: felis.datamodel.Table, columns: Mapping[str, Colum
["name", "id", "columns", "primaryKey", "constraints", "indexes", "description"],
),
)
for column in table_columns:
column.table = table
return table


Expand Down
2 changes: 2 additions & 0 deletions python/lsst/dax/apdb/sql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from .apdbMetadataSql import *
from .apdbSql import *
from .apdbSqlReplica import *
from .modelToSql import *

0 comments on commit 7c07e7a

Please sign in to comment.