initial commit
This commit is contained in:
1
database/.gitignore
vendored
Normal file
1
database/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
*.sqlite*
|
||||
26
database/factories/ContactFactory.php
Normal file
26
database/factories/ContactFactory.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Contact>
|
||||
*/
|
||||
class ContactFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'first_name' => $this->faker->firstName(),
|
||||
'last_name' => $this->faker->lastName(),
|
||||
'email' => $this->faker->unique()->safeEmail(),
|
||||
'id' => $this->faker->uuid(),
|
||||
];
|
||||
}
|
||||
}
|
||||
31
database/factories/ContactImportFactory.php
Normal file
31
database/factories/ContactImportFactory.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\ContactImport>
|
||||
*/
|
||||
class ContactImportFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->faker->uuid(),
|
||||
'queue_at' => $this->faker->dateTime(),
|
||||
'started_at' => $this->faker->optional()->dateTime(),
|
||||
'finished_at' => $this->faker->optional()->dateTime(),
|
||||
'total_processed' => $this->faker->numberBetween(0, 1000),
|
||||
'errors' => $this->faker->numberBetween(0, 100),
|
||||
'duplicates' => $this->faker->numberBetween(0, 100),
|
||||
'file' => $this->faker->filePath(),
|
||||
'state' => $this->faker->randomElement(['PENDING', 'RUNNING', 'DONE']),
|
||||
];
|
||||
}
|
||||
}
|
||||
35
database/migrations/0001_01_01_000001_create_cache_table.php
Normal file
35
database/migrations/0001_01_01_000001_create_cache_table.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
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('cache', function (Blueprint $table) {
|
||||
$table->string('key')->primary();
|
||||
$table->mediumText('value');
|
||||
$table->integer('expiration')->index();
|
||||
});
|
||||
|
||||
Schema::create('cache_locks', function (Blueprint $table) {
|
||||
$table->string('key')->primary();
|
||||
$table->string('owner');
|
||||
$table->integer('expiration')->index();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('cache');
|
||||
Schema::dropIfExists('cache_locks');
|
||||
}
|
||||
};
|
||||
57
database/migrations/0001_01_01_000002_create_jobs_table.php
Normal file
57
database/migrations/0001_01_01_000002_create_jobs_table.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
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('jobs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('queue')->index();
|
||||
$table->longText('payload');
|
||||
$table->unsignedTinyInteger('attempts');
|
||||
$table->unsignedInteger('reserved_at')->nullable();
|
||||
$table->unsignedInteger('available_at');
|
||||
$table->unsignedInteger('created_at');
|
||||
});
|
||||
|
||||
Schema::create('job_batches', function (Blueprint $table) {
|
||||
$table->string('id')->primary();
|
||||
$table->string('name');
|
||||
$table->integer('total_jobs');
|
||||
$table->integer('pending_jobs');
|
||||
$table->integer('failed_jobs');
|
||||
$table->longText('failed_job_ids');
|
||||
$table->mediumText('options')->nullable();
|
||||
$table->integer('cancelled_at')->nullable();
|
||||
$table->integer('created_at');
|
||||
$table->integer('finished_at')->nullable();
|
||||
});
|
||||
|
||||
Schema::create('failed_jobs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('uuid')->unique();
|
||||
$table->text('connection');
|
||||
$table->text('queue');
|
||||
$table->longText('payload');
|
||||
$table->longText('exception');
|
||||
$table->timestamp('failed_at')->useCurrent();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('jobs');
|
||||
Schema::dropIfExists('job_batches');
|
||||
Schema::dropIfExists('failed_jobs');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Tpetry\PostgresqlEnhanced\Schema\Blueprint;
|
||||
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::createFunctionOrReplace(
|
||||
name: 'update_contacts_ts_name',
|
||||
parameters: [],
|
||||
return: 'trigger',
|
||||
language: 'plpgsql',
|
||||
body: <<<'SQL'
|
||||
BEGIN
|
||||
IF TG_OP = 'INSERT'
|
||||
OR NEW.first_name IS DISTINCT FROM OLD.first_name
|
||||
OR NEW.last_name IS DISTINCT FROM OLD.last_name THEN
|
||||
NEW.ts_name =
|
||||
to_tsvector('simple',
|
||||
coalesce(NEW.first_name, '') || ' ' || coalesce(NEW.last_name, '')
|
||||
);
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END;
|
||||
SQL);
|
||||
|
||||
Schema::createExtensionIfNotExists('pg_trgm');
|
||||
|
||||
Schema::create('contacts', function (Blueprint $table) {
|
||||
$table->uuid('id')->primary();
|
||||
$table->string('first_name', 100)->nullable();
|
||||
$table->string('last_name', 100)->nullable();
|
||||
$table->string('email', 254)->unique();
|
||||
$table->tsvector('ts_name');
|
||||
$table->trigger('ts_name_contacts_build_vector', 'update_contacts_ts_name()', 'BEFORE INSERT OR UPDATE')->forEachRow(); // mising indexes (GIN!!! etc.)
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
DB::statement('CREATE INDEX idx_contacts_email_trgm ON contacts USING GIN (email gin_trgm_ops)');
|
||||
DB::statement('CREATE INDEX idx_contacts_ts_name ON contacts USING GIN (ts_name);');
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('contacts');
|
||||
Schema::dropFunctionIfExists('update_contacts_ts_name');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
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('contact_imports', function (Blueprint $table) {
|
||||
$table->uuid('id')->primary();
|
||||
$table->timestamp('queue_at')->default(DB::raw('CURRENT_TIMESTAMP'));
|
||||
$table->timestamp('started_at')->nullable();
|
||||
$table->timestamp('finished_at')->nullable();
|
||||
$table->unsignedInteger('total_processed')->default(0);
|
||||
$table->unsignedInteger('errors')->default(0);
|
||||
$table->unsignedInteger('duplicates')->default(0);
|
||||
$table->string('file');
|
||||
$table->enum('state', ['PENDING', 'RUNNING', 'DONE', 'FAILED'])->default('PENDING');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('contact_imports');
|
||||
}
|
||||
};
|
||||
19
database/seeders/ContactSeeder.php
Normal file
19
database/seeders/ContactSeeder.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\Contact;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class ContactSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
Contact::factory()
|
||||
->count(100)
|
||||
->create();
|
||||
}
|
||||
}
|
||||
16
database/seeders/DatabaseSeeder.php
Normal file
16
database/seeders/DatabaseSeeder.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class DatabaseSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Seed the application's database.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
$this->call(ContactSeeder::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user