kitloong / laravel-migrations-generator

Laravel Migrations Generator: Automatically generate your migrations from an existing database schema.
MIT License
2.51k stars 278 forks source link

Type enum already exists #3

Closed msbsh closed 4 years ago

msbsh commented 4 years ago

I'm getting an Type enum already exists exception when trying to execute migrate:generate and don't fully understand, what I'm missing here. See (truncated) stacktrace below.

php artisan migrate:generate -vvv
Using connection: mysql

   Doctrine\DBAL\DBALException

  Type enum already exists.

  at vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:254
    250|      * @return \Doctrine\DBAL\DBALException
    251|      */
    252|     public static function typeExists($name)
    253|     {
  > 254|         return new self('Type ' . $name . ' already exists.');
    255|     }
    256|
    257|     /**
    258|      * @param string $name

  1   vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/TypeRegistry.php:68
      Doctrine\DBAL\DBALException::typeExists("enum")

  2   vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Type.php:246
      Doctrine\DBAL\Types\TypeRegistry::register("enum", Object(KitLoong\MigrationsGenerator\Types\EnumType))

  3   vendor/kitloong/laravel-migrations-generator/src/KitLoong/MigrationsGenerator/Generators/SchemaGenerator.php:90
      Doctrine\DBAL\Types\Type::addType("enum", "KitLoong\MigrationsGenerator\Types\EnumType")

  4   vendor/kitloong/laravel-migrations-generator/src/Xethron/MigrationsGenerator/MigrateGenerateCommand.php:132
      KitLoong\MigrationsGenerator\Generators\SchemaGenerator::initialize("mysql")
kitloong commented 4 years ago

Hello, thank you for report.

Unfortunately I can't reproduce your issue from my end.

The error is due to type enum is registered 2 times, while in the package enum should only register one time which is when command is triggered.

Do you mind to show me your composer.json to see if there is any conflict on this?

Probably 2 migration packages with same command php artisan migrate:generate or something

msbsh commented 4 years ago

Sure thing and thanks for your support. I'm actually fairly new to composer/laravel/artisan in general, so I really much appreciate your support on this.

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": [
        "framework",
        "laravel"
    ],
    "license": "MIT",
    "require": {
        "php": "^7.2.5",
        "fideloper/proxy": "^4.2",
        "fruitcake/laravel-cors": "^1.0",
        "guzzlehttp/guzzle": "^6.3",
        "laravel/framework": "^7.0",
        "laravel/tinker": "^2.0",
        "tcg/voyager": "^1.3"
    },
    "require-dev": {
        "facade/ignition": "^2.0",
        "fzaninotto/faker": "^1.9.1",
        "kitloong/laravel-migrations-generator": "^4.0",
        "mockery/mockery": "^1.3.1",
        "nunomaduro/collision": "^4.1",
        "phpunit/phpunit": "^8.5"
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    },
    "repositories": {
        "hooks": {
            "type": "composer",
            "url": "https://larapack.io"
        }
    }
}
kitloong commented 4 years ago

Thank you for sharing.

The generator has conflict with Larapack\DoctrineSupport\DoctrineSupportServiceProvider

protected function addDoctrineTypes(Connection $connection)
    {
        $name = $connection->getDriverName();

        foreach (Arr::get($this->types, $name, []) as $type => $handler) {
            if (!Type::hasType($type)) {
                Type::addType($type, $handler);
            }

            $connection->getDoctrineConnection()
                ->getDatabasePlatform()
                ->registerDoctrineTypeMapping($type, $type);
        }
    }

I should probably implement Type::hasType too to avoid conflict.

I will push a commit soon to fix this.

kitloong commented 4 years ago

New tag has been released Kindly run composer update kitloong/laravel-migrations-generator to update.