Improve installed database detection

This commit is contained in:
Darko Gjorgjijoski
2024-02-04 19:37:09 +01:00
parent 8981066b27
commit aa68332cf6
13 changed files with 161 additions and 49 deletions

View File

@@ -6,6 +6,7 @@ use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use InvoiceShelf\Models\CompanySetting;
use InvoiceShelf\Models\RecurringInvoice;
use InvoiceShelf\Space\InstallUtils;
class Kernel extends ConsoleKernel
{
@@ -28,7 +29,7 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
if (\Storage::disk('local')->has('database_created')) {
if (InstallUtils::isDbCreated()) {
$schedule->command('check:invoices:status')
->daily();

View File

@@ -4,6 +4,7 @@ namespace InvoiceShelf\Http\Controllers\V1\Installation;
use Illuminate\Http\Request;
use InvoiceShelf\Http\Controllers\Controller;
use InvoiceShelf\Space\InstallUtils;
class FinishController extends Controller
{
@@ -14,7 +15,7 @@ class FinishController extends Controller
*/
public function __invoke(Request $request)
{
\Storage::disk('local')->put('database_created', 'database_created');
InstallUtils::createDbMarker();
return response()->json(['success' => true]);
}

View File

@@ -5,17 +5,18 @@ namespace InvoiceShelf\Http\Controllers\V1\Installation;
use Illuminate\Http\Request;
use InvoiceShelf\Http\Controllers\Controller;
use InvoiceShelf\Models\Setting;
use InvoiceShelf\Space\InstallUtils;
class OnboardingWizardController extends Controller
{
/**
* Handle the incoming request.
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function getStep(Request $request)
{
if (! \Storage::disk('local')->has('database_created')) {
if (! InstallUtils::dbMarkerExists()) {
return response()->json([
'profile_complete' => 0,
]);

View File

@@ -4,6 +4,7 @@ namespace InvoiceShelf\Http\Middleware;
use Closure;
use InvoiceShelf\Models\FileDisk;
use InvoiceShelf\Space\InstallUtils;
class ConfigMiddleware
{
@@ -15,7 +16,7 @@ class ConfigMiddleware
*/
public function handle($request, Closure $next)
{
if (\Storage::disk('local')->has('database_created')) {
if (InstallUtils::isDbCreated()) {
if ($request->has('file_disk_id')) {
$file_disk = FileDisk::find($request->file_disk_id);
} else {

View File

@@ -4,6 +4,7 @@ namespace InvoiceShelf\Http\Middleware;
use Closure;
use InvoiceShelf\Models\Setting;
use InvoiceShelf\Space\InstallUtils;
class InstallationMiddleware
{
@@ -15,14 +16,12 @@ class InstallationMiddleware
*/
public function handle($request, Closure $next)
{
if (! \Storage::disk('local')->has('database_created')) {
return redirect('/installation');
}
if (\Storage::disk('local')->has('database_created')) {
if (Setting::getSetting('profile_complete') !== 'COMPLETED') {
return redirect('/installation');
if (! InstallUtils::isDbCreated() || Setting::getSetting('profile_complete') !== 'COMPLETED') {
if (InstallUtils::dbMarkerExists()) {
InstallUtils::deleteDbMarker();
}
return redirect('/installation');
}
return $next($request);

View File

@@ -4,6 +4,7 @@ namespace InvoiceShelf\Http\Middleware;
use Closure;
use InvoiceShelf\Models\Setting;
use InvoiceShelf\Space\InstallUtils;
class RedirectIfInstalled
{
@@ -15,7 +16,7 @@ class RedirectIfInstalled
*/
public function handle($request, Closure $next)
{
if (\Storage::disk('local')->has('database_created')) {
if (InstallUtils::dbMarkerExists()) {
if (Setting::getSetting('profile_complete') === 'COMPLETED') {
return redirect('login');
}

View File

@@ -3,8 +3,8 @@
namespace InvoiceShelf\Providers;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
use InvoiceShelf\Space\InstallUtils;
class AppServiceProvider extends ServiceProvider
{
@@ -18,7 +18,7 @@ class AppServiceProvider extends ServiceProvider
Paginator::useBootstrapThree();
$this->loadJsonTranslationsFrom(resource_path('scripts/locales'));
if (\Storage::disk('local')->has('database_created') && Schema::hasTable('abilities')) {
if (InstallUtils::isDbCreated()) {
$this->addMenus();
}
}

View File

@@ -4,7 +4,6 @@ namespace InvoiceShelf\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Schema;
class ViewServiceProvider extends ServiceProvider
{
@@ -25,12 +24,10 @@ class ViewServiceProvider extends ServiceProvider
*/
public function boot()
{
if (\Storage::disk('local')->has('database_created') && Schema::hasTable('settings')) {
View::share('login_page_logo', get_app_setting('login_page_logo'));
View::share('login_page_heading', get_app_setting('login_page_heading'));
View::share('login_page_description', get_app_setting('login_page_description'));
View::share('admin_page_title', get_app_setting('admin_page_title'));
View::share('copyright_text', get_app_setting('copyright_text'));
}
View::share('login_page_logo', get_app_setting('login_page_logo'));
View::share('login_page_heading', get_app_setting('login_page_heading'));
View::share('login_page_description', get_app_setting('login_page_description'));
View::share('admin_page_title', get_app_setting('admin_page_title'));
View::share('copyright_text', get_app_setting('copyright_text'));
}
}

View File

@@ -0,0 +1,86 @@
<?php
namespace InvoiceShelf\Space;
use League\Flysystem\FilesystemException;
class InstallUtils
{
/**
* Check if database is created
*
* @return bool|int|string
*/
public static function isDbCreated()
{
return self::tableExists('users');
}
/**
* Check if database is created
*
* @return bool|int|string
*/
public static function tableExists($table)
{
static $cache = [];
if (isset($cache[$table])) {
return $cache[$table];
}
try {
$flag = self::dbMarkerExists();
if ($flag) {
$flag &= \Schema::hasTable($table);
}
} catch (\Illuminate\Database\QueryException $e) {
$flag = false;
} catch (\Exception $e) {
$flag = false;
}
$cache[$table] = $flag;
return $flag;
}
/**
* Check if database created marker exists
*
* @return bool
*/
public static function dbMarkerExists()
{
try {
$flag = \Storage::disk('local')->has('database_created');
} catch (FilesystemException $e) {
$flag = false;
}
return $flag;
}
/**
* Creates the database marker
*
* @return void
*/
public static function createDbMarker()
{
\Storage::disk('local')->put('database_created', 'database_created');
}
/**
* Deletes the database marker
*
* @return void
*/
public static function deleteDbMarker()
{
try {
\Storage::disk('local')->delete('database_created');
} catch (\Exception $e) {
}
}
}

View File

@@ -5,6 +5,7 @@ use InvoiceShelf\Models\CompanySetting;
use InvoiceShelf\Models\Currency;
use InvoiceShelf\Models\CustomField;
use InvoiceShelf\Models\Setting;
use InvoiceShelf\Space\InstallUtils;
/**
* Get company setting
@@ -13,9 +14,11 @@ use InvoiceShelf\Models\Setting;
*/
function get_company_setting($key, $company_id)
{
if (\Storage::disk('local')->has('database_created')) {
return CompanySetting::getSetting($key, $company_id);
if (! InstallUtils::isDbCreated()) {
return null;
}
return CompanySetting::getSetting($key, $company_id);
}
/**
@@ -26,9 +29,11 @@ function get_company_setting($key, $company_id)
*/
function get_app_setting($key)
{
if (\Storage::disk('local')->has('database_created')) {
return Setting::getSetting($key);
if (! InstallUtils::isDbCreated()) {
return null;
}
return Setting::getSetting($key);
}
/**
@@ -38,22 +43,23 @@ function get_app_setting($key)
*/
function get_page_title($company_id)
{
if (! InstallUtils::isDbCreated()) {
return null;
}
$routeName = Route::currentRouteName();
$pageTitle = null;
$defaultPageTitle = 'InvoiceShelf - Self Hosted Invoicing Platform';
if (\Storage::disk('local')->has('database_created')) {
if ($routeName === 'customer.dashboard') {
$pageTitle = CompanySetting::getSetting('customer_portal_page_title', $company_id);
return $pageTitle ? $pageTitle : $defaultPageTitle;
}
$pageTitle = Setting::getSetting('admin_page_title');
if ($routeName === 'customer.dashboard') {
$pageTitle = CompanySetting::getSetting('customer_portal_page_title', $company_id);
return $pageTitle ? $pageTitle : $defaultPageTitle;
}
$pageTitle = Setting::getSetting('admin_page_title');
return $pageTitle ? $pageTitle : $defaultPageTitle;
}
/**

View File

@@ -0,0 +1,23 @@
<?php
use Illuminate\Database\Migrations\Migration;
use InvoiceShelf\Models\Setting;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Setting::setSetting('version', '1.0.0');
}
/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};

View File

@@ -6,6 +6,7 @@ use Illuminate\Database\Seeder;
use InvoiceShelf\Models\Address;
use InvoiceShelf\Models\Setting;
use InvoiceShelf\Models\User;
use InvoiceShelf\Space\InstallUtils;
class DemoSeeder extends Seeder
{
@@ -24,6 +25,6 @@ class DemoSeeder extends Seeder
Setting::setSetting('profile_complete', 'COMPLETED');
\Storage::disk('local')->put('database_created', 'database_created');
InstallUtils::createDbMarker();
}
}

View File

@@ -308,23 +308,22 @@ async function checkUpdate() {
isCheckingforUpdate.value = true
let response = await axios.get('/api/v1/check/update')
isCheckingforUpdate.value = false
if (!response.data.version) {
if (!response.data.release) {
notificationStore.showNotification({
title: 'Info!',
type: 'info',
message: t('settings.update_app.latest_message'),
})
return
return;
}
if (response.data) {
updateData.isMinor = response.data.is_minor
updateData.version = response.data.version.version
description.value = response.data.version.description
requiredExtentions.value = response.data.version.extensions
updateData.version = response.data.release.version
description.value = response.data.release.description
requiredExtentions.value = response.data.release.extensions
isUpdateAvailable.value = true
minPhpVesrion.value = response.data.version.minimum_php_version
deletedFiles.value = response.data.version.deleted_files
minPhpVesrion.value = response.data.release.min_php_version
}
} catch (e) {
isUpdateAvailable.value = false
@@ -363,7 +362,6 @@ function onUpdateApp() {
let updateParams = {
version: updateData.version,
installed: currentVersion.value,
deleted_files: deletedFiles.value,
path: path || null,
}
@@ -377,10 +375,7 @@ function onUpdateApp() {
}
// on finish
if (
currentStep.translationKey ==
'settings.update_app.finishing_update'
) {
if (currentStep.translationKey == 'settings.update_app.finishing_update') {
isUpdating.value = false
notificationStore.showNotification({
type: 'success',