node_configuration.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 BookerEntry(Base):
  15. __tablename__ = "booker_entries"
  16. __ddl__ = """
  17. CREATE TABLE IF NOT EXISTS `booker_entries` (
  18. `id` BIGINT NOT NULL AUTO_INCREMENT,
  19. `node_id` VARCHAR(64) NOT NULL,
  20. `identifier` VARCHAR(64) NOT NULL,
  21. `enable` SMALLINT NOT NULL DEFAULT 0,
  22. `debug` SMALLINT NOT NULL DEFAULT 0,
  23. `need_account` SMALLINT NOT NULL DEFAULT 1,
  24. `need_proxy` SMALLINT NOT NULL DEFAULT 1,
  25. `proxy_pool` JSON NOT NULL,
  26. `proxy_cd` INT NOT NULL DEFAULT 300,
  27. `session_max_life` INT NOT NULL DEFAULT 1800,
  28. `active_time_start` VARCHAR(5) NOT NULL DEFAULT '00:00',
  29. `active_time_end` VARCHAR(5) NOT NULL DEFAULT '23:59',
  30. `website` VARCHAR(512) NOT NULL DEFAULT '',
  31. `booker` JSON NOT NULL,
  32. `plugin_config` JSON NOT NULL,
  33. `appointment_types` JSON NOT NULL,
  34. `free_config` JSON NOT NULL,
  35. `version` INT NOT NULL DEFAULT 0,
  36. `updated_by` VARCHAR(64) NULL DEFAULT 'system',
  37. `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  38. `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  39. PRIMARY KEY (`id`),
  40. UNIQUE KEY `uk_booker_node_identifier` (`node_id`,`identifier`),
  41. KEY `idx_booker_identifier` (`identifier`),
  42. KEY `idx_booker_updated_at` (`updated_at`)
  43. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Booker节点配置表';
  44. """
  45. id = Column(BigInteger, primary_key=True, autoincrement=True)
  46. node_id = Column(String(64), nullable=False)
  47. identifier = Column(String(64), nullable=False)
  48. enable = Column(SmallInteger, nullable=False, default=0)
  49. debug = Column(SmallInteger, nullable=False, default=0)
  50. need_account = Column(SmallInteger, nullable=False, default=1)
  51. need_proxy = Column(SmallInteger, nullable=False, default=1)
  52. proxy_pool = Column(JSON, nullable=False, default=list)
  53. proxy_cd = Column(Integer, nullable=False, default=300)
  54. session_max_life = Column(Integer, nullable=False, default=1800)
  55. active_time_start = Column(String(5), nullable=False, default="00:00")
  56. active_time_end = Column(String(5), nullable=False, default="23:59")
  57. website = Column(String(512), nullable=False, default="")
  58. booker = Column(JSON, nullable=False, default=dict)
  59. plugin_config = Column(JSON, nullable=False, default=dict)
  60. appointment_types = Column(JSON, nullable=False, default=list)
  61. free_config = Column(JSON, nullable=False, default=dict)
  62. version = Column(Integer, nullable=False, default=0)
  63. updated_by = Column(String(64), nullable=True, default="system")
  64. created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
  65. updated_at = Column(
  66. DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False
  67. )
  68. __table_args__ = (
  69. UniqueConstraint("node_id", "identifier", name="uk_booker_node_identifier"),
  70. Index("idx_booker_identifier", "identifier"),
  71. Index("idx_booker_updated_at", "updated_at"),
  72. )
  73. class SentinelEntry(Base):
  74. __tablename__ = "sentinel_entries"
  75. __ddl__ = """
  76. CREATE TABLE IF NOT EXISTS `sentinel_entries` (
  77. `id` BIGINT NOT NULL AUTO_INCREMENT,
  78. `node_id` VARCHAR(64) NOT NULL,
  79. `identifier` VARCHAR(64) NOT NULL,
  80. `enable` SMALLINT NOT NULL DEFAULT 0,
  81. `debug` SMALLINT NOT NULL DEFAULT 0,
  82. `need_account` SMALLINT NOT NULL DEFAULT 1,
  83. `need_proxy` SMALLINT NOT NULL DEFAULT 1,
  84. `proxy_pool` JSON NOT NULL,
  85. `proxy_cd` INT NOT NULL DEFAULT 300,
  86. `session_max_life` INT NOT NULL DEFAULT 1800,
  87. `active_time_start` VARCHAR(5) NOT NULL DEFAULT '00:00',
  88. `active_time_end` VARCHAR(5) NOT NULL DEFAULT '23:59',
  89. `website` VARCHAR(512) NOT NULL DEFAULT '',
  90. `sentinel` JSON NOT NULL,
  91. `plugin_config` JSON NOT NULL,
  92. `appointment_types` JSON NOT NULL,
  93. `free_config` JSON NOT NULL,
  94. `version` INT NOT NULL DEFAULT 0,
  95. `updated_by` VARCHAR(64) NULL DEFAULT 'system',
  96. `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  97. `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  98. PRIMARY KEY (`id`),
  99. UNIQUE KEY `uk_sentinel_node_identifier` (`node_id`,`identifier`),
  100. KEY `idx_sentinel_identifier` (`identifier`),
  101. KEY `idx_sentinel_updated_at` (`updated_at`)
  102. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Sentinel监听节点配置表';
  103. """
  104. id = Column(BigInteger, primary_key=True, autoincrement=True)
  105. node_id = Column(String(64), nullable=False)
  106. identifier = Column(String(64), nullable=False)
  107. enable = Column(SmallInteger, nullable=False, default=0)
  108. debug = Column(SmallInteger, nullable=False, default=0)
  109. need_account = Column(SmallInteger, nullable=False, default=1)
  110. need_proxy = Column(SmallInteger, nullable=False, default=1)
  111. proxy_pool = Column(JSON, nullable=False, default=list)
  112. proxy_cd = Column(Integer, nullable=False, default=300)
  113. session_max_life = Column(Integer, nullable=False, default=1800)
  114. active_time_start = Column(String(5), nullable=False, default="00:00")
  115. active_time_end = Column(String(5), nullable=False, default="23:59")
  116. website = Column(String(512), nullable=False, default="")
  117. sentinel = Column(JSON, nullable=False, default=dict)
  118. plugin_config = Column(JSON, nullable=False, default=dict)
  119. appointment_types = Column(JSON, nullable=False, default=list)
  120. free_config = Column(JSON, nullable=False, default=dict)
  121. version = Column(Integer, nullable=False, default=0)
  122. updated_by = Column(String(64), nullable=True, default="system")
  123. created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
  124. updated_at = Column(
  125. DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False
  126. )
  127. __table_args__ = (
  128. UniqueConstraint("node_id", "identifier", name="uk_sentinel_node_identifier"),
  129. Index("idx_sentinel_identifier", "identifier"),
  130. Index("idx_sentinel_updated_at", "updated_at"),
  131. )
  132. class StandaloneEntry(Base):
  133. __tablename__ = "standalone_entries"
  134. __ddl__ = """
  135. CREATE TABLE IF NOT EXISTS `standalone_entries` (
  136. `id` BIGINT NOT NULL AUTO_INCREMENT,
  137. `node_id` VARCHAR(64) NOT NULL,
  138. `order_id` VARCHAR(64) NULL,
  139. `identifier` VARCHAR(64) NOT NULL,
  140. `enable` SMALLINT NOT NULL DEFAULT 1,
  141. `debug` SMALLINT NOT NULL DEFAULT 0,
  142. `plugin_config` JSON NOT NULL,
  143. `account` JSON NOT NULL,
  144. `proxy_pool` JSON NOT NULL,
  145. `query_wait` JSON NOT NULL,
  146. `login_interval` INT NOT NULL DEFAULT 1800,
  147. `session_max_life` INT NOT NULL DEFAULT 1800,
  148. `active_time_start` VARCHAR(5) NOT NULL DEFAULT '00:00',
  149. `active_time_end` VARCHAR(5) NOT NULL DEFAULT '23:59',
  150. `appointment_types` JSON NOT NULL,
  151. `user_preferences` JSON NOT NULL,
  152. `free_config` JSON NOT NULL,
  153. `version` INT NOT NULL DEFAULT 0,
  154. `updated_by` VARCHAR(64) NULL DEFAULT 'system',
  155. `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  156. `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  157. PRIMARY KEY (`id`),
  158. UNIQUE KEY `uk_standalone_node_identifier` (`node_id`,`identifier`),
  159. KEY `idx_standalone_identifier` (`identifier`),
  160. KEY `idx_standalone_order_id` (`order_id`),
  161. KEY `idx_standalone_updated_at` (`updated_at`)
  162. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='独立节点配置表';
  163. """
  164. id = Column(BigInteger, primary_key=True, autoincrement=True)
  165. node_id = Column(String(64), nullable=False)
  166. order_id = Column(String(64), nullable=True)
  167. identifier = Column(String(64), nullable=False)
  168. enable = Column(SmallInteger, nullable=False, default=1)
  169. debug = Column(SmallInteger, nullable=False, default=0)
  170. plugin_config = Column(JSON, nullable=False, default=dict)
  171. account = Column(JSON, nullable=False, default=dict)
  172. proxy_pool = Column(JSON, nullable=False, default=list)
  173. query_wait = Column(JSON, nullable=False, default=dict)
  174. login_interval = Column(Integer, nullable=False, default=1800)
  175. session_max_life = Column(Integer, nullable=False, default=1800)
  176. active_time_start = Column(String(5), nullable=False, default="00:00")
  177. active_time_end = Column(String(5), nullable=False, default="23:59")
  178. appointment_types = Column(JSON, nullable=False, default=list)
  179. user_preferences = Column(JSON, nullable=False, default=dict)
  180. free_config = Column(JSON, nullable=False, default=dict)
  181. version = Column(Integer, nullable=False, default=0)
  182. updated_by = Column(String(64), nullable=True, default="system")
  183. created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
  184. updated_at = Column(
  185. DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False
  186. )
  187. __table_args__ = (
  188. UniqueConstraint("node_id", "identifier", name="uk_standalone_node_identifier"),
  189. Index("idx_standalone_identifier", "identifier"),
  190. Index("idx_standalone_order_id", "order_id"),
  191. Index("idx_standalone_updated_at", "updated_at"),
  192. )
  193. class NodeConfigurationRevision(Base):
  194. __tablename__ = "node_configuration_revisions"
  195. __ddl__ = """
  196. CREATE TABLE IF NOT EXISTS `node_configuration_revisions` (
  197. `id` BIGINT NOT NULL AUTO_INCREMENT,
  198. `entry_type` VARCHAR(32) NOT NULL,
  199. `entry_id` BIGINT NOT NULL,
  200. `version` INT NOT NULL DEFAULT 0,
  201. `action` VARCHAR(32) NOT NULL DEFAULT 'update',
  202. `snapshot` JSON NOT NULL,
  203. `updated_by` VARCHAR(64) NULL DEFAULT 'system',
  204. `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  205. PRIMARY KEY (`id`),
  206. KEY `idx_node_config_rev_type_id` (`entry_type`,`entry_id`)
  207. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='节点配置版本历史表';
  208. """
  209. id = Column(BigInteger, primary_key=True, autoincrement=True)
  210. entry_type = Column(String(32), nullable=False, index=True)
  211. entry_id = Column(BigInteger, nullable=False, index=True)
  212. version = Column(Integer, nullable=False, default=0)
  213. action = Column(String(32), nullable=False, default="update")
  214. snapshot = Column(JSON, nullable=False, default=dict)
  215. updated_by = Column(String(64), nullable=True, default="system")
  216. created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
  217. __table_args__ = (
  218. Index("idx_node_config_rev_type_id", "entry_type", "entry_id"),
  219. )
  220. class ClusterNodeEntry(Base):
  221. __tablename__ = "cluster_nodes"
  222. __ddl__ = """
  223. CREATE TABLE IF NOT EXISTS `cluster_nodes` (
  224. `id` BIGINT NOT NULL AUTO_INCREMENT,
  225. `node_id` VARCHAR(64) NOT NULL,
  226. `name` VARCHAR(128) NOT NULL,
  227. `ip_address` VARCHAR(64) NULL,
  228. `region` VARCHAR(64) NULL,
  229. `status` SMALLINT NOT NULL DEFAULT 1 COMMENT '1:启用, 0:停用',
  230. `notes` VARCHAR(512) NULL,
  231. `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  232. `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  233. PRIMARY KEY (`id`),
  234. UNIQUE KEY `uk_cluster_node_id` (`node_id`),
  235. KEY `idx_cluster_node_status` (`status`)
  236. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='集群节点注册档案表';
  237. """
  238. id = Column(BigInteger, primary_key=True, autoincrement=True)
  239. node_id = Column(String(64), nullable=False)
  240. name = Column(String(128), nullable=False)
  241. ip_address = Column(String(64), nullable=True)
  242. region = Column(String(64), nullable=True)
  243. status = Column(SmallInteger, nullable=False, default=1)
  244. notes = Column(String(512), nullable=True)
  245. created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
  246. updated_at = Column(
  247. DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False
  248. )
  249. __table_args__ = (
  250. UniqueConstraint("node_id", name="uk_cluster_node_id"),
  251. Index("idx_cluster_node_status", "status"),
  252. )