Spaces:
Running

axeronis commited on
Commit
5a6ba6b
·
1 Parent(s): be18371

fix: clean up migration - only add qualification fields to leads

Browse files
backend/alembic/versions/2026_01_08_1434-8116ec174400_add_qualification_fields_to_lead.py CHANGED
@@ -9,8 +9,6 @@ from typing import Sequence, Union
9
 
10
  from alembic import op
11
  import sqlalchemy as sa
12
- import sqlmodel
13
- from sqlalchemy.dialects import postgresql
14
 
15
  # revision identifiers, used by Alembic.
16
  revision: str = '8116ec174400'
@@ -20,443 +18,23 @@ depends_on: Union[str, Sequence[str], None] = None
20
 
21
 
22
  def upgrade() -> None:
23
- # ### commands auto generated by Alembic - please adjust! ###
24
- op.create_table('site_settings',
25
- sa.Column('id', sa.Uuid(), nullable=False),
26
- sa.Column('key', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
27
- sa.Column('value', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
28
- sa.Column('updated_at', sa.DateTime(), nullable=False),
29
- sa.Column('updated_by', sa.Uuid(), nullable=True),
30
- sa.PrimaryKeyConstraint('id')
31
- )
32
- op.create_index(op.f('ix_site_settings_key'), 'site_settings', ['key'], unique=True)
33
- op.create_table('booking_resource_services',
34
- sa.Column('id', sa.Uuid(), nullable=False),
35
- sa.Column('resource_id', sa.Uuid(), nullable=False),
36
- sa.Column('service_id', sa.Uuid(), nullable=False),
37
- sa.ForeignKeyConstraint(['resource_id'], ['booking_resources.id'], ),
38
- sa.ForeignKeyConstraint(['service_id'], ['booking_services.id'], ),
39
- sa.PrimaryKeyConstraint('id')
40
- )
41
- op.create_index(op.f('ix_booking_resource_services_resource_id'), 'booking_resource_services', ['resource_id'], unique=False)
42
- op.create_index(op.f('ix_booking_resource_services_service_id'), 'booking_resource_services', ['service_id'], unique=False)
43
- op.drop_index('ix_crm_sync_logs_integration_id', table_name='crm_sync_logs')
44
- op.drop_index('ix_crm_sync_logs_tenant_id', table_name='crm_sync_logs')
45
- op.drop_table('crm_sync_logs')
46
- op.drop_table('resource_services')
47
- op.drop_index('ix_lead_crm_links_crm_contact_id', table_name='lead_crm_links')
48
- op.drop_index('ix_lead_crm_links_crm_lead_id', table_name='lead_crm_links')
49
- op.drop_index('ix_lead_crm_links_integration_id', table_name='lead_crm_links')
50
- op.drop_index('ix_lead_crm_links_lead_id', table_name='lead_crm_links')
51
- op.drop_index('ix_lead_crm_links_tenant_id', table_name='lead_crm_links')
52
- op.drop_table('lead_crm_links')
53
- op.drop_table('crm_field_mappings')
54
- op.drop_index('ix_crm_integrations_tenant_id', table_name='crm_integrations')
55
- op.drop_table('crm_integrations')
56
- op.create_index(op.f('ix_booking_appointments_resource_id'), 'booking_appointments', ['resource_id'], unique=False)
57
- op.add_column('booking_resources', sa.Column('sort_order', sa.Integer(), nullable=False))
58
- op.alter_column('booking_resources', 'is_active',
59
- existing_type=sa.BOOLEAN(),
60
- nullable=False)
61
- op.alter_column('booking_resources', 'created_at',
62
- existing_type=postgresql.TIMESTAMP(),
63
- nullable=False)
64
- op.drop_constraint('fk_booking_resources_user_id', 'booking_resources', type_='foreignkey')
65
- op.create_foreign_key(None, 'booking_resources', 'users', ['user_id'], ['id'])
66
- op.drop_column('booking_resources', 'updated_at')
67
- op.alter_column('knowledge_files', 'file_type',
68
- existing_type=sa.VARCHAR(length=50),
69
- nullable=False)
70
- op.alter_column('knowledge_files', 'send_condition',
71
- existing_type=sa.VARCHAR(length=50),
72
- nullable=False)
73
- op.alter_column('knowledge_files', 'is_active',
74
- existing_type=sa.BOOLEAN(),
75
- nullable=False)
76
- op.alter_column('knowledge_files', 'sort_order',
77
- existing_type=sa.INTEGER(),
78
- nullable=False)
79
- op.alter_column('knowledge_files', 'created_at',
80
- existing_type=postgresql.TIMESTAMP(),
81
- nullable=False)
82
- op.alter_column('knowledge_files', 'updated_at',
83
- existing_type=postgresql.TIMESTAMP(),
84
- nullable=False)
85
- op.alter_column('lead_content_progress', 'created_at',
86
- existing_type=postgresql.TIMESTAMP(),
87
- nullable=False)
88
- op.drop_constraint('uq_lead_content_progress_lead_file', 'lead_content_progress', type_='unique')
89
  op.add_column('leads', sa.Column('qualification_data', sa.JSON(), nullable=True))
90
- op.add_column('leads', sa.Column('qualification_complete', sa.Boolean(), nullable=False))
91
- op.add_column('leads', sa.Column('qualification_notified', sa.Boolean(), nullable=False))
92
- op.alter_column('leads', 'needs_attention',
93
- existing_type=sa.BOOLEAN(),
94
- nullable=False,
95
- existing_server_default=sa.text('false'))
96
- op.alter_column('notification_settings', 'notify_escalations',
97
- existing_type=sa.BOOLEAN(),
98
- nullable=False,
99
- existing_server_default=sa.text('true'))
100
- op.alter_column('partner_payouts', 'status',
101
- existing_type=sa.VARCHAR(),
102
- nullable=False,
103
- existing_server_default=sa.text("'pending'::character varying"))
104
- op.alter_column('partner_payouts', 'payout_method',
105
- existing_type=sa.VARCHAR(),
106
- nullable=False)
107
- op.alter_column('partner_payouts', 'created_at',
108
- existing_type=postgresql.TIMESTAMP(),
109
- nullable=False)
110
- op.alter_column('partners', 'total_withdrawn',
111
- existing_type=sa.DOUBLE_PRECISION(precision=53),
112
- nullable=False,
113
- existing_server_default=sa.text("'0'::double precision"))
114
- op.alter_column('partners', 'referrals_count',
115
- existing_type=sa.INTEGER(),
116
- nullable=False,
117
- existing_server_default=sa.text('0'))
118
- op.alter_column('partners', 'active_referrals',
119
- existing_type=sa.INTEGER(),
120
- nullable=False,
121
- existing_server_default=sa.text('0'))
122
- op.drop_index('ix_partners_user_id', table_name='partners')
123
- op.create_index(op.f('ix_partners_user_id'), 'partners', ['user_id'], unique=True)
124
- op.create_foreign_key(None, 'partners', 'users', ['user_id'], ['id'])
125
- op.alter_column('payment_methods', 'payment_type',
126
- existing_type=sa.VARCHAR(length=50),
127
- nullable=False)
128
- op.alter_column('payment_methods', 'send_condition',
129
- existing_type=sa.VARCHAR(length=50),
130
- nullable=False)
131
- op.alter_column('payment_methods', 'is_active',
132
- existing_type=sa.BOOLEAN(),
133
- nullable=False)
134
- op.alter_column('payment_methods', 'sort_order',
135
- existing_type=sa.INTEGER(),
136
- nullable=False)
137
- op.alter_column('payment_methods', 'created_at',
138
- existing_type=postgresql.TIMESTAMP(),
139
- nullable=False)
140
- op.alter_column('payment_methods', 'updated_at',
141
- existing_type=postgresql.TIMESTAMP(),
142
- nullable=False)
143
- op.alter_column('referred_tenants', 'total_paid',
144
- existing_type=sa.DOUBLE_PRECISION(precision=53),
145
- nullable=False,
146
- existing_server_default=sa.text("'0'::double precision"))
147
- op.alter_column('referred_tenants', 'total_commission',
148
- existing_type=sa.DOUBLE_PRECISION(precision=53),
149
- nullable=False,
150
- existing_server_default=sa.text("'0'::double precision"))
151
- op.alter_column('referred_tenants', 'is_active',
152
- existing_type=sa.BOOLEAN(),
153
- nullable=False,
154
- existing_server_default=sa.text('true'))
155
- op.alter_column('referred_tenants', 'created_at',
156
- existing_type=postgresql.TIMESTAMP(),
157
- nullable=False)
158
- op.alter_column('tenants', 'stripe_customer_id',
159
- existing_type=sa.TEXT(),
160
- type_=sqlmodel.sql.sqltypes.AutoString(),
161
- existing_nullable=True)
162
- op.alter_column('tenants', 'stripe_subscription_id',
163
- existing_type=sa.TEXT(),
164
- type_=sqlmodel.sql.sqltypes.AutoString(),
165
- existing_nullable=True)
166
- op.alter_column('tenants', 'whatsapp_config',
167
- existing_type=postgresql.JSONB(astext_type=sa.Text()),
168
- type_=sa.JSON(),
169
- existing_nullable=True)
170
- op.alter_column('tenants', 'ai_settings',
171
- existing_type=postgresql.JSONB(astext_type=sa.Text()),
172
- type_=sa.JSON(),
173
- existing_nullable=True,
174
- existing_server_default=sa.text('\'{"custom_fields": ["Бюджет", "Срочность", "Статус решения"], "selling_style": "Consultative", "business_description": "Мы продаем услуги и товары", "ideal_customer_profile": "Целевой клиент с реальной потребностью"}\'::jsonb'))
175
- op.alter_column('tenants', 'banned_words',
176
- existing_type=postgresql.JSONB(astext_type=sa.Text()),
177
- type_=sa.JSON(),
178
- existing_nullable=True,
179
- existing_server_default=sa.text("'[]'::jsonb"))
180
- op.alter_column('tenants', 'timezone',
181
- existing_type=sa.VARCHAR(),
182
- nullable=False,
183
- existing_server_default=sa.text("'Asia/Bishkek'::character varying"))
184
- op.alter_column('tenants', 'region',
185
- existing_type=sa.VARCHAR(),
186
- nullable=False,
187
- existing_server_default=sa.text("'kg'::character varying"))
188
- op.alter_column('tenants', 'booking_settings',
189
- existing_type=postgresql.JSONB(astext_type=sa.Text()),
190
- type_=sa.JSON(),
191
- existing_nullable=True)
192
- op.alter_column('tenants', 'voice_id',
193
- existing_type=sa.VARCHAR(),
194
- nullable=False,
195
- existing_server_default=sa.text("'professional_male'::character varying"))
196
- op.alter_column('tenants', 'voice_enabled',
197
- existing_type=sa.BOOLEAN(),
198
- nullable=False,
199
- existing_server_default=sa.text('true'))
200
- op.create_index(op.f('ix_tenants_subscription_plan'), 'tenants', ['subscription_plan'], unique=False)
201
- # ### end Alembic commands ###
202
 
203
 
204
  def downgrade() -> None:
205
- # ### commands auto generated by Alembic - please adjust! ###
206
- op.drop_index(op.f('ix_tenants_subscription_plan'), table_name='tenants')
207
- op.alter_column('tenants', 'voice_enabled',
208
- existing_type=sa.BOOLEAN(),
209
- nullable=True,
210
- existing_server_default=sa.text('true'))
211
- op.alter_column('tenants', 'voice_id',
212
- existing_type=sa.VARCHAR(),
213
- nullable=True,
214
- existing_server_default=sa.text("'professional_male'::character varying"))
215
- op.alter_column('tenants', 'booking_settings',
216
- existing_type=sa.JSON(),
217
- type_=postgresql.JSONB(astext_type=sa.Text()),
218
- existing_nullable=True)
219
- op.alter_column('tenants', 'region',
220
- existing_type=sa.VARCHAR(),
221
- nullable=True,
222
- existing_server_default=sa.text("'kg'::character varying"))
223
- op.alter_column('tenants', 'timezone',
224
- existing_type=sa.VARCHAR(),
225
- nullable=True,
226
- existing_server_default=sa.text("'Asia/Bishkek'::character varying"))
227
- op.alter_column('tenants', 'banned_words',
228
- existing_type=sa.JSON(),
229
- type_=postgresql.JSONB(astext_type=sa.Text()),
230
- existing_nullable=True,
231
- existing_server_default=sa.text("'[]'::jsonb"))
232
- op.alter_column('tenants', 'ai_settings',
233
- existing_type=sa.JSON(),
234
- type_=postgresql.JSONB(astext_type=sa.Text()),
235
- existing_nullable=True,
236
- existing_server_default=sa.text('\'{"custom_fields": ["Бюджет", "Срочность", "Статус решения"], "selling_style": "Consultative", "business_description": "Мы продаем услуги и товары", "ideal_customer_profile": "Целевой клиент с реальной потребностью"}\'::jsonb'))
237
- op.alter_column('tenants', 'whatsapp_config',
238
- existing_type=sa.JSON(),
239
- type_=postgresql.JSONB(astext_type=sa.Text()),
240
- existing_nullable=True)
241
- op.alter_column('tenants', 'stripe_subscription_id',
242
- existing_type=sqlmodel.sql.sqltypes.AutoString(),
243
- type_=sa.TEXT(),
244
- existing_nullable=True)
245
- op.alter_column('tenants', 'stripe_customer_id',
246
- existing_type=sqlmodel.sql.sqltypes.AutoString(),
247
- type_=sa.TEXT(),
248
- existing_nullable=True)
249
- op.alter_column('referred_tenants', 'created_at',
250
- existing_type=postgresql.TIMESTAMP(),
251
- nullable=True)
252
- op.alter_column('referred_tenants', 'is_active',
253
- existing_type=sa.BOOLEAN(),
254
- nullable=True,
255
- existing_server_default=sa.text('true'))
256
- op.alter_column('referred_tenants', 'total_commission',
257
- existing_type=sa.DOUBLE_PRECISION(precision=53),
258
- nullable=True,
259
- existing_server_default=sa.text("'0'::double precision"))
260
- op.alter_column('referred_tenants', 'total_paid',
261
- existing_type=sa.DOUBLE_PRECISION(precision=53),
262
- nullable=True,
263
- existing_server_default=sa.text("'0'::double precision"))
264
- op.alter_column('payment_methods', 'updated_at',
265
- existing_type=postgresql.TIMESTAMP(),
266
- nullable=True)
267
- op.alter_column('payment_methods', 'created_at',
268
- existing_type=postgresql.TIMESTAMP(),
269
- nullable=True)
270
- op.alter_column('payment_methods', 'sort_order',
271
- existing_type=sa.INTEGER(),
272
- nullable=True)
273
- op.alter_column('payment_methods', 'is_active',
274
- existing_type=sa.BOOLEAN(),
275
- nullable=True)
276
- op.alter_column('payment_methods', 'send_condition',
277
- existing_type=sa.VARCHAR(length=50),
278
- nullable=True)
279
- op.alter_column('payment_methods', 'payment_type',
280
- existing_type=sa.VARCHAR(length=50),
281
- nullable=True)
282
- op.drop_constraint(None, 'partners', type_='foreignkey')
283
- op.drop_index(op.f('ix_partners_user_id'), table_name='partners')
284
- op.create_index('ix_partners_user_id', 'partners', ['user_id'], unique=False)
285
- op.alter_column('partners', 'active_referrals',
286
- existing_type=sa.INTEGER(),
287
- nullable=True,
288
- existing_server_default=sa.text('0'))
289
- op.alter_column('partners', 'referrals_count',
290
- existing_type=sa.INTEGER(),
291
- nullable=True,
292
- existing_server_default=sa.text('0'))
293
- op.alter_column('partners', 'total_withdrawn',
294
- existing_type=sa.DOUBLE_PRECISION(precision=53),
295
- nullable=True,
296
- existing_server_default=sa.text("'0'::double precision"))
297
- op.alter_column('partner_payouts', 'created_at',
298
- existing_type=postgresql.TIMESTAMP(),
299
- nullable=True)
300
- op.alter_column('partner_payouts', 'payout_method',
301
- existing_type=sa.VARCHAR(),
302
- nullable=True)
303
- op.alter_column('partner_payouts', 'status',
304
- existing_type=sa.VARCHAR(),
305
- nullable=True,
306
- existing_server_default=sa.text("'pending'::character varying"))
307
- op.alter_column('notification_settings', 'notify_escalations',
308
- existing_type=sa.BOOLEAN(),
309
- nullable=True,
310
- existing_server_default=sa.text('true'))
311
- op.alter_column('leads', 'needs_attention',
312
- existing_type=sa.BOOLEAN(),
313
- nullable=True,
314
- existing_server_default=sa.text('false'))
315
  op.drop_column('leads', 'qualification_notified')
316
  op.drop_column('leads', 'qualification_complete')
317
  op.drop_column('leads', 'qualification_data')
318
- op.create_unique_constraint('uq_lead_content_progress_lead_file', 'lead_content_progress', ['lead_id', 'knowledge_file_id'], postgresql_nulls_not_distinct=False)
319
- op.alter_column('lead_content_progress', 'created_at',
320
- existing_type=postgresql.TIMESTAMP(),
321
- nullable=True)
322
- op.alter_column('knowledge_files', 'updated_at',
323
- existing_type=postgresql.TIMESTAMP(),
324
- nullable=True)
325
- op.alter_column('knowledge_files', 'created_at',
326
- existing_type=postgresql.TIMESTAMP(),
327
- nullable=True)
328
- op.alter_column('knowledge_files', 'sort_order',
329
- existing_type=sa.INTEGER(),
330
- nullable=True)
331
- op.alter_column('knowledge_files', 'is_active',
332
- existing_type=sa.BOOLEAN(),
333
- nullable=True)
334
- op.alter_column('knowledge_files', 'send_condition',
335
- existing_type=sa.VARCHAR(length=50),
336
- nullable=True)
337
- op.alter_column('knowledge_files', 'file_type',
338
- existing_type=sa.VARCHAR(length=50),
339
- nullable=True)
340
- op.add_column('booking_resources', sa.Column('updated_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=True))
341
- op.drop_constraint(None, 'booking_resources', type_='foreignkey')
342
- op.create_foreign_key('fk_booking_resources_user_id', 'booking_resources', 'users', ['user_id'], ['id'], ondelete='SET NULL')
343
- op.alter_column('booking_resources', 'created_at',
344
- existing_type=postgresql.TIMESTAMP(),
345
- nullable=True)
346
- op.alter_column('booking_resources', 'is_active',
347
- existing_type=sa.BOOLEAN(),
348
- nullable=True)
349
- op.drop_column('booking_resources', 'sort_order')
350
- op.drop_index(op.f('ix_booking_appointments_resource_id'), table_name='booking_appointments')
351
- op.create_table('crm_integrations',
352
- sa.Column('id', sa.UUID(), autoincrement=False, nullable=False),
353
- sa.Column('tenant_id', sa.UUID(), autoincrement=False, nullable=False),
354
- sa.Column('crm_type', postgresql.ENUM('AMOCRM', 'BITRIX24', 'HUBSPOT', 'PIPEDRIVE', 'SALESFORCE', 'ZOHO', 'CUSTOM_WEBHOOK', name='crmtype'), autoincrement=False, nullable=False),
355
- sa.Column('integration_name', sa.VARCHAR(), autoincrement=False, nullable=False),
356
- sa.Column('api_url', sa.VARCHAR(), autoincrement=False, nullable=True),
357
- sa.Column('api_key', sa.TEXT(), autoincrement=False, nullable=True),
358
- sa.Column('api_secret', sa.TEXT(), autoincrement=False, nullable=True),
359
- sa.Column('access_token', sa.TEXT(), autoincrement=False, nullable=True),
360
- sa.Column('refresh_token', sa.TEXT(), autoincrement=False, nullable=True),
361
- sa.Column('token_expires_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
362
- sa.Column('oauth_config', postgresql.JSON(astext_type=sa.Text()), autoincrement=False, nullable=True),
363
- sa.Column('sync_direction', postgresql.ENUM('BIDIRECTIONAL', 'TO_CRM', 'FROM_CRM', name='syncdirection'), autoincrement=False, nullable=False),
364
- sa.Column('auto_sync', sa.BOOLEAN(), autoincrement=False, nullable=False),
365
- sa.Column('sync_interval_minutes', sa.INTEGER(), autoincrement=False, nullable=False),
366
- sa.Column('field_mapping', postgresql.JSON(astext_type=sa.Text()), autoincrement=False, nullable=True),
367
- sa.Column('sync_only_hot_leads', sa.BOOLEAN(), autoincrement=False, nullable=False),
368
- sa.Column('sync_statuses', postgresql.JSON(astext_type=sa.Text()), autoincrement=False, nullable=True),
369
- sa.Column('webhook_url', sa.VARCHAR(), autoincrement=False, nullable=True),
370
- sa.Column('webhook_secret', sa.VARCHAR(), autoincrement=False, nullable=True),
371
- sa.Column('webhook_headers', postgresql.JSON(astext_type=sa.Text()), autoincrement=False, nullable=True),
372
- sa.Column('status', postgresql.ENUM('ACTIVE', 'PAUSED', 'ERROR', 'DISABLED', name='syncstatus'), autoincrement=False, nullable=False),
373
- sa.Column('is_active', sa.BOOLEAN(), autoincrement=False, nullable=False),
374
- sa.Column('last_sync_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
375
- sa.Column('last_successful_sync_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
376
- sa.Column('total_synced_leads', sa.INTEGER(), autoincrement=False, nullable=False),
377
- sa.Column('failed_sync_count', sa.INTEGER(), autoincrement=False, nullable=False),
378
- sa.Column('last_error', sa.TEXT(), autoincrement=False, nullable=True),
379
- sa.Column('created_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
380
- sa.Column('updated_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
381
- sa.Column('created_by_user_id', sa.UUID(), autoincrement=False, nullable=True),
382
- sa.ForeignKeyConstraint(['created_by_user_id'], ['users.id'], name='crm_integrations_created_by_user_id_fkey'),
383
- sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], name='crm_integrations_tenant_id_fkey'),
384
- sa.PrimaryKeyConstraint('id', name='crm_integrations_pkey'),
385
- postgresql_ignore_search_path=False
386
- )
387
- op.create_index('ix_crm_integrations_tenant_id', 'crm_integrations', ['tenant_id'], unique=False)
388
- op.create_table('crm_field_mappings',
389
- sa.Column('id', sa.UUID(), autoincrement=False, nullable=False),
390
- sa.Column('crm_type', postgresql.ENUM('AMOCRM', 'BITRIX24', 'HUBSPOT', 'PIPEDRIVE', 'SALESFORCE', 'ZOHO', 'CUSTOM_WEBHOOK', name='crmtype'), autoincrement=False, nullable=False),
391
- sa.Column('template_name', sa.VARCHAR(), autoincrement=False, nullable=False),
392
- sa.Column('description', sa.TEXT(), autoincrement=False, nullable=True),
393
- sa.Column('mapping_config', postgresql.JSON(astext_type=sa.Text()), autoincrement=False, nullable=True),
394
- sa.Column('is_default', sa.BOOLEAN(), autoincrement=False, nullable=False),
395
- sa.Column('is_public', sa.BOOLEAN(), autoincrement=False, nullable=False),
396
- sa.Column('created_by_tenant_id', sa.UUID(), autoincrement=False, nullable=True),
397
- sa.Column('created_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
398
- sa.Column('updated_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
399
- sa.ForeignKeyConstraint(['created_by_tenant_id'], ['tenants.id'], name='crm_field_mappings_created_by_tenant_id_fkey'),
400
- sa.PrimaryKeyConstraint('id', name='crm_field_mappings_pkey')
401
- )
402
- op.create_table('lead_crm_links',
403
- sa.Column('id', sa.UUID(), autoincrement=False, nullable=False),
404
- sa.Column('tenant_id', sa.UUID(), autoincrement=False, nullable=False),
405
- sa.Column('lead_id', sa.UUID(), autoincrement=False, nullable=False),
406
- sa.Column('integration_id', sa.UUID(), autoincrement=False, nullable=False),
407
- sa.Column('crm_contact_id', sa.VARCHAR(), autoincrement=False, nullable=True),
408
- sa.Column('crm_lead_id', sa.VARCHAR(), autoincrement=False, nullable=True),
409
- sa.Column('crm_company_id', sa.VARCHAR(), autoincrement=False, nullable=True),
410
- sa.Column('crm_url', sa.VARCHAR(), autoincrement=False, nullable=True),
411
- sa.Column('crm_data', postgresql.JSON(astext_type=sa.Text()), autoincrement=False, nullable=True),
412
- sa.Column('last_synced_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
413
- sa.Column('sync_hash', sa.VARCHAR(), autoincrement=False, nullable=True),
414
- sa.Column('created_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
415
- sa.Column('updated_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
416
- sa.ForeignKeyConstraint(['integration_id'], ['crm_integrations.id'], name='lead_crm_links_integration_id_fkey'),
417
- sa.ForeignKeyConstraint(['lead_id'], ['leads.id'], name='lead_crm_links_lead_id_fkey'),
418
- sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], name='lead_crm_links_tenant_id_fkey'),
419
- sa.PrimaryKeyConstraint('id', name='lead_crm_links_pkey')
420
- )
421
- op.create_index('ix_lead_crm_links_tenant_id', 'lead_crm_links', ['tenant_id'], unique=False)
422
- op.create_index('ix_lead_crm_links_lead_id', 'lead_crm_links', ['lead_id'], unique=False)
423
- op.create_index('ix_lead_crm_links_integration_id', 'lead_crm_links', ['integration_id'], unique=False)
424
- op.create_index('ix_lead_crm_links_crm_lead_id', 'lead_crm_links', ['crm_lead_id'], unique=False)
425
- op.create_index('ix_lead_crm_links_crm_contact_id', 'lead_crm_links', ['crm_contact_id'], unique=False)
426
- op.create_table('resource_services',
427
- sa.Column('id', sa.UUID(), autoincrement=False, nullable=False),
428
- sa.Column('resource_id', sa.UUID(), autoincrement=False, nullable=False),
429
- sa.Column('service_id', sa.UUID(), autoincrement=False, nullable=False),
430
- sa.ForeignKeyConstraint(['resource_id'], ['booking_resources.id'], name='resource_services_resource_id_fkey', ondelete='CASCADE'),
431
- sa.ForeignKeyConstraint(['service_id'], ['booking_services.id'], name='resource_services_service_id_fkey', ondelete='CASCADE'),
432
- sa.PrimaryKeyConstraint('id', name='resource_services_pkey')
433
- )
434
- op.create_table('crm_sync_logs',
435
- sa.Column('id', sa.UUID(), autoincrement=False, nullable=False),
436
- sa.Column('tenant_id', sa.UUID(), autoincrement=False, nullable=False),
437
- sa.Column('integration_id', sa.UUID(), autoincrement=False, nullable=False),
438
- sa.Column('lead_id', sa.UUID(), autoincrement=False, nullable=True),
439
- sa.Column('sync_direction', sa.VARCHAR(), autoincrement=False, nullable=False),
440
- sa.Column('operation', sa.VARCHAR(), autoincrement=False, nullable=False),
441
- sa.Column('success', sa.BOOLEAN(), autoincrement=False, nullable=False),
442
- sa.Column('error_message', sa.TEXT(), autoincrement=False, nullable=True),
443
- sa.Column('request_payload', postgresql.JSON(astext_type=sa.Text()), autoincrement=False, nullable=True),
444
- sa.Column('response_data', postgresql.JSON(astext_type=sa.Text()), autoincrement=False, nullable=True),
445
- sa.Column('crm_lead_id', sa.VARCHAR(), autoincrement=False, nullable=True),
446
- sa.Column('crm_contact_id', sa.VARCHAR(), autoincrement=False, nullable=True),
447
- sa.Column('crm_deal_id', sa.VARCHAR(), autoincrement=False, nullable=True),
448
- sa.Column('duration_ms', sa.INTEGER(), autoincrement=False, nullable=True),
449
- sa.Column('created_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
450
- sa.ForeignKeyConstraint(['integration_id'], ['crm_integrations.id'], name='crm_sync_logs_integration_id_fkey'),
451
- sa.ForeignKeyConstraint(['lead_id'], ['leads.id'], name='crm_sync_logs_lead_id_fkey'),
452
- sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], name='crm_sync_logs_tenant_id_fkey'),
453
- sa.PrimaryKeyConstraint('id', name='crm_sync_logs_pkey')
454
- )
455
- op.create_index('ix_crm_sync_logs_tenant_id', 'crm_sync_logs', ['tenant_id'], unique=False)
456
- op.create_index('ix_crm_sync_logs_integration_id', 'crm_sync_logs', ['integration_id'], unique=False)
457
- op.drop_index(op.f('ix_booking_resource_services_service_id'), table_name='booking_resource_services')
458
- op.drop_index(op.f('ix_booking_resource_services_resource_id'), table_name='booking_resource_services')
459
- op.drop_table('booking_resource_services')
460
- op.drop_index(op.f('ix_site_settings_key'), table_name='site_settings')
461
- op.drop_table('site_settings')
462
- # ### end Alembic commands ###
 
9
 
10
  from alembic import op
11
  import sqlalchemy as sa
 
 
12
 
13
  # revision identifiers, used by Alembic.
14
  revision: str = '8116ec174400'
 
18
 
19
 
20
  def upgrade() -> None:
21
+ """Add qualification tracking fields to leads table."""
22
+ # Add qualification_data JSON column (nullable)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  op.add_column('leads', sa.Column('qualification_data', sa.JSON(), nullable=True))
24
+
25
+ # Add qualification_complete boolean with default False
26
+ op.add_column('leads', sa.Column('qualification_complete', sa.Boolean(), nullable=True, server_default=sa.text('false')))
27
+ op.execute("UPDATE leads SET qualification_complete = false WHERE qualification_complete IS NULL")
28
+ op.alter_column('leads', 'qualification_complete', nullable=False)
29
+
30
+ # Add qualification_notified boolean with default False
31
+ op.add_column('leads', sa.Column('qualification_notified', sa.Boolean(), nullable=True, server_default=sa.text('false')))
32
+ op.execute("UPDATE leads SET qualification_notified = false WHERE qualification_notified IS NULL")
33
+ op.alter_column('leads', 'qualification_notified', nullable=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
 
36
  def downgrade() -> None:
37
+ """Remove qualification tracking fields from leads table."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  op.drop_column('leads', 'qualification_notified')
39
  op.drop_column('leads', 'qualification_complete')
40
  op.drop_column('leads', 'qualification_data')