Skip to content

Commit

Permalink
Fixed for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PavlosIsaris committed Nov 15, 2024
1 parent bcaee27 commit bd20bea
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions database/helpers/ColumnTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,28 @@

class ColumnTypeHelper {
public static function getColumnType(string $tableName, string $columnName): string {
$databaseName = Config::get('database.connections.mysql.database');
$connection = Config::get('database.default');
$databaseName = Config::get("database.connections.$connection.database");

return DB::selectOne('
SELECT DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = ?
AND TABLE_NAME = ?
AND COLUMN_NAME = ?
', [$databaseName, $tableName, $columnName])->DATA_TYPE;
if ($connection === 'sqlite' || $connection === 'sqlite_testing') {
$result = DB::select("
PRAGMA table_info($tableName)
");
foreach ($result as $column) {
if ($column->name === $columnName) {
return $column->type;
}
}
} else {
return DB::selectOne('
SELECT DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = ?
AND TABLE_NAME = ?
AND COLUMN_NAME = ?
', [$databaseName, $tableName, $columnName])->DATA_TYPE;
}

throw new \Exception("Column $columnName not found in table $tableName");
}
}

0 comments on commit bd20bea

Please sign in to comment.