Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrations not being generated, but schema recognized #1419

Closed
rkeet opened this issue Mar 20, 2024 · 4 comments
Closed

Migrations not being generated, but schema recognized #1419

rkeet opened this issue Mar 20, 2024 · 4 comments

Comments

@rkeet
Copy link

rkeet commented Mar 20, 2024

Bug Report

Q A
BC Break no
Version 3.3.0
composer.json
{
    "type": "project",
    "license": "proprietary",
    "minimum-stability": "stable",
    "prefer-stable": true,
    "require": {
        "php": ">=8.3",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "api-platform/core": "^3.2",
        "doctrine/dbal": "^3",
        "doctrine/doctrine-bundle": "^2.11",
        "doctrine/doctrine-migrations-bundle": "^3.3",
        "doctrine/orm": "^3.1",
        "nelmio/cors-bundle": "^2.4",
        "phpdocumentor/reflection-docblock": "^5.3",
        "phpstan/phpdoc-parser": "^1.26",
        "ramsey/uuid-doctrine": "^2.0",
        "symfony/asset": "^7.0",
        "symfony/console": "^7.0",
        "symfony/dotenv": "^7.0",
        "symfony/expression-language": "^7.0",
        "symfony/flex": "^2",
        "symfony/framework-bundle": "^7.0",
        "symfony/property-access": "^7.0",
        "symfony/property-info": "^7.0",
        "symfony/runtime": "^7.0",
        "symfony/security-bundle": "^7.0",
        "symfony/serializer": "^7.0",
        "symfony/twig-bundle": "^7.0",
        "symfony/validator": "^7.0",
        "symfony/yaml": "^7.0",
        "webonyx/graphql-php": "^15.11"
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.5",
        "phpstan/phpstan": "^1.10",
        "phpunit/phpunit": "^11.0",
        "roave/security-advisories": "dev-latest",
        "squizlabs/php_codesniffer": "^3.9",
        "symfony/phpunit-bridge": "^7.0"
    },
    "config": {
        "allow-plugins": {
            "php-http/discovery": true,
            "symfony/flex": true,
            "symfony/runtime": true
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/",
            "Fixtures\\": "fixtures/"
        }
    },
    "replace": {
        "symfony/polyfill-ctype": "*",
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php72": "*",
        "symfony/polyfill-php73": "*",
        "symfony/polyfill-php74": "*",
        "symfony/polyfill-php80": "*",
        "symfony/polyfill-php81": "*",
        "symfony/polyfill-php82": "*"
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install %PUBLIC_DIR%": "symfony-cmd"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "allow-contrib": false,
            "require": "^7.0"
        }
    }
}

Summary

Multiple problems with the same result:

Workaround: run bin/console doctrine:database:create, which will create the whole database.

Additional (likely related) issues and observations:

  • Running bin/console doctrine:schema:validate correctly validates the schema. Showing valid if the schema is matching entities and invalid if a table is deleted.
  • running bin/console doctrine:schema:dump-schema correctly dumps the database schema into a migration.

Current behavior

No migrations are being generated (up or down).

How to reproduce

  • Use ApiPlatform (install using composer.json included above)
  • Configure Doctrine to use XML entity mapping and use a directory for these files (e.g. ./config/doctrine)
    • Mapping in ./config/packages/doctrine.yml:
          mappings:
              App:
                  type: xml
                  is_bundle: false
                  dir: '%kernel.project_dir%/config/doctrine'
                  prefix: 'App'
                  alias: App
  • Have migrations stored in ./migrations, ensure config matches in ./config/packages/doctrine_migrations.yml -> 'DoctrineMigrations': '%kernel.project_dir%/migrations'
  • Create an Entity class. Have the XML mapping in above mentioned directory.

Expected behavior

  • Up and down migrations being generated

Full config/classes used:

compose.yml - extract for PostgreSQL
# Connection String  in .env file: 
# DATABASE_URL="postgresql://postgres:secret_root_password@127.0.0.1:5432/local_db_server?serverVersion=16.2&charset=utf8"

version: "3.8"
services:

    database:
        image: postgres:16.2-alpine3.19
        command: ["postgres", "-c", "log_statement=all"]
        volumes:
            - db_data:/var/lib/postgresql
        networks:
            - default
        ports:
            - ${DB_PORT:-5432}:5432
        environment:
            # Local IDE, use POSTGRES_DB & POSTGRES_PASSWORD
            # Local IDE user: postgres
            # Local IDE host & port are "localhost" and port from "ports" above
            - POSTGRES_DB=local_db_server
            - POSTGRES_PASSWORD=secret_root_password
            # Don't use the below password
            - POSTGRES_ROOT_PASSWORD=root_user_pass
        restart: unless-stopped

volumes:
    db_data:
src/Event/Event.php
namespace App\Event;

use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;

class Event
{
    private ?int $id = null;
    public UuidInterface $uuid;

    public function __construct(
        public string $name
    ) {
        $this->setName($name);
        $this->uuid = Uuid::uuid4();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getUuid(): UuidInterface
    {
        return $this->uuid;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): void
    {
        $this->name = $name;
    }
}
config/doctrine/Event.Event.orm.xml
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                          https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

    <entity name="App\Event\Event" table="events">
        <id name="id" type="integer">
            <generator strategy="IDENTITY"/>
            <options>
                <option name="unsigned">1</option>
            </options>
        </id>

        <field name="uuid" type="uuid" unique="true"/>
        <field name="name"/>

    </entity>

</doctrine-mapping>
@stof
Copy link
Member

stof commented Mar 20, 2024

bin/console doctrine:migrations:generate is explicitly about generating an empty migration. So I don't understand why you consider that a problem.

@rkeet
Copy link
Author

rkeet commented Mar 20, 2024

@stof Wow, you're here fast. Apologies for mentioning the :generate, was still updating the main thing (verifying what I wrote). And indeed, you're correct, that one is for empties, the report is only about :diff not generating anything (corrected now).

@stof
Copy link
Member

stof commented Mar 20, 2024

Does the table of your entity already exist in the database at the time you run the diff command ? If yes, getting an empty up method is totally expected as the command is about generating a migration with the diff between your current database schema and the expected schema of the mapping (btw, in such case, the down migration should also be empty if there were not the public about CREATE SCHEMA public, which would then not create a migration at all but display a message saying that there is no difference).

doctrine:schema:dump-schema is about creating a single migration creating the whole schema (which is meant to be used in conjunction with the rollup command to replace a bunch of migration with a single one). this one indeed does not have the CREATE SCHEMA public bug because it does not rely on introspecting the database at all.

@rkeet
Copy link
Author

rkeet commented Mar 20, 2024

It was happening when the DB was empty and with only the doctrine_migration_versions table present.

However:

Sincy my previous comment I made some changes but also deleted and restarted the whole environment (removed containers/volumes), and now it all works fine. I'm actually unsure as to what I changed with regards to entities or Doctrine configuration.

Sorry to have wasted your time; closing as no longer reproducable (on my machine).

@rkeet rkeet closed this as completed Mar 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants