from datetime import datetime from sqlalchemy import ( Column, Integer, BigInteger, SmallInteger, String, DateTime, JSON, UniqueConstraint, Index, ) from app.core.database import Base class Provider(Base): __tablename__ = "providers" __ddl__ = """ CREATE TABLE IF NOT EXISTS `providers` ( `id` BIGINT NOT NULL AUTO_INCREMENT, `name` VARCHAR(128) NOT NULL, `enabled` SMALLINT NOT NULL DEFAULT 1, `plugin_config` JSON NOT NULL, `role_templates` JSON NOT NULL, `version` INT NOT NULL DEFAULT 0, `updated_by` VARCHAR(64) NULL DEFAULT 'system', `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), UNIQUE KEY `uk_providers_name` (`name`), KEY `idx_providers_enabled` (`enabled`), KEY `idx_providers_updated_at` (`updated_at`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='服务商凭据与配置表'; """ id = Column(BigInteger, primary_key=True, autoincrement=True) name = Column(String(128), nullable=False, unique=True, index=True) enabled = Column(SmallInteger, nullable=False, default=1) plugin_config = Column(JSON, nullable=False, default=dict) role_templates = Column(JSON, nullable=False, default=dict) version = Column(Integer, nullable=False, default=0) updated_by = Column(String(64), nullable=True, default="system") created_at = Column(DateTime, default=datetime.utcnow, nullable=False) updated_at = Column( DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False ) __table_args__ = ( UniqueConstraint("name", name="uk_providers_name"), Index("idx_providers_enabled", "enabled"), Index("idx_providers_updated_at", "updated_at"), )