Skip to content

Commit

Permalink
Merge pull request #150 from biigle/merge-pgvector
Browse files Browse the repository at this point in the history
Move feature vector models to default database connection
  • Loading branch information
mzur authored Dec 13, 2023
2 parents 95132b4 + 3eb56de commit 6d5bff4
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 95 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
docker pull ghcr.io/biigle/worker:latest
- name: Start test database
run: docker-compose up -d --no-build database_testing vector_database_testing && sleep 5
run: docker-compose up -d --no-build database_testing && sleep 5
working-directory: ../core

- name: Run tests
Expand Down
7 changes: 0 additions & 7 deletions src/AnnotationCandidateFeatureVector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ class AnnotationCandidateFeatureVector extends Model
{
use HasFactory, HasNeighbors;

/**
* The database connection associated with the model.
*
* @var string
*/
protected $connection = 'pgvector';

/**
* The table associated with the model.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Biigle\Modules\Maia\Database\Factories;

use Biigle\Modules\Maia\MaiaJob;
use Biigle\Modules\Maia\AnnotationCandidate;
use Biigle\Modules\Maia\AnnotationCandidateFeatureVector;
use Illuminate\Database\Eloquent\Factories\Factory;

Expand All @@ -23,7 +24,7 @@ class AnnotationCandidateFeatureVectorFactory extends Factory
public function definition()
{
return [
'id' => $this->faker->unique()->randomDigit(),
'id' => AnnotationCandidate::factory(),
'job_id' => MaiaJob::factory(),
'vector' => range(0, 383),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Biigle\Modules\Maia\Database\Factories;

use Biigle\Modules\Maia\MaiaJob;
use Biigle\Modules\Maia\TrainingProposal;
use Biigle\Modules\Maia\TrainingProposalFeatureVector;
use Illuminate\Database\Eloquent\Factories\Factory;

Expand All @@ -23,7 +24,7 @@ class TrainingProposalFeatureVectorFactory extends Factory
public function definition()
{
return [
'id' => $this->faker->unique()->randomDigit(),
'id' => TrainingProposal::factory(),
'job_id' => MaiaJob::factory(),
'vector' => range(0, 383),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,9 @@
*/
public function up()
{
Schema::connection('pgvector')
->create('maia_training_proposal_feature_vectors', function (Blueprint $table) {
// Don't use increments() because it should throw an error if this is not
// manually provided. It should be the ID of the model in the main DB.
$table->unsignedInteger('id');
$table->unsignedInteger('job_id')->index();
$table->vector('vector', 384);

$table->primary('id');
});

Schema::connection('pgvector')
->create('maia_annotation_candidate_feature_vectors', function (Blueprint $table) {
// Don't use increments() because it should throw an error if this is not
// manually provided. It should be the ID of the model in the main DB.
$table->unsignedInteger('id');
$table->unsignedInteger('job_id')->index();
$table->vector('vector', 384);

$table->primary('id');
});
// This migration set up tables in a separate pgvector database. The database was
// removed later. The migration was cleared so data in the separate database
// cannot be accidentally deleted.
}

/**
Expand All @@ -43,9 +25,6 @@ public function up()
*/
public function down()
{
Schema::connection('pgvector')
->dropIfExists('maia_training_proposal_feature_vectors');
Schema::connection('pgvector')
->dropIfExists('maia_annotation_candidate_feature_vectors');
//
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('maia_training_proposal_feature_vectors', function (Blueprint $table) {
$table->unsignedBigInteger('id');
$table->foreign('id')
->references('id')
->on('maia_training_proposals')
->onDelete('cascade');

$table->unsignedInteger('job_id')->index();
$table->foreign('job_id')
->references('id')
->on('maia_jobs')
->onDelete('cascade');

$table->vector('vector', 384);
});

Schema::create('maia_annotation_candidate_feature_vectors', function (Blueprint $table) {
$table->unsignedBigInteger('id');
$table->foreign('id')
->references('id')
->on('maia_annotation_candidates')
->onDelete('cascade');

$table->unsignedInteger('job_id')->index();
$table->foreign('job_id')
->references('id')
->on('maia_jobs')
->onDelete('cascade');

$table->vector('vector', 384);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('maia_training_proposal_feature_vectors');
Schema::dropIfExists('maia_annotation_candidate_feature_vectors');
}
};
22 changes: 0 additions & 22 deletions src/Listeners/DeleteAnnotationFeatureVectors.php

This file was deleted.

2 changes: 0 additions & 2 deletions src/MaiaServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Biigle\Modules\Maia\Events\MaiaJobContinued;
use Biigle\Modules\Maia\Events\MaiaJobCreated;
use Biigle\Modules\Maia\Events\MaiaJobDeleting;
use Biigle\Modules\Maia\Listeners\DeleteAnnotationFeatureVectors;
use Biigle\Modules\Maia\Listeners\DispatchMaiaJob;
use Biigle\Modules\Maia\Listeners\DispatchObjectDetection;
use Biigle\Modules\Maia\Listeners\PrepareDeleteAnnotationPatches;
Expand Down Expand Up @@ -66,7 +65,6 @@ public function boot(Modules $modules, Router $router)
Event::listen(MaiaJobContinued::class, PruneTrainingProposalPatches::class);
Event::listen(MaiaJobContinued::class, PruneTrainingProposalFeatureVectors::class);
Event::listen(MaiaJobDeleting::class, PrepareDeleteAnnotationPatches::class);
Event::listen(MaiaJobDeleting::class, DeleteAnnotationFeatureVectors::class);
}

/**
Expand Down
7 changes: 0 additions & 7 deletions src/TrainingProposalFeatureVector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ class TrainingProposalFeatureVector extends Model
{
use HasFactory, HasNeighbors;

/**
* The database connection associated with the model.
*
* @var string
*/
protected $connection = 'pgvector';

/**
* The table associated with the model.
*
Expand Down
8 changes: 8 additions & 0 deletions tests/AnnotationCandidateFeatureVectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Biigle\Tests\Modules\Maia;

use Biigle\Modules\Maia\AnnotationCandidate;
use Biigle\Modules\Maia\AnnotationCandidateFeatureVector;
use Biigle\Shape;
use TestCase;
Expand All @@ -14,4 +15,11 @@ public function testAttributes()
$this->assertNotNull($model->job_id);
$this->assertNotNull($model->vector);
}

public function testCascadeDelete()
{
$model = AnnotationCandidateFeatureVector::factory()->create();
AnnotationCandidate::find($model->id)->delete();
$this->assertNull($model->fresh());
}
}
29 changes: 0 additions & 29 deletions tests/Listeners/DeleteAnnotationFeatureVectorsTest.php

This file was deleted.

16 changes: 16 additions & 0 deletions tests/MaiaJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Biigle\Tests\Modules\Maia;

use Biigle\Modules\Maia\AnnotationCandidateFeatureVector;
use Biigle\Modules\Maia\TrainingProposalFeatureVector;
use Biigle\Modules\Maia\Events\MaiaJobCreated;
use Biigle\Modules\Maia\Events\MaiaJobDeleting;
use Biigle\Modules\Maia\MaiaJob;
Expand Down Expand Up @@ -144,4 +146,18 @@ public function testShouldShowTrainingProposals()
];
$this->assertTrue($this->model->shouldShowTrainingProposals());
}

public function testDeleteFeatureVectorsCascade()
{
$tp = TrainingProposalFeatureVector::factory()->create([
'job_id' => $this->model->id,
]);
$ac = AnnotationCandidateFeatureVector::factory()->create([
'job_id' => $this->model->id,
]);

$this->model->delete();
$this->assertNull($tp->fresh());
$this->assertNull($ac->fresh());
}
}
8 changes: 8 additions & 0 deletions tests/TrainingProposalFeatureVectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Biigle\Tests\Modules\Maia;

use Biigle\Modules\Maia\TrainingProposal;
use Biigle\Modules\Maia\TrainingProposalFeatureVector;
use Biigle\Shape;
use TestCase;
Expand All @@ -14,4 +15,11 @@ public function testAttributes()
$this->assertNotNull($model->job_id);
$this->assertNotNull($model->vector);
}

public function testCascadeDelete()
{
$model = TrainingProposalFeatureVector::factory()->create();
TrainingProposal::find($model->id)->delete();
$this->assertNull($model->fresh());
}
}

0 comments on commit 6d5bff4

Please sign in to comment.