gpt4 book ai didi

php - SQLite SQLSTATE[HY000] : General error: 8 attempt to write a readonly database

转载 作者:行者123 更新时间:2023-12-03 11:22:57 37 4
gpt4 key购买 nike

我正在使用 SQLite 连接和 Doctrine 迁移来进行 PHPUnit 的功能测试。
我正在 setUp 中从头开始进行数据库迁移方法:

public function setUp()
{
parent::setUp();

@unlink(__DIR__ . '/../../../../../../../var/sqlite.db');

exec('./vendor/bin/doctrine-migrations migrations:migrate --db-configuration=migrations-db-test.php --configuration=migrations_test.yml --no-interaction');
}
然后我可以从数据库中写入/读取。例如。:
public function test_add_event_should_add_event()
{
$service = $this->getAdEventComparativesUpdateService();
$request = AdEventComparativesUpdateServiceRequest::make(self::AD_ID, self::USER_IP);
$response = $service->execute($request);

$this->assertEquals(1, $response->getTotal());
}
它有效。即使我使用相同的参数调用两次服务,它也确实有效。在这种情况下,它只需要第一次写:
public function test_add_two_same_events_should_add_one_event()
{
$service = $this->getAdEventComparativesUpdateService();
$request = AdEventComparativesUpdateServiceRequest::make(self::AD_ID, self::USER_IP);
// Call twice
$service->execute($request);
$response = $service->execute($request);

$this->assertEquals(1, $response->getTotal());
}
当我必须测试两个必须同时编写的调用时,问题就来了:
public function test_add_two_different_events_should_add_two_events()
{
$service = $this->getAdEventComparativesUpdateService();
$request = AdEventComparativesUpdateServiceRequest::make(self::AD_ID, self::USER_IP);
$response = $service->execute($request);

$service = $this->getAdEventComparativesUpdateService();
$request = AdEventComparativesUpdateServiceRequest::make(self::AD_ID, self::OTHER_USER_IP);
$response = $service->execute($request); // **** It fails here

$this->assertEquals(2, $response->getTotal());
}
错误来了:
  1. XXX::test_add_two_different_events_should_add_two_eventDoctrine\DBAL\Exception\ReadOnlyException: An exception occurred while executing 'INSERT INTO xxx (xxx, xxx, xxx, xxx) VALUES (?, ?, ?, ?)' with params [xxx, "xxx", "xxx", "xxx"]:

SQLSTATE[HY000]: General error: 8 attempt to write a readonly database


xxx/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractSQLiteDriver.php:78xxx/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:128xxx/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php:178xxx/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:281xxx/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1014xxx/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:378xxx/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:356xxx/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:235xxx/DoctrineSession.php:32xxx/TransactionalApplicationService.php:39xxx/xxx/test.php:100


我尝试在调用之间更改数据库文件权限,但没有任何变化:
public function test_add_two_different_events_should_add_two_event()
{
$service = $this->getAdEventComparativesUpdateService();
$request = AdEventComparativesUpdateServiceRequest::make(self::AD_ID, self::USER_IP);
$response = $service->execute($request);

chmod(__DIR__ . '/../../../../../../../var', 0777);
chmod(__DIR__ . '/../../../../../../../var/sqlite.db', 0777);
chown(__DIR__ . '/../../../../../../../var', 'www-data');
chgrp(__DIR__ . '/../../../../../../../var', 'www-data');
chown(__DIR__ . '/../../../../../../../var/sqlite.db', 'www-data');
chgrp(__DIR__ . '/../../../../../../../var/sqlite.db', 'www-data');
//die;
// Here I checked the /var sqlite.db permissions. They are 0777

$service = $this->getAdEventComparativesUpdateService();
$request = AdEventComparativesUpdateServiceRequest::make(self::AD_ID, self::OTHER_USER_IP);
$response = $service->execute($request);

$this->assertEquals(2, $response->getTotal());
}
知道错误可能来自哪里吗?每个服务电话都会调用 persist + flush在这种情况下。

最佳答案

很难回答这个问题,因为我们没有真正的 [mre]。最常见的是这是一个权限问题,但看起来您已经探索了这条路线。我想我有预感这里发生了什么:
看起来单元测试在每次测试时都会重新创建数据库,即取消链接数据库文件,然后当打开连接时,如果数据库文件不存在,则会自动创建它。因此,在两次写入尝试的情况下,连接首先打开并保持打开状态。然后数据库文件 unlink 被相同或另一个测试调用,随后的写入命令失败。

关于php - SQLite SQLSTATE[HY000] : General error: 8 attempt to write a readonly database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63189635/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com