4

I have tried to execute the cli command ./doctrine generate-migrations-diff and a version file has been created correctly in the right folder.

The message is: generate-migrations-diff - Generated migration classes successfully from difference

Then I try to execute another cli command ./doctrine migrate and a message appears to me saying: migrate - migrated successfully to version #1 but when I open the class any modification have been done. Why?

This is the version1 file:

<?php

class Version1 extends Doctrine_Migration_Base
{
public function up()
{
    $this->removeColumn('addresses', 'address_test');
}

public function down()
{
    $this->addColumn('addresses', 'address_test', 'string', '', array(
         'fixed' => '0',
         'unsigned' => '',
         'primary' => '',
         'notnull' => '1',
         'autoincrement' => '',
         ));
}
}

?>

This is the YAML. I have deleted the field: address_test

Addresses:
  connection: doctrine
  tableName: addresses
  columns:
    address_id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: true
    address:
      type: string()
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    city:
      type: string(150)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    code:
      type: string(20)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    country_id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    base:
      type: integer(1)
      fixed: false
      unsigned: false
      primary: false
      default: '0'
      notnull: true
      autoincrement: false
    latitude:
      type: string(20)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    longitude:
      type: string(20)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    customer_id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false

This is the BaseAddresses class generated by the migrate command: bindComponent('Addresses', 'doctrine');

/**
 * BaseAddresses
 * 
 * This class has been auto-generated by the Doctrine ORM Framework
 * 
 * @property integer $address_id
 * @property string $address
 * @property string $city
 * @property string $code
 * @property integer $country_id
 * @property integer $base
 * @property string $latitude
 * @property string $longitude
 * @property integer $customer_id
 * @property Countries $Countries
 * @property Customers $Customers
 * 
 * @package    ##PACKAGE##
 * @subpackage ##SUBPACKAGE##
 * @author     ##NAME## <##EMAIL##>
 * @version    SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $
 */
abstract class BaseAddresses extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->setTableName('addresses');
        $this->hasColumn('address_id', 'integer', 4, array(
             'type' => 'integer',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => true,
             'autoincrement' => true,
             'length' => '4',
             ));
        $this->hasColumn('address', 'string', null, array(
             'type' => 'string',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'notnull' => false,
             'autoincrement' => false,
             'length' => '',
             ));
        $this->hasColumn('city', 'string', 150, array(
             'type' => 'string',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'notnull' => false,
             'autoincrement' => false,
             'length' => '150',
             ));
        $this->hasColumn('code', 'string', 20, array(
             'type' => 'string',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'notnull' => false,
             'autoincrement' => false,
             'length' => '20',
             ));
        $this->hasColumn('country_id', 'integer', 4, array(
             'type' => 'integer',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'notnull' => false,
             'autoincrement' => false,
             'length' => '4',
             ));
        $this->hasColumn('base', 'integer', 1, array(
             'type' => 'integer',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'default' => '0',
             'notnull' => true,
             'autoincrement' => false,
             'length' => '1',
             ));
        $this->hasColumn('latitude', 'string', 20, array(
             'type' => 'string',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'notnull' => false,
             'autoincrement' => false,
             'length' => '20',
             ));
        $this->hasColumn('longitude', 'string', 20, array(
             'type' => 'string',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'notnull' => false,
             'autoincrement' => false,
             'length' => '20',
             ));
        $this->hasColumn('customer_id', 'integer', 4, array(
             'type' => 'integer',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'notnull' => true,
             'autoincrement' => false,
             'length' => '4',
             ));
    }

    public function setUp()
    {
        parent::setUp();
        $this->hasOne('Countries', array(
             'local' => 'country_id',
             'foreign' => 'country_id'));

        $this->hasOne('Customers', array(
             'local' => 'customer_id',
             'foreign' => 'customer_id',
             'onDelete' => 'CASCADE'));
    }
}

In some website I have read that I have to execute the command: build-all in order to generate a version updated of the class addresses but I get this error:

SQLSTATE[HY000]: General error: 1005 Can't create table 'web63db1.#sql-3e6_11d' (errno: 121). Failing Query: "ALTER TABLE addresses ADD CONSTRAINT addresses_customer_id_customers_customer_id FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ON DELETE CASCADE". Failing Query: ALTER TABLE addresses ADD CONSTRAINT addresses_customer_id_customers_customer_id FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ON DELETE CASCADE

What have I do? Thanks in advance.

2 Answers 2

8

Migrations are only for changing your Database, not your model classes. You need to build your model classes afterwards.

So your normal way of working will be:

  1. Change your schema.yml
  2. Create your Migration file (generate-migrations-diff)
  3. Run your migration against the database (migrate), your database should be changed now
  4. Update your model-classes (build-all)
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Timo, after the migrate command I have executed the build-all but I get this error: SQLSTATE[HY000]: General error: 1007 Can't create database 'mydb'; database exists.
1

As I can't comment, let me modify Timo's answer:

  1. Change Your schema.yml
  2. Create Migration File (doctrine:generate-migrations-diff) (Note: this is comparing schema against Class files)
  3. Run your Migration (doctrine:migrate)(this runs the migrations based on the migration version number in your database)
  4. Build your class files. (doctrine:build --all-classes OR doctrine:build-model & doctrine:build-forms & doctrine:build-filters) (Note: this will build all your class files but will not try to update your database.)

I highly recommend backing up your database before running any migrations for the first time. If a migration fails (i.e. because you typoed your schema), you can end up with a database that is stuck between migrations and will need to be scrapped. I usually, backup my test-db and then import the live db to test the migration on. I'm more than slightly terrified of failing a migration on the live server.

Its important to note that the 'doctrine:generate-migrations-diff' compares the schema.yml to your currently generated class files. Don't build your classes before generating your migration

To deploy on a server, you will need to upload your schema.yml and your new migration files. Then just do steps 3 & 4 (and then clear your cache as a matter of habit) and you're good to go.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.