| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace OroCRM\Bundle\SalesBundle\Migrations\Schema\v1_25; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Doctrine\DBAL\Schema\Schema; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Oro\Bundle\MigrationBundle\Migration\Migration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Oro\Bundle\MigrationBundle\Migration\QueryBag; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Oro\Bundle\MigrationBundle\Migration\OrderedMigrationInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Oro\Bundle\MigrationBundle\Migration\SqlMigrationQuery; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class AddLeadAddressTable implements Migration, OrderedMigrationInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     public function getOrder() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         return 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     public function up(Schema $schema, QueryBag $queries) | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         $this->addMigrationQueries($queries); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         self::createLeadAddressTable($schema); | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * Create orocrm_sales_lead_address table | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param Schema $schema | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public static function createLeadAddressTable(Schema $schema) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $table = $schema->createTable('orocrm_sales_lead_address'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $table->addColumn('id', 'integer', ['autoincrement' => true]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $table->addColumn('owner_id', 'integer', ['notnull' => false]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $table->addColumn('region_code', 'string', ['notnull' => false, 'length' => 16]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $table->addColumn('country_code', 'string', ['notnull' => false, 'length' => 2]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $table->addColumn('is_primary', 'boolean', ['notnull' => false]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $table->addColumn('label', 'string', ['notnull' => false, 'length' => 255]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $table->addColumn('street', 'string', ['notnull' => false, 'length' => 500]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $table->addColumn('street2', 'string', ['notnull' => false, 'length' => 500]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $table->addColumn('city', 'string', ['notnull' => false, 'length' => 255]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $table->addColumn('postal_code', 'string', ['notnull' => false, 'length' => 255]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $table->addColumn('organization', 'string', ['notnull' => false, 'length' => 255]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $table->addColumn('region_text', 'string', ['notnull' => false, 'length' => 255]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $table->addColumn('name_prefix', 'string', ['notnull' => false, 'length' => 255]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $table->addColumn('first_name', 'string', ['notnull' => false, 'length' => 255]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $table->addColumn('middle_name', 'string', ['notnull' => false, 'length' => 255]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $table->addColumn('last_name', 'string', ['notnull' => false, 'length' => 255]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $table->addColumn('name_suffix', 'string', ['notnull' => false, 'length' => 255]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $table->addColumn('created', 'datetime', []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $table->addColumn('updated', 'datetime', []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $table->setPrimaryKey(['id']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $table->addIndex(['owner_id'], 'IDX_LEAD_ADDRESS_OWNER', []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $table->addIndex(['country_code'], 'IDX_LEAD_ADDRESS_COUNTRY', []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $table->addIndex(['region_code'], 'IDX_LEAD_ADDRESS_REGION', []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $table->addForeignKeyConstraint( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             $schema->getTable('orocrm_sales_lead'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             ['owner_id'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             ['id'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             ['onDelete' => 'CASCADE', 'onUpdate' => null] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $table->addForeignKeyConstraint( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $schema->getTable('oro_dictionary_region'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             ['region_code'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             ['combined_code'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             ['onDelete' => null, 'onUpdate' => null] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $table->addForeignKeyConstraint( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $schema->getTable('oro_dictionary_country'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             ['country_code'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             ['iso2_code'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             ['onDelete' => null, 'onUpdate' => null] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * Migrate addresses to the new table. Mark address as primary | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param QueryBag $queries | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     protected function addMigrationQueries(QueryBag $queries) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $sql = 'INSERT INTO orocrm_sales_lead_address(owner_id, region_code, country_code, is_primary, label, street,' . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                ' street2, city, postal_code, organization, region_text, name_prefix, first_name, middle_name,' . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                ' last_name, name_suffix, created, updated)' . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                'SELECT lead.id, addr.region_code, addr.country_code,' . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                ' \'1\', addr.label, addr.street, addr.street2, addr.city, addr.postal_code, addr.organization,' . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                ' addr.region_text, addr.name_prefix, addr.first_name, addr.middle_name, addr.last_name,' . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                ' addr.name_suffix, addr.created, addr.updated FROM oro_address as addr' . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                ' INNER JOIN orocrm_sales_lead as lead on lead.address_id = addr.id'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $queries->addPostQuery(new SqlMigrationQuery($sql)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 100 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |  |