Dockerise Symfony 和 PostgreSQL 数据库,产品中没有数据,开发中正常

问题描述 投票:0回答:1

我在 docker 容器(php-fpm-alpine)中有一个 symfony 6.3 Web 应用程序,它使用了 mysql 容器。 将 mysql 数据库迁移到 postgresql 后,我更新 .env

DATABASE_URL
以切换到 postgresql 数据库,它在 dev 中工作正常,它可以检索数据库中的数据! 我想推送到生产环境,如果我首先没有正确设置 postgresql 密码(错误 500 docker 日志返回:
connection to server at "my_postgres_service_name" (x.x.x.x), port xxxx failed: FATAL:  password authentication failed for user
,正确设置后,我没有更多错误......但我没有检索任何数据!

        php bin/console doctrine:mapping:info returns all entities [OK]
        php bin/console doctrine:schema:validate returns :  

Mapping
-------

 [FAIL] The entity-class App\Entity\Sample mapping is invalid:
 * The field App\Entity\Sample#result is on the inverse side of a bi-directional relationship, but the specified mappedBy association on the target-entity App\Entity\Result#sample does not contain the required 'inversedBy="result"' attribute.


Database
--------

 [OK] The query yielded an empty result set.

修正后:

Mapping
-------                                                                                                                      
 [OK] The mapping files are correct.                                                                                    
Database
--------

[critical] Error thrown while running command "'d:s:v'". Message: "Environment variable not found: "MESSENGER_TRANSPORT_DSN"."

In EnvVarProcessor.php line 187:
                                                              
  Environment variable not found: "MESSENGER_TRANSPORT_DSN".  

2023-10-20T13:21:49+00:00 [info] User Deprecated: The annotation mapping driver is deprecated and will be removed in Doctrine ORM 3.0, please migrate to the attribute or XML driver. (AnnotationDriver.php:68 called by App_KernelDevDebugContainer.php:1355, https://github.com/doctrine/orm/issues/10098, package doctrine/orm)
2023-10-20T13:21:49+00:00 [info] User Deprecated: Column::setCustomSchemaOptions() is deprecated. Use setPlatformOptions() instead. (Column.php:424 called by Column.php:92, https://github.com/doctrine/dbal/pull/5476, package doctrine/dbal)

但在开发中,它总是运行良好。

我的 PHP 版本不同,所以我将 dev 和 prod 切换到 php 8.2.11,而没有对我的问题进行任何更改

我检查了env文件,更改了DATABASE_URL数据库密码 - >错误500,放回去,没有错误......但总是没有数据

我测试了 DBAL 访问 SELECT * FROMan_usual_table -> ok en dev,错误 404in prod

如果我连接到数据库容器

docker exec -it database_postgresql_container psql -U read-write-username database-name
并运行上述请求,它会返回正确的记录,因此使用的用户角色具有正确的权限

正如解释en https://stackoverflow.com/a/49045589/6614155,我尝试在实体中添加

@ORM\Table(schema="my-schema-name")
,开发中没有更改(始终正确运行),产品中没有更改(始终没有数据)

我还有一个 postgREST 容器,可以在 postgresql 数据库上使用 API,在开发和生产中具有相同的端口。

我找了好几天也没明白。欢迎帮助!

编辑: 为了摆脱 symfony 和主义,我使用最少的代码创建了一个文件来读取表格。如果我在我的开发主机上运行它,那就没问题,但如果我从我的容器运行它,我会收到错误

Call to undefined function pg_connect()

phpinfo() 返回我的(开发)主机(使用 phpbrew 安装的 php 与 dev/prod 中的版本相同):

    Configure Command =>  './configure' … '--with-pgsql=/usr/bin' '--with-pdo-pgsql=/usr/bin'

    PDO

    PDO support => enabled 

    PDO drivers => mysql, pgsql pdo_mysql
    pdo_pgsql

    PDO Driver for PostgreSQL => enabled

    PostgreSQL(libpq) Version => 14.9
…

在 docker php 容器(开发和生产)中:

我有

    PDO

    PDO support => enabled 

    PDO drivers => mysql, pgsql pdo_mysql
    pdo_pgsql

    PDO Driver for PostgreSQL => enabled

    PostgreSQL(libpq) Version => 15.4

但是没有

--with-pdo-pgsql
也没有
--with-pgsql

即使我已经进入了我的 Dockerfile :

    RUN apk --no-cache update \ 
    && apk --no-cache add bash icu-dev libzip-dev zip postgresql-dev \ 
    && docker-php-ext-configure intl \ 
    && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \ 
    && docker-php-ext-install pdo pdo_pgsql intl zip \ 
    && echo "Europe/Paris" > /etc/timezone

我在 php.ini 中没有进行任何更改,但我理解正确,docker-php-ext-configure 或 install 就可以了

即使这是一个线索,它也不能解释为什么它在开发中有效而不是在生产中有效!

postgresql symfony docker-compose doctrine-orm
1个回答
0
投票

经过多次测试,我在不使用 symfony/doctrine 的情况下运行了一个到 postgres 的简单 php 连接,基于 https://www.php.net/manual/en/pgsql.examples-basic.php 和同样的问题,没有数据,没有错误。

所以我连接到我的 postgresql 容器,运行

 \dn
          List of schemas
   Name   |          Owner          
----------+-------------------------
my-schema-name | <user_name>
 public   | postgres
(2 rows)

<my_data_base_name>=# select * from period_list; # request on default schema -> public !!!!
 id | fr_name | gb_name | wiki_data_alignment | bnf_alignment
----+---------+---------+---------------------+---------------
(0 rows)

<my_data_base_name>=# select * from my-schema-name.period_list; # if I define my app schema name, I have got correct datas
 id |        fr_name        |       gb_name       | wiki_data_alignment | bnf_alignment
----+-----------------------+---------------------+---------------------+---------------
  1 | Paléolithique         | Paleolithic         | Q40203              | 
  2 | …

并且理解,我可能会犯一个错误,而不是在公共模式中定义相同的表。

解决方案:

DROP SCHEMA public CASCADE; # delete public default schema, unused
ALTER DATABASE <my_data_base_name> SET search_path = <my-user-name>,<my-schema-name> 

第二行是一个解决方法,我在 SF 中测试

@ORM\Table('my-schema-name.my-table')
,但在产品中不起作用(可能是缓存问题)......我应该找到正确的解决方案来定义 SF 中的模式。

有关对我有帮助的默认架构的链接 https://stackoverflow.com/a/9067777/6614155

© www.soinside.com 2019 - 2024. All rights reserved.