/*
 * Copyright 2016 Peter Kenji Yamanaka
 *
 * 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.
 */

package com.pyamsoft.padlock.model.sql;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import timber.log.Timber;

final class PadLockOpenHelper extends SQLiteOpenHelper {

  private static final int DATABASE_VERSION = 2;

  public PadLockOpenHelper(final Context context) {
    super(context.getApplicationContext(), "padlock_db", null, DATABASE_VERSION);
  }

  @Override public void onCreate(SQLiteDatabase sqLiteDatabase) {
    Timber.d("onCreate");
    sqLiteDatabase.execSQL(PadLockEntry.CREATE_TABLE);
  }

  @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
    Timber.d("onUpgrade from old version %d to new %d", oldVersion, newVersion);
  }
}

@@ -767,16 +767,72 @@
 Helper;%0A
+import android.text.TextUtils;%0Aimport java.util.Locale;%0A
 import t
@@ -849,16 +849,16 @@
 Timber;%0A
-
 %0Afinal c
@@ -954,17 +954,17 @@
 RSION = 
-2
+1
 ;%0A%0A  pub
@@ -1344,24 +1344,24 @@
 wVersion) %7B%0A
-
     Timber.d
@@ -1429,14 +1429,1820 @@
 rsion);%0A
+    if (oldVersion == 1 && newVersion == 2) %7B%0A      upgradeVersion1To2(sqLiteDatabase);%0A    %7D%0A  %7D%0A%0A  private static void upgradeVersion1To2(SQLiteDatabase sqLiteDatabase) %7B%0A    Timber.d(%22Upgrading from Version 1 to 2 drops the displayName column%22);%0A%0A    // Remove the columns we don't want anymore from the table's list of columns%0A    Timber.d(%22Gather a list of the remaining columns%22);%0A    final String%5B%5D updatedTableColumns = %7B%0A        PadLockEntry.PACKAGENAME, PadLockEntry.ACTIVITYNAME, PadLockEntry.LOCKCODE,%0A        PadLockEntry.LOCKUNTILTIME, PadLockEntry.IGNOREUNTILTIME, PadLockEntry.SYSTEMAPPLICATION%0A    %7D;%0A%0A    final String columnsSeperated = TextUtils.join(%22,%22, updatedTableColumns);%0A    Timber.d(%22Column seperated: %25s%22, columnsSeperated);%0A%0A    final String tableName = PadLockEntry.TABLE_NAME;%0A    final String oldTable = tableName + %22_old%22;%0A    final String alterTable =%0A        String.format(Locale.getDefault(), %22ALTER TABLE %25s RENAME TO %25s%22, tableName, oldTable);%0A    final String insertIntoNewTable =%0A        String.format(Locale.getDefault(), %22INSERT INTO %25s(%25s) SELECT %25s FROM %25s%22, tableName,%0A            columnsSeperated, columnsSeperated, oldTable);%0A    final String dropOldTable = String.format(Locale.getDefault(), %22DROP TABLE %25s%22, oldTable);%0A%0A    // Move the existing table to an old table%0A    Timber.d(%22EXEC SQL: %25s%22, alterTable);%0A    sqLiteDatabase.execSQL(alterTable);%0A%0A    // Creating the table on its new format (no redundant columns)%0A    Timber.d(%22EXEC SQL: %25s%22, PadLockEntry.CREATE_TABLE);%0A    sqLiteDatabase.execSQL(PadLockEntry.CREATE_TABLE);%0A%0A    // Populating the table with the data%0A    Timber.d(%22EXEC SQL: %25s%22, insertIntoNewTable);%0A    sqLiteDatabase.execSQL(insertIntoNewTable);%0A%0A    Timber.d(%22EXEC SQL: %25s%22, dropOldTable);%0A    sqLiteDatabase.execSQL(dropOldTable);%0A
   %7D%0A%7D%0A

Upgrade SQL version and provide migration