Skip to content

Commit 91967a8

Browse files
committed
fix: default value for boolean
1 parent 20cce06 commit 91967a8

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/SQLLoader.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function createColumnsFromHeaders(string $table, array $columns): array
103103
]))->pluck('name')->toArray();
104104

105105
$booleans = $schemaColumns->filter(
106-
fn ($column) => $column['nullable'] === false && $column['type'] === 'char'
106+
fn ($column) => $column['nullable'] === false && $column['type'] === 'char' && $column['length'] === 1
107107
)->pluck('name')->toArray();
108108

109109
foreach ($columns as $key => $column) {
@@ -117,6 +117,7 @@ public function createColumnsFromHeaders(string $table, array $columns): array
117117

118118
if (in_array($column, $booleans)) {
119119
$default = trim((string) $schemaColumns->where('name', $column)->first()['default']);
120+
$default = $default ?: "'0'"; // set value to 0 if default is empty since column is not nullable
120121
$columns[$key] = "{$escapedColumn} \"DECODE(:{$column}, '', {$default}, :{$column})\"";
121122

122123
continue;

tests/Feature/SQLLoaderTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,25 @@
155155
assertStringContainsString("\"EMAIL\",\n", $controlFile);
156156
assertStringContainsString('"PHONE" FILLER', $controlFile);
157157
assertStringContainsString('"CREATED_AT" DATE', $controlFile);
158-
assertStringContainsString("\"IS_ACTIVE\" \"DECODE(:is_active, '', '1', :is_active)\"\n", $controlFile);
158+
assertStringContainsString("\"IS_ACTIVE\" \"DECODE(:is_active, '', '1', :is_active)\"", $controlFile);
159+
});
160+
161+
test('it can detect BOOLEAN columns and set the default value to 0 if no default was defined', function () {
162+
Process::fake();
163+
164+
$loader = new SQLLoader();
165+
$loader->inFile(__DIR__.'/../data/filler.dat')
166+
->as('users.ctl')
167+
->withHeaders()
168+
->into('users_bool_no_default')
169+
->execute();
170+
171+
$controlFile = $loader->buildControlFile();
172+
assertStringContainsString("\"NAME\",\n", $controlFile);
173+
assertStringContainsString("\"EMAIL\",\n", $controlFile);
174+
assertStringContainsString('"PHONE" FILLER', $controlFile);
175+
assertStringContainsString('"CREATED_AT" DATE', $controlFile);
176+
assertStringContainsString("\"IS_ACTIVE\" \"DECODE(:is_active, '', '0', :is_active)\"", $controlFile);
159177
});
160178

161179
test('it accepts withHeader on input file with wildcard', function () {

0 commit comments

Comments
 (0)