Repository navigation
Description
Steps to reproduce
- On NethServer 7, have a WebTop 5 installation whose
webtop5database usesSQL_ASCIIencoding 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 withnfollowed by a dangling0xc2, 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
- Add a pre-migration check in the NS7 migration tool for invalid UTF-8 in
SQL_ASCIIdatabases, before Finish. - Report a clear error pointing to the invalid table/row.
- Make
50restore_dbidempotent (recreate thepgdatavolume 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
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsToDo
Activity
stephdl commentedon Dec 1, 2026
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_ASCIIaccepts 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?
federicoballarini commentedon Dec 1, 2026
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.
lucagasparini commentedon Dec 2, 2026
In my opinion, the only viable solution is the following:
This could be achieved using the following sequence of operations:
stephdl commentedon Dec 3, 2026
@DavidePrincipi please could you add your salt ?
stephdl commentedon Dec 3, 2026
Reproduction
I reproduced the failure on a fresh NS7 and NS8 1.4.5.
Root cause of SQL_ASCII
SQL_ASCIIis the default on every NS7, not a misconfigured OS. My test NS7 hasLANG="en_US.UTF-8"in/etc/locale.conf. After a cleanyum install nethserver-webtop5, every database is stillSQL_ASCII:/var/lib/pgsql/initdb.logshows why:nethserver-postgresql-confrunsservice postgresql initdbfrom an event action. That environment has noLANG, soinitdbfalls back to localeCandSQL_ASCII. ThenCREATE database webtop5;inherits it fromtemplate1.SQL_ASCIIdoes not validate anything. WebTop writes UTF-8, so almost all data is valid. But a truncated multibyte character, for example°(c2 b0) cut afterc2, is stored without any error.Steps
On NS7, install
nethserver-webtop5and create one event in the UI.Append a dangling byte to its title:
Add an attendee row and an
events_icalendarsrow for the same event.Add a second event with valid accented text (
Réunion d'été à Genève, 25°C).Run the detection script from the issue description. It reports exactly one row:
Migrate WebTop to NS8 and click Finish.
Result
import-moduleaborts at50restore_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.
Find the bad rows. The first column of the output is the primary key:
Run the loop from the issue description first to know which tables are affected.
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:Run the detection script again. It must print nothing.
Retry the migration.
I would not convert the whole database with
pg_dump --encoding=LATIN1. On aSQL_ASCIIserver this does not convert anything at dump time.psqlthen 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
SQL_ASCIIdatabases before Finish. Block Finish while bad rows exist.50restore_dbidempotent by recreating thepgdatavolume before restoring, so a retry is safe.stephdl commentedon Dec 3, 2026
Follow-up: migration without the bad byte
I removed the dangling
0xc2on the same NS7 and added a valid accented event, with an attendee and anevents_icalendarsrow. 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
UTF8and the bytes are identical to NS7. No field containsÃorÂ.So
SQL_ASCIIis 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_timestampandrevision_timestampof 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.