Skip to content

Commit df6aa58

Browse files
committed
Improved database seeding with progress bars and nullable city attributes.
1 parent ee9c915 commit df6aa58

File tree

5 files changed

+89
-7
lines changed

5 files changed

+89
-7
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## v1.1.0
2+
3+
#### Published at: 2025-03-03
4+
5+
- Minor BUG fixes
6+
- Improved database seeding with progress bars
7+
18
## v1.0.1
29

310
#### Published at: 2025-03-03

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "milenmk/laravel-locations",
33
"description": "Add Countries, Cities, Areas, Languages and Currencies models to your Laravel application",
4-
"version": "1.0.1",
4+
"version": "1.1.0",
55
"license": "MIT",
66
"autoload": {
77
"psr-4": {

database/data/languages.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@
732732
{
733733
"id": "74",
734734
"iso": "en",
735-
"name": "Borɔfo",
735+
"name": "English",
736736
"arabic": null,
737737
"created_at": null,
738738
"updated_at": null,
@@ -752,7 +752,7 @@
752752
{
753753
"id": "76",
754754
"iso": "bg",
755-
"name": "Bɔlgeria kasa",
755+
"name": "Bulgarian",
756756
"arabic": null,
757757
"created_at": null,
758758
"updated_at": null,

database/migrations/2025_03_03_015824_create_cities_table.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ public function up(): void
1414
$table->id();
1515
$table->string('name')->index();
1616
$table->unsignedBigInteger('country_id');
17-
$table->json('translations');
18-
$table->json('timezones')->nullable();
19-
$table->decimal('lat');
20-
$table->decimal('lng');
17+
$table->json('translations')->nullable();
18+
$table->json('timezone')->nullable();
19+
$table->decimal('lat')->nullable();
20+
$table->decimal('lng')->nullable();
2121
$table->boolean('is_activated')->default(1)->nullable();
2222
$table->timestamps();
2323

src/Console/MilenmkLocationsSeedCommand.php

+75
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,113 @@ public function handle(): void
3535
$languageFileExists = file_exists($languageJson);
3636

3737
if ($countryFileExists) {
38+
$this->outputComponents()->info('Seeding the database with countries data.');
39+
3840
$countries = json_decode(file_get_contents($countryJson), true);
41+
42+
$progress = $this->getOutput()->createProgressBar(count($countries));
43+
$progress->setFormat("%message%\n %current%/%max% [%bar%] %percent:3s%%");
44+
$progress->setMessage('Countries');
45+
$progress->setEmptyBarCharacter(''); // light shade character \u2591
46+
$progress->setProgressCharacter('');
47+
$progress->setBarCharacter('');
48+
3949
foreach ($countries as $country) {
4050
DB::table('countries')->insert($country);
51+
$progress->advance();
4152
}
53+
54+
$progress->finish();
55+
$progress->clear();
4256
}
4357

4458
if ($cityFileExists) {
59+
$this->outputComponents()->info('Seeding the database with cities data.');
60+
4561
$cities = json_decode(file_get_contents($cityJson), true);
62+
63+
$progress = $this->getOutput()->createProgressBar(count($cities));
64+
$progress->setFormat("%message%\n %current%/%max% [%bar%] %percent:3s%%");
65+
$progress->setMessage('Cities');
66+
$progress->setEmptyBarCharacter(''); // light shade character \u2591
67+
$progress->setProgressCharacter('');
68+
$progress->setBarCharacter('');
69+
4670
foreach ($cities as $city) {
4771
DB::table('cities')->insert($city);
72+
73+
$progress->advance();
4874
}
75+
76+
$progress->finish();
77+
$progress->clear();
4978
}
5079

5180
if ($areaFileExists) {
81+
$this->outputComponents()->info('Seeding the database with areas data.');
82+
$this->outputComponents()->warn('This WILL take a while :( Time for a coffee :)');
83+
5284
$areas = json_decode(file_get_contents($areaJson), true);
85+
86+
$progress = $this->getOutput()->createProgressBar(count($areas));
87+
$progress->setFormat("%message%\n %current%/%max% [%bar%] %percent:3s%%");
88+
$progress->setMessage('Areas');
89+
$progress->setEmptyBarCharacter(''); // light shade character \u2591
90+
$progress->setProgressCharacter('');
91+
$progress->setBarCharacter('');
92+
5393
foreach ($areas as $area) {
5494
DB::table('areas')->insert($area);
95+
96+
$progress->advance();
5597
}
98+
99+
$progress->finish();
100+
$progress->clear();
56101
}
57102

58103
if ($currencyFileExists) {
104+
$this->outputComponents()->info('Seeding the database with currencies data.');
105+
59106
$currencies = json_decode(file_get_contents($currencyJson), true);
107+
108+
$progress = $this->getOutput()->createProgressBar(count($currencies));
109+
$progress->setFormat("%message%\n %current%/%max% [%bar%] %percent:3s%%");
110+
$progress->setMessage('Currencies');
111+
$progress->setEmptyBarCharacter(''); // light shade character \u2591
112+
$progress->setProgressCharacter('');
113+
$progress->setBarCharacter('');
114+
60115
foreach ($currencies as $currency) {
61116
DB::table('currencies')->insert($currency);
117+
118+
$progress->advance();
62119
}
120+
121+
$progress->finish();
122+
$progress->clear();
63123
}
64124

65125
if ($languageFileExists) {
126+
$this->outputComponents()->info('Seeding the database with languages data.');
127+
66128
$languages = json_decode(file_get_contents($languageJson), true);
129+
130+
$progress = $this->getOutput()->createProgressBar(count($languages));
131+
$progress->setFormat("%message%\n %current%/%max% [%bar%] %percent:3s%%");
132+
$progress->setMessage('Languages');
133+
$progress->setEmptyBarCharacter(''); // light shade character \u2591
134+
$progress->setProgressCharacter('');
135+
$progress->setBarCharacter('');
136+
67137
foreach ($languages as $language) {
68138
DB::table('languages')->insert($language);
139+
140+
$progress->advance();
69141
}
142+
143+
$progress->finish();
144+
$progress->clear();
70145
}
71146

72147
$this->info('Seeding the database with locations data is completed.');

0 commit comments

Comments
 (0)