Reputation: 2874
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
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):
Upvotes: 5
Reputation: 2981
you could use doctrine:schema:create --dump-sql to generate the sql for creation and put this into the first migration version
Upvotes: 9
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