Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 610
Adam Zielinski Avatar asked May 25 '17 12:05

Adam Zielinski


People also ask

What is migration in Symfony?

Database migrations are a way to safely update your database schema both locally and on production. Instead of running the doctrine:schema:update command or applying the database changes manually with SQL statements, migrations allow to replicate the changes in your database schema in a safe manner.

What is an ORM migration?

Migrations are a type of version control for your database. They allow a team to modify the database schema and stay up to date on the current schema state. Migrations are typically paired with the Schema Builder to easily manage your database's schema.

What is repository in Symfony?

A repository is a way to retrieve entities, so put on repositories any method you need to get them, such as getUserByEmail or whatever.

What is doctrine repository?

A repository in a term used by many ORMs (Object Relational Mappers), doctrine is just one of these. It means the place where our data can be accessed from, a repository of data. This is to distinguish it from a database as a repository does not care how its data is stored.

What is doctrine migrations in Symfony?

Instead of running the doctrine:schema:update command or applying the database changes manually with SQL statements, migrations allow to replicate the changes in your database schema in a safe manner. Migrations are available in Symfony applications via the DoctrineMigrationsBundle , which uses the external Doctrine Database Migrations library.

How do I migrate data from doctrine to another database?

Right and data safe way is to use Doctrine migrations. Doctrine can automatically generate migration for you, but that migration will contain the same SQL code like doctrine:schema:update command, and it doesn't care about the existing data. To migrate the data, you will have to modify migration file, and we will talk about that later in the text.

How do I install doctrine in Symfony?

First, install Doctrine support via the orm Symfony pack , as well as the MakerBundle, which will help generate some code: The database connection information is stored as an environment variable called DATABASE_URL. For development, you can find and customize this inside .env:

What is Symfony and how does it work?

Symfony provides all the tools you need to use databases in your applications thanks to Doctrine, the best set of PHP libraries to work with databases. These tools support relational databases like MySQL and PostgreSQL and also NoSQL databases like MongoDB. Databases are a broad topic, so the documentation is divided in three articles:


1 Answers

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

like image 193
pscheit Avatar answered Nov 15 '22 07:11

pscheit