github.com/status-im/status-go@v1.1.0/protocol/sqlite/db_test.go (about) 1 package sqlite 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 8 "github.com/status-im/status-go/appdatabase" 9 "github.com/status-im/status-go/t/helpers" 10 ) 11 12 // TestCommunitiesMigrationDirty tests the communities migration when 13 // dirty flag has been set to true. 14 // We first make it fail, then clean up so that it can be replayed, and 15 // then execute again, and we should be all migrated. 16 func TestCommunitiesMigrationDirty(t *testing.T) { 17 // Open the db for the first time. 18 db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{}) 19 require.NoError(t, err) 20 21 // Create a communities table, so that migration will fail 22 _, err = db.Exec(`CREATE TABLE communities_communities (a varchar);`) 23 require.NoError(t, err) 24 25 // Migrate the database, this should fail 26 err = Migrate(db) 27 require.Error(t, err) 28 29 // Version and dirty should be true and set to communities migration 30 var version uint 31 var dirty bool 32 33 err = db.QueryRow(`SELECT version, dirty FROM `+migrationsTable).Scan(&version, &dirty) 34 require.NoError(t, err) 35 36 require.True(t, dirty) 37 require.Equal(t, communitiesMigrationVersion, version) 38 39 // Drop communities table and re-run migrations 40 41 _, err = db.Exec(`DROP TABLE communities_communities`) 42 43 require.NoError(t, err) 44 45 // Migrate the database, this should work 46 err = Migrate(db) 47 require.NoError(t, err) 48 49 // Make sure communities table is present 50 51 var name string 52 err = db.QueryRow(`SELECT name FROM sqlite_master WHERE type='table' AND name='communities_communities'`).Scan(&name) 53 54 require.NoError(t, err) 55 require.Equal(t, "communities_communities", name) 56 57 } 58 59 // TestCommunitiesMigrationNotDirty tests the communities migration when 60 // dirty flag has been set to false, and the communities migration has 61 // effectively been skipped. 62 // We first make it fail, then clean up so that it can be replayed, set 63 // dirty to false and then execute again, and we should be all migrated. 64 func TestCommunitiesMigrationNotDirty(t *testing.T) { 65 // Open the db for the first time. 66 db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{}) 67 require.NoError(t, err) 68 69 // Create a communities table, so that migration will fail 70 _, err = db.Exec(`CREATE TABLE communities_communities (a varchar);`) 71 require.NoError(t, err) 72 73 // Migrate the database, this should fail 74 err = Migrate(db) 75 require.Error(t, err) 76 77 // Set dirty to false 78 // Disabling linter as migrationsTable is controlled by us 79 _, err = db.Exec(`UPDATE ` + migrationsTable + ` SET dirty = 0`) // nolint: gosec 80 require.NoError(t, err) 81 82 // Version and dirty should be true and set to communities migration 83 var version uint 84 var dirty bool 85 86 err = db.QueryRow(`SELECT version, dirty FROM `+migrationsTable).Scan(&version, &dirty) 87 require.NoError(t, err) 88 89 require.False(t, dirty) 90 require.Equal(t, communitiesMigrationVersion, version) 91 92 // Drop communities table and re-run migrations 93 _, err = db.Exec(`DROP TABLE communities_communities`) 94 95 require.NoError(t, err) 96 97 // Migrate the database, this should work 98 err = Migrate(db) 99 require.NoError(t, err) 100 101 // Make sure communities table is present 102 103 var name string 104 err = db.QueryRow(`SELECT name FROM sqlite_master WHERE type='table' AND name='communities_communities'`).Scan(&name) 105 106 require.NoError(t, err) 107 require.Equal(t, "communities_communities", name) 108 109 }