Skip to content

Commit

Permalink
fix issue #26
Browse files Browse the repository at this point in the history
  • Loading branch information
yuweiguocn committed May 31, 2017
1 parent cb0de6c commit d9a206c
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ The original code is from [stackoverflow](http://stackoverflow.com/a/30334668/71
```
dependencies {
compile 'org.greenrobot:greendao:3.2.0'
compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v1.3.0'
compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v1.4.0'
}
```
or (greendao 3.0 below)
Expand All @@ -47,7 +47,7 @@ public class MySQLiteOpenHelper extends DaoMaster.OpenHelper {
super(context, name, factory);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
public void onUpgrade(Database db, int oldVersion, int newVersion) {
MigrationHelper.migrate(db,TestDataDao.class,TestData2Dao.class,TestData3Dao.class);
}
}
Expand Down
4 changes: 2 additions & 2 deletions README_CH.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ GreenDaoUpgradeHelper是一个greenDao的数据库升级帮助类。使用它可
```
dependencies {
compile 'org.greenrobot:greendao:3.2.0'
compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v1.3.0'
compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v1.4.0'
}
```
如果你使用的greendao是3.0以前的版本,请使用下面的依赖:
Expand All @@ -45,7 +45,7 @@ public class MySQLiteOpenHelper extends DaoMaster.OpenHelper {
super(context, name, factory);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
public void onUpgrade(Database db, int oldVersion, int newVersion) {
MigrationHelper.migrate(db,TestDataDao.class,TestData2Dao.class,TestData3Dao.class);
}
}
Expand Down
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ dependencies {

// compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v1.1.0'
compile project(':library')
compile 'net.zetetic:android-database-sqlcipher:3.5.7@aar'
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
Expand All @@ -11,8 +12,12 @@
import com.github.yuweiguocn.demo.greendao.base.BaseActivity;
import com.github.yuweiguocn.demo.greendao.db.DaoMaster;
import com.github.yuweiguocn.demo.greendao.db.MySQLiteOpenHelper;
import com.github.yuweiguocn.demo.greendao.db.TestData2;
import com.github.yuweiguocn.demo.greendao.db.TestData2Dao;
import com.github.yuweiguocn.library.greendao.MigrationHelper;

import java.util.Date;

public class MainActivity extends BaseActivity {

private DaoMaster daoMaster;
Expand All @@ -27,8 +32,12 @@ protected void onCreate(Bundle savedInstanceState) {

MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this, "test.db",
null);
daoMaster = new DaoMaster(helper.getWritableDatabase());
daoMaster = new DaoMaster(helper.getEncryptedWritableDb("12342"));
TestData2 testData2 = new TestData2(0L, "12342", 123L, new Date(), 1234, true);
TestData2Dao testData2Dao = daoMaster.newSession().getTestData2Dao();
testData2Dao.insert(testData2);

Log.d("MigrationHelper", "TestData2 " + testData2Dao.loadAll().toString());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@

import com.github.yuweiguocn.library.greendao.MigrationHelper;

import org.greenrobot.greendao.database.Database;

/**
* Created by Growth on 2016/3/3.
*/
public class MySQLiteOpenHelper extends DaoMaster.OpenHelper {
public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
super(context, name, factory);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
public void onUpgrade(Database db, int oldVersion, int newVersion) {
MigrationHelper.migrate(db,TestDataDao.class,TestData2Dao.class,TestData3Dao.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

/**
*
* please call {@link #migrate(SQLiteDatabase, Class[])}
* please call {@link #migrate(SQLiteDatabase, Class[])} or {@link #migrate(Database, Class[])}
*
*/
public final class MigrationHelper {
Expand All @@ -31,9 +31,12 @@ public final class MigrationHelper {
private static final String SQLITE_TEMP_MASTER = "sqlite_temp_master";

public static void migrate(SQLiteDatabase db, Class<? extends AbstractDao<?, ?>>... daoClasses) {
printLog("【The Old Database Version】" + db.getVersion());
Database database = new StandardDatabase(db);
migrate(database, daoClasses);
}

printLog("【The Old Database Version】" + db.getVersion());
public static void migrate(Database database, Class<? extends AbstractDao<?, ?>>... daoClasses) {
printLog("【Generate temp table】start");
generateTempTables(database, daoClasses);
printLog("【Generate temp table】complete");
Expand Down

0 comments on commit d9a206c

Please sign in to comment.