Adam Zielinski
Adam Zielinski

Reputation: 2874

How do I generate an initial/base migration for existing Symfony+Doctrine application?

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?

Upvotes: 11

Views: 12024

Answers (3)

learner
learner

Reputation: 391

And what about a little :

bin/console doctrine:migrations:diff --from-empty-schema

With the flag --from-empty-schema it will do exactly what you're asking for.

After that you can (must) manually add an entry in the doctrine_migration_versions table if the DB is already set up and you want to (have to) run /bin/console doctrine:migrations:migrate. Like that (screenshot from adminer):

enter image description here

Upvotes: 5

pscheit
pscheit

Reputation: 2981

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

Upvotes: 9

Jake Litwicki
Jake Litwicki

Reputation: 232

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_

Upvotes: 2

Related Questions