Please enable JavaScript.
Coggle requires JavaScript to display documents.
ROOM / SQLITE DB, image - Coggle Diagram
ROOM / SQLITE DB
MIGRATIONS
REF
https://stackoverflow.com/a/66982403/10010404
https://medium.com/androiddevelopers/understanding-migrations-with-room-f01e04b07929
https://developer.android.com/training/data-storage/room/migrating-db-versions
RULE
CHANGE DATABASE SCHEMA
DON'T UPDATE VERSION
APP CRASH :skull:
UPGRADE VERSION, NO MIGRATIONS
APP CRASH
DB TABLES ARE DROPPED
USERS LOVE DATA
SQLite
FIRST TRY UPGRADE VERSIONS
BEFORE OPEN DATABASE
SQLiteOpenHelper.onUpgrade
identity hash String
SCENERIOS
version increased, fallback to destructive migration enabled — database is cleared
.fallbackToDestructiveMigration() :star:
NEED UPDATE VERSION (OTHERWISE WILL CRASH) :warning:
OR APP CRASH DUE TO CHANGE DATA TYPE
EVERY TIME THRE IS UPDATE IN DB SCHEMA
DATABASE IS CLEARED AND REPLACED
ALL PREVIOUS DB DATA IS LOST
APP WON'T CRASH :check:
discussions
https://stackoverflow.com/a/59044728/10010404
(DENY and my app crashes due to change:
https://stackoverflow.com/a/45967940/10010404
)
IDEAL FOR MOST APPS
WITH INTERNET REQUEST TO FETCH ALL DATA AND UPSERT IN DB
version increased, migration provided — data is kept
.addMigrations()
Migration with simple schema changes :star:
MULTIPLE DATABASE VERSION INCREMENTS
Automated Migrations
ROOM AUTO-MIGRATION
REF
https://medium.com/androiddevelopers/room-auto-migrations-d5370b0ca6eb
UPDATE MIGRATION WHENEVER THERE IS A CHANGE
exportSchema = true is required :warning:
REQUIRES SET ANNOTATION
NOT WORKING :red_cross:
NOT FINDING PATH
SOLUTION?
https://developer.android.com/training/data-storage/room/migrating-db-versions#set_schema_location_using_annotation_processor_option
MORE COMPLEX DATA STRUCTURE
AutoMigrationSpec
EXAMPLES DELETE AND RENAME COLUMNS
COMBINATION AND PRIORITY BETWEEN MIGRATION AND AUTO MIGRATION
version 2.4.0-alpha01 and highe
Migrate Room to Kotlin Multiplaform
INFO
APP INSPECTION
YOU CAN SEEE THE ROOM DB
CONNECT APP TO EMULATOR
USES SQLITE
IN-MEMORY
SERVERLESS RELATIONAL DB
NO INSTALL
ONLY 500KB
FILE-BASED
MOST USED DB IN THE WORLD
SQLite was written in the ANSI-C language
LIMIT
2000 COLUMNS
many experienced database designers will argue that a well-normalized database will never need more than 100 columns in a table.
SQLITE PRODS TIPS
VALIDATION SQL
Room validates SQL queries at compile time. This means that if there's a problem with your query, a compilation error occurs instead of a runtime failure.
COMPILE-TIME
NO DATABASE ACCESS MAIN THREAD
To prevent queries from blocking the UI, Room does not allow database access on the main thread
Room doesn't allow object references
DAO (Data Access Object)
Design Pattern / Interface
makes data in db
easier and organized
Entity
Data
Mapping Sqlite Table
Define data using Room entities
Write asynchronous DAO queries
ONE-SHOT WRITE/READ
OBSERVABLE READ
Observable queries in Room have one important limitation: the query reruns whenever any row in the table is updated, whether or not that row is in the result set. You can ensure that the UI is only notified when the actual query results change by applying the distinctUntilChanged()
REFS
https://www.youtube.com/watch?v=bOd3wO0uFr8
https://developer.android.com/reference/androidx/room/package-summary
JETPACK ROOM OFFICIAL
Prepopulate Room DB
app to start with a database that is already loaded with a specific set of data.
createFromAsset()
createFromFile()
TYPES
COMPLEX DATA
TYPE CONVERTERS
ALL SUPPORT DATA
NULL , INTEGER , REAL , TEXT , BLOB . This translates to null, Int , Long , Float , Double , String and ByteArray types in Kotlin
POJO
To reference multiple entities at the same time using Room, you instead create a POJO that contains each entity, then write a query that joins the corresponding tables.
Plain Old Java Object
STORAGE
https://litestream.io/
INSTALL
ROOM ANNOTATIONS