Skip to content

Commit

Permalink
Update database schema and and add inital layout
Browse files Browse the repository at this point in the history
Signed-off-by: starry-shivam <[email protected]>
  • Loading branch information
starry-shivam committed May 7, 2024
1 parent 19b309d commit 990c621
Show file tree
Hide file tree
Showing 13 changed files with 433 additions and 12 deletions.
190 changes: 190 additions & 0 deletions app/schemas/com.starry.greenstash.database.core.AppDatabase/6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
{
"formatVersion": 1,
"database": {
"version": 6,
"identityHash": "b16df1594b1494e7947402d9e7a822da",
"entities": [
{
"tableName": "saving_goal",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`title` TEXT NOT NULL, `targetAmount` REAL NOT NULL, `deadline` TEXT NOT NULL, `goalImage` BLOB, `additionalNotes` TEXT NOT NULL, `priority` INTEGER NOT NULL DEFAULT 2, `reminder` INTEGER NOT NULL DEFAULT false, `goalIconId` TEXT DEFAULT 'Image', `archived` INTEGER NOT NULL DEFAULT false, `goalId` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)",
"fields": [
{
"fieldPath": "title",
"columnName": "title",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "targetAmount",
"columnName": "targetAmount",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "deadline",
"columnName": "deadline",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "goalImage",
"columnName": "goalImage",
"affinity": "BLOB",
"notNull": false
},
{
"fieldPath": "additionalNotes",
"columnName": "additionalNotes",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "priority",
"columnName": "priority",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "2"
},
{
"fieldPath": "reminder",
"columnName": "reminder",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "goalIconId",
"columnName": "goalIconId",
"affinity": "TEXT",
"notNull": false,
"defaultValue": "'Image'"
},
{
"fieldPath": "archived",
"columnName": "archived",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "goalId",
"columnName": "goalId",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"goalId"
]
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "transaction",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`ownerGoalId` INTEGER NOT NULL, `type` INTEGER NOT NULL, `timeStamp` INTEGER NOT NULL, `amount` REAL NOT NULL, `notes` TEXT NOT NULL, `transactionId` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, FOREIGN KEY(`ownerGoalId`) REFERENCES `saving_goal`(`goalId`) ON UPDATE NO ACTION ON DELETE CASCADE )",
"fields": [
{
"fieldPath": "ownerGoalId",
"columnName": "ownerGoalId",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "type",
"columnName": "type",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "timeStamp",
"columnName": "timeStamp",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "amount",
"columnName": "amount",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "notes",
"columnName": "notes",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "transactionId",
"columnName": "transactionId",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"transactionId"
]
},
"indices": [
{
"name": "index_transaction_ownerGoalId",
"unique": false,
"columnNames": [
"ownerGoalId"
],
"orders": [],
"createSql": "CREATE INDEX IF NOT EXISTS `index_transaction_ownerGoalId` ON `${TABLE_NAME}` (`ownerGoalId`)"
}
],
"foreignKeys": [
{
"table": "saving_goal",
"onDelete": "CASCADE",
"onUpdate": "NO ACTION",
"columns": [
"ownerGoalId"
],
"referencedColumns": [
"goalId"
]
}
]
},
{
"tableName": "widget_data",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`appWidgetId` INTEGER NOT NULL, `goalId` INTEGER NOT NULL, PRIMARY KEY(`appWidgetId`))",
"fields": [
{
"fieldPath": "appWidgetId",
"columnName": "appWidgetId",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "goalId",
"columnName": "goalId",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"appWidgetId"
]
},
"indices": [],
"foreignKeys": []
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'b16df1594b1494e7947402d9e7a822da')"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ import com.starry.greenstash.database.widget.WidgetData

@Database(
entities = [Goal::class, Transaction::class, WidgetData::class],
version = 5,
version = 6,
exportSchema = true,
autoMigrations = [
AutoMigration(from = 1, to = 2),
AutoMigration(from = 2, to = 3),
AutoMigration(from = 3, to = 4),
AutoMigration(from = 4, to = 5)
AutoMigration(from = 4, to = 5),
AutoMigration(from = 5, to = 6)
]
)
@TypeConverters(Converters::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ data class Goal(
val reminder: Boolean,
// Added in database schema v5
@ColumnInfo(defaultValue = "Image")
val goalIconId: String?
val goalIconId: String?,
// Added in database schema v6
@ColumnInfo(defaultValue = "false")
val archived: Boolean = false
) {
@PrimaryKey(autoGenerate = true)
var goalId: Long = 0L
Expand Down
82 changes: 77 additions & 5 deletions app/src/main/java/com/starry/greenstash/database/goal/GoalDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,21 @@ import kotlinx.coroutines.flow.Flow

@Dao
interface GoalDao {
// Insert related functions ==========================================================

/**
* Insert goal.
* @param goal Goal to insert.
* @return Id of inserted goal.
*/
@Insert
suspend fun insertGoal(goal: Goal): Long

/**
* Insert goal with transactions.
* This method is used when restoring data from backup file.
* @param goalsWithTransactions List of GoalWithTransactions.
*/
@Transaction
suspend fun insertGoalWithTransaction(goalsWithTransactions: List<GoalWithTransactions>) {
goalsWithTransactions.forEach { goalWithTransactions ->
Expand All @@ -55,58 +66,119 @@ interface GoalDao {
}
}

// Update related functions ==========================================================

/**
* Update goal.
* @param goal Goal to update.
*/
@Update
suspend fun updateGoal(goal: Goal)

// Delete related functions ==========================================================

/**
* Delete goal by id.
* @param goalId Id of goal.
*/
@Query("DELETE FROM saving_goal WHERE goalId = :goalId")
suspend fun deleteGoal(goalId: Long)

// Get related functions ==========================================================

/**
* Get all unarchived goals.
* @return List of GoalWithTransactions.
*/
@Transaction
@Query("SELECT * FROM saving_goal")
@Query("SELECT * FROM saving_goal WHERE archived = 0")
suspend fun getAllGoals(): List<GoalWithTransactions>

/**
* Get all unarchived goals as LiveData.
* @return LiveData of List of GoalWithTransactions.
*/
@Transaction
@Query("SELECT * FROM saving_goal")
@Query("SELECT * FROM saving_goal WHERE archived = 0")
fun getAllGoalsAsLiveData(): LiveData<List<GoalWithTransactions>>

/**
* Get goal by id.
* @param goalId Id of goal.
* @return Goal.
*/
@Query("SELECT * FROM saving_goal WHERE goalId = :goalId")
suspend fun getGoalById(goalId: Long): Goal?

/**
* Get goal with transactions.
* @param goalId Id of goal.
* @return GoalWithTransactions.
*/
@Transaction
@Query("SELECT * FROM saving_goal WHERE goalId = :goalId")
suspend fun getGoalWithTransactionById(goalId: Long): GoalWithTransactions?

/**
* Get goal with transactions as Flow.
* @param goalId Id of goal.
* @return Flow of GoalWithTransactions.
*/
@Transaction
@Query("SELECT * FROM saving_goal WHERE goalId = :goalId")
fun getGoalWithTransactionByIdAsFlow(goalId: Long): Flow<GoalWithTransactions?>

/**
* Get all unarchived goals sorted by name.
* @param sortOrder 1 for ascending, 2 for descending.
* @return Flow of List of GoalWithTransactions.
*/
@Transaction
@Query(
"SELECT * FROM saving_goal ORDER BY " +
"SELECT * FROM saving_goal WHERE archived = 0 ORDER BY " +
"CASE WHEN :sortOrder = 1 THEN title END ASC, " +
"CASE WHEN :sortOrder = 2 THEN title END DESC "
)
fun getAllGoalsByTitle(sortOrder: Int): Flow<List<GoalWithTransactions>>

/**
* Get all unarchived goals sorted by target amount.
* @param sortOrder 1 for ascending, 2 for descending.
* @return Flow of List of GoalWithTransactions.
*/
@Transaction
@Query(
"SELECT * FROM saving_goal ORDER BY " +
"SELECT * FROM saving_goal WHERE archived = 0 ORDER BY " +
"CASE WHEN :sortOrder = 1 THEN targetAmount END ASC, " +
"CASE WHEN :sortOrder = 2 THEN targetAmount END DESC "
)
fun getAllGoalsByAmount(sortOrder: Int): Flow<List<GoalWithTransactions>>

/**
* Get all unarchived goals sorted by priority.
* @param sortOrder 1 for ascending, 2 for descending.
* @return Flow of List of GoalWithTransactions.
*/
@Transaction
@Query(
"SELECT * FROM saving_goal ORDER BY " +
"SELECT * FROM saving_goal WHERE archived = 0 ORDER BY " +
"CASE WHEN :sortOrder = 1 THEN priority END ASC, " +
"CASE WHEN :sortOrder = 2 THEN priority END DESC "
)
fun getAllGoalsByPriority(sortOrder: Int): Flow<List<GoalWithTransactions>>

/**
* Get all archived goals.
* @return Flow of List of GoalWithTransactions.
*/
@Transaction
@Query("SELECT * FROM saving_goal WHERE archived = 1")
fun getAllArchivedGoals(): Flow<List<GoalWithTransactions>>

/**
* For internal use with insertGoalWithTransaction() method only,
* Please use Transaction Dao for transaction related operations.
* @param transactions List of transactions.
*/
@Insert
suspend fun insertTransactions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,24 @@ import androidx.room.Update
@Dao
interface TransactionDao {

/**
* Insert transaction.
* @param transaction Transaction to insert.
*/
@Insert
suspend fun insertTransaction(transaction: Transaction)

/**
* Delete transaction.
* @param transaction Transaction to delete.
*/
@Delete
suspend fun deleteTransaction(transaction: Transaction)

/**
* Update transaction.
* @param transaction Transaction to update.
*/
@Update
suspend fun updateTransaction(transaction: Transaction)
}
Loading

0 comments on commit 990c621

Please sign in to comment.