provider.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from datetime import datetime
  2. from sqlalchemy import (
  3. Column,
  4. Integer,
  5. BigInteger,
  6. SmallInteger,
  7. String,
  8. DateTime,
  9. JSON,
  10. UniqueConstraint,
  11. Index,
  12. )
  13. from app.core.database import Base
  14. class Provider(Base):
  15. __tablename__ = "providers"
  16. __ddl__ = """
  17. CREATE TABLE IF NOT EXISTS `providers` (
  18. `id` BIGINT NOT NULL AUTO_INCREMENT,
  19. `name` VARCHAR(128) NOT NULL,
  20. `enabled` SMALLINT NOT NULL DEFAULT 1,
  21. `plugin_config` JSON NOT NULL,
  22. `role_templates` JSON NOT NULL,
  23. `version` INT NOT NULL DEFAULT 0,
  24. `updated_by` VARCHAR(64) NULL DEFAULT 'system',
  25. `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  26. `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  27. PRIMARY KEY (`id`),
  28. UNIQUE KEY `uk_providers_name` (`name`),
  29. KEY `idx_providers_enabled` (`enabled`),
  30. KEY `idx_providers_updated_at` (`updated_at`)
  31. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='服务商凭据与配置表';
  32. """
  33. id = Column(BigInteger, primary_key=True, autoincrement=True)
  34. name = Column(String(128), nullable=False, unique=True, index=True)
  35. enabled = Column(SmallInteger, nullable=False, default=1)
  36. plugin_config = Column(JSON, nullable=False, default=dict)
  37. role_templates = Column(JSON, nullable=False, default=dict)
  38. version = Column(Integer, nullable=False, default=0)
  39. updated_by = Column(String(64), nullable=True, default="system")
  40. created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
  41. updated_at = Column(
  42. DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False
  43. )
  44. __table_args__ = (
  45. UniqueConstraint("name", name="uk_providers_name"),
  46. Index("idx_providers_enabled", "enabled"),
  47. Index("idx_providers_updated_at", "updated_at"),
  48. )