Android Open Source - sqlite-provider Migrations






From Project

Back to project page sqlite-provider.

License

The source code is released under:

Apache License

If you think the Android project sqlite-provider listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package novoda.lib.sqliteprovider.migration;
//from   ww  w .  j  a  v  a  2  s.  c o  m
import static novoda.lib.sqliteprovider.util.Log.Migration.*;

import android.content.res.AssetManager;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import novoda.lib.sqliteprovider.util.Log;
import novoda.lib.sqliteprovider.util.SQLFile;

import java.io.*;
import java.util.*;

public class Migrations {

    private final SortedSet<String> migrations;

    private final int startDate;

    public Migrations() {
        this(-1);
    }

    public Migrations(int startDate) {
        this.startDate = startDate;
        migrations = new TreeSet<String>(comparator);
    }

    public boolean add(String migration) {
        if (shouldInsert(migration)) {
            return migrations.add(migration);
        }
        return false;
    }

    private boolean shouldInsert(String migration) {
        return extractDate(migration) > startDate;
    }

    private int extractDate(String migration) {
        try {
            return Integer.parseInt(migration.split("_", 0)[0]);
        } catch (NumberFormatException e) {
            Log.Migration.e("Invalid int, returning -1.", e);
            return -1;
        }
    }

    public SortedSet<String> getMigrationsFiles() {
        return migrations;
    }

    /**
     * Comparator against filename: <date>_create.sql vs <date2>_create.sql will compare date with date2
     */
    private final Comparator<String> comparator = new Comparator<String>() {
        @Override
        public int compare(String file, String another) {
            return Integer.valueOf(extractDate(file)).compareTo(Integer.valueOf(extractDate(another)));
        }
    };

    public static void migrate(SQLiteDatabase db, AssetManager manager, String assetLocation) throws IOException {

        if (infoLoggingEnabled()) {
            i("current DB version is: " + db.getVersion());
        }

        String[] sqls = manager.list(assetLocation);

        if (sqls.length == 0) {
            w("No SQL file found in asset folder");
            return;
        }

        Migrations migrations = new Migrations(db.getVersion());
        Reader reader;

        for (String sqlfile : sqls) {
            migrations.add(sqlfile);
        }

        for (String sql : migrations.getMigrationsFiles()) {
            reader = new InputStreamReader(manager.open(assetLocation + File.separator + sql, AssetManager.ACCESS_RANDOM));
            if (infoLoggingEnabled()) {
                i("executing SQL file: " + assetLocation + File.separator + sql);
            }
            try {
                db.beginTransaction();
                for (String insert : SQLFile.statementsFrom(reader)) {
                    if (TextUtils.isEmpty(insert.trim())) {
                        continue;
                    }
                    if (infoLoggingEnabled()) {
                        i("executing insert: " + insert);
                    }
                    db.execSQL(insert);
                }
                db.setTransactionSuccessful();

            } catch (SQLException exception) {
                e("error in migrate against file: " + sql, exception);
            } finally {
                db.endTransaction();
            }
        }

        if (migrations.getMigrationsFiles().size() > 0) {
            int v = migrations.extractDate(migrations.getMigrationsFiles().last());
            db.setVersion(v);
            if (infoLoggingEnabled()) {
                i("setting version of DB to: " + v);
            }
        }
    }

    public static int getVersion(AssetManager assets, String migrationsPath) throws IOException {
        int version = 1;
        String[] sqls = assets.list(migrationsPath);
        if (sqls.length == 0) {
            w("You need to add atleast one SQL file in your assets/" + migrationsPath + " folder");
        } else {
            Migrations migrations = new Migrations(-1);
            for (String sqlfile : sqls) {
                migrations.add(sqlfile);
            }
            version = (migrations.extractDate(migrations.getMigrationsFiles().last()));
            if (infoLoggingEnabled()) {
                i("current migration file version is: " + version);
            }
        }
        return version;
    }
}




Java Source Code List

com.novoda.sqliteprovider.demo.NovodaApplication.java
com.novoda.sqliteprovider.demo.domain.Firework.java
com.novoda.sqliteprovider.demo.domain.Groups.java
com.novoda.sqliteprovider.demo.domain.Shop.java
com.novoda.sqliteprovider.demo.domain.UseCaseFactory.java
com.novoda.sqliteprovider.demo.domain.UseCaseInfo.java
com.novoda.sqliteprovider.demo.loader.FireworkLoader.java
com.novoda.sqliteprovider.demo.loader.FireworkSaver.java
com.novoda.sqliteprovider.demo.persistance.DatabaseConstants.java
com.novoda.sqliteprovider.demo.persistance.DatabaseReader.java
com.novoda.sqliteprovider.demo.persistance.DatabaseWriter.java
com.novoda.sqliteprovider.demo.persistance.FireworkReader.java
com.novoda.sqliteprovider.demo.persistance.FireworkWriter.java
com.novoda.sqliteprovider.demo.provider.FireworkProvider.java
com.novoda.sqliteprovider.demo.provider.FireworkUriConstants.java
com.novoda.sqliteprovider.demo.simple.provider.FireworkProvider.java
com.novoda.sqliteprovider.demo.simple.ui.MainActivity.java
com.novoda.sqliteprovider.demo.simple.ui.MainFragment.java
com.novoda.sqliteprovider.demo.ui.AddFireworkActivity.java
com.novoda.sqliteprovider.demo.ui.FindDistinctFireworksActivity.java
com.novoda.sqliteprovider.demo.ui.FindFireworkWithPkActivity.java
com.novoda.sqliteprovider.demo.ui.FindFireworksFromOneShopActivity.java
com.novoda.sqliteprovider.demo.ui.FindThreeFireworksActivity.java
com.novoda.sqliteprovider.demo.ui.FireworkActivity.java
com.novoda.sqliteprovider.demo.ui.MainActivity.java
com.novoda.sqliteprovider.demo.ui.ViewAllFireworksActivity.java
com.novoda.sqliteprovider.demo.ui.ViewShopActivity.java
com.novoda.sqliteprovider.demo.ui.WellStockedShopActivity.java
com.novoda.sqliteprovider.demo.ui.adapter.FireworkAdapter.java
com.novoda.sqliteprovider.demo.ui.base.NovodaActivity.java
com.novoda.sqliteprovider.demo.ui.fragment.AddFireworkFragment.java
com.novoda.sqliteprovider.demo.ui.fragment.FindFireworkWithPkFragment.java
com.novoda.sqliteprovider.demo.ui.fragment.FindFireworksFromOneShopFragment.java
com.novoda.sqliteprovider.demo.ui.fragment.ShopFragment.java
com.novoda.sqliteprovider.demo.ui.fragment.UriSqlFragment.java
com.novoda.sqliteprovider.demo.ui.input.DemoMenu.java
com.novoda.sqliteprovider.demo.ui.input.OnPrimaryKeyInputError.java
com.novoda.sqliteprovider.demo.ui.util.FromXML.java
com.novoda.sqliteprovider.demo.ui.widget.UriSqlView.java
com.novoda.sqliteprovider.demo.util.Log.java
novoda.lib.sqliteprovider.ContentProviderTest.java
novoda.lib.sqliteprovider.cursor.EmptyCursor.java
novoda.lib.sqliteprovider.migration.Migrations.java
novoda.lib.sqliteprovider.provider.ImplLogger.java
novoda.lib.sqliteprovider.provider.LoggedContentProvider.java
novoda.lib.sqliteprovider.provider.SQLiteContentProviderImpl.java
novoda.lib.sqliteprovider.provider.SQLiteContentProvider.java
novoda.lib.sqliteprovider.provider.action.InsertHelper.java
novoda.lib.sqliteprovider.sqlite.AssetHelperTest.java
novoda.lib.sqliteprovider.sqlite.ExtendedSQLiteOpenHelperTest.java
novoda.lib.sqliteprovider.sqlite.ExtendedSQLiteOpenHelper.java
novoda.lib.sqliteprovider.sqlite.ExtendedSQLiteQueryBuilder.java
novoda.lib.sqliteprovider.sqlite.IDatabaseMetaInfo.java
novoda.lib.sqliteprovider.sqlite.InsertHelperTest.java
novoda.lib.sqliteprovider.sqlite.SQLiteUri.java
novoda.lib.sqliteprovider.testproviders.TestProvider.java
novoda.lib.sqliteprovider.util.DBUtilsTest.java
novoda.lib.sqliteprovider.util.DBUtils.java
novoda.lib.sqliteprovider.util.DatabaseUtils.java
novoda.lib.sqliteprovider.util.Log.java
novoda.lib.sqliteprovider.util.SQLFile.java
novoda.lib.sqliteprovider.util.UriInspector.java
novoda.lib.sqliteprovider.util.UriToSqlAttributes.java
novoda.lib.sqliteprovider.util.UriUtils.java
novoda.rest.database.ModularSQLiteOpenHelper.java
novoda.rest.database.Persister.java
novoda.rest.database.SQLiteConflictClause.java
novoda.rest.database.SQLiteFileParser.java
novoda.rest.database.SQLiteInserter.java
novoda.rest.database.SQLiteTableCreatorWrapper.java
novoda.rest.database.SQLiteTableCreator.java
novoda.rest.database.SQLiteType.java
novoda.rest.database.SQLiteUtil.java
novoda.rest.database.UriQueryBuilder.java
novoda.rest.database.UriTableCreator.java
novoda.rest.database.listener.NodeInsertListener.java
novoda.rest.database.listener.SQLiteInsertListener.java
novoda.rest.database.listener.SQLiteTableCreateListener.java