6
votes

If I already use migrations, I can easily generate incremental one using: app/console doctrine:migrations:diff.

But assume I have an existing application that does not use migrations yet. doctrine:migrations:diff will just generate a diff between the current database schema and doctrine entities. The problem is I need to have an initial/first migration consisting of CREATE TABLE for every entity created up to this point. My current workaround is to create an empty database, switch credentials in parameters.yml, and run doctrine:migrations:diff then.

I don't like this solution - is there a better one?

2
Why don't you create an initial SQL which is gonna be run in the first migration with the table CREATIONS without the INSERTS?Fernando Caraballo

2 Answers

6
votes

you could use doctrine:schema:create --dump-sql to generate the sql for creation and put this into the first migration version

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/tools.html#database-schema-generation

2
votes

If the table does not exist as a Doctrine Entity, you'll need to manually create a migration class for it, as well as any separate Fixtures.

<?php

namespace DoctrineMigrations;

use DoctrineDBALMigrationsAbstractMigration,
DoctrineDBALSchemaSchema;

class Version00001 extends AbstractMigration
{
    public function up(Schema $schema)
    {
        $this->addSql('CREATE TABLE MY_CUSTOM_PREFIX_example (id INT NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB');
    }

    public function down(Schema $schema)
    {
        $this->addSql('DROP TABLE MY_CUSTOM_PREFIX_example');
    }
}

Also make sure that you exclude these custom tables using filters within your Doctrine configuration (@see: http://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html#manual-tables)

For this example you'd replace t_ with MY_CUSTOM_PREFIX_