$sql .= "ALTER TABLE `db1_anymarket_anymarketcategories_store` CHANGE  `anymarketcategories01_id` `anymarketcategories_id` int(11) NOT NULL;";
    $sql .= "ALTER TABLE `db1_anymarket_anymarketcategories_store` ADD CONSTRAINT `fk13_anymarketcategories_id` FOREIGN KEY ( `anymarketcategories_id` ) REFERENCES `db1_anymarket_anymarketcategories` ( `entity_id` );";
}
$table = 'db1_anymarket_anymarketlog01';
if (_tableExists($table, $resource)) {
    $sql .= "ALTER TABLE db1_anymarket_anymarketlog01 RENAME db1_anymarket_anymarketlog;";
    $sql .= "ALTER TABLE db1_anymarket_anymarketlog01_store RENAME db1_anymarket_anymarketlog_store;";
    $sql .= "SET foreign_key_checks = 0;";
    $sql .= "ALTER TABLE `db1_anymarket_anymarketlog_store` CHANGE  `anymarketlog01_id` `anymarketlog_id` int(11) NOT NULL;";
    $sql .= "ALTER TABLE `db1_anymarket_anymarketlog_store` ADD CONSTRAINT `fk13_anymarketlog_id` FOREIGN KEY ( `anymarketlog_id` ) REFERENCES `db1_anymarket_anymarketlog` ( `entity_id` );";
}
$table = 'db1_anymarket_anymarketorders01';
if (_tableExists($table, $resource)) {
    $sql .= "ALTER TABLE db1_anymarket_anymarketorders01 RENAME db1_anymarket_anymarketorders;";
    $sql .= "ALTER TABLE db1_anymarket_anymarketorders01_store RENAME db1_anymarket_anymarketorders_store;";
    $sql .= "SET foreign_key_checks = 0;";
    $sql .= "ALTER TABLE `db1_anymarket_anymarketorders_store` CHANGE  `anymarketorders01_id` `anymarketorders_id` int(11) NOT NULL;";
    $sql .= "ALTER TABLE `db1_anymarket_anymarketorders_store` ADD CONSTRAINT `fk13_anymarketcategories_id` FOREIGN KEY ( `anymarketcategories_id` ) REFERENCES `db1_anymarket_anymarketcategories` ( `entity_id` );";
}
$table = 'db1_anymarket_anymarketproducts01';
if (_tableExists($table, $resource)) {
    $sql .= "ALTER TABLE db1_anymarket_anymarketproducts01 RENAME db1_anymarket_anymarketproducts;";
    $sql .= "ALTER TABLE db1_anymarket_anymarketproducts01_store RENAME db1_anymarket_anymarketproducts_store;";
    $sql .= "SET foreign_key_checks = 0;";
    $sql .= "ALTER TABLE `db1_anymarket_anymarketproducts_store` CHANGE  `anymarketproducts01_id` `anymarketproducts_id` int(11) NOT NULL;";
    $sql .= "ALTER TABLE `db1_anymarket_anymarketproducts_store` ADD CONSTRAINT `fk13_anymarketcategories_id` FOREIGN KEY ( `anymarketcategories_id` ) REFERENCES `db1_anymarket_anymarketcategories` ( `entity_id` );";
}
if ($sql != '') {
    $installer->run($sql);
}
$installer->endSetup();
    if (!Mage::getStoreConfig('uol_pagseguro/store/reference')) {
        $ref = current(current($results));
    }
} else {
    // Creates a reference to 5 characters that will be used as the only reference that store, the transaction PagSeguro
    $ref = Mage::helper('pagseguro')->createReference(5, 'true', 'true');
}
if (!Mage::getStoreConfig('uol_pagseguro/store/reference')) {
    // save the reference of store
    Mage::getConfig()->saveConfig('uol_pagseguro/store/reference', $ref);
}
$table = $tp . 'pagseguro_sales_code';
$new_table = $tp . 'pagseguro_orders';
// checks if exists the table
if (_tableExists($table, $resource)) {
    if (!_tableExists($new_table, $resource)) {
        //copies the transaction table used in version 2.4
        $sql .= "CREATE TABLE `" . $new_table . "` LIKE `" . $table . "`;";
        $sql .= "INSERT `" . $new_table . "` SELECT * FROM `" . $table . "`;";
        // change the table adding sent column
        $sql .= "ALTER TABLE `" . $new_table . "` ADD sent int DEFAULT 0;";
        $sql .= "ALTER TABLE `" . $new_table . "` ADD environment varchar(40);";
    }
} else {
    // Checks for the pagseguro_orders table if it does not exist is created
    $sql .= "CREATE TABLE IF NOT EXISTS `" . $new_table . "` (\n             `entity_id` int(11) NOT NULL AUTO_INCREMENT,\n             `order_id` int(11),\n             `transaction_code` varchar(80) NOT NULL,\n             `sent` int DEFAULT 0,\n             `environment` varchar(40),\n             PRIMARY KEY (`entity_id`)\n             ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
}
$table = $tp . "sales_order_status";
// Verifies that no record of the status PagSeguro created, if you have not created
$sql .= "INSERT INTO `" . $table . "` (STATUS, label)\n         SELECT p.status, p.label FROM(SELECT 'chargeback_debitado_ps' AS STATUS, 'Chargeback Debitado' AS label) p\n         WHERE (SELECT COUNT(STATUS) FROM `" . $table . "` WHERE STATUS = 'chargeback_debitado_ps') = 0;\n\n         INSERT INTO `" . $table . "` (STATUS, label)\n         SELECT p.status, p.label FROM(SELECT 'em_contestacao_ps' AS STATUS, 'Em Contestação' AS label) p\n         WHERE (SELECT COUNT(STATUS) FROM `" . $table . "` WHERE STATUS = 'em_contestacao_ps') = 0;";
$table = $tp . "sales_order_status_state";