Skip to content

Commit 7cbd2e2

Browse files
authored
fix: setting value persistence on SQL Server (#172)
* fix(database): use VARCHAR(MAX) for SQLSRV setting values * fix(database): serialize boolean setting values as strings
1 parent 9f1c23a commit 7cbd2e2

3 files changed

Lines changed: 74 additions & 1 deletion

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodeIgniter\Settings\Database\Migrations;
6+
7+
use CodeIgniter\Database\Forge;
8+
use CodeIgniter\Database\Migration;
9+
use CodeIgniter\Settings\Config\Settings;
10+
11+
class ConvertSqlsrvValueColumn extends Migration
12+
{
13+
private readonly Settings $config;
14+
15+
public function __construct(?Forge $forge = null)
16+
{
17+
$this->config = config('Settings');
18+
$this->DBGroup = $this->config->database['group'] ?? null;
19+
20+
parent::__construct($forge);
21+
}
22+
23+
public function up(): void
24+
{
25+
if ($this->db->getPlatform() !== 'SQLSRV') {
26+
return;
27+
}
28+
29+
$this->forge->modifyColumn($this->config->database['table'], [
30+
'value' => [
31+
'type' => 'VARCHAR',
32+
'constraint' => 'MAX',
33+
'null' => true,
34+
],
35+
]);
36+
}
37+
38+
public function down(): void
39+
{
40+
if ($this->db->getPlatform() !== 'SQLSRV') {
41+
return;
42+
}
43+
44+
$this->forge->modifyColumn($this->config->database['table'], [
45+
'value' => [
46+
'type' => 'TEXT',
47+
'null' => true,
48+
],
49+
]);
50+
}
51+
}

src/Handlers/BaseHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function persistPendingProperties(): void
109109
protected function prepareValue($value)
110110
{
111111
if (is_bool($value)) {
112-
return (int) $value;
112+
return $value ? '1' : '0';
113113
}
114114

115115
if (is_array($value) || is_object($value)) {

tests/BaseHandlerTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests;
6+
7+
use CodeIgniter\Settings\Handlers\ArrayHandler;
8+
use Tests\Support\TestCase;
9+
10+
/**
11+
* @internal
12+
*/
13+
final class BaseHandlerTest extends TestCase
14+
{
15+
public function testPrepareValueSerializesBooleansAsStrings(): void
16+
{
17+
$prepareValue = self::getPrivateMethodInvoker(new ArrayHandler(), 'prepareValue');
18+
19+
$this->assertSame('1', $prepareValue(true));
20+
$this->assertSame('0', $prepareValue(false));
21+
}
22+
}

0 commit comments

Comments
 (0)