@@ -35,6 +35,7 @@ def upgrade() -> None:
35
35
6.0.1 is still validating checksums so we keep checksum from 6.0.1 in the
36
36
database.
37
37
"""
38
+ _check_visit_null_filter ()
38
39
_do_migration (nullable = False , manager_version = "6.0.2" )
39
40
40
41
@@ -85,3 +86,37 @@ def _do_migration(nullable: bool, manager_version: str) -> None:
85
86
86
87
count = attributes .update (f"version:{ MANAGER } " , manager_version )
87
88
assert count == 1 , "expected to update single row"
89
+
90
+
91
+ def _check_visit_null_filter ():
92
+ """In some cases we saw that visit.physical_filter column contains NULLs,
93
+ and we need to replace that with some non-NULL values.
94
+ """
95
+ mig_context = context .get_context ()
96
+ bind = mig_context .bind
97
+ schema = mig_context .version_table_schema
98
+ metadata = sa .schema .MetaData (bind , schema = schema )
99
+
100
+ visit = sa .schema .Table ("visit" , metadata , autoload_with = bind , schema = schema )
101
+ sql = (
102
+ sa .select (sa .func .count ())
103
+ .select_from (visit )
104
+ .where (visit .columns ["physical_filter" ] == None ) # noqa: E711
105
+ )
106
+ num_nulls = bind .execute (sql ).scalar ()
107
+ if num_nulls > 0 :
108
+
109
+ # User needs to pass special argument.
110
+ config = context .config
111
+ null_filter_value = config .get_section_option ("daf_butler_migrate_options" , "null_filter_value" )
112
+ if null_filter_value is None :
113
+ raise ValueError (
114
+ "NULL values were found for visit.physical_filter. They need to be replaced with"
115
+ " non-NULL values. Please use `--options null_filter_value=<value>` command line option."
116
+ )
117
+ else :
118
+ op .execute (
119
+ visit .update ()
120
+ .where (visit .columns ["physical_filter" ] == None ) # noqa: E711
121
+ .values (physical_filter = null_filter_value )
122
+ )
0 commit comments