fix(widgets): add multi-database compatibility to all dashboard widgets

Replace SQLite-specific functions with database-agnostic expressions to support
  MySQL, SQLite, PostgreSQL, and SQL Server across all Filament dashboard widgets.

  - Fix strftime() date formatting in SubscriptionMetrics, RevenueMetrics, and TrialPerformance
  - Fix CAST AS REAL syntax in ChurnAnalysis widget
  - Add getDateFormatExpression() method for date function compatibility
  - Add getCastExpression() method for CAST syntax compatibility
  - Support MySQL/MariaDB, SQLite, PostgreSQL, and SQL Server drivers
  - Maintain identical functionality across all database types

  Fixes multiple SQLSTATE[42000] syntax errors when using MySQL/MariaDB databases.
This commit is contained in:
idevakk
2025-12-02 07:09:30 -08:00
parent 659325c01d
commit e60973c391
4 changed files with 67 additions and 5 deletions

View File

@@ -134,9 +134,11 @@ class TrialPerformance extends ChartWidget
private function getTrialExtensionsTrend(): array
{
$dateFormat = $this->getDateFormatExpression();
return TrialExtension::query()
->select(
DB::raw("strftime('%Y-%m', trial_extensions.granted_at) as month"),
DB::raw("{$dateFormat} as month"),
DB::raw('COUNT(*) as extensions'),
DB::raw('SUM(extension_days) as total_days')
)
@@ -152,6 +154,19 @@ class TrialPerformance extends ChartWidget
->toArray();
}
private function getDateFormatExpression(): string
{
$connection = DB::connection()->getDriverName();
return match ($connection) {
'sqlite' => "strftime('%Y-%m', trial_extensions.granted_at)",
'mysql', 'mariadb' => "DATE_FORMAT(trial_extensions.granted_at, '%Y-%m')",
'pgsql' => "TO_CHAR(trial_extensions.granted_at, 'YYYY-MM')",
'sqlsrv' => "FORMAT(trial_extensions.granted_at, 'yyyy-MM')",
default => "DATE_FORMAT(trial_extensions.granted_at, '%Y-%m')", // fallback to MySQL format
};
}
private function getAverageTrialLength(): float
{
return Subscription::query()