I would like to know about how symfony2 console commands cache:clear and cache:warmup works in regards to changes in doctrine entities.
I've read this http://blog.whiteoctober.co.uk/2014/02/25/symfony2-cache-warmup-explained/ and this https://stackoverflow.com/a/17787070/779320. There it is stated that symfony2 warm-up generates Doctrine proxies.
Thing is, I frequently have a situation after application deploy using capifony where I must run doctrine migrations. After that I always run cache:clear and cache:warmup.
According to the links above (if I understand correctly), if there is any entity addition/deletion, I should run cache:warmup. But what if there is only entity's fields changes. Should I run both cache:clear and cache:warmup commands necessary to be run or just one? Or, which one is necessary? Or, not at all?
Migrations are responsible for persistence layer only. It has nothing to do with EntityProxies, it only makes Database in sync with Model - that's all.
In production, during deployment, you probably run git pull and composer install, which clears the cache, so migrations shoud run just after that.
Reason - after composer install your new code and Model itself is ready to use new fields/entities, but the Database still falls behind, so migrations keeps it in sync.
Related
I am running sonata admin and I have an existing working entity/admin. I've added another 2 columns to the doctrine orm, the entity, the admin and ran the app/console doctrine:schema:update commands to get the fields in to the database successfully.
On local dev environment, this works great.
On production, the new fields aren't saving.
When saving the entity within sonata admin if I manually call $this->isMyNewField() in preUpdate or postUpdate I get the correct result of true or false depending on whether i checked the box or not.
However doctrine doesn't seem to be correctly registering the field at all, i've logged the sql that it is running on update and persist and it literally doesn't even try and do anything with the field whatsoever on either.
If I change the AppKernel to run on 'dev' rather than 'prod' (on production environment) then all works as expected and doctrine picks it up without an issue.
Ive tried clearing doctrine metadata, clearing cache, restarting servers, changing field types, removing in doctrine and re-adding (via the entity orm xml file)
Still no luck. Any other ideas I can try?
Thanks
Kevin
Hi all turns out it was the command
app/console doctrine:cache:clear-metadata
That needed running, I had tried this without success so the big thing I missed off it was the env so the command was
app/console doctrine:cache:clear-metadata --env=prod
Was fine after this, very frustrating
I recently updated my schema in Symphony 2.7 project with Doctrine, but I was still getting an error. It turned out that I had to run:
app/console redis:flushall
In order for the schema to update accordingly and not be mapped incorrectly.
Is there a way for me to implement schema changes without having to kill user sessions when I run redis:flushall?
It's very inconvenient when deploying code to a production server to have to kill user sessions.
Thanks for your help!
I have some beginners questions regarding Symfony 2 which I cannot get clear answers for from previous questions (perhaps because they are genuinely basic)
When you create a new symfony2 project from the command line and specific the database name and passwords, is this meant to automatically create the database (which you can see in phpmyadmin) or does one manually do this.
Following from this, if one creates a number of entities and then uses
doctrine:schema:update
Should the specified tables be automatically created in the database you have specified in the projects "parameters.yml" file.
I have performed "doctrine:schema:update --force" which then gave me
Updating database schema...
Database schema updated successfully! "2" queries were executed
But no tables were created. So I tried again, to see what the message would be...
unknown-ec:35:86:4d:41:5e:symfony simonalice$ php app/console doctrine:schema:update --force
Nothing to update - your database is already in sync with the current entity metadata.
unknown-ec:35:86:4d:41:5e:symfony simonalice$
So clearly Doctrine thinks its in synch - but no tables in phpmyadmin.
Clearly complete beginners stuff....but I would be grateful for some steerage on this from a Symfony 2 veteran.
To answer your questions:
No, creating a new Symfony project will not create your database (or the user connecting to it). You still need to do that and I would recommend you create a dedicated user for your application with suitable permissions. You'll need to use a database user with administrative privileges to do this. For security reasons, it's best to not use your database administrator account with your application. To instruct doctrine to create your database (once you have your db user and connection parameters set), you can run the php app/console doctrine:database:create console command.
Yes, running the doctrine:schema:update console command will generate your database entities, but it won't/can't create your database. You can also use the --force option to apply changes you've made since the last update. These updates will still be bound by any column constraints you've defined, so if for example you change an existing nullable column to not null, you'll get an error if records already exist with null values.
Hope that helps.
We're using Doctrine Migrations to keep the application DB in sync across versions. The app will be installed each time we get a new customer meaning a new DB for each installation.
Now, the new DB will have all DB changes in the migrations file in place but will try to execute the migration files as they are not registered in the migrations table.
What's the best way to handle this situation?
Just after fresh install to fill current schema use this commands:
Create new empty chema of current version
./bin/console doctrine:schema:create
Fill out the migration records with the current version (without actually run the migrations - schema already has current version after first command)
./bin/console doctrine:migrations:version --add --all
This command adds records of the migrations and doctrine:migrations:status will show you that there is no migrations need for the current verions.
Thats all!
As mentioned in my comment, I've successfully created a blank database schema just by using Doctrine migrations. I believe it's necessary to create the empty database first (php app/console doctrine:database:create) and then run the migrations task.
The only potential issue I can think of is that any base data that your app requires to function will either need to be in the migrations files or will need to be inserted separately.
As an aside, running the all the migrations in one go has picked up a couple of small errors that I'd missed when just executing one at a time.
I think you can pick the very first migration class, copy it and rename with just one second before.
So, if your first class is 2016060712284351 you should create a new class called 2016060712284350. This way it is executed just before the real migrations.
In this newly created class you setup your database creating all needed tables. This way, when you run migrations the first thing is done is to create the database with all the tables and then the real migrations are run.
Anyway I'm not sure this is the real way to proceed.
Migrations are needed to update the database schema, but if you are creating a new database for each user, each of the new users will have the new schema as per entities mapping. So a migration is not needed.
You need to run migration only for OLD USERS' DATABASES, am I right?
On production we use amazon opsworks to orchestrate our machines. I've created a chef recipe to deploy new code (so actually creating a new release folder, do things there, then switch symlink to new folder) and another recipe that reconfigures our symfony app changing a yml file when a new database slave is added and so on.
The problem is that the reconfigure stage happens on current running code and since I've to run cache:clear and cache:warmup the requests in the meanwhile fails since they don't find cache files/folders.
How could I clear the cache instantly? Maybe warming it up in another folder and then switching those?
Better solutions?
Found by myself that cache:clear (without --no-warmup) actually does the warmup in another folder and exchanges them on finish.
The reason I wasn't using that was due some library issues on warmup when not using --no-warmup && cache:warmup which seems to be solved now.
Let's see how things works after this change and if I don't get these problems anymore
UPDATE:
I did some tests of firing up new machines, running cache:clear under heavy request load and no php warnings/errors so far.
I think this is a definitive solution for my problem