How to Generate SQL Query for Room Database Migration with new table

Ngima Sherpa
2 min readOct 2, 2021

“At the source of every error which is blamed on the computer, you will find at least two human errors, one of which is the error of blaming it on the computer.” — Tom Gilb

Suppose you want to add new table/entity to your existing room database. You need to create a Migration and add it to your Room Database. It would be simple to create sqlQueries for an Entity with small number of parameters. But if the Entity is more complex then it would be more error-prone. So I was searching for the solution to generate the sql queries from entity. So I have found a simple solution for that:

STEP 1. Add Schema Export Config in your module build.gradle file

STEP 2. Add your Entity

Add it to your database

STEP 3. Build the project, now it should exports the schemas in app/schemas folder.

STEP 4. Copy createSql query and define your Migration

Replace ${TABLE_NAME} with actual table name, in our case tests

Now you could add your MIGRATION in to the database.

That’s it. Find an example project in Github

--

--

Ngima Sherpa
Ngima Sherpa

Written by Ngima Sherpa

Mobile Developer | UI Designer

No responses yet