Coder Social home page Coder Social logo

roomigrant's Introduction

Roomigrant

Release

Roomigrant is a helper library to automatically generate Android Room library migrations using compile-time code generation.

Add to your project

To get a Git project into your build:

Step 1. Add JitPack in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

Step 2. Add actual Roomigrant library and compiler (preferrably after Room dependencies):

dependencies {
    // room
    implementation 'android.arch.persistence.room:runtime:1.1.1'
    kapt 'android.arch.persistence.room:compiler:1.1.1'
    // roomigrant
    implementation 'com.github.MatrixDev.Roomigrant:RoomigrantLib:0.1.1'
    kapt 'com.github.MatrixDev.Roomigrant:RoomigrantCompiler:0.1.1'
}

More info can be found at https://jitpack.io/#MatrixDev/Roomigrant

How does it work?

Roomigrant uses scheme files generated by Room library and generates migrations base on difference between them. This means that Room schem generation must be enabled in build.gradle file:

android {
    defaultConfig {
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
            }
        }
    }
}

After that actual database class should be annotated to enable migrations generator:

@Database(version = 5, entities = [Object1Dbo::class, Object2Dbo::class])
@GenerateRoomMigrations
abstract class Database : RoomDatabase() {
    // ...
}

And finally migrations can be added to actual Room database creation:

Room.databaseBuilder(appContext, Database::class.java, "database.sqlite")
		.addMigrations(*Database_Migrations.build())
		.build()

Default rules

Roominator will try its best to migrate everything by itself. It default rules are:

  • columns that have new affinity/type will use SQLite's CAST method
  • null cells became not nullable will take default value for thir's affinity/type
  • new columns will be initialized to default value -- 0 for INTEGER -- 0.0 for REAL -- "" for TEXT or BLOB

Because of SQLite limitations when any change other than adding new column is done:

  • new merge table will be created
  • data copied from original table to merge table
  • original table will be removed
  • merge table will be renamed back to original

Custom rules

Sometimes it will be required to write custom migration rules. It can be done by adding classes to GenerateRoomMigrations annotation:

@Database(version = 5, entities = [Object1Dbo::class, Object2Dbo::class])
@GenerateRoomMigrations(Rules::class)
abstract class Database : RoomDatabase() {
    // ...
}

And after that adding methods annotated with FieldMigrationRule annotation to provided classes:

// version 3 had Object1Dbo.intVal column
// version 4 has Object1Dbo.intValRenamed column
@FieldMigrationRule(version1 = 3, version2 = 4, table = "Object1Dbo", field = "intValRenamed")
fun migrate_3_4_Object1Dbo_intVal(): String {
	return "`Object1Dbo`.`intVal`"
}

Returned value will be injected as-is to final SQL statement when copying/updating that field.

Custom code can also be invoked before and after each migration:

@OnMigrationStartRule(version1 = 1, version2 = 2)
fun migrate_1_2_before(db: SupportSQLiteDatabase, version1: Int, version2: Int) {
	val cursor = db.query("pragma table_info(Object1Dbo)")
	assert(cursor.count == 1)
}

@OnMigrationEndRule(version1 = 1, version2 = 2)
fun migrate_1_2_after(db: SupportSQLiteDatabase, version1: Int, version2: Int) {
	val cursor = db.query("pragma table_info(Object1Dbo)")
	assert(cursor.count == 3)
}

Todos

  • Add table and column names escaping
  • Add foreign key support (currently they are completely ignored)
  • Some internal optimizations

License

MIT License

Copyright (c) 2018 Rostyslav Lesovyi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

roomigrant's People

Contributors

matrixdev avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.