Android schema utils


Source link: https://github.com/futuresimple/android-schema-utils

Android schema utils

Android library for simplifying database schema and migrations management.

Features

The library provides the fluent API, which allows you to define current schema:

@SuppressWarnings("deprecation") private static final Schemas SCHEMA = Schemas.Builder
  .currentSchema(10,

new TableDefinition(Tables.ADDRESSES, ImmutableList.<TableDefinitionOperation>builder()

 .addAll(getCommonColumns())

 .add(

  new AddColumn(Addresses.REGION, "TEXT"),

  new AddColumn(Addresses.ZIP, "TEXT"),

  new AddColumn(Addresses.COUNTRY, "TEXT"),

  new AddColumn(Addresses.CITY, "TEXT"),

  new AddColumn(Addresses.STREET, "TEXT ")

 )

 .build()

),

// more tables
  )

Migrations that have to be performed:

 .upgradeTo(7, recreate(Tables.STATS))

And downgrades that you need to apply to get the previous db schema:

 .downgradeTo(4,

new TableDowngrade(Tables.ADDRESSES,

 new DropColumn(Addresses.REGION)

)
  )
  .downgradeTo(2, dropTable(Tables.ADDRESSES))
  .build();

This might look like a tedious, unnecessary work. In reality it is tedious, but very helpful work. It reduces the usual db.execSQL() boilerplate in onCreate and onUpgrade to this:

@Override public void onCreate(SQLiteDatabase db) {

Schema currentSchema = SCHEMA.getCurrentSchema();

for (String table : currentSchema.getTables()) {

  db.execSQL(currentSchema.getCreateTableStatement(table));

}
 
}
  @Override public void onUpgrade(final SQLiteDatabase db, int oldVersion, int newVersion) {

SCHEMA.upgrade(oldVersion, mContext, db);
 
}

Bulletproof workaround for SQLite's ALTER TABLE deficiencies

SQLite's ALTER TABLE supports only renaming the table or adding a new column. If you want to do anything more fancy like dropping the column, you have to do the following things:

  • Rename table with old schema to some temporary name
  • Create table with new schema
  • Move the data from renamed table to new table
  • Drop the old table

This library provides tested wrapper for this boilerplate with nice API:

TableMigration migration = TableMigration
  .of(Tables.MY_TABLE)
  .withMapping(MyTable.SOME_COLUMN, "NULL")
  .to(CREATE_MY_TABLE)
  .build();

migrationHelper.performMigrations(db, migration);

Warning: the MigrationHelper is not thread-safe (but seriously, why on earth would you want to perform sequential schema migrations in parallel?).

Write only non-trivial migrations

Most of the migrations you perform are trivial: dropping column, adding nullable column, adding table, dropping table, etc. If you specify complete schema history (which you should do anyways), this library will figure out trivial migrations for you. Of course you can still define fully custom migrations or just combine your custom migrations with our automagic:

.upgradeTo(2,
  auto(),
  new SimpleMigration() {

 @Override

 public void apply(SQLiteDatabase db, Schema schema) {

// usual db.execSQL() 

 
}

  
}
 )

Reduce merge conflicts

In your Schemas definition you can include release checkpoints. All revision numbers before this checkpoint are in fact offsets from this revision. It helps a lot when you are merging two branches, which introduced changes to your schema.

You still have to merge the section of code with currentSchema and you still have to make sure that both branches haven't performed the same changes, but you don't have to juggle the revision numbers and in 90% of cases you just need to decide which batch of changes should go first.

Automatic db index creation

Define the relationships between your data models using Thneed and use this information to generate the proper indexes:

for (SQLiteIndex index : AutoIndexer.generateIndexes(MODEL_GRAPH)) {

db.execSQL(AutoIndexer.getCreateStatement(index));
 
}

Note that this will generate the indexes for both ends of the relationships, which might not be exactly what you want. For example it will generate the index for primary keys referenced from other columns. We provide the Predicate factory to filter the generation results:

FluentIterable<SQLiteIndex> indexes = FluentIterable
  .from(AutoIndexer.generateIndexes(MODEL_GRAPH))
  .filter(Predicates.not(isIndexOnColumn(ModelColumns.ID)))
  .filter(Predicates.not(isIndexOnColumn(BaseColumns._ID)));

And if you don't want index generation, you can still use our API to get the create index statement (although, I admit, it's not a killer feature):

AutoIndexer.getCreateStatement(new SQLiteIndex("my_table", "foobar_id"));

Hint: when you're automagically generating the indexes, you want to automagically clean them up as well. Use SQLiteMaster to do this:

SQLiteMaster.dropIndexes(db);

SQLiteMaster

Set of utils for getting existing db schema information from sqlite_master table.

You can use the schema information in your SQLiteOpenHelper's onCreate and onUpgrade to remove some boilerplate code. Compare:

db.execSQL("DROP TRIGGER IF EXISTS trigger_a");
 db.execSQL("DROP TRIGGER IF EXISTS trigger_b");
 db.execSQL("DROP TRIGGER IF EXISTS trigger_c");
 // ... db.execSQL("DROP TRIGGER IF EXISTS trigger_z");

With:

SQLiteMaster.dropTriggers(db);

You can perform similar operations with views, tables and indexes, or you can access the full schema information using getSQLiteSchemaParts(SQLiteDatabase db, SQLiteSchemaPartType partType) or getSQLiteSchemaParts(SQLiteDatabase db), which return the list of SQLiteSchemaPart objects:

public class SQLiteSchemaPart {

public final String name;
public final String sql;
public final String type; 
}

What you do with that information is completely up to you.

Is it safe to use?

Our tests indicate that there are no issues whatsoever on API level 8+ (Android 2.2). We haven't tested earlier versions, so consider yourself warned (and please let us know if you confirm it works on lower API levels!).

Usage

Just add the dependency to your build.gradle:

dependencies {

  compile 'com.getbase.android.schema:library:0.7' 
}

License

Copyright (C) 2013 Jerzy Chalupski  Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.  

Resources

Create awesome image gallery using ScrollGalleryView. Just add your images.

Features:

  • Easy way to select images in gallery (thumbnails)
  • Zooming
  • Thumbnail borders

Android library for creating different tags for your content. Library uses TextView as a parent class. Example usages can be found in app module (folder).

Java library for forismatic.com API. It's a simple way to get the most inspirational quotes.

QuickReaderView allows to show splitted by words text in TextView based view with specified speed.

Simple way to filter your ListView or GridView content.

Easy SharedPreference Engine foR ANDROid.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes