Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(db): init boundaries data using cahyadsn/wilayah_boundaries #104

Merged
merged 5 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: "2"

checks:
similar-code:
enabled: false
method-complexity:
config:
threshold: 9

plugins:
fixme:
enabled: true
phpcodesniffer:
enabled: false
phpmd:
enabled: true
config:
rulesets: workbench/phpmd.xml

ratings:
paths:
- src/**.php
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# Exclude unused files
# see: https://redd.it/2jzp6k
.codeclimate.yml export-ignore
.editorconfig export-ignore
.env.example export-ignore
.gitattributes export-ignore
Expand Down
64 changes: 33 additions & 31 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ on:
branches: [main]
# paths: ['.github/**.yml', 'src/*', 'tests/*']

env:
DB_USERNAME: ${{ github.repository_owner }}
DB_PASSWORD: secret

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
Expand All @@ -31,33 +27,23 @@ jobs:
runs-on: ubuntu-latest
needs: configs
env:
DB_NUSA: nusantara

services:
mysql:
image: mysql:8.0
env:
MYSQL_ROOT_PASSWORD: ${{ env.DB_PASSWORD }}
MYSQL_DATABASE: ${{ env.DB_NUSA }}
MYSQL_USER: ${{ env.DB_USERNAME }}
MYSQL_PASSWORD: ${{ env.DB_PASSWORD }}
options: >-
--health-cmd="mysqladmin ping"
--health-interval=10s
--health-timeout=5s
--health-retries=3
ports:
- 3306:3306

permissions:
actions: read
DB_USERNAME: root
DB_PASSWORD: root
DB_DATABASE: ${{ github.repository_owner }}
UPSTREAM_DB_DATABASE: nusantara

steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: true

- name: Setup MySQL
run: |
sudo /etc/init.d/mysql start
mysql -e 'CREATE DATABASE ${{ env.DB_DATABASE }};' -u${{ env.DB_USERNAME }} -p${{ env.DB_PASSWORD }}
mysql -e 'CREATE DATABASE ${{ env.UPSTREAM_DB_DATABASE }};' -u${{ env.DB_USERNAME }} -p${{ env.DB_PASSWORD }}

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
Expand Down Expand Up @@ -86,29 +72,39 @@ jobs:
run: |
./vendor/bin/testbench nusa:import --ansi
./vendor/bin/testbench nusa:stat -dw --ansi
git status -s resources
git status -s {database,resources}

# - name: Get file changed
# uses: tj-actions/changed-files@v45
# with:
# exclude_submodules: true
# # files: resources/**

- name: Upload current stat
- name: Store current stat
if: ${{ github.event_name != 'pull_request' }}
uses: actions/upload-artifact@v4
with:
name: upstream-stats
path: tests/stats.json
if-no-files-found: ignore

- name: Store static resources
uses: actions/upload-artifact@v4
with:
name: static-resources
path: |
database/nusa.sqlite
resources/static/**

tests:
name: Test on PHP ${{ matrix.php }} Laravel ${{ matrix.laravel }} DB ${{ matrix.db }}
needs: [configs, prepare]
runs-on: ubuntu-latest
env:
DB_CONNECTION: ${{ matrix.db }}
DB_DATABASE: ${{ github.repository_owner }}
DB_USERNAME: ${{ github.repository_owner }}
DB_PASSWORD: secret

services:
postgresql:
Expand Down Expand Up @@ -177,13 +173,20 @@ jobs:
composer require "laravel/framework=${{ matrix.laravel }}" --no-update
composer update --prefer-dist --no-interaction --no-progress

- name: Restore static resources
uses: actions/download-artifact@v4
with:
name: static-resources

- name: Run migrations
run: |
composer testbench vendor:publish -- --tag creasi-migrations
composer testbench migrate
./vendor/bin/testbench vendor:publish --ansi --tag creasi-migrations
./vendor/bin/testbench migrate --ansi

- name: Run tests
run: composer test -- --coverage
run: |
./vendor/bin/testbench config:show database --ansi
./vendor/bin/testbench package:test --ansi --coverage

- name: Generate reports for CodeClimate
id: reports
Expand All @@ -195,7 +198,7 @@ jobs:
curl -LSs $CC_TEST_REPORTER_URL > ./cc-test-reporter && chmod +x ./cc-test-reporter
./cc-test-reporter format-coverage -t clover -o $CODECLIMATE_REPORT tests/reports/clover.xml

- name: Upload tests reports
- name: Store tests reports
uses: actions/upload-artifact@v4
if: needs.configs.outputs.has-codeclimate == '1'
with:
Expand All @@ -212,4 +215,3 @@ jobs:
with:
has-coveralls: ${{ needs.configs.outputs.has-coveralls == '1' }}
has-codeclimate: ${{ needs.configs.outputs.has-codeclimate == '1' }}

3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "workbench/submodules/cahyadsn-wilayah_kodepos"]
path = workbench/submodules/cahyadsn-wilayah_kodepos
url = https://github.com/cahyadsn/wilayah_kodepos.git
[submodule "workbench/submodules/cahyadsn-wilayah_boundaries"]
path = workbench/submodules/cahyadsn-wilayah_boundaries
url = https://github.com/cahyadsn/wilayah_boundaries.git
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -883,26 +883,39 @@ using `sqlite` driver. Let us know if you had any issue using another database d

```sh
composer install
pnpm install
```

3. Create new database, by default we use the following configuration :
3. Copy `workbench/.env.example` to `workbench/.env` and update the content you desire

```sh
DB_CONNECTION=mysql # Your main db connection to test againsts
DB_HOST=127.0.0.1
DB_DATABASE=creasi_test # To store the testing data
DB_USERNAME=creasico
DB_PASSWORD=secret

- `dbname` : `nusantara`
- `dbhost` : `127.0.0.1`
- `dbuser` : `root`
- `dbpass` : `secret`
UPSTREAM_DB_DATABASE=nusantara # To store the upstream data
```

4. Create new database to store our upstream and testing data:

```sh
mysql -e 'create database nusantara;'
mysql -e 'create database creasi_test;' # Based on the value of `DB_DATABASE`
mysql -e 'create database nusantara;' # Based on the value of `UPSTREAM_DB_DATABASE`
```

4. Last but not least, run `db:import` command
5. Last but not least, run `db:import` command

```sh
composer db:import
```

If you were using different configuration you can edit [this file](https://github.com/creasico/laravel-nusa/blob/94cd261d7726b9a5cb46cdef4aa9914522a33b4a/tests/NusaTest.php#L16-L19) but please don't submit your changes.
As you might noticed that we use 3 different databases to develop and maintain this library. Here's the explanation :

- `database/nusa.sqlite` is the main database in this library that will be included when you install this library as a dependency
- `DB_DATABASE` is mainly for testing purposes, it simulate the actual application where this library installed on
- `UPSTREAM_DB_DATABASE` is contains the upstream database tables that will be used as source of truth for this library

Once you've done with your meaningful contributions, run the following command to ensure everythings is works as expected.

Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
"issues": "https://github.com/creasico/laravel-nusa/issues"
},
"scripts": {
"post-autoload-dump": [
"testbench vendor:publish --tag=creasi-migrations"
],
"fix": [
"pint --preset laravel"
],
Expand Down
8 changes: 8 additions & 0 deletions database/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ classDiagram
+int regency_code
+int province_code
+string name
+double latitude
+double longitude
+province() Province
+regency() Regency
+villages() Village[]
Expand All @@ -45,6 +47,8 @@ classDiagram
+int regency_code
+int province_code
+string name
+double latitude
+double longitude
+int postal_code
+province() Province
+regency() Regency
Expand Down Expand Up @@ -84,6 +88,8 @@ classDiagram
| `regency_code` | `char(4)` | `foreign` | - |
| `province_code` | `char(2)` | `foreign` | - |
| `name` | `varchar` | - | - |
| `latitude` | `double`, `nullable` | - | - |
| `longitude` | `double`, `nullable` | - | - |

**Relation Properties**
- `regency_code` : reference `regencies`
Expand All @@ -98,6 +104,8 @@ classDiagram
| `regency_code` | `char(4)` | `foreign` | - |
| `province_code` | `char(2)` | `foreign` | - |
| `name` | `varchar` | - | - |
| `latitude` | `double`, `nullable` | - | - |
| `longitude` | `double`, `nullable` | - | - |
| `postal_code` | `char(5)`, `nullable` | - | - |

**Relation Properties**
Expand Down
4 changes: 4 additions & 0 deletions database/migrations/0000_00_00_000000_create_nusa_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public function up(): void
{
$tableNames = config('creasi.nusa.table_names');

if (Schema::hasTable($tableNames['provinces'])) {
return;
}

Schema::create($tableNames['provinces'], function (Blueprint $table) {
$table->char('code', 2)->primary();
$table->string('name', 50)->index();
Expand Down
2 changes: 2 additions & 0 deletions tests/Models/DistrictTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public function it_should_has_many_districts(Collection $districts): Collection
{
$districts->each(function (District $district) {
$this->assertIsInt($district->code, 'Code should be int');
$this->assertIsFloat($district->latitude, \sprintf('Latitude of district "%s" should be float', $district->code));
$this->assertIsFloat($district->longitude, \sprintf('Longitude of district "%s" should be float', $district->code));
$this->assertInstanceOf(Collection::class, $district->postal_codes, 'Postal Codes should be instance of collection');

$this->assertInstanceOf(DistrictContract::class, $district);
Expand Down
4 changes: 2 additions & 2 deletions tests/Models/RegencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public function it_should_has_many_regencies(Collection $regencies): Collection
$regencies->each(function (Regency $regency) {
$this->assertIsInt($regency->code, 'Code should be int');
// Comment this out due to https://github.com/cahyadsn/wilayah/pull/47
// $this->assertIsFloat($regency->latitude, 'Latitude should be float');
// $this->assertIsFloat($regency->longitude, 'Longitude should be float');
// $this->assertIsFloat($regency->latitude, \sprintf('Latitude of regency "%s" should be float', $regency->code));
// $this->assertIsFloat($regency->longitude, \sprintf('Longitude of regency "%s" should be float', $regency->code));
$this->assertInstanceOf(Collection::class, $regency->postal_codes, 'Postal Codes should be instance of collection');

$this->assertInstanceOf(RegencyContract::class, $regency);
Expand Down
2 changes: 2 additions & 0 deletions tests/Models/VillageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public function it_should_has_many_villages(Collection $villages): Collection
{
$villages->each(function (Village $village) {
$this->assertIsInt($village->code, 'Code should be int');
$this->assertIsFloat($village->latitude, \sprintf('Latitude of village "%s" should be float', $village->code));
$this->assertIsFloat($village->longitude, \sprintf('Longitude of village "%s" should be float', $village->code));
if ($village->postal_code) {
$this->assertIsInt($village->postal_code, 'Postal Code should be int');
}
Expand Down
43 changes: 2 additions & 41 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,15 @@

namespace Creasi\Tests;

use Database\Seeders\DatabaseSeeder;
use Illuminate\Config\Repository;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase as Orchestra;

class TestCase extends Orchestra
{
use DatabaseMigrations;
use WithWorkbench;

private static $shouldMigrate = true;

protected function defineDatabaseMigrations()
{
$nusa = \config('database.connections.nusa', []);

if (self::$shouldMigrate) {
$this->recreateDatabase($nusa['database']);

$this->loadMigrationsWithoutRollbackFrom(
\realpath(\dirname($nusa['database'])).'/migrations'
);
}
}

protected function defineDatabaseSeeders()
{
if (self::$shouldMigrate) {
$this->seed(DatabaseSeeder::class);

self::$shouldMigrate = false;
}
}

private function recreateDatabase(string $path)
{
if (\file_exists($path)) {
\unlink($path);
}

\touch($path);

return $path;
}

private function mergeConfig(Repository $config, string $key, array $value)
protected function defineEnvironment($app)
{
$config->set($key, array_merge($config->get($key, []), $value));
// $app['config']->set('database.default', env('DB_CONNECTION'));
}
}
3 changes: 2 additions & 1 deletion workbench/.env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_DATABASE=creasi_test
DB_USERNAME=root
DB_PASSWORD=

DB_NUSA=nusantara
UPSTREAM_DB_DATABASE=nusantara
Loading
Loading