Skip to content

Commit ff2bd73

Browse files
committed
move migration test to versions
I also had to update some migration scripts since new version of room enforces nullability
1 parent b72f68d commit ff2bd73

File tree

9 files changed

+82
-85
lines changed

9 files changed

+82
-85
lines changed

PersistenceMigrationsSample/app/build.gradle

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
apply plugin: 'com.android.application'
1818

1919
android {
20-
compileSdkVersion rootProject.ext.compileSdkVersion
21-
buildToolsVersion rootProject.ext.buildToolsVersion
20+
compileSdkVersion build_versions.target_sdk
21+
buildToolsVersion build_versions.build_tools
2222

2323
defaultConfig {
2424
applicationId "com.example.android.persistence.migration"
25-
minSdkVersion rootProject.ext.minSdkVersion
26-
targetSdkVersion rootProject.ext.targetSdkVersion
25+
minSdkVersion build_versions.min_sdk
26+
targetSdkVersion build_versions.targetSdkVersion
2727
versionCode 1
2828
versionName "1.0"
2929
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
@@ -85,37 +85,37 @@ android {
8585

8686
dependencies {
8787
//Support libraries
88-
implementation "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
89-
implementation "com.android.support:support-v4:$rootProject.supportLibraryVersion"
90-
implementation "com.android.support:design:$rootProject.supportLibraryVersion"
91-
implementation "android.arch.persistence.room:runtime:$rootProject.roomVersion"
92-
annotationProcessor "android.arch.persistence.room:compiler:$rootProject.roomVersion"
88+
implementation deps.support.app_compat
89+
implementation deps.support.v4
90+
implementation deps.support.design
91+
implementation deps.room.runtime
92+
annotationProcessor deps.room.compiler
9393

9494
// Dependencies for local unit tests
95-
testImplementation "junit:junit:$rootProject.ext.junitVersion"
96-
testImplementation "org.mockito:mockito-all:$rootProject.ext.mockitoVersion"
97-
testImplementation "org.hamcrest:hamcrest-all:$rootProject.ext.hamcrestVersion"
95+
testImplementation deps.junit
96+
testImplementation deps.mockito.all
97+
testImplementation deps.hamcrest
9898

9999
// Android Testing Support Library's runner and rules
100-
androidTestImplementation "com.android.support.test:runner:$rootProject.ext.runnerVersion"
101-
androidTestImplementation "com.android.support.test:rules:$rootProject.ext.rulesVersion"
102-
androidTestImplementation "android.arch.persistence.room:testing:$rootProject.roomVersion"
100+
androidTestCompile deps.atsl.runner
101+
androidTestCompile deps.atsl.rules
102+
androidTestCompile deps.room.testing
103+
androidTestCompile deps.arch_core.testing
103104

104105
// Dependencies for Android unit tests
105-
androidTestImplementation "junit:junit:$rootProject.ext.junitVersion"
106-
androidTestImplementation "org.mockito:mockito-core:$rootProject.ext.mockitoVersion"
107-
androidTestImplementation 'com.google.dexmaker:dexmaker:1.2'
108-
androidTestImplementation 'com.google.dexmaker:dexmaker-mockito:1.2'
106+
androidTestCompile deps.junit
107+
androidTestCompile deps.mockito.core, { exclude group: 'net.bytebuddy' }
108+
androidTestCompile deps.dexmaker
109109

110110
// Espresso UI Testing
111-
androidTestImplementation "com.android.support.test.espresso:espresso-core:$rootProject.espressoVersion"
112-
androidTestImplementation "com.android.support.test.espresso:espresso-contrib:$rootProject.espressoVersion"
113-
androidTestImplementation "com.android.support.test.espresso:espresso-intents:$rootProject.espressoVersion"
111+
androidTestCompile deps.espresso.core
112+
androidTestCompile deps.espresso.contrib
113+
androidTestCompile deps.espresso.intents
114114

115115
// Resolve conflicts between main and test APK:
116-
androidTestImplementation "com.android.support:support-annotations:$rootProject.supportLibraryVersion"
117-
androidTestImplementation "com.android.support:support-v4:$rootProject.supportLibraryVersion"
118-
androidTestImplementation "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
119-
androidTestImplementation "com.android.support:design:$rootProject.supportLibraryVersion"
120-
116+
// Resolve conflicts between main and test APK:
117+
androidTestCompile deps.support.annotations
118+
androidTestCompile deps.support.v4
119+
androidTestCompile deps.support.app_compat
120+
androidTestCompile deps.support.design
121121
}

PersistenceMigrationsSample/app/schemas/com.example.android.persistence.migrations.UsersDatabase/2.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22
"formatVersion": 1,
33
"database": {
44
"version": 2,
5-
"identityHash": "a146667337bb1ac18bbca63bc5d59421",
5+
"identityHash": "99c7946712f93a4d723efbe10a500eb0",
66
"entities": [
77
{
88
"tableName": "users",
9-
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`userid` INTEGER, `username` TEXT, PRIMARY KEY(`userid`))",
9+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`userid` INTEGER NOT NULL, `username` TEXT, PRIMARY KEY(`userid`))",
1010
"fields": [
1111
{
1212
"fieldPath": "mId",
1313
"columnName": "userid",
14-
"affinity": "INTEGER"
14+
"affinity": "INTEGER",
15+
"notNull": true
1516
},
1617
{
1718
"fieldPath": "mUserName",
1819
"columnName": "username",
19-
"affinity": "TEXT"
20+
"affinity": "TEXT",
21+
"notNull": false
2022
}
2123
],
2224
"primaryKey": {
@@ -31,7 +33,7 @@
3133
],
3234
"setupQueries": [
3335
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
34-
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"a146667337bb1ac18bbca63bc5d59421\")"
36+
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"99c7946712f93a4d723efbe10a500eb0\")"
3537
]
3638
}
3739
}

PersistenceMigrationsSample/app/schemas/com.example.android.persistence.migrations.UsersDatabase/3.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,29 @@
22
"formatVersion": 1,
33
"database": {
44
"version": 3,
5-
"identityHash": "9ce74730f03594d59d1780980ad39b73",
5+
"identityHash": "30a079c09b902b7e6d50fb4eca6380b0",
66
"entities": [
77
{
88
"tableName": "users",
9-
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`userid` INTEGER, `username` TEXT, `last_update` INTEGER, PRIMARY KEY(`userid`))",
9+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`userid` INTEGER NOT NULL, `username` TEXT, `last_update` INTEGER, PRIMARY KEY(`userid`))",
1010
"fields": [
1111
{
1212
"fieldPath": "mId",
1313
"columnName": "userid",
14-
"affinity": "INTEGER"
14+
"affinity": "INTEGER",
15+
"notNull": true
1516
},
1617
{
1718
"fieldPath": "mUserName",
1819
"columnName": "username",
19-
"affinity": "TEXT"
20+
"affinity": "TEXT",
21+
"notNull": false
2022
},
2123
{
2224
"fieldPath": "mDate",
2325
"columnName": "last_update",
24-
"affinity": "INTEGER"
26+
"affinity": "INTEGER",
27+
"notNull": false
2528
}
2629
],
2730
"primaryKey": {
@@ -36,7 +39,7 @@
3639
],
3740
"setupQueries": [
3841
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
39-
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"9ce74730f03594d59d1780980ad39b73\")"
42+
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"30a079c09b902b7e6d50fb4eca6380b0\")"
4043
]
4144
}
4245
}

PersistenceMigrationsSample/app/schemas/com.example.android.persistence.migrations.UsersDatabase/4.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,29 @@
22
"formatVersion": 1,
33
"database": {
44
"version": 4,
5-
"identityHash": "12ffd58553365c5cebce6e857e5041eb",
5+
"identityHash": "243f98c56672d3dcf342b1c0e8371721",
66
"entities": [
77
{
88
"tableName": "users",
9-
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`userid` TEXT, `username` TEXT, `last_update` INTEGER, PRIMARY KEY(`userid`))",
9+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`userid` TEXT NOT NULL, `username` TEXT, `last_update` INTEGER, PRIMARY KEY(`userid`))",
1010
"fields": [
1111
{
1212
"fieldPath": "mId",
1313
"columnName": "userid",
14-
"affinity": "TEXT"
14+
"affinity": "TEXT",
15+
"notNull": true
1516
},
1617
{
1718
"fieldPath": "mUserName",
1819
"columnName": "username",
19-
"affinity": "TEXT"
20+
"affinity": "TEXT",
21+
"notNull": false
2022
},
2123
{
2224
"fieldPath": "mDate",
2325
"columnName": "last_update",
24-
"affinity": "INTEGER"
26+
"affinity": "INTEGER",
27+
"notNull": false
2528
}
2629
],
2730
"primaryKey": {
@@ -36,7 +39,7 @@
3639
],
3740
"setupQueries": [
3841
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
39-
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"12ffd58553365c5cebce6e857e5041eb\")"
42+
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"243f98c56672d3dcf342b1c0e8371721\")"
4043
]
4144
}
4245
}

PersistenceMigrationsSample/app/src/androidTestRoom_Common/java/com/example/android/persistence/migrations/SqliteDatabaseTestHelper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public static void insertUser(int userid, String userName, SqliteTestDbOpenHelpe
2323
public static void createTable(SqliteTestDbOpenHelper helper) {
2424
SQLiteDatabase db = helper.getWritableDatabase();
2525

26-
db.execSQL("CREATE TABLE IF NOT EXISTS users (userid INTEGER PRIMARY KEY, username TEXT )");
26+
db.execSQL("CREATE TABLE IF NOT EXISTS users (userid INTEGER PRIMARY KEY NOT NULL,"
27+
+ " username TEXT )");
2728

2829
db.close();
2930
}

PersistenceMigrationsSample/app/src/room2/java/com/example/android/persistence/migrations/UsersDatabase.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,15 @@ public void migrate(SupportSQLiteDatabase database) {
6767
static final Migration MIGRATION_2_3 = new Migration(2, 3) {
6868
@Override
6969
public void migrate(SupportSQLiteDatabase database) {
70-
database.execSQL("ALTER TABLE Users "
71-
+ " ADD COLUMN last_update INTEGER");
70+
database.execSQL("CREATE TABLE IF NOT EXISTS Users_new ("
71+
+ "`userid` INTEGER NOT NULL,"
72+
+ "`username` TEXT,"
73+
+ "`last_update` INTEGER,"
74+
+ "PRIMARY KEY(`userid`))");
75+
database.execSQL("INSERT INTO Users_new "
76+
+ " SELECT userid, username, null FROM Users");
77+
database.execSQL("DROP TABLE Users");
78+
database.execSQL("ALTER TABLE Users_new RENAME TO users");
7279
}
7380
};
7481

PersistenceMigrationsSample/app/src/room3/java/com/example/android/persistence/migrations/User.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
import android.arch.persistence.room.Entity;
2121
import android.arch.persistence.room.Ignore;
2222
import android.arch.persistence.room.PrimaryKey;
23+
import android.support.annotation.NonNull;
2324

2425
import java.util.Date;
25-
import java.util.Random;
2626
import java.util.UUID;
2727

2828
/**
@@ -33,6 +33,7 @@ public class User {
3333

3434
@PrimaryKey
3535
@ColumnInfo(name = "userid")
36+
@NonNull
3637
private String mId;
3738

3839
@ColumnInfo(name = "username")

PersistenceMigrationsSample/app/src/room3/java/com/example/android/persistence/migrations/UsersDatabase.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,15 @@ public void migrate(SupportSQLiteDatabase database) {
6767
static final Migration MIGRATION_2_3 = new Migration(2, 3) {
6868
@Override
6969
public void migrate(SupportSQLiteDatabase database) {
70-
database.execSQL("ALTER TABLE users "
71-
+ " ADD COLUMN last_update INTEGER");
70+
database.execSQL("CREATE TABLE IF NOT EXISTS Users_new ("
71+
+ "`userid` INTEGER NOT NULL,"
72+
+ "`username` TEXT,"
73+
+ "`last_update` INTEGER,"
74+
+ "PRIMARY KEY(`userid`))");
75+
database.execSQL("INSERT INTO Users_new "
76+
+ " SELECT userid, username, null FROM Users");
77+
database.execSQL("DROP TABLE Users");
78+
database.execSQL("ALTER TABLE Users_new RENAME TO users");
7279
}
7380
};
7481

@@ -87,8 +94,10 @@ public void migrate(SupportSQLiteDatabase database) {
8794
// to do:
8895
// Create the new table
8996
database.execSQL(
90-
"CREATE TABLE users_new (userid TEXT, username TEXT, last_update INTEGER,"
91-
+ " PRIMARY KEY(userid))");
97+
"CREATE TABLE users_new (userid TEXT NOT NULL,"
98+
+ "username TEXT,"
99+
+ "last_update INTEGER,"
100+
+ "PRIMARY KEY(userid))");
92101
// Copy the data
93102
database.execSQL("INSERT INTO users_new (userid, username, last_update) "
94103
+ "SELECT userid, username, last_update "
Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,18 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4+
apply from: '../versions.gradle'
5+
addRepos(repositories)
46

5-
repositories {
6-
google()
7-
jcenter()
8-
}
97
dependencies {
10-
classpath 'com.android.tools.build:gradle:3.0.0-beta4'
11-
12-
// NOTE: Do not place your application dependencies here; they belong
13-
// in the individual module build.gradle files
8+
classpath deps.android_gradle_plugin
149
}
1510
}
1611

1712
allprojects {
18-
repositories {
19-
google()
20-
jcenter()
21-
}
13+
addRepos(repositories)
2214
}
2315

2416
task clean(type: Delete) {
2517
delete rootProject.buildDir
2618
}
27-
28-
// Define versions in a single place
29-
ext {
30-
// Sdk and tools
31-
minSdkVersion = 14
32-
targetSdkVersion = 26
33-
compileSdkVersion = 26
34-
buildToolsVersion = '26.0.1'
35-
36-
// App dependencies
37-
supportLibraryVersion = '26.0.2'
38-
guavaVersion = '18.0'
39-
junitVersion = '4.12'
40-
mockitoVersion = '1.10.19'
41-
powerMockito = '1.6.2'
42-
hamcrestVersion = '1.3'
43-
runnerVersion = '1.0.1'
44-
rulesVersion = '1.0.1'
45-
espressoVersion = '3.0.1'
46-
roomVersion = '1.0.0-alpha9'
47-
}

0 commit comments

Comments
 (0)