From b2f1046eb7d4f42957ac555e096d32432adf58c8 Mon Sep 17 00:00:00 2001 From: Prem Nirmal Date: Thu, 16 Jun 2022 09:58:28 +0100 Subject: [PATCH] Add new properties to Quotes table, add migration for that --- app/build.gradle | 8 +- .../3.json | 260 ++++++++++++++++++ .../premnirmal/ticker/components/AppModule.kt | 2 + .../ticker/debug/DbViewerViewModel.kt | 34 +++ .../github/premnirmal/ticker/repo/QuotesDB.kt | 3 +- .../premnirmal/ticker/repo/StocksStorage.kt | 26 +- .../premnirmal/ticker/repo/data/QuoteRow.kt | 17 +- .../ticker/repo/migrations/Migrations.kt | 31 ++- .../activity_quote_detail.xml | 9 +- .../main/res/layout/activity_quote_detail.xml | 9 +- app/src/main/res/values-v21/styles.xml | 4 + app/version.properties | 4 +- 12 files changed, 390 insertions(+), 17 deletions(-) create mode 100644 app/schemas/com.github.premnirmal.ticker.repo.QuotesDB/3.json diff --git a/app/build.gradle b/app/build.gradle index 3ed3b323..bdeb13f3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -82,6 +82,12 @@ android { arguments = ["room.schemaLocation": "$projectDir/schemas".toString()] } } + + kapt { + arguments { + arg("room.schemaLocation", "$projectDir/schemas") + } + } } sourceSets { @@ -193,7 +199,7 @@ ext { OKHTTP_VERSION = "4.9.0" ROBOLECTRIC_VERSION = "4.3.1" COROUTINES_VERSION = "1.6.0" - ROOM_VERSION = "2.4.1" + ROOM_VERSION = "2.4.2" WORK_VERSION = "2.7.1" } diff --git a/app/schemas/com.github.premnirmal.ticker.repo.QuotesDB/3.json b/app/schemas/com.github.premnirmal.ticker.repo.QuotesDB/3.json new file mode 100644 index 00000000..5d49e87a --- /dev/null +++ b/app/schemas/com.github.premnirmal.ticker.repo.QuotesDB/3.json @@ -0,0 +1,260 @@ +{ + "formatVersion": 1, + "database": { + "version": 3, + "identityHash": "ded8ebb6cda3d9b4d96eb448091cd1b3", + "entities": [ + { + "tableName": "QuoteRow", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`symbol` TEXT NOT NULL, `name` TEXT NOT NULL, `last_trade_price` REAL NOT NULL, `change_percent` REAL NOT NULL, `change` REAL NOT NULL, `exchange` TEXT NOT NULL, `currency` TEXT NOT NULL, `is_post_market` INTEGER NOT NULL, `annual_dividend_rate` REAL NOT NULL, `annual_dividend_yield` REAL NOT NULL, `dayHigh` REAL, `dayLow` REAL, `previousClose` REAL NOT NULL, `open` REAL, `regularMarketVolume` REAL, `peRatio` REAL, `fiftyTwoWeekLowChange` REAL, `fiftyTwoWeekLowChangePercent` REAL, `fiftyTwoWeekHighChange` REAL, `fiftyTwoWeekHighChangePercent` REAL, `fiftyTwoWeekLow` REAL, `fiftyTwoWeekHigh` REAL, `dividendDate` REAL, `earningsDate` REAL, `marketCap` REAL, PRIMARY KEY(`symbol`))", + "fields": [ + { + "fieldPath": "symbol", + "columnName": "symbol", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "name", + "columnName": "name", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "lastTradePrice", + "columnName": "last_trade_price", + "affinity": "REAL", + "notNull": true + }, + { + "fieldPath": "changeInPercent", + "columnName": "change_percent", + "affinity": "REAL", + "notNull": true + }, + { + "fieldPath": "change", + "columnName": "change", + "affinity": "REAL", + "notNull": true + }, + { + "fieldPath": "stockExchange", + "columnName": "exchange", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "currency", + "columnName": "currency", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "isPostMarket", + "columnName": "is_post_market", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "annualDividendRate", + "columnName": "annual_dividend_rate", + "affinity": "REAL", + "notNull": true + }, + { + "fieldPath": "annualDividendYield", + "columnName": "annual_dividend_yield", + "affinity": "REAL", + "notNull": true + }, + { + "fieldPath": "dayHigh", + "columnName": "dayHigh", + "affinity": "REAL", + "notNull": false + }, + { + "fieldPath": "dayLow", + "columnName": "dayLow", + "affinity": "REAL", + "notNull": false + }, + { + "fieldPath": "previousClose", + "columnName": "previousClose", + "affinity": "REAL", + "notNull": true + }, + { + "fieldPath": "open", + "columnName": "open", + "affinity": "REAL", + "notNull": false + }, + { + "fieldPath": "regularMarketVolume", + "columnName": "regularMarketVolume", + "affinity": "REAL", + "notNull": false + }, + { + "fieldPath": "peRatio", + "columnName": "peRatio", + "affinity": "REAL", + "notNull": false + }, + { + "fieldPath": "fiftyTwoWeekLowChange", + "columnName": "fiftyTwoWeekLowChange", + "affinity": "REAL", + "notNull": false + }, + { + "fieldPath": "fiftyTwoWeekLowChangePercent", + "columnName": "fiftyTwoWeekLowChangePercent", + "affinity": "REAL", + "notNull": false + }, + { + "fieldPath": "fiftyTwoWeekHighChange", + "columnName": "fiftyTwoWeekHighChange", + "affinity": "REAL", + "notNull": false + }, + { + "fieldPath": "fiftyTwoWeekHighChangePercent", + "columnName": "fiftyTwoWeekHighChangePercent", + "affinity": "REAL", + "notNull": false + }, + { + "fieldPath": "fiftyTwoWeekLow", + "columnName": "fiftyTwoWeekLow", + "affinity": "REAL", + "notNull": false + }, + { + "fieldPath": "fiftyTwoWeekHigh", + "columnName": "fiftyTwoWeekHigh", + "affinity": "REAL", + "notNull": false + }, + { + "fieldPath": "dividendDate", + "columnName": "dividendDate", + "affinity": "REAL", + "notNull": false + }, + { + "fieldPath": "earningsDate", + "columnName": "earningsDate", + "affinity": "REAL", + "notNull": false + }, + { + "fieldPath": "marketCap", + "columnName": "marketCap", + "affinity": "REAL", + "notNull": false + } + ], + "primaryKey": { + "columnNames": [ + "symbol" + ], + "autoGenerate": false + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "HoldingRow", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `quote_symbol` TEXT NOT NULL, `shares` REAL NOT NULL, `price` REAL NOT NULL)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": false + }, + { + "fieldPath": "quoteSymbol", + "columnName": "quote_symbol", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "shares", + "columnName": "shares", + "affinity": "REAL", + "notNull": true + }, + { + "fieldPath": "price", + "columnName": "price", + "affinity": "REAL", + "notNull": true + } + ], + "primaryKey": { + "columnNames": [ + "id" + ], + "autoGenerate": true + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "PropertiesRow", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `properties_quote_symbol` TEXT NOT NULL, `notes` TEXT NOT NULL, `alert_above` REAL NOT NULL, `alert_below` REAL NOT NULL)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": false + }, + { + "fieldPath": "quoteSymbol", + "columnName": "properties_quote_symbol", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "notes", + "columnName": "notes", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "alertAbove", + "columnName": "alert_above", + "affinity": "REAL", + "notNull": true + }, + { + "fieldPath": "alertBelow", + "columnName": "alert_below", + "affinity": "REAL", + "notNull": true + } + ], + "primaryKey": { + "columnNames": [ + "id" + ], + "autoGenerate": true + }, + "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, 'ded8ebb6cda3d9b4d96eb448091cd1b3')" + ] + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/github/premnirmal/ticker/components/AppModule.kt b/app/src/main/kotlin/com/github/premnirmal/ticker/components/AppModule.kt index a354cfb5..a9560df8 100644 --- a/app/src/main/kotlin/com/github/premnirmal/ticker/components/AppModule.kt +++ b/app/src/main/kotlin/com/github/premnirmal/ticker/components/AppModule.kt @@ -15,6 +15,7 @@ import com.github.premnirmal.ticker.repo.QuoteDao import com.github.premnirmal.ticker.repo.QuotesDB import com.github.premnirmal.ticker.repo.StocksStorage import com.github.premnirmal.ticker.repo.migrations.MIGRATION_1_2 +import com.github.premnirmal.ticker.repo.migrations.MIGRATION_2_3 import com.github.premnirmal.ticker.widget.WidgetDataProvider import dagger.Module import dagger.Provides @@ -61,6 +62,7 @@ class AppModule(private val app: StocksApp) { QuotesDB::class.java, "quotes-db" ) .addMigrations(MIGRATION_1_2) + .addMigrations(MIGRATION_2_3) .build() } diff --git a/app/src/main/kotlin/com/github/premnirmal/ticker/debug/DbViewerViewModel.kt b/app/src/main/kotlin/com/github/premnirmal/ticker/debug/DbViewerViewModel.kt index 9c1fca07..7467e108 100644 --- a/app/src/main/kotlin/com/github/premnirmal/ticker/debug/DbViewerViewModel.kt +++ b/app/src/main/kotlin/com/github/premnirmal/ticker/debug/DbViewerViewModel.kt @@ -52,6 +52,23 @@ class DbViewerViewModel(application: Application) : AndroidViewModel(application SymbolNameLast trade price ChangeChange %Exchange CurrencyDividend + annualDividendRate + annualDividendYield + dayHigh + dayLow + previousClose + open + regularMarketVolume + peRatio + fiftyTwoWeekLowChange + fiftyTwoWeekLowChangePercent + fiftyTwoWeekHighChange + fiftyTwoWeekHighChangePercent + fiftyTwoWeekLow + fiftyTwoWeekHigh + dividendDate + earningsDate + marketCap """ ) @@ -112,6 +129,23 @@ class DbViewerViewModel(application: Application) : AndroidViewModel(application "" } ) + .append("${quote.annualDividendRate}") + .append("${quote.annualDividendYield}") + .append("${quote.dayHigh}") + .append("${quote.dayLow}") + .append("${quote.previousClose}") + .append("${quote.open}") + .append("${quote.regularMarketVolume}") + .append("${quote.peRatio}") + .append("${quote.fiftyTwoWeekLowChange}") + .append("${quote.fiftyTwoWeekLowChangePercent}") + .append("${quote.fiftyTwoWeekHighChange}") + .append("${quote.fiftyTwoWeekHighChangePercent}") + .append("${quote.fiftyTwoWeekLow}") + .append("${quote.fiftyTwoWeekHigh}") + .append("${quote.dividendDate}") + .append("${quote.earningsDate}") + .append("${quote.marketCap}") .append("") val holdings = it.holdings diff --git a/app/src/main/kotlin/com/github/premnirmal/ticker/repo/QuotesDB.kt b/app/src/main/kotlin/com/github/premnirmal/ticker/repo/QuotesDB.kt index 5f66c5cc..f8159b84 100644 --- a/app/src/main/kotlin/com/github/premnirmal/ticker/repo/QuotesDB.kt +++ b/app/src/main/kotlin/com/github/premnirmal/ticker/repo/QuotesDB.kt @@ -7,7 +7,8 @@ import com.github.premnirmal.ticker.repo.data.PropertiesRow import com.github.premnirmal.ticker.repo.data.QuoteRow @Database( - entities = [QuoteRow::class, HoldingRow::class, PropertiesRow::class], version = 2, + entities = [QuoteRow::class, HoldingRow::class, PropertiesRow::class], + version = 3, exportSchema = true ) abstract class QuotesDB : RoomDatabase() { diff --git a/app/src/main/kotlin/com/github/premnirmal/ticker/repo/StocksStorage.kt b/app/src/main/kotlin/com/github/premnirmal/ticker/repo/StocksStorage.kt index 60992502..cadad330 100644 --- a/app/src/main/kotlin/com/github/premnirmal/ticker/repo/StocksStorage.kt +++ b/app/src/main/kotlin/com/github/premnirmal/ticker/repo/StocksStorage.kt @@ -126,7 +126,15 @@ class StocksStorage { return QuoteRow( this.symbol, this.name, this.lastTradePrice, this.changeInPercent, this.change, this.stockExchange, this.currencyCode, - this.isPostMarket, this.annualDividendRate, this.annualDividendYield + this.isPostMarket, this.annualDividendRate, this.annualDividendYield, + this.dayHigh, this.dayLow, this.previousClose, this.open, + this.regularMarketVolume?.toFloat(), this.trailingPE, + this.fiftyTwoWeekLowChange, this.fiftyTwoWeekLowChangePercent, + this.fiftyTwoWeekHighChange, this.fiftyTwoWeekHighChangePercent, + this.fiftyTwoWeekLow, this.fiftyTwoWeekHigh, + this.dividendDate?.toFloat(), + this.earningsTimestamp?.toFloat(), + this.marketCap?.toFloat() ) } @@ -151,7 +159,21 @@ class StocksStorage { quote.isPostMarket = this.isPostMarket quote.annualDividendRate = this.annualDividendRate quote.annualDividendYield = this.annualDividendYield - + quote.dayHigh = this.dayHigh + quote.dayLow = this.dayLow + quote.previousClose = this.previousClose + quote.open = this.open + quote.regularMarketVolume = this.regularMarketVolume?.toLong() + quote.trailingPE = this.peRatio + quote.fiftyTwoWeekLowChange = this.fiftyTwoWeekLowChange + quote.fiftyTwoWeekLowChangePercent = this.fiftyTwoWeekLowChangePercent + quote.fiftyTwoWeekHighChange = this.fiftyTwoWeekHighChange + quote.fiftyTwoWeekHighChangePercent = this.fiftyTwoWeekHighChangePercent + quote.fiftyTwoWeekLow = this.fiftyTwoWeekLow + quote.fiftyTwoWeekHigh = this.fiftyTwoWeekHigh + quote.dividendDate = this.dividendDate?.toLong() + quote.earningsTimestamp = this.earningsDate?.toLong() + quote.marketCap = this.marketCap?.toLong() return quote } diff --git a/app/src/main/kotlin/com/github/premnirmal/ticker/repo/data/QuoteRow.kt b/app/src/main/kotlin/com/github/premnirmal/ticker/repo/data/QuoteRow.kt index 68b0bc60..5a66f916 100644 --- a/app/src/main/kotlin/com/github/premnirmal/ticker/repo/data/QuoteRow.kt +++ b/app/src/main/kotlin/com/github/premnirmal/ticker/repo/data/QuoteRow.kt @@ -15,5 +15,20 @@ data class QuoteRow( @ColumnInfo(name = "currency") val currency: String, @ColumnInfo(name = "is_post_market") val isPostMarket: Boolean, @ColumnInfo(name = "annual_dividend_rate") val annualDividendRate: Float, - @ColumnInfo(name = "annual_dividend_yield") val annualDividendYield: Float + @ColumnInfo(name = "annual_dividend_yield") val annualDividendYield: Float, + @ColumnInfo(name = "dayHigh") val dayHigh: Float?, + @ColumnInfo(name = "dayLow") val dayLow: Float?, + @ColumnInfo(name = "previousClose") val previousClose: Float, + @ColumnInfo(name = "open") val open: Float?, + @ColumnInfo(name = "regularMarketVolume") val regularMarketVolume: Float?, + @ColumnInfo(name = "peRatio") val peRatio: Float?, + @ColumnInfo(name = "fiftyTwoWeekLowChange") val fiftyTwoWeekLowChange: Float?, + @ColumnInfo(name = "fiftyTwoWeekLowChangePercent") val fiftyTwoWeekLowChangePercent: Float?, + @ColumnInfo(name = "fiftyTwoWeekHighChange") val fiftyTwoWeekHighChange: Float?, + @ColumnInfo(name = "fiftyTwoWeekHighChangePercent") val fiftyTwoWeekHighChangePercent: Float?, + @ColumnInfo(name = "fiftyTwoWeekLow") val fiftyTwoWeekLow: Float?, + @ColumnInfo(name = "fiftyTwoWeekHigh") val fiftyTwoWeekHigh: Float?, + @ColumnInfo(name = "dividendDate") val dividendDate: Float?, + @ColumnInfo(name = "earningsDate") val earningsDate: Float?, + @ColumnInfo(name = "marketCap") val marketCap: Float? ) diff --git a/app/src/main/kotlin/com/github/premnirmal/ticker/repo/migrations/Migrations.kt b/app/src/main/kotlin/com/github/premnirmal/ticker/repo/migrations/Migrations.kt index 6b8a9a88..e9db03e0 100644 --- a/app/src/main/kotlin/com/github/premnirmal/ticker/repo/migrations/Migrations.kt +++ b/app/src/main/kotlin/com/github/premnirmal/ticker/repo/migrations/Migrations.kt @@ -3,10 +3,12 @@ package com.github.premnirmal.ticker.repo.migrations import androidx.room.migration.Migration import androidx.sqlite.db.SupportSQLiteDatabase -// Migration from version 1 of the database to version 2: -// Delete field 'description' in table QuoteRow. -// Add fields 'is_post_market,annual_dividend_rate,annual_dividend_yield' in table QuoteRow. -// Add table PropertiesRow. +/** + * Migration from version 1 of the database to version 2: + * Delete field 'description' in table QuoteRow. + * Add fields 'is_post_market,annual_dividend_rate,annual_dividend_yield' in table QuoteRow. + * Add table PropertiesRow. + */ val MIGRATION_1_2 = object : Migration(1, 2) { override fun migrate(database: SupportSQLiteDatabase) { val TABLE_NAME = "QuoteRow" @@ -51,4 +53,25 @@ val MIGRATION_1_2 = object : Migration(1, 2) { "CREATE TABLE IF NOT EXISTS `PropertiesRow` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `properties_quote_symbol` TEXT NOT NULL, `notes` TEXT NOT NULL, `alert_above` REAL NOT NULL, `alert_below` REAL NOT NULL)" ) } +} + +val MIGRATION_2_3 = object : Migration(2, 3) { + override fun migrate(database: SupportSQLiteDatabase) { + val tableName = "QuoteRow" + database.execSQL("ALTER TABLE `$tableName` ADD COLUMN `dayHigh` REAL;") + database.execSQL("ALTER TABLE `$tableName` ADD COLUMN `dayLow` REAL;") + database.execSQL("ALTER TABLE `$tableName` ADD COLUMN `previousClose` REAL NOT NULL DEFAULT 0.0;") + database.execSQL("ALTER TABLE `$tableName` ADD COLUMN `open` REAL;") + database.execSQL("ALTER TABLE `$tableName` ADD COLUMN `regularMarketVolume` REAL;") + database.execSQL("ALTER TABLE `$tableName` ADD COLUMN `peRatio` REAL;") + database.execSQL("ALTER TABLE `$tableName` ADD COLUMN `fiftyTwoWeekLowChange` REAL;") + database.execSQL("ALTER TABLE `$tableName` ADD COLUMN `fiftyTwoWeekLowChangePercent` REAL;") + database.execSQL("ALTER TABLE `$tableName` ADD COLUMN `fiftyTwoWeekHighChange` REAL;") + database.execSQL("ALTER TABLE `$tableName` ADD COLUMN `fiftyTwoWeekHighChangePercent` REAL;") + database.execSQL("ALTER TABLE `$tableName` ADD COLUMN `fiftyTwoWeekLow` REAL;") + database.execSQL("ALTER TABLE `$tableName` ADD COLUMN `fiftyTwoWeekHigh` REAL;") + database.execSQL("ALTER TABLE `$tableName` ADD COLUMN `dividendDate` REAL;") + database.execSQL("ALTER TABLE `$tableName` ADD COLUMN `earningsDate` REAL;") + database.execSQL("ALTER TABLE `$tableName` ADD COLUMN `marketCap` REAL;") + } } \ No newline at end of file diff --git a/app/src/main/res/layout-sw600dp-land/activity_quote_detail.xml b/app/src/main/res/layout-sw600dp-land/activity_quote_detail.xml index 2d377adb..064d6ef3 100644 --- a/app/src/main/res/layout-sw600dp-land/activity_quote_detail.xml +++ b/app/src/main/res/layout-sw600dp-land/activity_quote_detail.xml @@ -69,6 +69,7 @@ android:orientation="horizontal"/> @@ -106,7 +108,8 @@ app:layout_constraintTop_toBottomOf="@id/price" app:layout_constraintBottom_toTopOf="@id/expand_graph" app:layout_constraintHorizontal_chainStyle="packed" - android:layout_margin="@dimen/quote_details_margin" + android:layout_margin="4dp" + android:layout_marginTop="@dimen/quote_details_margin" android:gravity="center" android:padding="@dimen/quote_details_padding" tools:text="+2.63" diff --git a/app/src/main/res/layout/activity_quote_detail.xml b/app/src/main/res/layout/activity_quote_detail.xml index fbaaf306..4ebb0e22 100644 --- a/app/src/main/res/layout/activity_quote_detail.xml +++ b/app/src/main/res/layout/activity_quote_detail.xml @@ -49,6 +49,7 @@ android:orientation="horizontal"/> @@ -86,7 +88,8 @@ app:layout_constraintTop_toBottomOf="@id/price" app:layout_constraintBottom_toTopOf="@id/expand_graph" app:layout_constraintHorizontal_chainStyle="packed" - android:layout_margin="@dimen/quote_details_margin" + android:layout_margin="4dp" + android:layout_marginTop="@dimen/quote_details_margin" android:gravity="center" android:padding="@dimen/quote_details_padding" tools:text="+2.63" diff --git a/app/src/main/res/values-v21/styles.xml b/app/src/main/res/values-v21/styles.xml index 1eb79333..68674418 100644 --- a/app/src/main/res/values-v21/styles.xml +++ b/app/src/main/res/values-v21/styles.xml @@ -38,6 +38,10 @@ @color/default_bg + +