/**
  * Add color attribute to default attribute set and create option
  */
 protected function setUp()
 {
     parent::setUp();
     $setup = new Mage_Catalog_Model_Resource_Setup('catalog_setup');
     $colorAttributeId = $setup->getAttributeId(Mage_Catalog_Model_Product::ENTITY, 'color');
     $setup->addAttributeToSet(Mage_Catalog_Model_Product::ENTITY, self::DEFAULT_ATTRIBUTE_SET, self::GENERAL_ATTRIBUTE_GROUP, $colorAttributeId);
     $setup->addAttributeOption(array('attribute_id' => $colorAttributeId, 'values' => ['Octarin']));
     $this->optionId = $setup->getConnection()->lastInsertId($setup->getTable('eav/attribute_option'));
 }
 /**
  * Run script
  *
  */
 public function run()
 {
     $installer = new Mage_Catalog_Model_Resource_Setup('core_setup');
     try {
         //grab all products ids that have 'no_selection' value in database
         $noSelectionSelect = $installer->getConnection()->select()->from(array('product' => $installer->getTable('catalog/product')), array('id' => 'product.entity_id'))->join(array('varchar_table' => $installer->getTable('catalog_product_entity_varchar')), 'varchar_table.entity_id = product.entity_id', array())->join(array('eav_table' => $installer->getTable('eav/attribute')), 'varchar_table.attribute_id = eav_table.attribute_id', array())->where('eav_table.attribute_code = "image" AND eav_table.entity_type_id = 4 AND varchar_table.value = "no_selection" AND varchar_table.store_id = 0');
         //get products ids that have missing image value record
         $missingImageValueSelectInner = $installer->getConnection()->select()->from(array('product' => $installer->getTable('catalog/product')), array('id' => 'product.entity_id'))->join(array('varchar_table' => $installer->getTable('catalog_product_entity_varchar')), 'varchar_table.entity_id = product.entity_id', array())->join(array('eav_table' => $installer->getTable('eav/attribute')), 'varchar_table.attribute_id = eav_table.attribute_id', array())->where('eav_table.attribute_code = "image" AND eav_table.entity_type_id = 4 AND varchar_table.store_id = 0');
         $missingImageValueSelect = $installer->getConnection()->select()->from(array('product' => $installer->getTable('catalog/product')), array('id' => 'product.entity_id'))->where('product.entity_id NOT IN ?', $missingImageValueSelectInner);
         //combine ids.
         $unitedSselect = $installer->getConnection()->select()->union(array($noSelectionSelect, $missingImageValueSelect));
         $ids = $installer->getConnection()->fetchCol($unitedSselect);
         Mage::log(sprintf("found %s candidats for setting image", count($ids)), null, 'image_set.log', true);
         //Get id to image relation from catalog_product_entity_media_gallery table
         $mediaSelect = $installer->getConnection()->select()->from(array('gallery' => $installer->getTable(Mage_Catalog_Model_Resource_Product_Attribute_Backend_Media::GALLERY_TABLE)), array('id' => 'entity_id', 'value'))->join(array('gallery_value' => $installer->getTable(Mage_Catalog_Model_Resource_Product_Attribute_Backend_Media::GALLERY_VALUE_TABLE)), 'gallery_value.value_id = gallery.value_id AND store_id = 0', array())->where('entity_id in (?)', $ids)->order('position asc');
         $query = $installer->getConnection()->query($mediaSelect);
         $idToImage = array();
         while ($row = $query->fetch()) {
             if (!isset($idToImage[$row['id']])) {
                 $idToImage[$row['id']] = $row['value'];
             }
         }
         //Mage::log($idToImage, null, 'image_set.log', true);
         /** @var Magebrew_SetImage_Model_Resource_Catalog_Product $resource */
         $resource = Mage::getModel('magebrew_setimage/resource_catalog_product');
         foreach ($idToImage as $id => $image) {
             $product = Mage::getModel('catalog/product')->setId($id);
             $product->setStoreId(Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID);
             $product->setImage($image);
             $product->setSmallImage($image);
             $product->setThumbnail($image);
             Mage::log(sprintf("trying to set image %s to product %s", $image, $id), null, 'image_set.log', true);
             $resource->saveAttribute($product, 'image');
             $resource->saveAttribute($product, 'small_image');
             $resource->saveAttribute($product, 'thumbnail');
             echo 'set images for product ' . $id . PHP_EOL;
         }
         echo 'Finished' . PHP_EOL;
     } catch (Exception $e) {
         echo $e->getMessage() . "\n";
     }
 }
 /**
  * Run script
  *
  */
 public function run()
 {
     $installer = new Mage_Catalog_Model_Resource_Setup('core_setup');
     try {
         //get products that have more then one image assigned
         $mediaSubSelect = $installer->getConnection()->select()->from(array('gallery' => $installer->getTable('catalog_product_entity_media_gallery')), array('id' => 'entity_id'))->group('entity_id')->having('count(entity_id) >1');
         //get id_to_all_assigned_images relation
         $mediaSelect = $installer->getConnection()->select()->from(array('gallery' => $installer->getTable('catalog_product_entity_media_gallery')), array('id' => 'entity_id', 'images' => 'GROUP_CONCAT(value)'))->join(array('sub_media' => $mediaSubSelect), 'sub_media.id = gallery.entity_id', array())->group('id');
         //get id_to_base_image relation
         $baseImageSelect = $installer->getConnection()->select()->from(array('product' => $installer->getTable('catalog/product')), array('id' => 'product.entity_id'))->join(array('varchar_table' => $installer->getTable('catalog_product_entity_varchar')), 'varchar_table.entity_id = product.entity_id', array('base_image' => 'value'))->join(array('eav_table' => $installer->getTable('eav/attribute')), 'varchar_table.attribute_id = eav_table.attribute_id', array())->where('eav_table.attribute_code = "image" AND eav_table.entity_type_id = 4 AND varchar_table.value <> "no_selection" AND varchar_table.value <> "" AND varchar_table.store_id = 0');
         $idToBase = $installer->getConnection()->fetchPairs($baseImageSelect);
         $baseToId = array_flip($idToBase);
         $imagesToDelete = array();
         $csvFileHandle = fopen(Mage::getBaseDir('var') . DS . 'duplicated_images.csv', 'w');
         $query = $installer->getConnection()->query($mediaSelect);
         while ($row = $query->fetch()) {
             $md5 = array();
             foreach (explode(',', $row['images']) as $image) {
                 $filepath = Mage::getBaseDir('media') . '/catalog/product' . $image;
                 if (file_exists($filepath)) {
                     $md5Hash = md5_file($filepath);
                     if (!isset($md5[$md5Hash])) {
                         $md5[$md5Hash] = $image;
                     } else {
                         if (!isset($baseToId[$image])) {
                             $imagesToDelete[] = $image;
                             fputcsv($csvFileHandle, array($row['id'], $image));
                         } else {
                             $imagesToDelete[] = $md5[$md5Hash];
                             fputcsv($csvFileHandle, array($row['id'], $md5[$md5Hash]));
                         }
                     }
                 }
             }
         }
         fclose($csvFileHandle);
         $where = array('value in(?)' => $imagesToDelete);
         echo 'executing of mass detele query ' . PHP_EOL;
         $installer->getConnection()->delete($installer->getTable('catalog_product_entity_media_gallery'), $where);
         foreach ($imagesToDelete as $image) {
             Mage::log($image, null, 'image_duplicate.log', true);
             if (unlink(Mage::getBaseDir('media') . '/catalog/product' . $image)) {
                 echo 'Deleted ' . Mage::getBaseDir('media') . '/catalog/product' . $image . PHP_EOL;
             } else {
                 echo 'Can not delete ' . Mage::getBaseDir('media') . '/catalog/product' . $image . PHP_EOL;
             }
         }
         echo 'Finished' . PHP_EOL;
     } catch (Exception $e) {
         echo $e->getMessage() . "\n";
     }
 }
<?php

/*Mage_core_Model_Resource_Setup*/
$this->startSetup();
// catalog resource model has all EAV related functions
$catalogInstaller = new Mage_Catalog_Model_Resource_Setup($this->_resourceName);
$this->run(" Update `{$catalogInstaller->getTable('catalog/eav_attribute')}` \r\n\tSET apply_to = CONCAT_WS(',', apply_to, 'event')\r\n    WHERE `apply_to` LIKE '%simple%' AND `apply_to` NOT LIKE '%event';\r\n");
// adding attributes
// add title for event
$catalogInstaller->addAttribute('catalog_product', 'event_title', array('label' => 'Event Title', 'required' => TRUE, 'group' => 'Event Settings', 'apply_to' => 'event'));
// add a select list for the event
$catalogInstaller->addAttribute('catalog_product', 'event_id', array('label' => 'Event ID', 'required' => TRUE, 'group' => 'Event Settings', 'input' => 'select', 'source' => 'master_example/product_attribute_source_event', 'apply_to' => 'event', 'note' => 'Select your event from list'));