用于测试环境的Symfony 4 sqlite数据库创建

时间:2019-05-18 18:59:38

标签: php symfony symfony4

出于集成测试的目的,我试图创建一个用于测试环境的sqlite数据库,但是当我运行php bin/console doctrine:schema:create --env=test时,我得到表已经存在的错误。我猜想是因为它并不是真正在创建新的测试数据库,而是在现有数据库上进行。

好像没有从env.test中读取。

我在config / test中创建了一个doctrine.yaml,如下所示:

doctrine:
dbal:
    driver: 'pdo_sqlite'
    url: 'sqlite:///%kernel.project_dir%/var/data/test.sqlite'

创建sqlite测试数据库缺少什么?

我遇到错误的奇怪事情:

In PDOConnection.php line 90:

 SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'user_menu' already exists 

为什么要使用SQL?不应该将它用于sqlite,对吗?

在config / packages / doctrine.yaml中,我有:

parameters:
# Adds a fallback DATABASE_URL if the env var is not set.
# This allows you to run cache:warmup even if your
# environment variables are not available yet.
# You should not need to change this value.
env(DATABASE_URL): ''

doctrine:
dbal:
    # configure these for your database server
    connections:
            gui:
                driver: 'pdo_mysql'
                server_version: '5.7'
                charset: utf8mb4
                default_table_options:
                    charset: utf8mb4
                    collate: utf8mb4_unicode_ci
                url: '%env(resolve:DATABASE_GUI_URL)%'
            upv6:
                driver: 'pdo_mysql'
                server_version: '5.7'
                charset: utf8mb4
                default_table_options:
                    charset: utf8mb4
                    collate: utf8mb4_unicode_ci
                url: '%env(resolve:DATABASE_UPV6_URL)%'
orm:
    auto_generate_proxy_classes:  true # <- change to true
    proxy_dir:            '%kernel.cache_dir%/doctrine/orm/Proxies'
    proxy_namespace:      Proxies

    entity_managers:
        gui:
            connection: gui
            mappings:
                Gui:
                    is_bundle: false
                    type: annotation
                    dir: '%kernel.project_dir%/src/Entity/Gui'
                    prefix: 'App\Entity\Gui'
                    alias: Gui
        upv6:
            connection: upv6
            mappings:
                Upv6:
                    is_bundle: false
                    type: annotation
                    dir: '%kernel.project_dir%/src/Entity/Upv6'
                    prefix: 'App\Entity\Upv6'
                    alias: Upv6

我的学说设置:

doctrine:
dbal:
    connections:
        gui:
            driver: pdo_mysql
            server_version: '5.7'
            charset: utf8mb4
            default_table_options:
                charset: utf8mb4
                collate: utf8mb4_unicode_ci
            url: '%env(resolve:DATABASE_GUI_URL)%'
            host: localhost
            port: null
            user: root
            password: null
            logging: true
            profiling: true
            options: {  }
            mapping_types: {  }
            slaves: {  }
            shards: {  }
        upv6:
            driver: pdo_mysql
            server_version: '5.7'
            charset: utf8mb4
            default_table_options:
                charset: utf8mb4
                collate: utf8mb4_unicode_ci
            url: '%env(resolve:DATABASE_UPV6_URL)%'
            host: localhost
            port: null
            user: root
            password: null
            logging: true
            profiling: true
            options: {  }
            mapping_types: {  }
            slaves: {  }
            shards: {  }
        default:
            driver: pdo_sqlite
            url: '%env(resolve:DATABASE_URL)%'
            host: localhost
            port: null
            user: root
            password: null
            logging: true
            profiling: true
            options: {  }
            mapping_types: {  }
            default_table_options: {  }
            slaves: {  }
            shards: {  }
    default_connection: default
    types: {  }
orm:
    auto_generate_proxy_classes: true
    proxy_dir: /var/www/symfony/var/cache/test/doctrine/orm/Proxies
    proxy_namespace: Proxies
    entity_managers:
        gui:
            connection: gui
            mappings:
                Gui:
                    is_bundle: false
                    type: annotation
                    dir: /var/www/symfony/src/Entity/Gui
                    prefix: App\Entity\Gui
                    alias: Gui
                    mapping: true
            query_cache_driver:
                type: array
                namespace: null
                cache_provider: null
            metadata_cache_driver:
                type: array
                namespace: null
                cache_provider: null
            result_cache_driver:
                type: array
                namespace: null
                cache_provider: null
            class_metadata_factory_name: Doctrine\ORM\Mapping\ClassMetadataFactory
            default_repository_class: Doctrine\ORM\EntityRepository
            auto_mapping: false
            naming_strategy: doctrine.orm.naming_strategy.default
            quote_strategy: doctrine.orm.quote_strategy.default
            entity_listener_resolver: null
            repository_factory: doctrine.orm.container_repository_factory
            hydrators: {  }
            filters: {  }
        upv6:
            connection: upv6
            mappings:
                Upv6:
                    is_bundle: false
                    type: annotation
                    dir: /var/www/symfony/src/Entity/Upv6
                    prefix: App\Entity\Upv6
                    alias: Upv6
                    mapping: true
            query_cache_driver:
                type: array
                namespace: null
                cache_provider: null
            metadata_cache_driver:
                type: array
                namespace: null
                cache_provider: null
            result_cache_driver:
                type: array
                namespace: null
                cache_provider: null
            class_metadata_factory_name: Doctrine\ORM\Mapping\ClassMetadataFactory
            default_repository_class: Doctrine\ORM\EntityRepository
            auto_mapping: false
            naming_strategy: doctrine.orm.naming_strategy.default
            quote_strategy: doctrine.orm.quote_strategy.default
            entity_listener_resolver: null
            repository_factory: doctrine.orm.container_repository_factory
            hydrators: {  }
            filters: {  }
    resolve_target_entities: {  }

1 个答案:

答案 0 :(得分:1)

此问题是由Symfony合并配置文件的方式引起的。

/config/packages/*.yml> /config/packages/<env>/*.yml

这导致将packages/doctrine.yml中定义的所有连接和实体管理器添加到您的packages/test/doctrine.yml

要查看Symfony将使用的合并配置设置,请运行:

php bin/console --env=test debug:config doctrine

由于运行bin/console --env=test doctrine:schema:create,将尝试为结果配置中存在的实体管理器的所有 ALL 创建架构。

要解决此问题,您需要将环境配置分为prodtestdev,或者使用.env.test更改Syfmony使用的URL。

取决于您需要访问实体经理的方式

示例

  

这只是如何隔离环境的示例   配置,请确保更改应用程序所需的任何值   要求

config / packages / doctrine.yml

parameters:
    # Adds a fallback DATABASE_URL if the env var is not set.
    # This allows you to run cache:warmup even if your
    # environment variables are not available yet.
    # You should not need to change this value.
    env(DATABASE_URL): ''

doctrine:
    orm:
        auto_generate_proxy_classes:  true # <- change to true
        proxy_dir:            '%kernel.cache_dir%/doctrine/orm/Proxies'
        proxy_namespace:      Proxies

config / packages / dev / doctrine.yml

doctrine:
    dbal:
        # configure these for your database server
        connections:
                gui:
                    driver: 'pdo_mysql'
                    server_version: '5.7'
                    charset: utf8mb4
                    default_table_options:
                        charset: utf8mb4
                        collate: utf8mb4_unicode_ci
                    url: '%env(resolve:DATABASE_GUI_URL)%'
                upv6:
                    driver: 'pdo_mysql'
                    server_version: '5.7'
                    charset: utf8mb4
                    default_table_options:
                        charset: utf8mb4
                        collate: utf8mb4_unicode_ci
                    url: '%env(resolve:DATABASE_UPV6_URL)%'        
        entity_managers:
            gui:
                connection: gui
                mappings:
                    Gui:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity/Gui'
                        prefix: 'App\Entity\Gui'
                        alias: Gui
            upv6:
                connection: upv6
                mappings:
                    Upv6:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity/Upv6'
                        prefix: 'App\Entity\Upv6'
                        alias: Upv6

config / packages / test / doctrine.yml

doctrine:
    dbal:
        driver: 'pdo_sqlite'
        url: 'sqlite:///%kernel.project_dir%/var/data/test.sqlite'

    #DEFINE THE ENTITY MANAGERS TO USE THE default CONNECTION
    #orm:
        #since the connections are not the same
        #you need to define your entity managers here...
        #entity_managers:
     #      gui:
     #          connection: default #<---- NOTICE DEFAULT and not gui 
     # (not sure what entity managers are needed for sqlite)
     #...

config / packages / prod / doctrine.yml

imports:
    - { resource: '../dev/doctrine.yaml' }

doctrine:
    orm:
        auto_generate_proxy_classes: false #<-- change to false
        metadata_cache_driver:
            type: service
            id: doctrine.system_cache_provider
        query_cache_driver:
            type: service
            id: doctrine.system_cache_provider
        result_cache_driver:
            type: service
            id: doctrine.result_cache_provider
     #...
相关问题