Hello geeks, In this blog we learn about Database Migration. Also, we will talk about how we can integrate Flyway (A database migration tool) with our Scala application.
What is Database migration?
We all might be aware that Git is a version control system, and we use it to keep track of changes in application development. Likewise, we have some database migration tools that we can use in database migration.
Database migration is the process of making and applying changes to the database schema. The database schemas need to be changed and updated as the application grows. Database migration helps us change or update the database schemas without impacting the existing data or functionality.
What is Flyway?
Flyway is an open-source database migration tool that helps us to manage the database migration process. It is actually helpful in projects where changes in the database need to be tracked.
- Migration Scripts: We write migration scripts in SQL or other supported scripting language to make changes in the database schema. These scripts are organized and executed in a specific order to ensure that changes are applied correctly and consistently.
- Migration History: Flyway maintains a history table to keep the record of which migration scripts have been applied to each database instance.
Integrating Flyway to your Scala Application
We can follow the following steps to integrate the Flyway into your Scala Application –
Method 1
- Step 1 – Add the dependency to build.sbt file.
libraryDependencies += "org.flywaydb" % "flyway-core" % "7.0.0"You will need the database credentials (URL, username, and password) for configuring the Flyway. I’m using scalalikejdb to get the connection with the database.
You can the following dependency for using scalalikejdbc –
libraryDependencies +="org.scalikejdbc" %% "scalikejdbc" % "4.2.1"Put the following code in Connection.scala object –
import com.typesafe.config.ConfigFactory import models.DbConfig import scalikejdbc._ class Connection { // Load configurations from application.conf private val config = ConfigFactory.load() // Database configuration keys private val dbConfigKey = "db.default" private val driverKey = s"$dbConfigKey.driver" private val urlKey = s"$dbConfigKey.url" private val userKey = s"$dbConfigKey.user" private val passwordKey = s"$dbConfigKey.password" // Extracting database configurations private val driver = config.getString(driverKey) private val url = config.getString(urlKey) private val user = config.getString(userKey) private val password = config.getString(passwordKey) // Initialize database connection pool Class.forName(driver) ConnectionPool.singleton(url, user, password) // Provide DB session for executing queries val session: DBSession = AutoSession val dbConfig: DbConfig = DbConfig(driver, url, user, password) }After adding the dependency, use the above code to get the DB connection.
Note – I have a case class ‘DbConfig’ for passing out the required credentials for Flyway configuration. - Step 2 – Put your scripts under the resource/db.migration.
You can go through the RedGate website for the right naming conventions of the scripts.
Then you can write your script as –
CREATE TABLE users( user_id SERIAL PRIMARY KEY, user_name VARCHAR(20) ) - Step 3 – Now, configure the Flyway in your driver object by using the following code –
object Driver extends App { val dbConnection = new Connection val dbConfig = dbConnection.dbConfig private val flywayMigration = new Flyway(DbConfig(dbConfig.driver, dbConfig.url, dbConfig.user, dbConfig.pass)) flywayMigration.migrateDatabase() }Then run the Driver object and you are done.
In the above method, we saw how we can Integrate Flyway by using library dependency, in build.sbt by importing the appropriate libraries inside code in the app.
In the next method, we will see how we can do the same thing with the help of sbt-plugin in plugins.sbt.
Method 2
- Step 1 – Set up the DB connection as I have mentioned in Method 1 -> Step 1.
- Step 2 – Add the following plugin in the project/plugins.sbt file.
addSbtPlugin("io.github.davidmweber" % "flyway-sbt" % "7.4.0")Load your sbt changes by clicking on the Load sbt Changes button or just use Ctrl+shift+O.
- Step 3 – Follow Step 2 from method 1.
- Step 3 – Put the following codes in build.sbt file.
enablePlugins(FlywayPlugin) flywayDriver := "org.postgresql.Driver" flywayUrl := "your_db_url" flywayUser := "your_db_user" flywayPassword := "your_password"
- Step 5 – After setting up all the above things run the following command on the terminal.
sbt flywayClean sbt flywayMigrate
And your migration is done.
click on Tech-Hub to take the reference, where I used the second method for Flyway integration. You take reference from there as well.
Then you can write your script as –