repositories.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. from datetime import datetime
  2. from sqlalchemy import func, select
  3. from sqlalchemy.orm import Session
  4. from app.db.models import AppDefinition, AppVersion, WorkflowDefinitionModel, WorkflowVersion
  5. class AppDefinitionRepository:
  6. def __init__(self, db: Session) -> None:
  7. self.db = db
  8. def create(
  9. self,
  10. *,
  11. tenant_id: str,
  12. code: str,
  13. name: str,
  14. description: str | None,
  15. owner_user_id: str | None,
  16. settings_json: dict | None,
  17. ) -> AppDefinition:
  18. entity = AppDefinition(
  19. tenant_id=tenant_id,
  20. code=code,
  21. name=name,
  22. description=description,
  23. owner_user_id=owner_user_id,
  24. settings_json=settings_json,
  25. )
  26. self.db.add(entity)
  27. self.db.commit()
  28. self.db.refresh(entity)
  29. return entity
  30. def list_by_tenant(self, tenant_id: str) -> list[AppDefinition]:
  31. stmt = select(AppDefinition).where(AppDefinition.tenant_id == tenant_id)
  32. return list(self.db.scalars(stmt))
  33. class WorkflowDefinitionRepository:
  34. def __init__(self, db: Session) -> None:
  35. self.db = db
  36. def create(
  37. self,
  38. *,
  39. tenant_id: str,
  40. app_id: str,
  41. code: str,
  42. name: str,
  43. workflow_type: str,
  44. ) -> WorkflowDefinitionModel:
  45. entity = WorkflowDefinitionModel(
  46. tenant_id=tenant_id,
  47. app_id=app_id,
  48. code=code,
  49. name=name,
  50. workflow_type=workflow_type,
  51. )
  52. self.db.add(entity)
  53. self.db.commit()
  54. self.db.refresh(entity)
  55. return entity
  56. def list_by_scope(self, *, tenant_id: str, app_id: str | None = None) -> list[WorkflowDefinitionModel]:
  57. stmt = select(WorkflowDefinitionModel).where(WorkflowDefinitionModel.tenant_id == tenant_id)
  58. if app_id:
  59. stmt = stmt.where(WorkflowDefinitionModel.app_id == app_id)
  60. return list(self.db.scalars(stmt))
  61. class AppVersionRepository:
  62. def __init__(self, db: Session) -> None:
  63. self.db = db
  64. def create(
  65. self,
  66. *,
  67. tenant_id: str,
  68. app_id: str,
  69. workflow_version_id: str,
  70. status: str,
  71. published_by: str | None,
  72. changelog: str | None,
  73. ) -> AppVersion:
  74. version_no = self._next_version_no(app_id)
  75. published_time = datetime.utcnow() if status == "published" else None
  76. entity = AppVersion(
  77. tenant_id=tenant_id,
  78. app_id=app_id,
  79. version_no=version_no,
  80. workflow_version_id=workflow_version_id,
  81. status=status,
  82. published_by=published_by,
  83. published_time=published_time,
  84. changelog=changelog,
  85. )
  86. self.db.add(entity)
  87. self.db.commit()
  88. self.db.refresh(entity)
  89. return entity
  90. def list_by_app(self, *, tenant_id: str, app_id: str) -> list[AppVersion]:
  91. stmt = (
  92. select(AppVersion)
  93. .where(AppVersion.tenant_id == tenant_id)
  94. .where(AppVersion.app_id == app_id)
  95. .order_by(AppVersion.version_no.desc())
  96. )
  97. return list(self.db.scalars(stmt))
  98. def _next_version_no(self, app_id: str) -> int:
  99. stmt = select(func.max(AppVersion.version_no)).where(AppVersion.app_id == app_id)
  100. current_max = self.db.scalar(stmt)
  101. return (current_max or 0) + 1
  102. class WorkflowVersionRepository:
  103. def __init__(self, db: Session) -> None:
  104. self.db = db
  105. def create(
  106. self,
  107. *,
  108. tenant_id: str,
  109. workflow_id: str,
  110. dsl_json: dict | None,
  111. compiled_plan_json: dict | None,
  112. schema_version: str | None,
  113. checksum: str | None,
  114. status: str,
  115. ) -> WorkflowVersion:
  116. version_no = self._next_version_no(workflow_id)
  117. entity = WorkflowVersion(
  118. tenant_id=tenant_id,
  119. workflow_id=workflow_id,
  120. version_no=version_no,
  121. dsl_json=dsl_json,
  122. compiled_plan_json=compiled_plan_json,
  123. schema_version=schema_version,
  124. checksum=checksum,
  125. status=status,
  126. )
  127. self.db.add(entity)
  128. self.db.commit()
  129. self.db.refresh(entity)
  130. return entity
  131. def list_by_workflow(self, *, tenant_id: str, workflow_id: str) -> list[WorkflowVersion]:
  132. stmt = (
  133. select(WorkflowVersion)
  134. .where(WorkflowVersion.tenant_id == tenant_id)
  135. .where(WorkflowVersion.workflow_id == workflow_id)
  136. .order_by(WorkflowVersion.version_no.desc())
  137. )
  138. return list(self.db.scalars(stmt))
  139. def get_by_id(self, *, tenant_id: str, workflow_version_id: str) -> WorkflowVersion | None:
  140. stmt = (
  141. select(WorkflowVersion)
  142. .where(WorkflowVersion.tenant_id == tenant_id)
  143. .where(WorkflowVersion.id == workflow_version_id)
  144. )
  145. return self.db.scalar(stmt)
  146. def _next_version_no(self, workflow_id: str) -> int:
  147. stmt = select(func.max(WorkflowVersion.version_no)).where(
  148. WorkflowVersion.workflow_id == workflow_id
  149. )
  150. current_max = self.db.scalar(stmt)
  151. return (current_max or 0) + 1