skip to Main Content

I follow the book the fast track, chapter ‘8.7 Migrer la base de données’.

I have my 2 Entities classes properly generated via symfony console make:entity :

ls -1 src/Entity/Co*
src/Entity/Comment.php
src/Entity/Conference.php

https://pastebin.com/6RJmQTEg
https://pastebin.com/XwZ7csS3

Ran symfony console make:migration, get only :


<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use DoctrineDBALSchemaSchema;
use DoctrineMigrationsAbstractMigration;

/**
 * Auto-generated Migration: Please modify to your needs!
 */
final class Version20200322201522 extends AbstractMigration
{
    public function getDescription() : string
    {
        return '';
    }

    public function up(Schema $schema) : void
    {
        // this up() migration is auto-generated, please modify it to your needs

    }

    public function down(Schema $schema) : void
    {
        // this down() migration is auto-generated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on 'postgresql'.');

        $this->addSql('CREATE SCHEMA public');
    }
}

Why there’s no SQL code generated ?

My .env:

APP_ENV=dev
APP_SECRET=xxx
DATABASE_URL=postgresql://127.0.0.1:5432/db?serverVersion=11&charset=utf8`

The command symfony run psql run well; I get psql prompt:

[mevatlave:~/sources/guestbook] master* ± symfony run psql
psql (11.7 (Debian 11.7-0+deb10u1))
Type "help" for help.

main=# 
symfony -V
Symfony CLI version v4.13.3 (Thu Mar 19 15:24:08 UTC 2020)
php bin/console debug:container --env-vars

Symfony Container Environment Variables
=======================================

 -------------- --------------- ---------------------------------------------------------------- 
  Name           Default value   Real value                                                      
 -------------- --------------- ---------------------------------------------------------------- 
  APP_SECRET     n/a             "4ec3efda62f1b3001e78459335893c3f"                              
  DATABASE_URL   n/a             "postgresql://127.0.0.1:5432/db?serverVersion=11&charset=utf8"  
 -------------- --------------- ---------------------------------------------------------------- 

 // Note real values might be different between web and CLI. 

Never edited Symfony config :

cat config/packages/doctrine.yaml
doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'

        # IMPORTANT: You MUST configure your server version,
        # either here or in the DATABASE_URL env var (see .env file)
        #server_version: '5.7'
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'AppEntity'
                alias: App

2

Answers


  1. Chosen as BEST ANSWER

    OK, found a bug in the book, the DSN is wrong :

    DATABASE_URL=postgresql://127.0.0.1:5432/db?serverVersion=11&charset=utf8

    Now : DATABASE_URL=postgresql://main:[email protected]:5432/db?serverVersion=11&charset=utf8

    Lack of username/password. Now symfony console make:migration works well


  2. Try clearing your cache first.

    Otherwise try this:
    You need doctrine:migrations:diff. That will generate a new migration pre-filled with the SQL that you would get from a doctrine:schema:update

    So:

    bin/console doctrine:migrations:diff
    OR
    bin/console d:m:d
    

    Then you go check the newly created file and if all is ok you execute it with:

    // (to be 100% sure take a backup of your database before doing:)
    
    bin/console doctrine:migrations:migrate
    OR
    bin/console d:m:m
    

    If you then want to go back because you made a mistake or whatever

    // Replace 20190215095613 with your migration number!
    bin/console doctrine:migrations:execute --down 20190215095613
    
    • Then you can modify the migration and again execute “bin/console d:m:m”
    • Or throw away the migration and start over with “bin/console d:m:d”
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search