Skip to content

Commit

Permalink
dump sqlite schema; move permissions to seeder
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKrisKrisu committed Mar 17, 2024
1 parent bf44ec8 commit dfeb90d
Show file tree
Hide file tree
Showing 15 changed files with 382 additions and 119 deletions.
4 changes: 2 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LOG_LEVEL=debug # You can change this to info, warning or error for production u
L5_SWAGGER_CONST_HOST=http://localhost:8000

# Change this according to your database configuration
# To use SQLite, have a look here: https://laravel.com/docs/8.x/database#sqlite-configuration1
# To use SQLite, have a look here: https://laravel.com/docs/10.x/database#sqlite-configuration
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
Expand All @@ -26,7 +26,7 @@ QUEUE_CONNECTION=sync
SESSION_LIFETIME=120

# To be able to send mails and debug them, you can set up a mailtrap.io account and enter your credentials here
# You can also take a look here for other configuration possibilities: https://laravel.com/docs/8.x/mail#configuration
# You can also take a look here for other configuration possibilities: https://laravel.com/docs/10.x/mail#configuration
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/API/v1/StationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public function merge(int $oldStationId, int $newStationId): StationResource {
Checkin::where('origin', $oldStation->ibnr)->update(['origin' => $newStation->ibnr]);
Checkin::where('destination', $oldStation->ibnr)->update(['destination' => $newStation->ibnr]);
Stopover::where('train_station_id', $oldStation->id)->update(['train_station_id' => $newStation->id]);
Trip::where('origin', $oldStation->ibnr)->update(['origin' => $newStation->ibnr]);
Trip::where('destination', $oldStation->ibnr)->update(['destination' => $newStation->ibnr]);
Trip::where('origin_id', $oldStation->id)->update(['origin_id' => $newStation->id]);
Trip::where('destination_id', $oldStation->id)->update(['destination_id' => $newStation->id]);
Event::where('station_id', $oldStation->id)->update(['station_id' => $newStation->id]);
EventSuggestion::where('station_id', $oldStation->id)->update(['station_id' => $newStation->id]);

Expand Down
6 changes: 3 additions & 3 deletions database/factories/TripFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use App\Enum\TripSource;
use App\Http\Controllers\TransportController;
use App\Models\HafasOperator;
use App\Models\Trip;
use App\Models\Station;
use App\Models\Stopover;
use App\Models\Trip;
use Illuminate\Database\Eloquent\Factories\Factory;

class TripFactory extends Factory
Expand All @@ -29,8 +29,8 @@ public function definition(): array {
'linename' => $this->faker->bothify('?? ##'),
'journey_number' => $this->faker->numberBetween(10000, 99999),
'operator_id' => HafasOperator::factory()->create()->id,
'origin' => $origin->ibnr,
'destination' => $destination->ibnr,
'origin_id' => $origin->id,
'destination_id' => $destination->id,
'polyline_id' => null, //Will be set in the configure function
'departure' => now()->subMinutes(15)->format('c'),
'arrival' => now()->addMinutes(80)->format('c'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,61 +1,10 @@
<?php

use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

return new class extends Migration
{
public function up(): void {
$roleAdmin = Role::create(['name' => 'admin']);
$roleEventModerator = Role::create(['name' => 'event-moderator']); //for accepting/denying events
Role::create(['name' => 'open-beta']); //for experimental features that can be enabled by users
Role::create(['name' => 'closed-beta']); //for experimental features that can be only enabled by admins for specific users

$permissionViewBackend = Permission::create(['name' => 'view-backend']);

$permissionViewEvents = Permission::create(['name' => 'view-events']);
$permissionAcceptEvents = Permission::create(['name' => 'accept-events']);
$permissionDenyEvents = Permission::create(['name' => 'deny-events']);
$permissionCreateEvents = Permission::create(['name' => 'create-events']);
$permissionUpdateEvents = Permission::create(['name' => 'update-events']);
$permissionDeleteEvents = Permission::create(['name' => 'delete-events']);

$roleAdmin->givePermissionTo($permissionViewBackend);
$roleAdmin->givePermissionTo($permissionViewEvents);
$roleAdmin->givePermissionTo($permissionAcceptEvents);
$roleAdmin->givePermissionTo($permissionDenyEvents);
$roleAdmin->givePermissionTo($permissionCreateEvents);
$roleAdmin->givePermissionTo($permissionUpdateEvents);
$roleAdmin->givePermissionTo($permissionDeleteEvents);

$roleEventModerator->givePermissionTo($permissionViewBackend);
$roleEventModerator->givePermissionTo($permissionViewEvents);
$roleEventModerator->givePermissionTo($permissionAcceptEvents);
$roleEventModerator->givePermissionTo($permissionDenyEvents);
$roleEventModerator->givePermissionTo($permissionUpdateEvents);

$oldAdmins = User::where('role', 10)->get();
foreach ($oldAdmins as $oldAdmin) {
$oldAdmin->assignRole('admin');
}

$experimentalUsers = User::where('experimental', true)->get();
foreach ($experimentalUsers as $experimentalUser) {
$experimentalUser->assignRole('open-beta');
}
}

public function down(): void {
$admins = User::role('admin')->get();
foreach ($admins as $admin) {
$admin->update(['role' => 10]);
}

Role::where('name', 'admin')->delete();
Role::where('name', 'event-moderator')->delete();
Role::where('name', 'open-beta')->delete();
Role::where('name', 'closed-beta')->delete();
// empty now - replaced by PermissionSeeder
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
return new class extends Migration
{
public function up(): void {
$permission = Permission::create(['name' => 'view event history']);
Role::findByName('admin')->givePermissionTo($permission);
}

public function down(): void {
Permission::findByName('view event history')->delete();
// empty now - replaced by PermissionSeeder
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,6 @@
return new class extends Migration
{
public function up(): void {
$permissionCreate = Permission::create(['name' => 'create stations']);
$permissionUpdate = Permission::create(['name' => 'update stations']);
$permissionDelete = Permission::create(['name' => 'delete stations']);

$roleAdmin = Role::findByName('admin');
$roleAdmin->givePermissionTo($permissionCreate);
$roleAdmin->givePermissionTo($permissionUpdate);
$roleAdmin->givePermissionTo($permissionDelete);
}

public function down(): void {
$permissionCreate = Permission::findByName('create stations');
$permissionUpdate = Permission::findByName('update stations');
$permissionDelete = Permission::findByName('delete stations');

$roleAdmin = Role::findByName('admin');
$roleAdmin->revokePermissionTo($permissionCreate);
$roleAdmin->revokePermissionTo($permissionUpdate);
$roleAdmin->revokePermissionTo($permissionDelete);

$permissionCreate->delete();
$permissionUpdate->delete();
$permissionDelete->delete();
// empty now - replaced by PermissionSeeder
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@
return new class extends Migration
{
public function up(): void {
$permission = Permission::create(['name' => 'view activity']);

$roleAdmin = Role::findByName('admin');
$roleAdmin->givePermissionTo($permission);
}

public function down(): void {
$permission = Permission::findByName('view activity');

$roleAdmin = Role::findByName('admin');
$roleAdmin->revokePermissionTo($permission);

$permission->delete();
// empty now - replaced by PermissionSeeder
}
};
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

return new class extends Migration
{

public function up(): void {
$permission = Permission::create(['name' => 'create-manual-trip']);

Role::findByName('admin')->givePermissionTo($permission);
Role::findByName('closed-beta')->givePermissionTo($permission);
}

public function down(): void {
$permission = Permission::findByName('create-manual-trip');

Role::findByName('admin')->revokePermissionTo($permission);
Role::findByName('closed-beta')->revokePermissionTo($permission);

$permission->delete();
// empty now - replaced by PermissionSeeder
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ public function up(): void {
'destination_id' => DB::raw('(SELECT id FROM train_stations WHERE ibnr = hafas_trips.destination)'),
]);

if (config('database.default') === 'sqlite') {
//warn the user that they need to manually drop the columns
echo PHP_EOL . PHP_EOL;
echo "\033[41m"; //console background color red
echo '--- WARNING ---' . PHP_EOL;
echo 'SQLite doesn\'t support dropping foreign keys' . PHP_EOL;
echo 'Please drop the columns "origin" and "destination" manually from the "hafas_trips" table.' . PHP_EOL;
echo '--- WARNING ---';
echo "\033[0m"; //reset console color
echo PHP_EOL . PHP_EOL;
return;
}

Schema::table('hafas_trips', static function(Blueprint $table) {
$table->dropForeign(['origin']);
$table->dropColumn('origin');
Expand Down
Loading

0 comments on commit dfeb90d

Please sign in to comment.