Skip to content

Commit

Permalink
set postges add documents
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Mar 25, 2024
1 parent 8f2d56c commit 58ac84c
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .env.github
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=testing
DB_USERNAME=postgres
DB_USERNAME=root
DB_PASSWORD=password

BROADCAST_DRIVER=log
Expand Down
51 changes: 28 additions & 23 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: CI-CD

on: [ push ]
on: [push]

jobs:
ci:
Expand All @@ -13,30 +13,26 @@ jobs:
APP_ENV: testing
BROADCAST_DRIVER: log
services:
postgres:
image: postgres:latest
env:
POSTGRES_PASSWORD: password
POSTGRES_DB: testing
ports:
- 5432/tcp
options: --health-cmd="pg_isready -U postgres" --health-interval=10s --health-timeout=5s --health-retries=5

database:
image: ankane/pgvector:latest
env:
POSTGRES_PASSWORD: password
POSTGRES_USER: root
POSTGRES_DB: testing
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
strategy:
fail-fast: false
matrix:
php-versions: [ "8.2" ]
php-versions: ["8.2"]
steps:
- uses: actions/checkout@v4

- name: Create the PostgreSQL database
run: |
sudo service postgresql start
PGPASSWORD=$POSTGRES_PASSWORD psql -h localhost -U $POSTGRES_USER -tc "SELECT 1 FROM pg_database WHERE datname = 'testing'" | grep -q 1 || PGPASSWORD=$POSTGRES_PASSWORD psql -h localhost -U $POSTGRES_USER -c "CREATE DATABASE testing"
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password

- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
with:
Expand All @@ -45,17 +41,26 @@ jobs:
extensions: mbstring, dom, fileinfo, pgsql, grpc, :psr
coverage: xdebug

- name: PHP Code Style (phpcs)
run: |
composer fix
- name: PHP Code Style (phpcs)
run: |
composer stan
- name: Enable PostgreSQL Extensions
run: |
sudo service postgresql start
sudo -u postgres psql -c "CREATE EXTENSION IF NOT EXISTS vector;" -d testing
PGPASSWORD=$DB_PASSWORD psql -h localhost -U $DB_USERNAME -d $DB_DATABASE -c "CREATE EXTENSION IF NOT EXISTS vector;"
env:
DB_PORT: ${{ job.services.postgres.ports['5432'] }}

- name: Test with phpunit
run: |
npm install && npm run build
XDEBUG_MODE=coverage php artisan test --coverage --min=50
XDEBUG_MODE=coverage php artisan test --coverage --min=50
env:
DB_PORT: ${{ job.services.mysql.ports['3306'] }}
DB_PORT: ${{ job.services.postgres.ports['5432'] }}

cd:
runs-on: ubuntu-latest
Expand Down
16 changes: 16 additions & 0 deletions app/Domains/Documents/StatusEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Domains\Documents;

use App\Helpers\EnumHelperTrait;

enum StatusEnum: string
{
use EnumHelperTrait;

case Pending = 'pending';
case Running = 'running';
case SummaryBuilding = 'summary_building';
case Complete = 'complete';
case Failed = 'failed';
}
26 changes: 26 additions & 0 deletions app/Domains/Documents/TypesEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Domains\Documents;

use App\Helpers\EnumHelperTrait;

/**
* @see settings in config/larachain.php:3
*/
enum TypesEnum: string
{
use EnumHelperTrait;

case WebHook = 'web_hook';
case ScrapeWebPage = 'scrape_web_page';
case PDF = 'pdf';
case CSV = 'csv';
case Txt = 'txt';
case Doc = 'doc';
case Docx = 'docx';
case Xls = 'xls';
case Xlsx = 'xlsx';
case Ppt = 'ppt';
case Pptx = 'pptx';

}
46 changes: 46 additions & 0 deletions app/Helpers/EnumHelperTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Helpers;

use ReflectionClass;

trait EnumHelperTrait
{
public static function enumToKeyValueArray(): array
{
$enumReflection = new ReflectionClass(self::class);
$cases = $enumReflection->getConstants();

$keyValueArray = [];
foreach ($cases as $case) {
$keyValueArray[$case->value] = str($case->name)->headline()->toString();
}

return $keyValueArray;
}

public static function random(): string
{
$enumReflection = new ReflectionClass(self::class);
$cases = $enumReflection->getConstants();
$randomKey = array_rand($cases);

return $cases[$randomKey]->value;
}

public static function selectOptions(): array
{
$enumReflection = new ReflectionClass(self::class);
$cases = $enumReflection->getConstants();

$keyValueArray = [];
foreach ($cases as $case) {
$keyValueArray[] = [
'id' => $case->value,
'name' => str($case->name)->headline()->toString(),
];
}

return $keyValueArray;
}
}
6 changes: 6 additions & 0 deletions app/Models/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
* Class Project
Expand All @@ -31,4 +32,9 @@ public function team(): BelongsTo
{
return $this->belongsTo(Team::class);
}

public function documents(): HasMany
{
return $this->hasMany(Document::class);
}
}
38 changes: 38 additions & 0 deletions app/Models/Document.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Models;

use App\Domains\Documents\StatusEnum;
use App\Domains\Documents\TypesEnum;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
* Class Document
*
* @property int $id
* @property int $collection_id
* @property string|null $summary
* @property string|null $file_path
* @property TypesEnum $type
* @property StatusEnum $status
* @property Collection $collection
*/
class Document extends Model
{
use HasFactory;

protected $guarded = [];

protected $cast = [
'type' => TypesEnum::class,
'status' => StatusEnum::class,
'status' => 'string',
];

public function collection(): BelongsTo
{
return $this->belongsTo(Collection::class);
}
}
2 changes: 1 addition & 1 deletion database/factories/CollectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function definition(): array
'name' => $this->faker->sentence,
'description' => $this->faker->paragraph,
'active' => $this->faker->boolean,
'team_id' => Team::factory()
'team_id' => Team::factory(),
];
}
}
30 changes: 30 additions & 0 deletions database/factories/DocumentFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Database\Factories;

use App\Domains\Documents\StatusEnum;
use App\Domains\Documents\TypesEnum;
use App\Models\Collection;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Document>
*/
class DocumentFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'type' => TypesEnum::random(),
'status' => StatusEnum::random(),
'summary' => $this->faker->text(),
'file_path' => $this->faker->url(),
'collection_id' => Collection::factory(),
];
}
}
34 changes: 34 additions & 0 deletions database/migrations/2024_03_25_020252_create_documents_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use App\Domains\Documents\StatusEnum;
use App\Models\Collection;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('documents', function (Blueprint $table) {
$table->id();
$table->string('type')->nullable();
$table->string('status')->default(StatusEnum::Pending);
$table->longText('summary')->nullable();
$table->string('file_path')->nullable();
$table->foreignIdFor(Collection::class);
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('documents');
}
};
21 changes: 21 additions & 0 deletions tests/Feature/Models/DocumentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Tests\Feature\Models;

use Tests\TestCase;

class DocumentTest extends TestCase
{
/**
* A basic feature test example.
*/
public function test_factory(): void
{
$model = \App\Models\Document::factory()->create();

$this->assertNotNull($model->collection_id);
$this->assertNotNull($model->collection->id);
$this->assertCount(1, $model->collection->documents);
$this->assertNotNull($model->collection->documents()->first()->id);
}
}
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Tests;

use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
use LazilyRefreshDatabase;
}

0 comments on commit 58ac84c

Please sign in to comment.