Skip to content

NS8 WebTop migration fails when NS7 DB (SQL_ASCII) contains invalid UTF-8 bytes #8400

Todo

Description

@federicoballarini

Steps to reproduce

  • On NethServer 7, have a WebTop 5 installation whose webtop5 database uses SQL_ASCII encoding and contains a row with an invalid UTF-8 sequence. To reproduce it on a test system:
    UPDATE calendar.events SET title = title || E' n\xc2' WHERE event_id = <id>;
    (in our production case an event title ended with n followed by a dangling 0xc2, i.e. a ° truncated after its first byte)
  • Start the NS7 → NS8 migration of Mail and WebTop with the NS7 migration tool and run Sync
  • Run Finish for WebTop

Expected behavior

Either the database is restored correctly on NS8, or the migration stops before Finish with a clear message pointing to the invalid data (table and row) so the administrator can fix it on NS7 and retry.

Actual behavior

The restore on NS8 fails while loading calendar.events:

    ERROR:  invalid byte sequence for encoding "UTF8": 0xc2 0x09
    CONTEXT:  COPY events, line 415
    pg_restore: [archiver (db)] COPY failed for table "events": ERROR:  invalid byte sequence for encoding "UTF8": 0xc2 0x09
    ERROR:  insert or update on table "events_attendees" violates foreign key constraint "events_attendees_event_id_fkey"
    ERROR:  insert or update on table "events_icalendars" violates foreign key constraint "events_icalendars_event_id_fkey"
    WARNING: errors ignored on restore: 3

The migration stops with an error that does not point to the cause. The 0x09 is the COPY field separator: the actual invalid byte is a dangling 0xc2. All other data in the database was valid UTF-8, even though the DB is declared SQL_ASCII, so converting the whole DB from ISO-8859-1 would double-encode accented characters and maybe is not a valid fix.

Invalid rows can be found on NS7 with:

    for t in $(runuser -u postgres -- psql webtop5 -Atc "SELECT quote_ident(schemaname)||'.'||quote_ident(tablename) FROM pg_tables WHERE schemaname NOT IN ('pg_catalog','information_schema')"); do
      n=$(runuser -u postgres -- psql webtop5 -c "COPY $t TO STDOUT" | LC_ALL=en_US.UTF-8 grep -caxv '.*')
      [ "$n" -gt 0 ] && echo "$t: $n invalid rows"
    done; echo "done"

Possible improvements

  1. Add a pre-migration check in the NS7 migration tool for invalid UTF-8 in SQL_ASCII databases, before Finish.
  2. Report a clear error pointing to the invalid table/row.
  3. Make 50restore_db idempotent (recreate the pgdata volume before restoring), so the migration can be safely retried.

Components

  • WebTop 1.4.5 (NS8)
  • NethServer 7 migration tool (nethserver-ns8-migration)

See also

Nethesis Helpdesk Ticket #208119

Activity

  1. stephdl commented on Dec 1, 2026

    @stephdl

    Thanks Federico, great report. The scan script is really handy.

    From what I see, the root cause is bad data in the NS7 database. SQL_ASCII accepts any byte, so the truncated ° went in without complaint. I don't think we can fix it automatically, because we can't guess the original character.

    About point 3, I think we're already covered. When the Finish fails, the abort removes the NS8 module with "preserve_data": false. A new attempt then starts from an empty database.

    Points 1 and 2 are the real pain. The migration is still the main path for many users, and today this error is very hard to understand without your analysis. My doubt is the scope. A check for invalid UTF-8 would catch this case, but the next broken database may fail in another way. Until we decide, your script here is a good reference. Anyone who hits invalid byte sequence for encoding "UTF8" during the WebTop restore can use it to find the bad rows on NS7, fix them, and retry.

    @DavidePrincipi @lucagasparini what do you think? Is a pre-check in the migration tool worth it, or is documenting the workaround enough for now?

  2. self-assigned this
    on Dec 1, 2026
  3. federicoballarini commented on Dec 1, 2026

    @federicoballarini
    MemberAuthor

    About point 3, I think we're already covered. When the Finish fails, the abort removes the NS8 module with "preserve_data": false. A new attempt then starts from an empty database.

    Antoher problem is that if the WebTop restore fails, also mail migration is failed, but Mail data is inconsistent becuase mail folders are already renamed and the domain already exists in the sqlite database.

  4. lucagasparini commented on Dec 2, 2026

    @lucagasparini

    In my opinion, the only viable solution is the following:

    • Check the database encoding on NS7 before finalizing the migration:
    ~]# su - postgres -c "psql -d webtop5 -c 'SHOW server_encoding;'"
       server_encoding
       -----------------
       SQL_ASCII
    
    • If the encoding is SQL_ASCII, perform an encoding conversion on NS7 before generating and transferring the final database dump to NS8.

    This could be achieved using the following sequence of operations:

    # 1. Switch to the postgres user
    su - postgres
    
    # 2. Create the new UTF8 database
    createdb -T template0 -E UTF8 webtop5_utf8
    
    # 3. Copy and convert the data directly
    set -o pipefail
    pg_dump --encoding=LATIN1 webtop5 | psql -X -v ON_ERROR_STOP=1 -d webtop5_utf8
    
    # 4. Terminate any active sessions that might still be using
    # the database and prevent the subsequent commands from succeeding
    psql -d postgres -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'webtop5' AND pid <> pg_backend_pid();"
    
    # 5. Rename the databases
    psql -d postgres -c "ALTER DATABASE webtop5 RENAME TO webtop5_ascii;"
    psql -d postgres -c "ALTER DATABASE webtop5_utf8 RENAME TO webtop5;"
    
  5. stephdl commented on Dec 3, 2026

    @stephdl

    @DavidePrincipi please could you add your salt ?

  6. stephdl commented on Dec 3, 2026

    @stephdl

    Reproduction

    I reproduced the failure on a fresh NS7 and NS8 1.4.5.

    Root cause of SQL_ASCII

    SQL_ASCII is the default on every NS7, not a misconfigured OS. My test NS7 has LANG="en_US.UTF-8" in /etc/locale.conf. After a clean yum install nethserver-webtop5, every database is still SQL_ASCII:

    webtop5   | postgres | SQL_ASCII | C | C |
    template1 | postgres | SQL_ASCII | C | C |
    

    /var/lib/pgsql/initdb.log shows why:

    The database cluster will be initialized with locale "C".
    The default database encoding has accordingly been set to "SQL_ASCII".
    

    nethserver-postgresql-conf runs service postgresql initdb from an event action. That environment has no LANG, so initdb falls back to locale C and SQL_ASCII. Then CREATE database webtop5; inherits it from template1.

    SQL_ASCII does not validate anything. WebTop writes UTF-8, so almost all data is valid. But a truncated multibyte character, for example ° (c2 b0) cut after c2, is stored without any error.

    Steps

    1. On NS7, install nethserver-webtop5 and create one event in the UI.

    2. Append a dangling byte to its title:

      UPDATE calendar.events SET title = title || E'\\xc2' WHERE event_id = 1;
      
    3. Add an attendee row and an events_icalendars row for the same event.

    4. Add a second event with valid accented text (Réunion d'été à Genève, 25°C).

    5. Run the detection script from the issue description. It reports exactly one row:

      calendar.events: 1 invalid rows
      
    6. Migrate WebTop to NS8 and click Finish.

    Result

    import-module aborts at 50restore_db:

    COPY failed for table "events": ERROR:  invalid byte sequence for encoding "UTF8": 0xc2 0x09
    insert or update on table "events_attendees" violates foreign key constraint "events_attendees_event_id_fkey"
    insert or update on table "events_icalendars" violates foreign key constraint "events_icalendars_event_id_fkey"
    WARNING: errors ignored on restore: 3
    action "import-module" status is "aborted" (1) at step 50restore_db
    

    COPY fails for the whole table, not only for the bad row. The valid accented event is lost too. So one bad byte drops every calendar event.

    Workaround for an affected server

    Fix the data on NS7 before Finish, then retry.

    1. Find the bad rows. The first column of the output is the primary key:

      runuser -u postgres -- psql webtop5 -c "COPY calendar.events TO STDOUT" \
        | LC_ALL=en_US.UTF-8 grep -naxv '.*' | cut -f1
      

      Run the loop from the issue description first to know which tables are affected.

    2. Correct each row. The original character cannot be guessed, so a human must fix the text, in the WebTop UI or with an UPDATE. For a trailing dangling byte, this is enough:

      UPDATE calendar.events SET title = rtrim(title, E'\\xc2') WHERE event_id = <id>;
      
    3. Run the detection script again. It must print nothing.

    4. Retry the migration.

    I would not convert the whole database with pg_dump --encoding=LATIN1. On a SQL_ASCII server this does not convert anything at dump time. psql then reads the bytes as Latin-1 and re-encodes them. Valid UTF-8 like é (c3 a9) becomes é. It would break every accented character to fix one row.

    Proposed fixes

    1. In nethserver-ns8-migration, run the invalid UTF-8 check on SQL_ASCII databases before Finish. Block Finish while bad rows exist.
    2. Report the table and primary key of each bad row, so the admin knows what to fix.
    3. Make 50restore_db idempotent by recreating the pgdata volume before restoring, so a retry is safe.
  7. stephdl commented on Dec 3, 2026

    @stephdl

    Follow-up: migration without the bad byte

    I removed the dangling 0xc2 on the same NS7 and added a valid accented event, with an attendee and an events_icalendars row. Text covers 2 to 4 byte characters: Fête de Noël, 30 € par personne, Zürich, œ, ß, 東京, 😀.

    The migration completed with no error. On NS8 the database is UTF8 and the bytes are identical to NS7. No field contains à or Â.

    So SQL_ASCII is not the problem. Every NS7 has it, and WebTop writes valid UTF-8 that loads as is on NS8. No conversion is needed, and the LATIN1 conversion would break every accent. The only problem is one invalid byte in one row.

    I don't think WebTop wrote that byte. The UI, CalDAV and z-push all go through the Java backend and JDBC, which only send valid UTF-8. It looks more like something rare, written by a script, an old import or a direct SQL write. Before we patch the migration tool for everyone, I'd like to understand where it comes from. @federicoballarini could you check creation_timestamp and revision_timestamp of the bad row, and whether this server had any custom script or import touching the WebTop database?

    Until we know more, I think the right path for this case is manual. Run the detection script on NS7, fix the bad rows by hand, then abort and retry the full migration, Mail included, since Mail is left half migrated after the WebTop failure. If we see the same error on other servers, we can reconsider a pre-check before Finish.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions