Skip to content

Commit

Permalink
refactor(global): Adding SQLServer support
Browse files Browse the repository at this point in the history
- Adding make instructions
- Adding docker-compose for MSSQL
- Fix listing issues, force casting type get auth company_id
  • Loading branch information
gnovaro committed Mar 9, 2024
1 parent 2baad84 commit 0b2be78
Show file tree
Hide file tree
Showing 26 changed files with 200 additions and 124 deletions.
18 changes: 16 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,29 @@ LOG_LEVEL=debug
# Database engine
DB_CONNECTION=mysql
#DB_CONNECTION=pgsql
#DB_CONNECTION=sqlsrv

#For local DB
# For Docker
#DB_HOST=crm-db
DB_HOST=127.0.0.1
#DB_PORT=5432

# MySQL / MariaDB
DB_PORT=3306
# Postgres
#DB_PORT=5432
# SQL Server
#DB_PORT=1433

# For SQL Server
#DB_DATABASE=prosperoflow_crm;LoginTimeout=30
DB_DATABASE=prosperoflow_crm
#DB_USERNAME=sa
DB_USERNAME=root
DB_PASSWORD=
# At least 8 characters in length.
# Must include three of the following four categories: uppercase letters, lowercase letters, base 10 digits, and/or
# non-alphanumeric characters.
DB_PASSWORD=Passw0rd!

BROADCAST_DRIVER=log
CACHE_DRIVER=file
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ build: ## Create docker build containers with MariaDB
build-pg: ## Create docker build containers with Postgres
docker-compose -f docker-compose.yml -f docker-compose.postgres.yml -f docker-compose.pgadmin.yml build

build-ms: ## Create docker build containers with MS SQL Server
docker-compose -f docker-compose.yml -f docker-compose.mssql.yml build

up: ## Start docker container with MariaDB
docker-compose -f docker-compose.yml -f docker-compose.mysql.yml -f docker-compose.pma.yml up -d

up-pg: ## Start docker container with Postgres
docker-compose -f docker-compose.yml -f docker-compose.postgres.yml -f docker-compose.pgadmin.yml up -d

up-ms: ## Start docker container with MS SQL Server
docker-compose -f docker-compose.yml -f docker-compose.mssql.yml up -d

down: ## Stop docker container
docker-compose down

Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/Account/AccountDeleteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function delete(Request $request, int $id)
//TODO add security check, add can('account delete')
$account = Account::find($id);
$account->delete();

return redirect()->back();
}
}
1 change: 1 addition & 0 deletions app/Http/Controllers/Account/AccountUpdateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function update(Request $request, int $id)
{
$account = Account::find($id);
$data['account'] = $account;

return view('account.account', $data);
}
}
21 changes: 16 additions & 5 deletions app/Http/Controllers/Api/Brand/BrandCreateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,23 @@ public function __construct(BrandRepository $brandRepository)
* summary="Create a Brand",
* tags={"Brand"},
* security={{"bearerAuth": {} }},
* @OA\Response(
* response="200",
* description="Brand found",
* @OA\JsonContent(ref="#/components/schemas/Brand")
* @OA\RequestBody(
* required=true,
* description="Brand data",
* @OA\JsonContent(
* required={"name"},
* @OA\Property(property="name", type="string", maxLength=80, example="Example Brand")
* ),
* ),
* @OA\Response(response="404", description="Brand not found")
* @OA\Response(
* response=201,
* description="Brand created successfully",
* @OA\JsonContent(ref="#/components/schemas/Brand")
* ),
* @OA\Response(
* response=400,
* description="Invalid input data"
* )
* )
*/
public function create(Request $request): JsonResponse
Expand Down
10 changes: 5 additions & 5 deletions app/Http/Controllers/Customer/CustomerIndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function index(Request $request)
if ($request->ajax()) {
$term = $request->term;

$customers = Customer::where('company_id', Auth::user()->company_id)
$customers = Customer::where('company_id', (int) Auth::user()->company_id)
->where('name', 'LIKE', "%$term%")
->get(['id', 'name as label']);

Expand Down Expand Up @@ -81,13 +81,13 @@ public function index(Request $request)
$data['colors'] = config('color');
$data['bootstrap_colors'] = ['text-bg-primary', 'text-bg-secondary', 'text-bg-success', 'text-bg-danger', 'text-bg-warning', 'text-bg-info'];
$data['countries'] = Country::orderBy('name')->get();
$data['customer_count'] = Customer::where('company_id', Auth::user()->company_id)->count();
$data['customers'] = $customer->getAllByCompanyId(Auth::user()->company_id, $search, $filters, $order_by);
$data['customer_count'] = Customer::where('company_id', (int) Auth::user()->company_id)->count();
$data['customers'] = $customer->getAllByCompanyId((int) Auth::user()->company_id, $search, $filters, $order_by);
$data['search'] = $search;
$data['sellers'] = $user->getAllActiveByCompany(Auth::user()->company_id);
$data['sellers'] = $user->getAllActiveByCompany((int) Auth::user()->company_id);
$data['statuses'] = $customer->getStatus();
// Temporary fix get this from configuration
$data['industries'] = (Auth::user()->company_id == 3) ? $industry->getAllByCompany(Auth::user()->company_id) : $industry->getAll();
$data['industries'] = ((int) Auth::user()->company_id == 3) ? $industry->getAllByCompany((int) Auth::user()->company_id) : $industry->getAll();

return view('customer.index', $data);
}
Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/Lead/LeadIndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ public function index(Request $request)
$data['colors'] = config('color');
$data['bootstrap_colors'] = ['text-bg-primary', 'text-bg-secondary', 'text-bg-success', 'text-bg-danger', 'text-bg-warning', 'text-bg-info'];
$data['countries'] = Country::orderBy('name')->get();
$data['lead_count'] = Lead::where('company_id', Auth::user()->company_id)->count();
$data['leads'] = $lead->getAllByCompanyId(Auth::user()->company_id, $search, $filters, $order_by);
$data['lead_count'] = Lead::where('company_id', (int) Auth::user()->company_id)->count();
$data['leads'] = $lead->getAllByCompanyId((int) Auth::user()->company_id, $search, $filters, $order_by);
$data['search'] = $search;
$data['sellers'] = $user->getAllActiveByCompany(Auth::user()->company_id);
$data['sellers'] = $user->getAllActiveByCompany((int) Auth::user()->company_id);
$data['statuses'] = $lead->getStatus();
// Temporary fix get this from configuration
$data['industries'] = (Auth::user()->company_id == 3) ? $industry->getAllByCompany(Auth::user()->company_id) : $industry->getAll();
$data['industries'] = ((int) Auth::user()->company_id == 3) ? $industry->getAllByCompany((int) Auth::user()->company_id) : $industry->getAll();
$data['filters'] = $filters;

return view('lead.index', $data);
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public function index(Request $request)
$order = new Order();
$lead = new Lead();
$customer = new Customer();
$data['order_count'] = $order->getPendingCount(Auth::user()->company_id);
$data['leads'] = $lead->getLatestByCompany(Auth::user()->company_id, 4);
$data['order_count'] = $order->getPendingCount((int) Auth::user()->company_id);
$data['leads'] = $lead->getLatestByCompany((int) Auth::user()->company_id, 4);
$data['customers'] = $customer
->whereCompanyId(Auth::user()->company_id)
->whereStatus(Customer::IN_PROGRESS)
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Order/OrderIndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class OrderIndexController extends MainController
public function index(Request $request)
{
$order = new Order();
$data['orders'] = $order->getAllActiveByCompany(Auth::user()->company_id);
$data['orders'] = $order->getAllActiveByCompany((int) Auth::user()->company_id);

return view('order/index', $data);
}
Expand Down
10 changes: 5 additions & 5 deletions app/Http/Controllers/Product/ProductIndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function index(Request $request)
if ($request->ajax()) {
$term = $request->term;

$products = Product::where('company_id', Auth::user()->company_id)
$products = Product::where('company_id', (int) Auth::user()->company_id)
->where('name', 'LIKE', "%$term%")
->orWhere('sku', 'LIKE', "%$term%")
->get(['id', 'name as label', 'price']);
Expand All @@ -32,10 +32,10 @@ public function index(Request $request)
$category = new Category();
$brand = new Brand();
$data['search'] = $search;
$data['product_count'] = Product::where('company_id', Auth::user()->company_id)->count();
$data['products'] = $product->getAllByCompanyId(Auth::user()->company_id, $search, $filters);
$data['categories'] = $category->getAllActiveByCompany(Auth::user()->company_id);
$data['brands'] = $brand->getAllActiveByCompany(Auth::user()->company_id);
$data['product_count'] = Product::where('company_id', (int) Auth::user()->company_id)->count();
$data['products'] = $product->getAllByCompanyId((int) Auth::user()->company_id, $search, $filters);
$data['categories'] = $category->getAllActiveByCompany((int) Auth::user()->company_id);
$data['brands'] = $brand->getAllActiveByCompany((int) Auth::user()->company_id);

return view('product/index', $data);
}
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ class Company extends Model

const ACTIVE = 1;

const DEFAULT_COMPANY = 1;

protected $table = 'company';

protected $fillable = [
Expand Down
3 changes: 2 additions & 1 deletion database/migrations/2021_01_01_000001_create_user_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CreateUserTable extends Migration
public function up()
{
Schema::create('user', function (Blueprint $table) {
$table->increments('id');
$table->id();
$table->unsignedBigInteger('company_id')->default(1); // Demo company
$table->string('first_name');
$table->string('last_name');
Expand All @@ -24,6 +24,7 @@ public function up()
$table->string('lang', 2)->default('en');
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function up()
{
Schema::create('category', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->id();
$table->unsignedBigInteger('company_id');
$table->string('name', 30);
$table->timestamps();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AlterUserAddDeletedAt extends Migration
return new class extends Migration
{
/**
* Run the migrations.
Expand All @@ -13,9 +13,13 @@ class AlterUserAddDeletedAt extends Migration
*/
public function up()
{
Schema::table('user', function (Blueprint $table) {
$table->softDeletes()->after('updated_at');
});
// Check if the 'deleted_at' column does not exist
if (! Schema::hasColumn('user', 'deleted_at')) {
// If it doesn't exist, add the 'deleted_at' column
Schema::table('user', function (Blueprint $table) {
$table->softDeletes()->after('updated_at');
});
}
}

/**
Expand All @@ -29,4 +33,4 @@ public function down()
$table->dropColumn('deleted_at');
});
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
*/
public function up()
{
// Then, add the foreign key constraint
Schema::table('user', function (Blueprint $table) {
$table->unsignedBigInteger('id')->change();
$table->foreign('company_id')->on('company')->references('id');
$table->foreign('company_id')->references('id')->on('company');
});
}

Expand All @@ -26,8 +26,9 @@ public function up()
*/
public function down()
{
// Drop the foreign key constraint
Schema::table('user', function (Blueprint $table) {
$table->dropForeign('user_company_id_foreign');
$table->dropForeign(['company_id']);
});
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public function up()
$table->dateTime('schedule_contact')->nullable()->after('zipcode');
$table->unsignedBigInteger('industry_id')->nullable()->after('schedule_contact');
$table->boolean('opt_in')->nullable()->default(true)->after('industry_id');
$table->enum('status', ['open', 'recall', 'quote', 'quoted', 'waiting_for_answer', 'standby', 'closed'])->nullable()->default('open')->after('country_id');
//$table->enum('status', ['open', 'recall', 'quote', 'quoted', 'waiting_for_answer', 'standby', 'closed'])->nullable()->default('open')->after('country_id');
$table->string('status', 20)->default('No contacted')->after('country_id');
});
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
<?php

use Illuminate\Console\Command;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
protected Command $command;

public function __construct(Command $command)
{
$this->command = $command;
}

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('lead', function (Blueprint $table) {
$table->fullText(['name', 'business_name']);
});
// Check if the database driver supports fulltext index creation (e.g., MySQL or PostgreSQL)
$supportedDrivers = ['mysql', 'pgsql'];
$currentDriver = config('database.default');

if (in_array($currentDriver, $supportedDrivers)) {
Schema::table('lead', function (Blueprint $table) {
$table->fullText(['name', 'business_name'], 'lead_fulltext');
});
} else {
// Output a warning indicating that the database driver does not support fulltext index creation
$this->command->info('Warning: Fulltext index creation is not supported by this database driver.');
}
}

/**
Expand All @@ -25,8 +42,17 @@ public function up()
*/
public function down()
{
Schema::table('lead', function (Blueprint $table) {
$table->dropFullText('lead_name_business_name_fulltext');
});
// Check if the database driver supports fulltext index creation
$supportedDrivers = ['mysql', 'pgsql'];
$currentDriver = config('database.default');

if (in_array($currentDriver, $supportedDrivers)) {
Schema::table('lead', function (Blueprint $table) {
$table->dropIndex('lead_name_business_name_fulltext');
});
} else {
// Output a warning indicating that the database driver does not support fulltext index creation
$this->command->info('Warning: Fulltext index creation is not supported by this database driver.');
}
}
};
Loading

0 comments on commit 0b2be78

Please sign in to comment.