Fixed a bug that if multiple accounts have the same subscribed subreddits and users, only one account's subscribed subreddits and users will be properly saved.

This commit is contained in:
Alex Ning 2019-10-20 11:49:30 +08:00
parent c9cbc6b5ff
commit 518b6f8471
3 changed files with 38 additions and 13 deletions

View File

@ -2,6 +2,7 @@ package ml.docilealligator.infinityforreddit;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
@ -19,7 +20,7 @@ import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUse
import ml.docilealligator.infinityforreddit.User.UserDao;
import ml.docilealligator.infinityforreddit.User.UserData;
@Database(entities = {Account.class, SubredditData.class, SubscribedSubredditData.class, UserData.class, SubscribedUserData.class}, version = 2)
@Database(entities = {Account.class, SubredditData.class, SubscribedSubredditData.class, UserData.class, SubscribedUserData.class}, version = 3)
public abstract class RedditDataRoomDatabase extends RoomDatabase {
private static RedditDataRoomDatabase INSTANCE;
@ -29,7 +30,7 @@ public abstract class RedditDataRoomDatabase extends RoomDatabase {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
RedditDataRoomDatabase.class, "reddit_data")
.addMigrations(MIGRATION_1_2)
.addMigrations(MIGRATION_1_2, MIGRATION_2_3)
.build();
}
}
@ -56,4 +57,27 @@ public abstract class RedditDataRoomDatabase extends RoomDatabase {
+ " ADD COLUMN is_favorite INTEGER DEFAULT 0 NOT NULL");
}
};
private static final Migration MIGRATION_2_3 = new Migration(2, 3) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
database.execSQL("CREATE TABLE subscribed_subreddits_temp " +
"(id TEXT NOT NULL, name TEXT, icon TEXT, username TEXT NOT NULL, " +
"is_favorite INTEGER NOT NULL, PRIMARY KEY(id, username), " +
"FOREIGN KEY(username) REFERENCES accounts(username) ON DELETE CASCADE)");
database.execSQL(
"INSERT INTO subscribed_subreddits_temp SELECT * FROM subscribed_subreddits");
database.execSQL("DROP TABLE subscribed_subreddits");
database.execSQL("ALTER TABLE subscribed_subreddits_temp RENAME TO subscribed_subreddits");
database.execSQL("CREATE TABLE subscribed_users_temp " +
"(name TEXT NOT NULL, icon TEXT, username TEXT NOT NULL, " +
"is_favorite INTEGER NOT NULL, PRIMARY KEY(name, username), " +
"FOREIGN KEY(username) REFERENCES accounts(username) ON DELETE CASCADE)");
database.execSQL(
"INSERT INTO subscribed_users_temp SELECT * FROM subscribed_users");
database.execSQL("DROP TABLE subscribed_users");
database.execSQL("ALTER TABLE subscribed_users_temp RENAME TO subscribed_users");
}
};
}

View File

@ -4,15 +4,13 @@ import androidx.annotation.NonNull;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.PrimaryKey;
import ml.docilealligator.infinityforreddit.Account.Account;
@Entity(tableName = "subscribed_subreddits", foreignKeys = @ForeignKey(entity = Account.class, parentColumns = "username",
@Entity(tableName = "subscribed_subreddits", primaryKeys = {"id", "username"},
foreignKeys = @ForeignKey(entity = Account.class, parentColumns = "username",
childColumns = "username", onDelete = ForeignKey.CASCADE))
public class SubscribedSubredditData {
@PrimaryKey
@NonNull
@ColumnInfo(name = "id")
private String id;
@ -20,12 +18,13 @@ public class SubscribedSubredditData {
private String name;
@ColumnInfo(name = "icon")
private String iconUrl;
@NonNull
@ColumnInfo(name = "username")
private String username;
@ColumnInfo(name = "is_favorite")
private boolean favorite;
public SubscribedSubredditData(@NonNull String id, String name, String iconUrl, String username,
public SubscribedSubredditData(@NonNull String id, String name, String iconUrl, @NonNull String username,
boolean favorite) {
this.id = id;
this.name = name;
@ -47,11 +46,12 @@ public class SubscribedSubredditData {
return iconUrl;
}
@NonNull
public String getUsername() {
return username;
}
public void setUsername(String username) {
public void setUsername(@NonNull String username) {
this.username = username;
}

View File

@ -4,25 +4,25 @@ import androidx.annotation.NonNull;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.PrimaryKey;
import ml.docilealligator.infinityforreddit.Account.Account;
@Entity(tableName = "subscribed_users", foreignKeys = @ForeignKey(entity = Account.class, parentColumns = "username",
@Entity(tableName = "subscribed_users", primaryKeys = {"name", "username"},
foreignKeys = @ForeignKey(entity = Account.class, parentColumns = "username",
childColumns = "username", onDelete = ForeignKey.CASCADE))
public class SubscribedUserData {
@PrimaryKey
@NonNull
@ColumnInfo(name = "name")
private String name;
@ColumnInfo(name = "icon")
private String iconUrl;
@NonNull
@ColumnInfo(name = "username")
private String username;
@ColumnInfo(name = "is_favorite")
private boolean favorite;
public SubscribedUserData(@NonNull String name, String iconUrl, String username, boolean favorite) {
public SubscribedUserData(@NonNull String name, String iconUrl, @NonNull String username, boolean favorite) {
this.name = name;
this.iconUrl = iconUrl;
this.username = username;
@ -38,11 +38,12 @@ public class SubscribedUserData {
return iconUrl;
}
@NonNull
public String getUsername() {
return username;
}
public void setUsername(String username) {
public void setUsername(@NonNull String username) {
this.username = username;
}