<commit_before>package com.fsck.k9.mailstore.migrations;


import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.VisibleForTesting;

import com.fsck.k9.controller.MessagingControllerCommands;
import com.fsck.k9.controller.MessagingControllerCommands.PendingCommand;
import com.fsck.k9.controller.PendingCommandSerializer;
import com.fsck.k9.helper.Utility;
import com.fsck.k9.mail.Flag;


public class MigrationTo56 {
    private static final String PENDING_COMMAND_MOVE_OR_COPY = "com.fsck.k9.MessagingController.moveOrCopy";
    private static final String PENDING_COMMAND_MOVE_OR_COPY_BULK = "com.fsck.k9.MessagingController.moveOrCopyBulk";
    private static final String PENDING_COMMAND_MOVE_OR_COPY_BULK_NEW = "com.fsck.k9.MessagingController.moveOrCopyBulkNew";
    private static final String PENDING_COMMAND_EMPTY_TRASH = "com.fsck.k9.MessagingController.emptyTrash";
    private static final String PENDING_COMMAND_SET_FLAG_BULK = "com.fsck.k9.MessagingController.setFlagBulk";
    private static final String PENDING_COMMAND_SET_FLAG = "com.fsck.k9.MessagingController.setFlag";
    private static final String PENDING_COMMAND_APPEND = "com.fsck.k9.MessagingController.append";
    private static final String PENDING_COMMAND_MARK_ALL_AS_READ = "com.fsck.k9.MessagingController.markAllAsRead";
    private static final String PENDING_COMMAND_EXPUNGE = "com.fsck.k9.MessagingController.expunge";


    public static void migratePendingCommands(SQLiteDatabase db) {
        List<PendingCommand> pendingCommmands = new ArrayList<>();
        for (OldPendingCommand oldPendingCommand : getPendingCommands(db)) {
            PendingCommand newPendingCommand = migratePendingCommand(oldPendingCommand);
            pendingCommmands.add(newPendingCommand);
        }

        db.execSQL("DROP TABLE IF EXISTS pending_commands");
        db.execSQL("CREATE TABLE pending_commands " +
                "(id INTEGER PRIMARY KEY, command TEXT, data TEXT)");

        PendingCommandSerializer pendingCommandSerializer = PendingCommandSerializer.getInstance();
        for (PendingCommand pendingCommand : pendingCommmands) {
            final ContentValues cv = new ContentValues();
            cv.put("command", pendingCommand.getCommandName());
            cv.put("data", pendingCommandSerializer.serialize(pendingCommand));
            db.insert("pending_commands", "command", cv);
        }<commit_msg>Fix multi-run upgrade bug in Migration<commit_after>package com.fsck.k9.mailstore.migrations;


import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.VisibleForTesting;

import com.fsck.k9.controller.MessagingControllerCommands;
import com.fsck.k9.controller.MessagingControllerCommands.PendingCommand;
import com.fsck.k9.controller.PendingCommandSerializer;
import com.fsck.k9.helper.Utility;
import com.fsck.k9.mail.Flag;


public class MigrationTo56 {
    private static final String PENDING_COMMAND_MOVE_OR_COPY = "com.fsck.k9.MessagingController.moveOrCopy";
    private static final String PENDING_COMMAND_MOVE_OR_COPY_BULK = "com.fsck.k9.MessagingController.moveOrCopyBulk";
    private static final String PENDING_COMMAND_MOVE_OR_COPY_BULK_NEW = "com.fsck.k9.MessagingController.moveOrCopyBulkNew";
    private static final String PENDING_COMMAND_EMPTY_TRASH = "com.fsck.k9.MessagingController.emptyTrash";
    private static final String PENDING_COMMAND_SET_FLAG_BULK = "com.fsck.k9.MessagingController.setFlagBulk";
    private static final String PENDING_COMMAND_SET_FLAG = "com.fsck.k9.MessagingController.setFlag";
    private static final String PENDING_COMMAND_APPEND = "com.fsck.k9.MessagingController.append";
    private static final String PENDING_COMMAND_MARK_ALL_AS_READ = "com.fsck.k9.MessagingController.markAllAsRead";
    private static final String PENDING_COMMAND_EXPUNGE = "com.fsck.k9.MessagingController.expunge";


    public static void migratePendingCommands(SQLiteDatabase db) {
        List<PendingCommand> pendingCommmands = new ArrayList<>();

        if (columnExists(db, "pending_commands", "arguments")) {
            for (OldPendingCommand oldPendingCommand : getPendingCommands(db)) {
                PendingCommand newPendingCommand = migratePendingCommand(oldPendingCommand);
                pendingCommmands.add(newPendingCommand);
            }

            db.execSQL("DROP TABLE IF EXISTS pending_commands");
            db.execSQL("CREATE TABLE pending_commands " +
                    "(id INTEGER PRIMARY KEY, command TEXT, data TEXT)");

            PendingCommandSerializer pendingCommandSerializer = PendingCommandSerializer.getInstance();
            for (PendingCommand pendingCommand : pendingCommmands) {
                final ContentValues cv = new ContentValues();
                cv.put("command", pendingCommand.getCommandName());
                cv.put("data", pendingCommandSerializer.serialize(pendingCommand));
                db.insert("pending_commands", "command", cv);
            }
        }
    }

    private static boolean columnExists(SQLiteDatabase db, String table, String columnName) {
        Cursor columnCursor  = db.rawQuery("PRAGMA table_info("+table+")", null);
        columnCursor.moveToFirst();
        boolean foundColumn = false;
        while (!columnCursor.isAfterLast()) {
            if(columnCursor.getString(1).equals(columnName)) {
                foundColumn = true;
                break;
            }
            columnCursor.moveToNext();
        }
        columnCursor.close();
        return foundColumn;
{'fraction_non_alphanumeric': 0.08763564616902335, 'fraction_numerical': 0.005754685958566261, 'mean_word_length': 4.12037037037037, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6649148', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1750, 'n_words': 323}