1- from sqlalchemy import Column , String , Boolean , DateTime , Integer , ForeignKey , Text , JSON
1+ import uuid
2+ from datetime import datetime
3+
4+ from sqlalchemy import JSON , Boolean , Column , DateTime , ForeignKey , Integer , String , Text
25from sqlalchemy .dialects .postgresql import UUID
36from sqlalchemy .orm import relationship
4- from datetime import datetime
5- import uuid
67
78from .database import Base
89
@@ -14,7 +15,7 @@ class Company(Base):
1415 name = Column (String (255 ), nullable = False )
1516 plan = Column (String (50 ), default = "free" ) # free, pro, enterprise
1617 created_at = Column (DateTime , default = datetime .utcnow )
17-
18+
1819 # Relationships
1920 users = relationship ("Membership" , back_populates = "company" )
2021 workflows = relationship ("Workflow" , back_populates = "company" )
@@ -28,7 +29,7 @@ class User(Base):
2829 password_hash = Column (Text , nullable = False )
2930 is_active = Column (Boolean , default = True )
3031 created_at = Column (DateTime , default = datetime .utcnow )
31-
32+
3233 # Relationships
3334 memberships = relationship ("Membership" , back_populates = "user" )
3435
@@ -38,7 +39,7 @@ class Role(Base):
3839
3940 id = Column (UUID (as_uuid = True ), primary_key = True , default = uuid .uuid4 )
4041 name = Column (String (50 ), unique = True , nullable = False ) # owner, admin, editor, runner, viewer
41-
42+
4243 # Relationships
4344 memberships = relationship ("Membership" , back_populates = "role" )
4445
@@ -48,10 +49,12 @@ class Membership(Base):
4849
4950 id = Column (UUID (as_uuid = True ), primary_key = True , default = uuid .uuid4 )
5051 user_id = Column (UUID (as_uuid = True ), ForeignKey ("users.id" , ondelete = "CASCADE" ), nullable = False )
51- company_id = Column (UUID (as_uuid = True ), ForeignKey ("companies.id" , ondelete = "CASCADE" ), nullable = False )
52+ company_id = Column (
53+ UUID (as_uuid = True ), ForeignKey ("companies.id" , ondelete = "CASCADE" ), nullable = False
54+ )
5255 role_id = Column (UUID (as_uuid = True ), ForeignKey ("roles.id" ), nullable = False )
5356 created_at = Column (DateTime , default = datetime .utcnow )
54-
57+
5558 # Relationships
5659 user = relationship ("User" , back_populates = "memberships" )
5760 company = relationship ("Company" , back_populates = "users" )
@@ -62,28 +65,40 @@ class Workflow(Base):
6265 __tablename__ = "workflows"
6366
6467 id = Column (UUID (as_uuid = True ), primary_key = True , default = uuid .uuid4 )
65- company_id = Column (UUID (as_uuid = True ), ForeignKey ("companies.id" , ondelete = "CASCADE" ), nullable = False , index = True )
68+ company_id = Column (
69+ UUID (as_uuid = True ),
70+ ForeignKey ("companies.id" , ondelete = "CASCADE" ),
71+ nullable = False ,
72+ index = True ,
73+ )
6674 name = Column (String (255 ), nullable = False )
6775 description = Column (Text )
6876 is_active = Column (Boolean , default = True )
6977 created_at = Column (DateTime , default = datetime .utcnow )
7078 updated_at = Column (DateTime , default = datetime .utcnow , onupdate = datetime .utcnow )
71-
79+
7280 # Relationships
7381 company = relationship ("Company" , back_populates = "workflows" )
74- versions = relationship ("WorkflowVersion" , back_populates = "workflow" , cascade = "all, delete-orphan" )
82+ versions = relationship (
83+ "WorkflowVersion" , back_populates = "workflow" , cascade = "all, delete-orphan"
84+ )
7585
7686
7787class WorkflowVersion (Base ):
7888 __tablename__ = "workflow_versions"
7989
8090 id = Column (UUID (as_uuid = True ), primary_key = True , default = uuid .uuid4 )
81- workflow_id = Column (UUID (as_uuid = True ), ForeignKey ("workflows.id" , ondelete = "CASCADE" ), nullable = False , index = True )
91+ workflow_id = Column (
92+ UUID (as_uuid = True ),
93+ ForeignKey ("workflows.id" , ondelete = "CASCADE" ),
94+ nullable = False ,
95+ index = True ,
96+ )
8297 version_number = Column (Integer , nullable = False )
8398 rules_json = Column (JSON , nullable = False ) # JSONB in PostgreSQL
8499 created_at = Column (DateTime , default = datetime .utcnow )
85100 created_by = Column (UUID (as_uuid = True ), ForeignKey ("users.id" ))
86-
101+
87102 # Relationships
88103 workflow = relationship ("Workflow" , back_populates = "versions" )
89104 executions = relationship ("Execution" , back_populates = "workflow_version" )
@@ -93,13 +108,20 @@ class Execution(Base):
93108 __tablename__ = "executions"
94109
95110 id = Column (UUID (as_uuid = True ), primary_key = True , default = uuid .uuid4 )
96- company_id = Column (UUID (as_uuid = True ), ForeignKey ("companies.id" , ondelete = "CASCADE" ), nullable = False , index = True )
97- workflow_version_id = Column (UUID (as_uuid = True ), ForeignKey ("workflow_versions.id" ), nullable = False )
111+ company_id = Column (
112+ UUID (as_uuid = True ),
113+ ForeignKey ("companies.id" , ondelete = "CASCADE" ),
114+ nullable = False ,
115+ index = True ,
116+ )
117+ workflow_version_id = Column (
118+ UUID (as_uuid = True ), ForeignKey ("workflow_versions.id" ), nullable = False
119+ )
98120 status = Column (String (50 ), default = "pending" ) # pending, running, success, failed
99121 started_at = Column (DateTime )
100122 finished_at = Column (DateTime )
101123 error_message = Column (Text )
102-
124+
103125 # Relationships
104126 workflow_version = relationship ("WorkflowVersion" , back_populates = "executions" )
105127 logs = relationship ("ExecutionLog" , back_populates = "execution" , cascade = "all, delete-orphan" )
@@ -110,13 +132,18 @@ class ExecutionLog(Base):
110132 __tablename__ = "execution_logs"
111133
112134 id = Column (UUID (as_uuid = True ), primary_key = True , default = uuid .uuid4 )
113- execution_id = Column (UUID (as_uuid = True ), ForeignKey ("executions.id" , ondelete = "CASCADE" ), nullable = False , index = True )
135+ execution_id = Column (
136+ UUID (as_uuid = True ),
137+ ForeignKey ("executions.id" , ondelete = "CASCADE" ),
138+ nullable = False ,
139+ index = True ,
140+ )
114141 step_index = Column (Integer , nullable = False )
115142 step_type = Column (String (50 ), nullable = False )
116143 message = Column (Text , nullable = False )
117144 affected_rows = Column (Integer , default = 0 )
118145 created_at = Column (DateTime , default = datetime .utcnow )
119-
146+
120147 # Relationships
121148 execution = relationship ("Execution" , back_populates = "logs" )
122149
@@ -125,7 +152,12 @@ class File(Base):
125152 __tablename__ = "files"
126153
127154 id = Column (UUID (as_uuid = True ), primary_key = True , default = uuid .uuid4 )
128- company_id = Column (UUID (as_uuid = True ), ForeignKey ("companies.id" , ondelete = "CASCADE" ), nullable = False , index = True )
155+ company_id = Column (
156+ UUID (as_uuid = True ),
157+ ForeignKey ("companies.id" , ondelete = "CASCADE" ),
158+ nullable = False ,
159+ index = True ,
160+ )
129161 original_filename = Column (String (255 ), nullable = False )
130162 storage_path = Column (Text , nullable = False )
131163 file_type = Column (String (50 ), nullable = False ) # input, output
@@ -136,9 +168,13 @@ class File(Base):
136168class ExecutionFile (Base ):
137169 __tablename__ = "execution_files"
138170
139- execution_id = Column (UUID (as_uuid = True ), ForeignKey ("executions.id" , ondelete = "CASCADE" ), primary_key = True )
140- file_id = Column (UUID (as_uuid = True ), ForeignKey ("files.id" , ondelete = "CASCADE" ), primary_key = True )
171+ execution_id = Column (
172+ UUID (as_uuid = True ), ForeignKey ("executions.id" , ondelete = "CASCADE" ), primary_key = True
173+ )
174+ file_id = Column (
175+ UUID (as_uuid = True ), ForeignKey ("files.id" , ondelete = "CASCADE" ), primary_key = True
176+ )
141177 role = Column (String (50 ), nullable = False ) # input, output
142-
178+
143179 # Relationships
144180 execution = relationship ("Execution" , back_populates = "files" )
0 commit comments