<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Satyajeet Mohalkar on Medium]]></title>
        <description><![CDATA[Stories by Satyajeet Mohalkar on Medium]]></description>
        <link>https://medium.com/@satyajeetmohalkar?source=rss-2e1557e7d10a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*9EVpKicaLx6H50nj</url>
            <title>Stories by Satyajeet Mohalkar on Medium</title>
            <link>https://medium.com/@satyajeetmohalkar?source=rss-2e1557e7d10a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 17:35:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@satyajeetmohalkar/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Pre-populating your app database using room using JSON file]]></title>
            <link>https://medium.com/@satyajeetmohalkar/pre-populating-your-app-database-using-room-using-json-file-abbc95140cc3?source=rss-2e1557e7d10a------2</link>
            <guid isPermaLink="false">https://medium.com/p/abbc95140cc3</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[room]]></category>
            <dc:creator><![CDATA[Satyajeet Mohalkar]]></dc:creator>
            <pubDate>Wed, 23 Nov 2022 17:13:18 GMT</pubDate>
            <atom:updated>2022-11-26T16:30:35.916Z</atom:updated>
            <content:encoded><![CDATA[<h3>Pre-populating your app database using Room and JSON file</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1TUze5rsKZSdSjVKJYCDwA.png" /><figcaption>src = <a href="https://blog.cloudflare.com/introducing-d1/">https://blog.cloudflare.com/introducing-d1/</a></figcaption></figure><p>In modern mobile apps, it is a common use case where we need some data to be stored locally. In this article , we will see how can we create room database with pre-populated data.</p><h3>Project setup : -</h3><p>let’s dive into simple application where we are showing list of users. Each item shows an ID and User Name.</p><p><strong>1.Entity class :-<br></strong>We are creating a User entity class which has non nullable fields userId and userName.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/faf99b4245b532f23c4941242ac828c3/href">https://medium.com/media/faf99b4245b532f23c4941242ac828c3/href</a></iframe><p><strong>2.Write queries for users :-<br></strong>We will write basic insert and select query to add and get a list of users.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0798d81d335759e37c6f7f9becaedd1a/href">https://medium.com/media/0798d81d335759e37c6f7f9becaedd1a/href</a></iframe><p><strong>3.Setup room database class :-</strong><br>create a user database class which will provide us the database instance.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/edc4c47f38044a95f64ad4f7ff11a17c/href">https://medium.com/media/edc4c47f38044a95f64ad4f7ff11a17c/href</a></iframe><p><strong>4.Write a mechanism to pre-populate data :-<br> </strong>After doing initial setup, now we will write logic to insert data into user database as soon as our app creates database for the first time. We have to create a class which extends<em> </em>RoomDatabase.Callback() . We will get a callback in onCreate() method when database is created. We will add logic to insert data in this method. We will use the context to get database instance in next steps.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/68d3e533e71843f86190df6d4942b965/href">https://medium.com/media/68d3e533e71843f86190df6d4942b965/href</a></iframe><p>Here we are pre-populating user database using a json file. The json file contains a list of objects which represents id and user name.</p><pre>//User Json file<br>[<br>  {<br>    &quot;userId&quot;:1,<br>    &quot;userName&quot;:&quot;Aditya &quot;<br>  },<br>  {<br>    &quot;userId&quot;:2,<br>    &quot;userName&quot;:&quot;Vijay&quot;<br>  },<br>  {<br>    &quot;userId&quot;:3,<br>    &quot;userName&quot;:&quot;Sham&quot;<br>  }<br><br>]</pre><p>We have to add this file under res/raw directory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/340/1*G5qKitpTrT-xT-eTmVmJlg.png" /><figcaption>user.json under res/raw folder</figcaption></figure><p><strong>5. Insert data into database : -<br></strong> Firstly we will create a user list from converting the json file into JSONArray. If the user list json array is not empty , we will traverse through all items and insert in one by one into our database.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/01d37499029ebd93c567ac078dc32e4f/href">https://medium.com/media/01d37499029ebd93c567ac078dc32e4f/href</a></iframe><p><strong>6. Add the callback to Room Builder :-</strong><br>While creating database instance, we will have to give the PrepopulateRoomCallback to room builder by calling <strong>addCallback</strong> method on the <strong>Room.databaseBuilder()</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/13e696dcef10146d2e46b2aebbdd0625/href">https://medium.com/media/13e696dcef10146d2e46b2aebbdd0625/href</a></iframe><p>Now once we launch the app, it will have some pre-populated users.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*BUVuRVV6J7lodspjGBikzg.gif" /></figure><p>That’s pretty much all of it.Room provides really easy way to achieve pre-populating data.So let’s take advantage of this library.</p><p><strong>You can find full code here :</strong>-</p><p><a href="https://github.com/satyajeet4047/Prepopulate-Room-Database">GitHub - satyajeet4047/Prepopulate-Room-Database</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=abbc95140cc3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Testing Room DB migrations]]></title>
            <link>https://medium.com/@satyajeetmohalkar/testing-room-db-migrations-94c3678e0a6?source=rss-2e1557e7d10a------2</link>
            <guid isPermaLink="false">https://medium.com/p/94c3678e0a6</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[room-database]]></category>
            <dc:creator><![CDATA[Satyajeet Mohalkar]]></dc:creator>
            <pubDate>Sat, 01 Oct 2022 09:09:23 GMT</pubDate>
            <atom:updated>2022-10-04T05:33:24.373Z</atom:updated>
            <content:encoded><![CDATA[<p>In my <a href="https://medium.com/@satyajeetmohalkar/writing-room-migrations-27695983161">previous post </a>, we saw how to write room database migrations. Making any changes to your database may lead to app crash or user losing the stored data hence it is important to provide correct migrations in such cases. The queries provided are not verified at compile time. Providing wrong queries also lead to crash on app startup. It is very important to verify the queries beforehand.</p><p>In this article , we will deep-dive into unit testing the migrations. Room provides helper classes to simulate the migration by creating dummy database with specific version.</p><h3>How it works?</h3><p>Room Provides testing class called MigrationTestHelper which validates the json schema for the specified versions. Room requires information about database version, database entities, identity hash from room_master_table . Here’s what room does to verify the migration :</p><ol><li>Room Creates a dummy database for given version</li><li>Run provided migrations</li><li>Validate the schema of start version and end version of migration.</li></ol><blockquote><em>Note : Room does not validate the existing data after migration. We need to manually verify it in our tests.</em></blockquote><h4><strong>Let’s setup the tests for migration :</strong></h4><p>1.Add testing dependency.</p><pre><em>//Room Testing<br></em>androidTestImplementation <strong>&quot;androidx.room:room-testing:2.4.3&quot;</strong></pre><p>2.To generate the schema for every version of our database, we need to specify the path to folder in our app/build.gradle file. Room will export the schema into json file at given path at compile time.</p><pre>defaultConfig <strong>{<br>    </strong>     kapt <strong>{<br>          </strong>arguments <strong>{   <br>               </strong>arg(<strong>&quot;room.schemaLocation&quot;</strong>,<br>                   + <strong>&quot;</strong>$projectDir<strong>/schemas&quot;</strong>.toString())<br>        <strong>}<br>    }<br>}</strong></pre><p>3. Add the schema location to source sets.</p><pre>android <strong>{<br>           ....</strong></pre><pre>   sourceSets <strong>{<br>       </strong><em>// Adds exported schema location as test app assets.<br>       </em>androidTest.assets.srcDirs <br>                    += files(<strong>&quot;</strong>$projectDir<strong>/schemas&quot;</strong>.toString())<br>    <strong>}<br>}</strong></pre><p>4. We have completed necessary testing setup. Now let’s write our test class. Room provides MigrationTestHelper to avoid creating / opening and closing the database for every test. This helps us to reduce a lot of boilerplate code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/20a36ed44dffaa9cf074af1841988988/href">https://medium.com/media/20a36ed44dffaa9cf074af1841988988/href</a></iframe><p>5. let’s write test for migration from version 1 to version 2. First we will create a database with version 1 and add some dummy data. Then we will run the migration to version 2 on the database. After that we will check the data integrity.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ad1e1d60e9b96af4312f8fc9b0277dbd/href">https://medium.com/media/ad1e1d60e9b96af4312f8fc9b0277dbd/href</a></iframe><p>6.Now let’s run the test and check the result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AmkIoTkH2XlBXgbPqpcbkA.png" /><figcaption>Test results</figcaption></figure><p><strong><em>Voilà !!! </em></strong>We have successfully verified the migration along with existing user data.</p><p>You can check out my article about writing room migrations here.</p><p><a href="https://medium.com/@satyajeetmohalkar/writing-room-migrations-27695983161">Writing Room Migrations</a></p><p><strong>Please refer to this Github link for complete example</strong> : <br><a href="https://github.com/satyajeet4047/RoomDatabaseExample/blob/main/app/src/androidTest/java/com/example/roomdatabaseexample/database/MigrationTests.kt">https://github.com/satyajeet4047/RoomDatabaseExample/blob/main/app/src/androidTest/java/com/example/roomdatabaseexample/database/MigrationTests.kt</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=94c3678e0a6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Writing Room Migrations]]></title>
            <link>https://medium.com/@satyajeetmohalkar/writing-room-migrations-27695983161?source=rss-2e1557e7d10a------2</link>
            <guid isPermaLink="false">https://medium.com/p/27695983161</guid>
            <category><![CDATA[room-database]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Satyajeet Mohalkar]]></dc:creator>
            <pubDate>Mon, 26 Sep 2022 09:48:58 GMT</pubDate>
            <atom:updated>2022-10-01T12:02:50.700Z</atom:updated>
            <content:encoded><![CDATA[<h3>Writing Room DB Migrations</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*K2nMsalaALhzBCkdycGV5Q.png" /></figure><p>While using Room database to store data locally , we usually come across scenarios where we add a new table or alter, delete existing columns from our database.</p><p>With Room if we don’t handle migrations properly , it could lead to a crash on app startup or users might lose the existing data depending on your database implementation.</p><h4>Scenarios where your app will crash or user might lose the data -</h4><ol><li>You have added , deleted new columns but not increased the database version.</li><li>You have increased the database version but not provided the migration.</li><li>You have increased the version , no migration provided but you have enabled <strong>fallBackToDestructiveMigration() </strong>in your database. This will clear the database on version updates.</li></ol><pre>val<strong> </strong>database = Room.databaseBuilder(context.<em>applicationContext</em>, WordDatabase::class.<em>java</em>, &quot;word_database&quot;)<br>    .<strong>fallBackToDestructiveMigration()</strong><br>    .build()</pre><h4><strong>How does it work internally?</strong></h4><ol><li>Room stores the identity hash of current version in <strong>room_master_table.</strong></li><li>While running the app with new version, room triggers the migration by calling native method onUpgrade().</li><li>In onUpgrade() method, it executes all the migrations provided and validated the schema.</li><li>If the schema is valid , room will create new tables and update the identity hash in <strong>room_master_table.</strong></li><li>If the schema is invalid, room will throw an IllegalStateException as following :</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x5Mu7VFAIFyLMSBOe1c__w.png" /><figcaption>db schema</figcaption></figure><h4>Let’s go through an example :</h4><p>Let’s assume that we have created a local database called <strong>word_database </strong>which contains a table called <strong>word_table.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b84dabd6da4eabfd5776908d868b6703/href">https://medium.com/media/b84dabd6da4eabfd5776908d868b6703/href</a></iframe><p>our Entity class contains an id as primary key column and word as second column in the first version of database.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/90b50e9de22c17f8f97a1b3f33ce706d/href">https://medium.com/media/90b50e9de22c17f8f97a1b3f33ce706d/href</a></iframe><p>Now we want to add a new column to store synonym of the word. let’s write a simple migration to achieve this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3dfa96447f8b0cf8dfe4efc15579ccf3/href">https://medium.com/media/3dfa96447f8b0cf8dfe4efc15579ccf3/href</a></iframe><p>We have added a new column <strong>synonym </strong>in our entity class.<strong> </strong>We have to provide the current version and the version we are migrating to as constructor arguments of <strong>Migration</strong> class. A migration can handle more than 1 version (e.g. we can provide a migration from version 1 to version 3). Inside migrate method we have to write sql queries to create or alter existing tables.</p><p>After writing required migration, we have to increase the database version</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ade3318e8fc487ca5c341e2b61ef564a/href">https://medium.com/media/ade3318e8fc487ca5c341e2b61ef564a/href</a></iframe><p>Now let’s provide the migration to our database by calling <strong>addMigration</strong> on our database.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/12fa640ce4fb00f7dd1c653892b0ff13/href">https://medium.com/media/12fa640ce4fb00f7dd1c653892b0ff13/href</a></iframe><p>here is the database :</p><p><strong>Version 1 :</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E6OmOuHpL0lFhYcsYPLyNg.png" /><figcaption>database snapshot for version 1</figcaption></figure><p><strong>Version 2 :</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4PJBQPz5fzYHlgPWK9CShA.png" /><figcaption>database snapshot for version 2</figcaption></figure><p><strong><em>Voilà !!! </em></strong>Now you have successfully added the new column in your database version 2 without loosing the existing user data.</p><p>Please refer to this Github link for complete example : <a href="https://github.com/satyajeet4047/RoomDatabaseExample">https://github.com/satyajeet4047/RoomDatabaseExample</a></p><p>Checkout part 2 here : <br><a href="https://medium.com/@satyajeetmohalkar/testing-room-db-migrations-94c3678e0a6">https://medium.com/@satyajeetmohalkar/testing-room-db-migrations-94c3678e0a6</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=27695983161" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>