How to migrate the Symfony demo app from sqlite to pdo_mysql - sqlite

I've just installed Symfony demo and it works like a charm. Then I was wondering why there is no question in the process of installing it? and I found that it uses a blog.sqlite file for its posts.
My question is how can I migrate from sqlite to pdo_mysql and what are necessary steps?
Thanks.

This demo application uses an embedded SQLite database to simplify setup, if you don't want to use SQLite, change the URL in parameters.yml or set the DATABASE_URL environment variable.
MySQL example:
# app/config/parameters.yml
parameters:
env(DATABASE_URL): 'mysql://user:pass#127.0.0.1:3306/symfony_demo'
Note: Make sure to use the correct user and pass credentials to connect to DB server.
You can also create the database and load the sample data from the command line:
$ cd symfony-demo/
$ php bin/console doctrine:database:create
$ php bin/console doctrine:schema:create
$ php bin/console doctrine:fixtures:load

Related

Symfony 4 doctrine use cache even in development environment

I am in development mode (APP_ENV=dev in .env), and everytime I change entities and run the 'make:migration' command, it returns me that there is no database changes.
After I run the 'cache:clear' command, it takes the changes into account and makes the migration file.
It's quite annoying. Do you have any idea from where it could come ?
I use the 'symfony server:start' to run the dev server, maybe it comes from here ?
If this is an annotation cache from a bundle that causing the issue, simple cache:clear might not help. Did you try do run doctrine:migration:diff?
You can also try to use --flush with your commands:
php bin/console doctrine:cache:clear-query --env=dev --flush
php bin/console doctrine:cache:clear-result --env=dev --flush
php bin/console doctrine:cache:clear-metadata --env=dev --flush
Update after comments:
What I meant, the complete way you use to deploy the entity changes.
As per Symfony 4, an entity update flow might look like below:
Update your entity class - your php/annotation changes
Clearing ALL Metadata cache entries
php bin/console doctrine:cache:clear-metadata
Generate a migration by comparing your current database to your mapping information.
php bin/console doctrine:migrations:diff
View the status of a set of migrations
php bin/console doctrine:migrations:status
Deploy migration
php bin/console doctrine:migrations:migrate --all-or-nothing
using option --all-or-nothing multiple migrations ran at the same time will be wrapped in a single transaction.
If one migration fails, all migrations will be rolled back.
More about migrations from official source: https://symfony.com/doc/master/bundles/DoctrineMigrationsBundle/index.html
Similar problem explained also here: Update an entity in Symfony 4?
Hope this will help and fix your issue.

Test and Dev Enviroment on same machine in Symfony Flex / Symfony 4

Can I put my databse configuration in a configuration file in config/packages/test/ somehow?
I want to run my tests on my development PC and do the dev work in parallel.
In Symfony 3 I had 2 configurations files
config_test.yaml and config_dev.yaml
where I could put in 2 different database connections.
Now the database connection is in the .env File, so I can not have 2 of them.
I also need to run my fixtures and database helpers and the 2 databases (dev and test) seperatly:
php bin/console doctrine:schema:update --force --env=dev
php bin/console doctrine:fixtures:load --no-interaction --env=dev
the solution was to remove the databse connection from the .env file and move it into seperated config files again:
config/packages/dev/doctrine.yaml:
parameters:
DATABASE_URL: 'mysql://root#localhost:3306/database'
and
config/packages/test/doctrine.yaml:
parameters:
DATABASE_URL: 'mysql://root#localhost:3306/database_test'
and inthe main doctrine configuration file config/packages/doctrine.yaml:
doctrine:
dbal:
url: '%DATABASE_URL%'
also in phpunit.xml.dist:
<env name="DATABASE_URL" value="mysql://root#localhost:3306/database_test" />
Now you can have your test and dev environment on the same system and your databse is not polluted by testing data and you can update your databases with:
php bin/console doctrine:schema:update --force --env=dev
php bin/console doctrine:schema:update --force --env=test

Name conflict in Doctrine after changing database name

Since I changed my main database name in Symfony, I cannot get things to work
as some part of Doctrine (I still haven't found out which) is still stuck with
the old name despite all my updates.
Here are the updates I tried :
Using php bin/console doctrine:database:drop --force followed by
php bin/console doctrine:database:create to try to "restart from scratch"
Destroying app/config/parameters.yml.dist and putting the new database
name in app/config/parameters.yml
Invoking php bin/console cache:clear and php bin/console doctrine:cache:clear-metadata and php bin/console doctrine:cache:clear-query and app/console doctrine:cache:clear-result
What did I forget to update ?
UPDATE : the current answer suggests using php bin/console doctrine:schema:create but does not specify when I should do it. A "restart from scratch" procedure would go something like this :
1. php bin/console doctrine:database:drop --force
2. php bin/console doctrine:database:create
3. php bin/console doctrine:generate:entities MyBundle:MyEntity
4. php bin/console doctrine:schema:update --force
5. php bin/console doctrine:schema:validate
Where should I insert php bin/console doctrine:schema:create in this
sequence ?
Create schema using doctrine:schema:create Check for any errors occuring. You need to establish if there is a configuration error or just sync issue
EDIT:
If you have already code with entites you dont need to run step 3,
just create schema instead. Use phpmyadmin to see what is happening on the database side to establis if database/tables are created. As long as you have correct settings on the both sides eg. user set in databse with create db privillages and same user in config file symfony side all should work correctly.

updatng schema after changes in entity - Symfony2 / Doctrine2

I just uploaded a changed entity to my server and wanted to update my schema via php app/console doctrine:shema:update --dump-sql. (I need to change an smallint into an longtext (simple_array type).) This worked fine on my test server, but on my productive server, after uploading the changed entity, symfony tells me that the schema doesnt need to be updated.
What did I forget?
do in production
app/console doctrine:shema:update --dump-sql -e prod
Symfony executes all console commands in the dev env by default, so you should to specify prod
app/console doctrine:schema:update --force

Can't manage to put data into my database

I am using Symfony 2.0 beta3 and I am facing a rather strange problem. I have
set up the ORM with a sqlite database following the instructions of
the website but when I type:
php app/console doctrine:database:create
then
php app/console doctrine:schema:create
and finally
php app/console doctrine:schema:update --force
I don't get any error (for example last command outputs "Database
schema updated successfully") but nothing is actually inserted into
the database. The table aren't even created. The database file is
generated after the first command but remains empty.
Since I have configured FOS_UserBundle I should have for example a
table fos_user…
When I type
php app/console doctrine:schema:update --dump-sql
It shows that three SQL requests are pending… If I execute these three requests using
SQLiteManager, it works (so the fos_user table is created)
but when I want to create a user using:
php app/console fos:user:create
I get a "SQLSTATE[HY000]: General error: 1 no such table: fos_user " error.
Why am I not given some error message when I run the first three
commands? Am I missing something?
I face the same behaviour using pdo_pgsql driver, except that the SQL
generated by the dump is not ANSI compliant so it can't be executed
for postgresql (use of datetime and autoincrement instead of timestamp
and serial)….
Any help would be much appreciated
I found this solution on the Symfony forums.
It would seem the helpers aren't creating two things that you need for the sqlite implementation to work correctly.
Firstly, in the parameters.ini for the project (tweak to suit):
database_path=%kernel.root_dir%/config/MyDatabase.db
Secondly, in the config.yml file in the doctrine.dbal section, add the following:
path: %database_path%
Then run:
php app/console doctrine:database:create
and it should generate the database for you in the app/config folder.
You will need to run:
php app/console doctrine:schema:create
to create the schema. Re-run the code that didn't work and it should work fine.
The FOSUserBundle is developed in sync to the symfony master.
Be sure to updated both, symfony and the FOSUserBundle.
Drop the schema (php app/console doctrine:schema:drop --force)
Manually check the db whether the schema is dropped. Otherwise do it per hand.
Clear the cache and ensure that it is writeable.
Run php app/console doctrine:schema:update --force again.

Resources