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?
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.
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.
A repository is a way to retrieve entities, so put on repositories any method you need to get them, such as getUserByEmail or whatever.
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.
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.
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.
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:
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With