Skip to content

Commit

Permalink
Truncate tables before seeding for consistent IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
djunehor committed Jan 10, 2020
1 parent a2012b7 commit d1ac282
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/database/seeds/CitiesTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class CitiesTableSeeder extends Seeder
{
Expand All @@ -14,7 +15,10 @@ class CitiesTableSeeder extends Seeder
public function run()
{
$citiesTable = config('location.cities_table', 'cities');

Schema::disableForeignKeyConstraints();
DB::table($citiesTable)->truncate();
Schema::enableForeignKeyConstraints();

$cities = array(
array('name' => "Bombuflat",'state_id' => 1),
Expand Down
4 changes: 4 additions & 0 deletions src/database/seeds/CountriesTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class CountriesTableSeeder extends Seeder
{
Expand Down Expand Up @@ -263,7 +264,10 @@ public function run()
array('id' => 246,'code' => 'ZW','name' => "Zimbabwe",'phonecode' => 263),
);

// Fix for issue #29 https://github.com/ichtrojan/laravel-location/issues/29
Schema::disableForeignKeyConstraints();
DB::table($countriesTable)->truncate();
Schema::enableForeignKeyConstraints();
DB::table($countriesTable)->insert($countries);
}
}
3 changes: 3 additions & 0 deletions src/database/seeds/StatesTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class StatesTableSeeder extends Seeder
{
Expand Down Expand Up @@ -4138,7 +4139,9 @@ public function run()
array('name' => "Matabeleland South",'country_id' => 246),
array('name' => "Midlands",'country_id' => 246)
);
Schema::disableForeignKeyConstraints();
DB::table($statesTable)->truncate();
Schema::enableForeignKeyConstraints();
DB::table($statesTable)->insert($states);
}
}

0 comments on commit d1ac282

Please sign in to comment.