Skip to content

Commit

Permalink
Migrates database.
Browse files Browse the repository at this point in the history
Adds new sorting fields to notes.
Adds new color field to category.
Adds migration from 4 to 5.
  • Loading branch information
coderPaddyS committed Nov 4, 2023
1 parent 4b03d45 commit 5f4a727
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
)
public abstract class NoteDatabase extends RoomDatabase {

public static final int VERSION = 4;
public static final int VERSION = 5;
public static final String DATABASE_NAME = "allthenotes";
private static NoteDatabase instance;

Expand Down Expand Up @@ -93,6 +93,43 @@ public void onCreate(@NonNull SupportSQLiteDatabase db) {
}
};

static final Migration MIGRATION_4_5 = new Migration(4,5) {

@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {

// Adds new color field
database.execSQL("ALTER TABLE categories ADD COLUMN color TEXT");

// Adds new fields to sort by
database.execSQL(
"CREATE TABLE notes_new (_id INTEGER NOT NULL DEFAULT 0,"
+ "in_trash INTEGER NOT NULL DEFAULT 0,"
+ "name TEXT NOT NULL DEFAULT 'TEXT',"
+ "type INTEGER NOT NULL DEFAULT 0,"
+ "category INTEGER NOT NULL DEFAULT 0,"
+ "content TEXT NOT NULL DEFAULT 'TEXT',"
+ "last_modified TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,"
+ "custom_order INTEGER NOT NULL DEFAULT 0,"
+ "PRIMARY KEY(_id));");
database.execSQL("INSERT INTO notes_new(_id, in_trash,name,type,category,content,custom_order) SELECT _id, in_trash,name,type,category,content,_id as custom_order FROM notes ORDER BY _id ASC;");
database.execSQL("DROP TABLE notes;");
database.execSQL("ALTER TABLE notes_new RENAME TO notes");
database.execSQL(
"CREATE TRIGGER [UpdateLastModified] AFTER UPDATE ON notes FOR EACH ROW WHEN NEW.last_modified = OLD.last_modified " +
"BEGIN " +
"UPDATE notes SET last_modified = DateTime('now') WHERE _id=NEW._id; " +
"END;"
);
database.execSQL(
"CREATE TRIGGER [InsertCustomOrder] AFTER INSERT ON notes FOR EACH ROW " +
"BEGIN " +
"UPDATE notes SET custom_order = _id WHERE _id=NEW._id; " +
"END;"
);
}
};

/**
* Provides data migration from database version 3 to 4 which checks for an error in the previous
* migration when a backup was imported
Expand Down Expand Up @@ -233,6 +270,7 @@ public void migrate(SupportSQLiteDatabase database) {
MIGRATION_1_2,
MIGRATION_1_3,
MIGRATION_2_3,
MIGRATION_3_4
MIGRATION_3_4,
MIGRATION_4_5
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@ import androidx.room.PrimaryKey
data class Category(
@PrimaryKey(autoGenerate = true)
val _id: Int,
val name: String) {
val name: String,
val color: String?
) {

constructor(name: String) : this(
constructor(name: String): this(
name = name,
_id = 0
_id = 0,
color = null
)
constructor(name: String, color: String?) : this(
name = name,
_id = 0,
color = color
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
*/
package org.secuso.privacyfriendlynotes.room.model

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import java.time.Instant
import java.util.Calendar

/**
* Provides note class with variables and constructor.
Expand All @@ -28,14 +31,20 @@ data class Note(
var content: String,
var type: Int,
var category: Int,
var in_trash: Int = 0) {
var in_trash: Int = 0,
@ColumnInfo(defaultValue = "CURRENT_TIMESTAMP")
var last_modified: String,
var custom_order: Int
) {

constructor(name: String, content: String, type: Int, category: Int) : this(
name = name,
content = content,
type = type,
category = category,
in_trash = 0,
_id = 0
_id = 0,
last_modified = Calendar.getInstance().time.toString(),
custom_order = 0
)
}

0 comments on commit 5f4a727

Please sign in to comment.