|
| 1 | +"""add chatbot, flow version, session and message tables + CHATBOT feature flag key |
| 2 | +
|
| 3 | +Revision ID: 088 |
| 4 | +Revises: 087 |
| 5 | +Create Date: 2026-10-20 00:00:00.000000 |
| 6 | +""" |
| 7 | + |
| 8 | +import sqlalchemy as sa |
| 9 | +import sqlmodel.sql.sqltypes |
| 10 | +from alembic import op |
| 11 | +from sqlalchemy.dialects import postgresql |
| 12 | + |
| 13 | +revision = "088" |
| 14 | +down_revision = "087" |
| 15 | +branch_labels = None |
| 16 | +depends_on = None |
| 17 | + |
| 18 | + |
| 19 | +def upgrade(): |
| 20 | + op.create_table( |
| 21 | + "chatbot", |
| 22 | + sa.Column( |
| 23 | + "id", |
| 24 | + sa.Uuid(), |
| 25 | + nullable=False, |
| 26 | + comment="Chatbot identifier used in API paths", |
| 27 | + ), |
| 28 | + sa.Column( |
| 29 | + "name", |
| 30 | + sqlmodel.sql.sqltypes.AutoString(length=255), |
| 31 | + nullable=False, |
| 32 | + comment="Display name of the chatbot shown in the UI", |
| 33 | + ), |
| 34 | + sa.Column( |
| 35 | + "organization_id", |
| 36 | + sa.Integer(), |
| 37 | + nullable=False, |
| 38 | + comment="Organization that owns the chatbot", |
| 39 | + ), |
| 40 | + sa.Column( |
| 41 | + "project_id", |
| 42 | + sa.Integer(), |
| 43 | + nullable=False, |
| 44 | + comment="Project that owns the chatbot", |
| 45 | + ), |
| 46 | + sa.Column( |
| 47 | + "inserted_at", |
| 48 | + sa.DateTime(), |
| 49 | + nullable=False, |
| 50 | + comment="Timestamp when the chatbot was created", |
| 51 | + ), |
| 52 | + sa.Column( |
| 53 | + "updated_at", |
| 54 | + sa.DateTime(), |
| 55 | + nullable=False, |
| 56 | + comment="Timestamp when the chatbot was last updated", |
| 57 | + ), |
| 58 | + sa.Column( |
| 59 | + "deleted_at", |
| 60 | + sa.DateTime(), |
| 61 | + nullable=True, |
| 62 | + comment="Soft-delete timestamp; the row is kept because sessions reference it", |
| 63 | + ), |
| 64 | + sa.ForeignKeyConstraint( |
| 65 | + ["organization_id"], ["organization.id"], ondelete="CASCADE" |
| 66 | + ), |
| 67 | + sa.ForeignKeyConstraint(["project_id"], ["project.id"], ondelete="CASCADE"), |
| 68 | + sa.PrimaryKeyConstraint("id"), |
| 69 | + ) |
| 70 | + op.create_index( |
| 71 | + op.f("ix_chatbot_organization_id"), |
| 72 | + "chatbot", |
| 73 | + ["organization_id"], |
| 74 | + unique=False, |
| 75 | + ) |
| 76 | + op.create_index( |
| 77 | + op.f("ix_chatbot_project_id"), "chatbot", ["project_id"], unique=False |
| 78 | + ) |
| 79 | + |
| 80 | + op.create_table( |
| 81 | + "chatbot_flow_version", |
| 82 | + sa.Column( |
| 83 | + "id", |
| 84 | + sa.Uuid(), |
| 85 | + nullable=False, |
| 86 | + comment="Flow version identifier pinned by sessions", |
| 87 | + ), |
| 88 | + sa.Column( |
| 89 | + "chatbot_id", |
| 90 | + sa.Uuid(), |
| 91 | + nullable=False, |
| 92 | + comment="Chatbot this flow version belongs to", |
| 93 | + ), |
| 94 | + sa.Column( |
| 95 | + "version", |
| 96 | + sa.Integer(), |
| 97 | + nullable=False, |
| 98 | + comment="Sequential version number per chatbot, starting at 1", |
| 99 | + ), |
| 100 | + sa.Column( |
| 101 | + "nodes", |
| 102 | + postgresql.JSONB(astext_type=sa.Text()), |
| 103 | + nullable=False, |
| 104 | + comment="React Flow nodes as saved by the UI: [{id, type (input/output/coordinator/onboarding/rag), position {x, y}, data}]; agent node data carries config_id, config_version, prompt", |
| 105 | + ), |
| 106 | + sa.Column( |
| 107 | + "edges", |
| 108 | + postgresql.JSONB(astext_type=sa.Text()), |
| 109 | + nullable=False, |
| 110 | + server_default=sa.text("'[]'::jsonb"), |
| 111 | + comment="React Flow edges as saved by the UI: [{id, source, target, sourceHandle, targetHandle, data}]", |
| 112 | + ), |
| 113 | + sa.Column( |
| 114 | + "is_live", |
| 115 | + sa.Boolean(), |
| 116 | + nullable=False, |
| 117 | + server_default=sa.text("false"), |
| 118 | + comment="True for the single published version new sessions run on; live versions are immutable, the highest non-live version is the draft", |
| 119 | + ), |
| 120 | + sa.Column( |
| 121 | + "inserted_at", |
| 122 | + sa.DateTime(), |
| 123 | + nullable=False, |
| 124 | + comment="Timestamp when the version was created", |
| 125 | + ), |
| 126 | + sa.Column( |
| 127 | + "updated_at", |
| 128 | + sa.DateTime(), |
| 129 | + nullable=False, |
| 130 | + comment="Timestamp when the draft was last saved or the version was published/unpublished", |
| 131 | + ), |
| 132 | + sa.ForeignKeyConstraint(["chatbot_id"], ["chatbot.id"], ondelete="CASCADE"), |
| 133 | + sa.PrimaryKeyConstraint("id"), |
| 134 | + sa.UniqueConstraint( |
| 135 | + "chatbot_id", |
| 136 | + "version", |
| 137 | + name="uq_chatbot_flow_version_chatbot_version", |
| 138 | + ), |
| 139 | + ) |
| 140 | + op.create_index( |
| 141 | + op.f("ix_chatbot_flow_version_chatbot_id"), |
| 142 | + "chatbot_flow_version", |
| 143 | + ["chatbot_id"], |
| 144 | + unique=False, |
| 145 | + ) |
| 146 | + op.create_index( |
| 147 | + "uq_chatbot_flow_version_live_chatbot", |
| 148 | + "chatbot_flow_version", |
| 149 | + ["chatbot_id"], |
| 150 | + unique=True, |
| 151 | + postgresql_where=sa.text("is_live"), |
| 152 | + ) |
| 153 | + |
| 154 | + op.create_table( |
| 155 | + "chatbot_session", |
| 156 | + sa.Column( |
| 157 | + "id", |
| 158 | + sa.Uuid(), |
| 159 | + nullable=False, |
| 160 | + comment="Session identifier returned to the caller", |
| 161 | + ), |
| 162 | + sa.Column( |
| 163 | + "chatbot_id", |
| 164 | + sa.Uuid(), |
| 165 | + nullable=False, |
| 166 | + comment="Chatbot this session runs; tenant scope comes from the chatbot", |
| 167 | + ), |
| 168 | + sa.Column( |
| 169 | + "flow_version_id", |
| 170 | + sa.Uuid(), |
| 171 | + nullable=False, |
| 172 | + comment="Live flow version pinned at creation, so publishing never changes a running conversation", |
| 173 | + ), |
| 174 | + sa.Column( |
| 175 | + "contact_ref", |
| 176 | + sqlmodel.sql.sqltypes.AutoString(length=500), |
| 177 | + nullable=False, |
| 178 | + comment="Caller-side identifier of the end user (e.g. a Glific contact id); opaque to Kaapi", |
| 179 | + ), |
| 180 | + sa.Column( |
| 181 | + "active_node_id", |
| 182 | + sqlmodel.sql.sqltypes.AutoString(length=255), |
| 183 | + nullable=False, |
| 184 | + comment="React Flow node id inside the pinned flow version that handles the next turn", |
| 185 | + ), |
| 186 | + sa.Column( |
| 187 | + "status", |
| 188 | + sa.Enum( |
| 189 | + "active", |
| 190 | + "inactive", |
| 191 | + name="chatbotsessionstatusenum", |
| 192 | + ), |
| 193 | + nullable=False, |
| 194 | + comment="Session in use: active, or inactive once superseded by a newer session for the same chatbot + contact_ref or the chatbot was deleted", |
| 195 | + ), |
| 196 | + sa.Column( |
| 197 | + "completion_status", |
| 198 | + sa.Enum( |
| 199 | + "in_progress", |
| 200 | + "complete", |
| 201 | + "abandoned", |
| 202 | + name="chatbotcompletionstatusenum", |
| 203 | + ), |
| 204 | + nullable=False, |
| 205 | + server_default="in_progress", |
| 206 | + comment="Field collection progress: in_progress, complete once every required field is gathered, or abandoned when deactivated while in_progress", |
| 207 | + ), |
| 208 | + sa.Column( |
| 209 | + "collected_fields", |
| 210 | + postgresql.JSON(astext_type=sa.Text()), |
| 211 | + nullable=False, |
| 212 | + comment="Flat map of field name -> value gathered from the user so far", |
| 213 | + ), |
| 214 | + sa.Column( |
| 215 | + "inserted_at", |
| 216 | + sa.DateTime(), |
| 217 | + nullable=False, |
| 218 | + comment="Timestamp when the session was created", |
| 219 | + ), |
| 220 | + sa.Column( |
| 221 | + "updated_at", |
| 222 | + sa.DateTime(), |
| 223 | + nullable=False, |
| 224 | + comment="Timestamp when the session was last updated", |
| 225 | + ), |
| 226 | + sa.ForeignKeyConstraint(["chatbot_id"], ["chatbot.id"], ondelete="RESTRICT"), |
| 227 | + sa.ForeignKeyConstraint( |
| 228 | + ["flow_version_id"], ["chatbot_flow_version.id"], ondelete="RESTRICT" |
| 229 | + ), |
| 230 | + sa.PrimaryKeyConstraint("id"), |
| 231 | + ) |
| 232 | + op.create_index( |
| 233 | + op.f("ix_chatbot_session_flow_version_id"), |
| 234 | + "chatbot_session", |
| 235 | + ["flow_version_id"], |
| 236 | + unique=False, |
| 237 | + ) |
| 238 | + op.create_index( |
| 239 | + op.f("ix_chatbot_session_contact_ref"), |
| 240 | + "chatbot_session", |
| 241 | + ["contact_ref"], |
| 242 | + unique=False, |
| 243 | + ) |
| 244 | + op.create_index( |
| 245 | + "ix_chatbot_session_chatbot_contact", |
| 246 | + "chatbot_session", |
| 247 | + ["chatbot_id", "contact_ref"], |
| 248 | + unique=False, |
| 249 | + ) |
| 250 | + op.create_index( |
| 251 | + "uq_chatbot_session_active_chatbot_contact", |
| 252 | + "chatbot_session", |
| 253 | + ["chatbot_id", "contact_ref"], |
| 254 | + unique=True, |
| 255 | + postgresql_where=sa.text("status = 'active'"), |
| 256 | + ) |
| 257 | + |
| 258 | + op.create_table( |
| 259 | + "chatbot_session_message", |
| 260 | + sa.Column("id", sa.Uuid(), nullable=False, comment="Unique identifier"), |
| 261 | + sa.Column( |
| 262 | + "session_id", |
| 263 | + sa.Uuid(), |
| 264 | + nullable=False, |
| 265 | + comment="Reference to the owning chatbot session", |
| 266 | + ), |
| 267 | + sa.Column( |
| 268 | + "role", |
| 269 | + sa.Enum("system", "user", "assistant", name="chatbotmessageroleenum"), |
| 270 | + nullable=False, |
| 271 | + comment="Speaker: user or assistant (system is reserved; the prompt lives on the flow node)", |
| 272 | + ), |
| 273 | + sa.Column( |
| 274 | + "content", |
| 275 | + sqlmodel.sql.sqltypes.AutoString(), |
| 276 | + nullable=False, |
| 277 | + comment="Message text", |
| 278 | + ), |
| 279 | + sa.Column( |
| 280 | + "node_id", |
| 281 | + sqlmodel.sql.sqltypes.AutoString(length=255), |
| 282 | + nullable=True, |
| 283 | + comment="React Flow node id of the pinned flow that was active for this turn", |
| 284 | + ), |
| 285 | + sa.Column( |
| 286 | + "inserted_at", |
| 287 | + sa.DateTime(), |
| 288 | + nullable=False, |
| 289 | + comment="Timestamp when the message was stored; defines transcript order", |
| 290 | + ), |
| 291 | + sa.ForeignKeyConstraint( |
| 292 | + ["session_id"], ["chatbot_session.id"], ondelete="CASCADE" |
| 293 | + ), |
| 294 | + sa.PrimaryKeyConstraint("id"), |
| 295 | + ) |
| 296 | + op.create_index( |
| 297 | + op.f("ix_chatbot_session_message_session_id"), |
| 298 | + "chatbot_session_message", |
| 299 | + ["session_id"], |
| 300 | + unique=False, |
| 301 | + ) |
| 302 | + |
| 303 | + # ADD VALUE cannot run inside the migration's transaction on all PG versions. |
| 304 | + with op.get_context().autocommit_block(): |
| 305 | + op.execute("ALTER TYPE featureflagkey ADD VALUE IF NOT EXISTS 'CHATBOT'") |
| 306 | + |
| 307 | + |
| 308 | +def downgrade(): |
| 309 | + op.drop_table("chatbot_session_message") |
| 310 | + op.drop_table("chatbot_session") |
| 311 | + op.drop_table("chatbot_flow_version") |
| 312 | + op.drop_table("chatbot") |
| 313 | + sa.Enum(name="chatbotmessageroleenum").drop(op.get_bind()) |
| 314 | + sa.Enum(name="chatbotcompletionstatusenum").drop(op.get_bind()) |
| 315 | + sa.Enum(name="chatbotsessionstatusenum").drop(op.get_bind()) |
0 commit comments