status-go/protocol/sqlite/ad_hoc_migration.go
Alex Jbanca e8c4b7647f
chore(upgradeSQLCipher): Upgrading SQLCipher to version 5.4.5 (#3559)
* chore(upgradeSQLCipher): Upgrading SQLCipher to version 5.4.5

Changes:
### github.com/mutecomm/go-sqlcipher
1. The improved crypto argorighms from go-sqlcipher v3 are merged in v4
	Tags:
	v4.4.2-status.1 - merge `burn_stack` improvement
	v4.4.2-status.2 - merge `SHA1` improvement
	v4.4.2-status.4- merge 'AES' improvement
2. Fixed `go-sqlcipher` to support v3 database in compatibility mode (`sqlcipher` already supports this) (Tag: v4.4.2-status.3)
3. Upgrade `sqlcipher` to v5.4.5 (Tag: v4.5.4-status.1)

### github.com/status-im/migrate/v4
1. Upgrade `go-sqlcipher` version in `github.com/status-im/migrate/v4`

### status-go
1. Upgrade `go-sqlcipher` and `migrate` modules in status-go
2. Configure the DB connections to open the DB in v3 compatibility mode

* chore(upgradeSQLCipher): Use sqlcipher v3 configuration to encrypt a plain text database

* chore(upgradeSQLCipher): Scanning NULL BLOB value should return nil

Fixing failing tests: TestSyncDeviceSuite/TestPairingSyncDeviceClientAsReceiver;  TestSyncDeviceSuite/TestPairingSyncDeviceClientAsSender

Considering the following configuration:
1. Table with BLOB column has 1 NULL value
2. Query the value
3. Rows.Scan(&dest sql.NullString)

Expected: dest.Valid == false; dest.String == nil
Actual: dest.Valid == true; dest.String == ""

* chore: Bump go-sqlcipher version to include NULL BLOB fix
2023-06-07 08:58:01 +03:00

68 lines
1.9 KiB
Go

package sqlite
import (
"database/sql"
_ "github.com/mutecomm/go-sqlcipher/v4" // We require go sqlcipher that overrides default implementation
"github.com/pkg/errors"
"github.com/status-im/migrate/v4"
)
const communitiesMigrationVersion uint = 1605075346
// FixCommunitiesMigration fixes an issue with a released migration
// In some instances if it was interrupted the migration would be skipped
// but marked as completed.
// What we do here is that we check whether we are at that migration, if
// so we check that the communities table is present, if not we re-run that
// migration.
func FixCommunitiesMigration(version uint, dirty bool, m *migrate.Migrate, db *sql.DB) error {
// If the version is not the same, ignore
if version != communitiesMigrationVersion {
return nil
}
// If it's dirty, it will be replayed anyway
if dirty {
return nil
}
// Otherwise we check whether it actually succeeded by checking for the
// presence of the communities_communities table
var name string
err := db.QueryRow(`SELECT name FROM sqlite_master WHERE type='table' AND name='communities_communities'`).Scan(&name)
// If the err is nil, it means the migration went through fine
if err == nil {
return nil
}
// If any other other, we return the error as that's unexpected
if err != sql.ErrNoRows {
return errors.Wrap(err, "failed to find the communities table")
}
// We replay the migration then
return ReplayLastMigration(version, m)
}
func ReplayLastMigration(version uint, m *migrate.Migrate) error {
// Force version if dirty so it's not dirty anymore
if err := m.Force(int(version)); err != nil {
return errors.Wrap(err, "failed to force migration")
}
// Step down 1 and we retry
if err := m.Steps(-1); err != nil {
return errors.Wrap(err, "failed to step down")
}
return nil
}
func ApplyAdHocMigrations(version uint, dirty bool, m *migrate.Migrate, db *sql.DB) error {
return FixCommunitiesMigration(version, dirty, m, db)
}