echo "upgrading definitions of all lists<br>";
$query = "SELECT " . DB_ID_FIELD_NAME . ", " . LISTTABLEDESCRIPTION_TITLE_FIELD_NAME . ", " . LISTTABLEDESCRIPTION_DEFINITION_FIELD_NAME . " FROM " . LISTTABLEDESCRIPTION_TABLE_NAME;
$query_result = $database->query($query);
if ($query_result != FALSE) {
    $all_lists = array();
    while ($row = $database->fetch($query_result)) {
        #echo "&nbsp;&nbsp;&nbsp;found list <strong>".$row[0]."</strong><br>";
        array_push($all_lists, $row);
    }
} else {
    fatal("could not find any lists");
}
foreach ($all_lists as $one_list) {
    $list_id = $one_list[DB_ID_FIELD_NAME];
    $list_name = $one_list[LISTTABLEDESCRIPTION_TITLE_FIELD_NAME];
    $table_name = ListTable::_convert_list_name_to_table_name($list_name);
    $definition = $one_list[LISTTABLEDESCRIPTION_DEFINITION_FIELD_NAME];
    $definition_array = (array) $json->decode(html_entity_decode($definition, ENT_QUOTES));
    echo "&nbsp;&nbsp;&nbsp;update list <strong>{$list_name}</strong><br>";
    $field_names = array_keys($definition_array);
    echo "&nbsp;&nbsp;&nbsp;updating list definition<br>\n";
    $new_definition = array();
    foreach ($field_names as $field_name) {
        $field_definition = $definition_array[$field_name];
        $field_definition_str = htmlentities($json->encode($field_definition), ENT_QUOTES);
        $field_type = $field_definition[0];
        $field_options = $field_definition[1];
        #echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;before: $field_definition_str<br>";
        #echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;found field <strong>".$field_name."</strong> of field type ".$field_type." with options ".$field_options."<br>\n";
        if (count($field_definition) != 2) {
            fatal("definition of list has incorrect number of columns");
echo "set active records and archived records for all lists<br>";
$query = "SELECT " . LISTTABLEDESCRIPTION_TITLE_FIELD_NAME . " FROM " . LISTTABLEDESCRIPTION_TABLE_NAME;
#echo "query=".$query."<br>";
$query_result = $database->query($query);
if ($query_result != FALSE) {
    $all_lists = array();
    while ($row = $database->fetch($query_result)) {
        #echo "&nbsp;&nbsp;&nbsp;found list <strong>".$row[0]."</strong><br>";
        array_push($all_lists, $row[0]);
    }
} else {
    fatal("could not find any lists");
}
foreach ($all_lists as $one_list) {
    echo "&nbsp;&nbsp;&nbsp;update list <strong>{$one_list}</strong><br>";
    $table_name = ListTable::_convert_list_name_to_table_name($one_list);
    # select active records
    $query = "SELECT COUNT(" . DB_ID_FIELD_NAME . ") FROM {$table_name} WHERE " . DB_TS_ARCHIVED_FIELD_NAME . "='" . DB_NULL_DATETIME . "'";
    #echo "query=".$query."<br>";
    $query_result = $database->query($query);
    if ($query_result == FALSE) {
        fatal("could not find active records of list: {$one_list}");
    }
    $result = $database->fetch($query_result);
    $active_records = $result[0];
    # select archived records
    $query = "SELECT COUNT(" . DB_ID_FIELD_NAME . ") FROM {$table_name} WHERE " . DB_TS_ARCHIVED_FIELD_NAME . ">'" . DB_NULL_DATETIME . "'";
    #echo "query=".$query."<br>";
    $query_result = $database->query($query);
    if ($query_result == FALSE) {
        fatal("could not find archived records of list: {$one_list}");