Skip to content

Commit

Permalink
Merge pull request #1639 from AndyTWF/srd-import-transactions
Browse files Browse the repository at this point in the history
fix: srd transaction imports
  • Loading branch information
AndyTWF authored Sep 20, 2024
2 parents 83b3e40 + 488264b commit 985a422
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 38 deletions.
1 change: 1 addition & 0 deletions .env.ci
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ CORE_SSO_CLIENT_SECRET=

# SRD
SRD_DOWNLOAD_URL="https://vatsim.uk/srd/%s/download"
SRD_IMPORT_ENABLED=true

# ACARS
HOPPIE_ACARS_LOGIN_CODE=testlogin
Expand Down
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ CORE_SSO_CLIENT_SECRET=secret

# SRD
SRD_DOWNLOAD_URL="https://vatsim.uk/srd/%s/download"
SRD_IMPORT_ENABLED=true

# ACARS
HOPPIE_ACARS_LOGIN_CODE=testlogin
Expand Down
5 changes: 4 additions & 1 deletion .env.testing.example
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ GITHUB_ISSUE_REPO_PLUGIN=uk-controller-plugin
GITHUB_ACCESS_TOKEN=testtoken
GITHUB_WEBHOOK_SECRET=testsecret
UKSF_LABEL_NAME_API=ukcp-api
UKSF_LABEL_NAME_PLUGIN=ukcp-plugin
UKSF_LABEL_NAME_PLUGIN=ukcp-plugin

# SRD Import
SRD_IMPORT_ENABLED=true
23 changes: 10 additions & 13 deletions app/Console/Commands/SrdImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,17 @@ public function handle()
$this->output->title('Starting SRD import');
$this->output->section('Dropping existing SRD data');

// Drop the current SRD data
DB::statement("SET foreign_key_checks=0");
DB::transaction(function () {
// Clear the existing data
$this->output->comment('Dropping SRD notes');
DB::table('srd_notes')->delete();
$this->output->comment('Dropping SRD routes');
DB::table('srd_routes')->delete();

$this->output->comment('Dropping SRD notes and route links');
DB::table('srd_note_srd_route')->truncate();
$this->output->comment('Dropping SRD notes');
DB::table('srd_notes')->truncate();
$this->output->comment('Dropping SRD routes');
DB::table('srd_routes')->truncate();

DB::statement("SET foreign_key_checks=1");

// Import the data
$this->importer->withOutput($this->output)->import($this->argument('file_name'), 'imports');
// Import the data
$this->output->comment('About to start SRD import');
$this->importer->withOutput($this->output)->import($this->argument('file_name'), 'imports');
});
$this->output->success('SRD import complete');
}
}
7 changes: 4 additions & 3 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ protected function schedule(Schedule $schedule)
$schedule->command('networkdata:update-controllers')->everyMinute()
->graceTimeInMinutes(3)
->withoutOverlapping(5);
// $schedule->command('srd:update')
// ->cron('0 1-7 * * *')
// ->doNotMonitor();
$schedule->command('srd:update')
->cron('0 1-7 * * *')
->when(config('srd.import_enabled'))
->doNotMonitor();
$schedule->command('horizon:snapshot')->everyFiveMinutes()->doNotMonitor();
$schedule->command('plugin-events:clean')->everyTenMinutes()->doNotMonitor();
$schedule->command('metars:update')->everyMinute();
Expand Down
1 change: 1 addition & 0 deletions config/srd.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

return [
'download_url' => env('SRD_DOWNLOAD_URL'),
'import_enabled' => env('SRD_IMPORT_ENABLED', false),
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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
{
// Update the srd_routes table to drop the foreign key constraint on the srd_note_srd_route table
Schema::table('srd_note_srd_route', function (Blueprint $table) {
$table->dropForeign('srd_note_srd_route_srd_note_id_foreign');
});

// Update the srd_notes table to make the id column a bigIncrements rather than a smallIncrements
Schema::table('srd_notes', function (Blueprint $table) {
$table->bigIncrements('id')->first()->change();
});

// Now update the srd_note_srd_route table to make the srd_note_id column a bigInteger rather than an integer
// and then re-add the foreign key constraint
Schema::table('srd_note_srd_route', function (Blueprint $table) {
$table->unsignedBigInteger('srd_note_id')->first()->change();
$table->foreign('srd_note_id')->references('id')->on('srd_notes')->onDelete('cascade');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};
35 changes: 14 additions & 21 deletions tests/app/Console/Commands/SrdImportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

use App\BaseFunctionalTestCase;
use App\Imports\Srd\SrdImport;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Storage;
use InvalidArgumentException;
use Maatwebsite\Excel\Excel;
use Mockery;
use Mockery\MockInterface;

class SrdImportTest extends BaseFunctionalTestCase
{
private $mockImporter;
private readonly SrdImport|MockInterface $mockImporter;

public function setUp(): void
{
Expand All @@ -32,7 +32,7 @@ public function testItThrowsExceptionIfFileNotFound()
Artisan::call('srd:import srd.csv');
}

public function testItTruncatesTablesAndCallsImporter()
public function testItDeletesDataAndCallsImporter()
{
Storage::fake('imports');
Storage::disk('imports')->put('srd.csv', 'testdata');
Expand All @@ -43,24 +43,17 @@ public function testItTruncatesTablesAndCallsImporter()
$this->mockImporter->shouldReceive('import')
->with('srd.csv', 'imports');

DB::partialMock();
DB::shouldReceive('statement')->with('SET foreign_key_checks=0')->once();

DB::shouldReceive('table')->with('srd_note_srd_route')
->andReturnSelf()
->once();
$mockBuilderSrdRoutes = Mockery::mock(Builder::class);
$mockBuilderSrdRoutes->shouldReceive('delete')->once();
$mockBuilderSrdNotes = Mockery::mock(Builder::class);
$mockBuilderSrdNotes->shouldReceive('delete')->once();

DB::shouldReceive('table')->with('srd_notes')
->andReturnSelf()
->once();

DB::shouldReceive('table')->with('srd_routes')
->once()
->andReturnSelf();

DB::shouldReceive('truncate')->andReturnSelf()->times(3);

DB::shouldReceive('statement')->with('SET foreign_key_checks=1')->once();
DB::partialMock();
DB::shouldReceive('table')->with('srd_routes')->once()->andReturn($mockBuilderSrdRoutes);
DB::shouldReceive('table')->with('srd_notes')->once()->andReturn($mockBuilderSrdNotes);
DB::shouldReceive('transaction')->once()->andReturnUsing(function ($callback) {
$callback();
});

Artisan::call('srd:import srd.csv');
}
Expand Down

0 comments on commit 985a422

Please sign in to comment.