0004_extraction_edit_accuracy.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """add extraction edit accuracy tracking
  2. Revision ID: 0004_extraction_edit_accuracy
  3. Revises: 0003_business_template_lengths
  4. Create Date: 2026-10-12
  5. """
  6. from __future__ import annotations
  7. import sqlalchemy as sa
  8. from alembic import op
  9. revision = "0004_extraction_edit_accuracy"
  10. down_revision = "0003_business_template_lengths"
  11. branch_labels = None
  12. depends_on = None
  13. def upgrade() -> None:
  14. op.add_column(
  15. "document_extractions",
  16. sa.Column("original_value", sa.Text(), nullable=True),
  17. )
  18. op.add_column(
  19. "document_extractions",
  20. sa.Column("edited", sa.Boolean(), nullable=False, server_default=sa.false()),
  21. )
  22. op.add_column(
  23. "document_extractions",
  24. sa.Column("edit_count", sa.Integer(), nullable=False, server_default="0"),
  25. )
  26. op.add_column(
  27. "document_extractions",
  28. sa.Column(
  29. "extraction_correct",
  30. sa.Boolean(),
  31. nullable=False,
  32. server_default=sa.true(),
  33. ),
  34. )
  35. op.add_column(
  36. "document_extractions",
  37. sa.Column("edit_reason", sa.Text(), nullable=True),
  38. )
  39. op.add_column(
  40. "document_extractions",
  41. sa.Column(
  42. "edit_judgement_source",
  43. sa.String(length=32),
  44. nullable=False,
  45. server_default="",
  46. ),
  47. )
  48. op.add_column(
  49. "document_extractions",
  50. sa.Column("edit_judgement_confidence", sa.Float(), nullable=True),
  51. )
  52. op.add_column(
  53. "document_extractions",
  54. sa.Column("edit_judged_at", sa.DateTime(timezone=True), nullable=True),
  55. )
  56. op.execute("UPDATE document_extractions SET original_value = '' WHERE original_value IS NULL")
  57. op.execute("UPDATE document_extractions SET edit_reason = '' WHERE edit_reason IS NULL")
  58. op.alter_column(
  59. "document_extractions",
  60. "original_value",
  61. existing_type=sa.Text(),
  62. nullable=False,
  63. )
  64. op.alter_column(
  65. "document_extractions",
  66. "edit_reason",
  67. existing_type=sa.Text(),
  68. nullable=False,
  69. )
  70. op.create_index(
  71. "ix_document_extractions_edited",
  72. "document_extractions",
  73. ["edited"],
  74. )
  75. op.create_index(
  76. "ix_document_extractions_extraction_correct",
  77. "document_extractions",
  78. ["extraction_correct"],
  79. )
  80. def downgrade() -> None:
  81. op.drop_index(
  82. "ix_document_extractions_extraction_correct",
  83. table_name="document_extractions",
  84. )
  85. op.drop_index(
  86. "ix_document_extractions_edited",
  87. table_name="document_extractions",
  88. )
  89. op.drop_column("document_extractions", "edit_judged_at")
  90. op.drop_column("document_extractions", "edit_judgement_source")
  91. op.drop_column("document_extractions", "edit_judgement_confidence")
  92. op.drop_column("document_extractions", "edit_reason")
  93. op.drop_column("document_extractions", "extraction_correct")
  94. op.drop_column("document_extractions", "edit_count")
  95. op.drop_column("document_extractions", "edited")
  96. op.drop_column("document_extractions", "original_value")