Quick and Easy Migration To Room

Yvonne Price
Nov 27, 2017 · 4 min read

Migrating Android apps to Room is just a couple of clicks away

Room is a new library that provides an abstraction layer on top of SQLite. It makes interactions with databases simpler, less error-prone by reducing the amount of code we write, in addition to checking our SQL statements at compile-time. Room was first introduced at Google I/O 2017. It reached production-ready status in November 2017.

Migrating our existing Android apps to Room is possible with a few manual steps:

  1. Add Room libraries as project dependencies
  2. Create an Entity class for each table in the SQLite database
  3. Create Data Access Objects (DAOs) for each Entity class
  4. Create a Migration class
  5. Create the Room Database class
  6. (Optional) Create necessary Type Converters

(For more details about manual migration to Room, please read “7 Steps to Room”, by Florina Muntenescu).

Wouldn’t it be convenient and efficient to generate Room classes from our existing database schemas automatically? …

Enter SQLScout

SQLScout is a plugin for Android Studio and IntelliJ IDEA that provides support for SQLite databases, including:

  • The ability to connect to databases in Android devices and the file system
  • A database schema explorer that displays the structure of databases
  • A SQL editor that provides all the features you’d expect from an IDE (syntax highlighting, code completion, reference navigation and refactoring) and the ability to execute SQL statements
  • A database console to view query results, edit table data, and export data to different formats (including Excel)
  • Database diagrams
Image for post
Image for post
Figure 1. SQLScout’s seamless integration with Android Studio

Introducing SQLScout 3.2.0!

This version of SQLScout is dedicated to provide support for generating Room Entity, DAO and Database classes from existing database schemas.

Automating Migration to Room — With SQLScout

With 3 clicks as shown in figure 2, SQLScout’s “Room Migration” wizard will handle the heavy-lifting, manual work for you.

Image for post
Image for post
Figure 2. SQLScout’s “Room Migration” wizard

After clicking the “Finish” button in the wizard, SQLScout will automatically:

  1. Add the Google Maven repository to your project, if needed
  2. Add the Room libraries as dependencies, if needed
  3. Generate Entity, DAO, Migration and Database classes from your existing database schema (the generated DAOs contain basic CRUD operations, which is a pretty good start)
  4. Generate a TypeConverter for java.util.Date, if needed
  5. Generate a detailed report explaining the changes made to your project and the classes that were generated

SQLScout generates 100% Kotlin code if your app is already using Kotlin.

Image for post
Image for post
Figure 3. SQLScout’s code generation in action

A Closer Look at the Generated Code

SQLScout generates code using the IDE’s own mechanism for generating Java and Kotlin code: code templates (for new classes and interfaces), and PSI (for fields, methods and functions), ensuring that the generated code follows your own coding style and preferences.

Image for post
Image for post
Figure 4. Room code generated by SQLScout

The followings are some examples of SQLScout’s generated code:

More Room to Go

SQLScout’s Room Migration feature is just the beginning. We are adding more support for Room-centric features.

For example, in the next version of SQLScout (3.2.1) you will be able to execute the SQL statements inside @Query annotations in any DAO class, just by clicking an action in the editor gutter (both Java and Kotlin are supported!)

Update (12/12/2017): SQLScout 3.3.0 includes this feature (and more!)

Image for post
Image for post
Figure 5. Executing a SQL statement inside a @Query annotation

By now, I hope this article has got you excited about SQLScout’s Room Migration feature.

Please share your experience with us at support at idescout.com. We’d like to hear from you.

Because migration to Room is a one-time deal, this feature is not available in SQLScout’s trail version. The rest of the functionalities are identical in both trail and paid versions.

Happy coding!

(Follow us on Google+ and Twitter)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store