Hi @sjlombardo - thank you once again for your time and valuable suggestions. You are right that the scheme was malformed and finally I have managed to migrate the database. Of course the problem was in my code, not in sqlcipher.
In one of previous messages I have written that “amalgamation was taken without any important changes”. However, there were some changes and it turned out that they were much more important that I thought.
8 years ago, when sqlcipher was incorporated to our project, the project was also using sqlite. Because of problems with overlapping symbols, there was a global rename made in the amalgamation version (‘sqlite’ was replaced with ‘sqlcipher’ using sed everywhere in code).
Unfortunately, not only symbols were modified but also sqlite system tables. While it really worked fine for all these years, the migration was not possible with unmodified sqlcipher.
To perform the migration, I have taken the newest sqlcipher (4.3.0) and performed a similar global rename but only for sqlite system tables:
for i in $(git grep --full-name -l sqlite_master); do perl -p -i -e 's/sqlite_master/sqlcipher_master/g' $i; done
for i in $(git grep --full-name -l sqlite3_master); do perl -p -i -e 's/sqlite3_master/sqlcipher3_master/g' $i; done
for i in $(git grep --full-name -l sqlite_temp_master); do perl -p -i -e 's/sqlite_temp_master/sqlcipher_temp_master/g' $i; done
for i in $(git grep --full-name -l sqlite_sequence); do perl -p -i -e 's/sqlite_sequence/sqlcipher_sequence/g' $i; done
for i in $(git grep --full-name -l sqlite_stat1); do perl -p -i -e 's/sqlite_stat1/sqlcipher_stat1/g' $i; done
for i in $(git grep --full-name -l sqlite_stat2); do perl -p -i -e 's/sqlite_stat2/sqlcipher_stat2/g' $i; done
for i in $(git grep --full-name -l sqlite_stat3); do perl -p -i -e 's/sqlite_stat3/sqlcipher_stat3/g' $i; done
for i in $(git grep --full-name -l sqlite_stat4); do perl -p -i -e 's/sqlite_stat4/sqlcipher_stat4/g' $i; done
for i in $(git grep --full-name -l sqlite_autoindex); do perl -p -i -e 's/sqlite_autoindex/sqlcipher_autoindex/g' $i; done
After applying above changes, I have managed to perform the migration with the modified sqlcipher with ‘PRAGMA cipher_migrate;’ command.
One problem that I still have to solve is how to migrate the malformed scheme to the proper scheme in runtime and use the upstream sqlcipher.
I would like to ask, is this possible in runtime to force rename sqlite system tables? I know that it is possible to edit schema with ‘PRAGMA writable_schema=ON’. However, renaming of sqlite system tables (with ALTER TABLE) is not allowed.
Is there any other way to force it in my case?
If force rename is not possible I will try to migrate my malformed database manually. I will use old modified amalgamation version of sqlcipher to load malformed database and save data to new database created using linked updated upstream sqlcipher.