diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-10-20 03:49:30 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-10-20 03:49:30 +0000 |
commit | 518b6f8471cfb7f32c0f80c2b102c3418d0307bb (patch) | |
tree | bf622443f9063f3a89f7a6d1fb8f67bbc4bf8b52 | |
parent | c9cbc6b5ffaeb3eb2fc4d87c0bc14d04f9676afc (diff) | |
download | infinity-for-reddit-518b6f8471cfb7f32c0f80c2b102c3418d0307bb.tar infinity-for-reddit-518b6f8471cfb7f32c0f80c2b102c3418d0307bb.tar.gz infinity-for-reddit-518b6f8471cfb7f32c0f80c2b102c3418d0307bb.tar.bz2 infinity-for-reddit-518b6f8471cfb7f32c0f80c2b102c3418d0307bb.tar.lz infinity-for-reddit-518b6f8471cfb7f32c0f80c2b102c3418d0307bb.tar.xz infinity-for-reddit-518b6f8471cfb7f32c0f80c2b102c3418d0307bb.tar.zst infinity-for-reddit-518b6f8471cfb7f32c0f80c2b102c3418d0307bb.zip |
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.
Diffstat (limited to '')
3 files changed, 38 insertions, 13 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java index bc73c266..7953ae75 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java @@ -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"); + } + }; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditData.java index 8cc75e5a..04b8700e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditData.java @@ -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; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserData.java index 701e7e5d..6c7bb7e7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserData.java @@ -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; } |