bcgov / entity

ServiceBC Registry Team working on Legal Entities
Apache License 2.0
23 stars 57 forks source link

Legal API - alembic script + model for configuration table #21024

Closed argush3 closed 2 days ago

argush3 commented 1 week ago

This ticket is for creating a generic configuration table to provide a mechanism where configuration can be perisisted and updateable. The table is essentially a key/value store.

There will only be one entry(NUM_DISSOLUTIONS_ALLOWED) in this table but it can be used for other configuration in the future.

image.png image.png

TODOs

Example validation code that might work

class Configuration(Base):
    id = Column(Integer, primary_key=True)
    name = Column(String, unique=True, nullable=False)
    val = Column(String, nullable=False)

    def validate_value(self):
        # Define keys that should have integer values
        int_names = {'NUM_DISSOLUTIONS_ALLOWED'}

        if self.name in int_names:
            try:
                # Attempt to convert the value to an integer
                int(self.val)
            except ValueError:
                raise ValueError(f"Value for key {self.name} must be an integer")

   # Listen to 'before_insert' and 'before_update' events
   @event.listens_for(Config, 'before_insert')
   @event.listens_for(Config, 'before_update')
   def receive_before_insert(mapper, connection, target):
      # Validate the value before it gets inserted/updated
      target.validate_value()