Skip to content

Commit 2a7b24d

Browse files
Adding function to upgrade a 1.x sqlcipher database to the 2.0 format
1 parent 6929436 commit 2a7b24d

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/net/sqlcipher/database/SQLiteDatabase.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,36 @@ public class SQLiteDatabase extends SQLiteClosable {
7373
private static final int EVENT_DB_OPERATION = 52000;
7474
private static final int EVENT_DB_CORRUPT = 75004;
7575

76+
public static void upgradeDatabaseFormatFromVersion1To2(File databaseToMigrate, String password) throws Exception {
77+
78+
File newDatabasePath = null;
79+
boolean renameDatabase = false;
80+
SQLiteDatabaseHook hook = new SQLiteDatabaseHook(){
81+
public void preKey(SQLiteDatabase database){
82+
database.execSQL("PRAGMA cipher_default_use_hmac = off");
83+
}
84+
public void postKey(SQLiteDatabase database){
85+
database.execSQL("PRAGMA cipher_default_use_hmac = on");
86+
}
87+
};
88+
89+
try {
90+
newDatabasePath = File.createTempFile("temp", "db", databaseToMigrate.getParentFile());
91+
SQLiteDatabase source = SQLiteDatabase.openOrCreateDatabase(databaseToMigrate, password, null, hook);
92+
source.rawExecSQL(String.format("ATTACH DATABASE '%s' as newdb", newDatabasePath.getAbsolutePath()));
93+
source.rawExecSQL("SELECT sqlcipher_export('newdb')");
94+
source.rawExecSQL("DETACH DATABASE newdb");
95+
source.close();
96+
renameDatabase = true;
97+
} catch(Exception e){
98+
throw e;
99+
}
100+
if(renameDatabase){
101+
databaseToMigrate.delete();
102+
newDatabasePath.renameTo(databaseToMigrate);
103+
}
104+
}
105+
76106
private static void loadICUData(Context context) {
77107

78108
try {
@@ -81,8 +111,8 @@ private static void loadICUData(Context context) {
81111
if(!icuDir.exists()) icuDir.mkdirs();
82112
File icuDataFile = new File(icuDir, "icudt46l.dat");
83113
if(!icuDataFile.exists()) {
84-
ZipInputStream in = new ZipInputStream(context.getAssets().open("icudt46l.zip"));
85-
in.getNextEntry();
114+
ZipInputStream in = new ZipInputStream(context.getAssets().open("icudt46l.zip"));
115+
in.getNextEntry();
86116

87117
OutputStream out = new FileOutputStream(icuDataFile);
88118
byte[] buf = new byte[1024];

0 commit comments

Comments
 (0)