示例#1
0
  private boolean executeMigrations(SQLiteDatabase db, int oldVersion, int newVersion) {
    boolean migrationExecuted = false;
    try {
      final List<String> files = Arrays.asList(Cache.getContext().getAssets().list(MIGRATION_PATH));
      Collections.sort(files, new NaturalOrderComparator());

      db.beginTransaction();
      try {
        for (String file : files) {
          try {
            final int version = Integer.valueOf(file.replace(".sql", ""));

            if (version > oldVersion && version <= newVersion) {
              executeSqlScript(db, file);
              migrationExecuted = true;

              Log.i(file + " executed succesfully.");
            }
          } catch (NumberFormatException e) {
            Log.w("Skipping invalidly named file: " + file, e);
          }
        }
        db.setTransactionSuccessful();
      } finally {
        db.endTransaction();
      }
    } catch (IOException e) {
      Log.e("Failed to execute migrations.", e);
    }

    return migrationExecuted;
  }
示例#2
0
  private void executeSqlScript(SQLiteDatabase db, String file) {

    InputStream stream = null;

    try {
      stream = Cache.getContext().getAssets().open(MIGRATION_PATH + "/" + file);

      if (Configuration.SQL_PARSER_DELIMITED.equalsIgnoreCase(mSqlParser)) {
        executeDelimitedSqlScript(db, stream);

      } else {
        executeLegacySqlScript(db, stream);
      }

    } catch (IOException e) {
      Log.e("Failed to execute " + file, e);

    } finally {
      IOUtils.closeQuietly(stream);
    }
  }