File size: 3,499 Bytes
b4f2238
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from __future__ import annotations

from django.db import migrations, models


def _build_legacy_task_model() -> type[models.Model]:
    class LegacyDocumentRecognitionTask(models.Model):
        file_path = models.CharField(max_length=1024)
        original_filename = models.CharField(max_length=256)
        status = models.CharField(max_length=32, default="pending")
        document_type = models.CharField(max_length=32, null=True, blank=True)
        case_number = models.CharField(max_length=128, null=True, blank=True)
        key_time = models.DateTimeField(null=True, blank=True)
        confidence = models.FloatField(null=True, blank=True)
        extraction_method = models.CharField(max_length=32, null=True, blank=True)
        raw_text = models.TextField(null=True, blank=True)
        renamed_file_path = models.CharField(max_length=1024, null=True, blank=True)
        binding_success = models.BooleanField(null=True)
        case = models.ForeignKey(
            "cases.Case",
            on_delete=models.SET_NULL,
            null=True,
            blank=True,
            db_constraint=False,
            related_name="+",
        )
        case_log = models.ForeignKey(
            "cases.CaseLog",
            on_delete=models.SET_NULL,
            null=True,
            blank=True,
            db_constraint=False,
            related_name="+",
        )
        binding_message = models.CharField(max_length=512, null=True, blank=True)
        binding_error_code = models.CharField(max_length=64, null=True, blank=True)
        error_message = models.TextField(null=True, blank=True)
        notification_sent = models.BooleanField(default=False)
        notification_sent_at = models.DateTimeField(null=True, blank=True)
        notification_error = models.TextField(null=True, blank=True)
        notification_file_sent = models.BooleanField(default=False)
        created_at = models.DateTimeField(auto_now_add=True)
        started_at = models.DateTimeField(null=True, blank=True)
        finished_at = models.DateTimeField(null=True, blank=True)

        class Meta:
            app_label = "automation"
            db_table = "automation_documentrecognitiontask"
            managed = True

    return LegacyDocumentRecognitionTask


def _create_table(apps, schema_editor) -> None:
    model = _build_legacy_task_model()
    table_name = model._meta.db_table
    existing_tables = set(schema_editor.connection.introspection.table_names())
    if table_name in existing_tables:
        return

    schema_editor.create_model(model)
    schema_editor.add_index(
        model,
        models.Index(fields=["status", "created_at"], name="automation__status_b57405_idx"),
    )
    schema_editor.add_index(
        model,
        models.Index(fields=["case"], name="automation__case_id_13ae57_idx"),
    )
    schema_editor.add_index(
        model,
        models.Index(fields=["notification_sent"], name="automation__notific_6b9b00_idx"),
    )


def _drop_table(apps, schema_editor) -> None:
    model = _build_legacy_task_model()
    table_name = model._meta.db_table
    existing_tables = set(schema_editor.connection.introspection.table_names())
    if table_name not in existing_tables:
        return
    schema_editor.delete_model(model)


class Migration(migrations.Migration):
    dependencies = [
        ("automation", "0005_restore_documentrecognitiontask_table"),
    ]

    operations = [
        migrations.RunPython(_create_table, _drop_table),
    ]