Skip to content

Commit

Permalink
chore: add migration script for new field and data fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
stdavis committed Nov 6, 2024
1 parent fc658cf commit fc5d809
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions scripts/migrations/migration_239.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env python
# * coding: utf8 *
"""
migration_239.py
A module that adds a field for recording the submitter's email address in addition to fixing some values so that they match their associated domain values.
Ref: https://github.com/agrc/electrofishing/issues/239
"""

import arcpy

arcpy.env.workspace = r"..."


def add_field():
field_name = "SUBMITTER"
table_name = "SamplingEvents"
describe = arcpy.da.Describe(table_name)
if field_name not in [field.name for field in describe["fields"]]:
print(f"adding {field_name} field")
arcpy.management.AddField(
table_name,
field_name=field_name,
field_type="TEXT",
field_length=255,
field_alias="Submitter Email",
)
else:
print(f"{field_name} field already exists")


def fix_values():
mappings = [
("backpack", "Backpack"),
("canoebarge", "Canoe/Barge"),
("raftboat", "Raft/Boat"),
]
field_name = "TYPE"
print("fixing equipment type values...")
layer = arcpy.management.MakeFeatureLayer("Equipment", "equipment_layer")

for old_value, new_value in mappings:
print(f"fixing {old_value} to {new_value}")
arcpy.management.SelectLayerByAttribute(
layer, "NEW_SELECTION", f"{field_name} = '{old_value}'"
)
count = arcpy.management.GetCount(layer)[0]
print(f"count: {count}")
arcpy.management.CalculateField(layer, field_name, f"'{new_value}'", "PYTHON3")
arcpy.management.SelectLayerByAttribute(layer, "CLEAR_SELECTION")


add_field()
fix_values()

0 comments on commit fc5d809

Please sign in to comment.