Skip to content

Commit

Permalink
Create trigger. Caught one case but not the other.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielballan committed Nov 7, 2023
1 parent 1cdb59c commit e5954a2
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tiled/catalog/orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
Index,
Integer,
Unicode,
event,
text,
)
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import Mapped, mapped_column, relationship
Expand Down Expand Up @@ -146,6 +148,31 @@ class DataSourceAssetAssociation(Base):
)


@event.listens_for(DataSourceAssetAssociation.__table__, "after_create")
def create_trigger_unique_parameter_num_null_check(target, connection, **kw):
connection.execute(
text(
"""
CREATE TRIGGER unique_parameter_num_null_check
BEFORE INSERT ON data_source_asset_association
WHEN NEW.num IS NULL
BEGIN
SELECT RAISE(ABORT, 'Can only insert num=NULL if no other row exists for the same parameter')
WHERE EXISTS
(
SELECT 1
FROM data_source_asset_association
WHERE parameter = NEW.parameter
AND data_source_id = NEW.data_source_id
);
END"""
)
)
# Additionally ensure that we cannot mix NULL and INTEGER values of num for
# a given data_source_id and parameter, and that there cannot be multiple
# instances of NULL.


class DataSource(Timestamped, Base):
"""
The describes how to open one or more file/blobs to extract data for a Node.
Expand Down

0 comments on commit e5954a2

Please sign in to comment.