Skip to content

Commit 226ad51

Browse files
refactor: convert FQCN morph types to aliases (#187)
## Summary - Add migrations to deduplicate external identities and convert morph types to aliases - Update all modules (identity, events, gamification, bot-discord, economy, activity) to use morph aliases instead of FQCN ## Changes - **database**: 3 new migrations for morph type normalization - **identity**: update polymorphic morph types to use aliases - **events/gamification**: use morph aliases for polymorphic relations - **bot-discord/economy/activity**: update morph types across modules <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Deduplicated external identities, merged related messages and experience, and removed orphaned user accounts * Normalized polymorphic type identifiers to prevent provider-resolution mismatches * **Chores** * Registered explicit polymorphic mappings and standardized morph-type handling across the app * Switched test/runtime setup to PostgreSQL and updated CI/workflow/test scripts * Added migrations to convert morph types and adjust schema; bumped tooling/package versions * **Other** * Enforced unique usernames in test data generation and minor README text update <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Gabriel do Carmo Vieira <48625433+gvieira18@users.noreply.github.com>
1 parent 6f151f3 commit 226ad51

41 files changed

Lines changed: 825 additions & 455 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.env.testing.example

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ LOG_CHANNEL=stack
88
LOG_DEPRECATIONS_CHANNEL=null
99
LOG_LEVEL=debug
1010

11-
DB_CONNECTION=testing
12-
DB_HOST=mysql
13-
DB_PORT=3306
14-
DB_DATABASE=dev_he4rtbot
15-
DB_USERNAME=root
16-
DB_PASSWORD=root
11+
DB_CONNECTION=pgsql
12+
DB_HOST=127.0.0.1
13+
DB_PORT=5432
14+
DB_DATABASE=test_he4rtbot
15+
DB_USERNAME=postgres
16+
DB_PASSWORD=postgres
1717

1818
BROADCAST_DRIVER=log
1919
CACHE_DRIVER=file

.github/workflows/_pest.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ jobs:
2727
concurrency:
2828
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}-tests
2929
cancel-in-progress: true
30+
31+
services:
32+
postgres:
33+
image: postgres:18-alpine
34+
env:
35+
POSTGRES_USER: postgres
36+
POSTGRES_PASSWORD: postgres
37+
POSTGRES_DB: test_he4rtbot
38+
ports:
39+
- 5432/tcp
40+
options: >-
41+
--health-cmd pg_isready
42+
--health-interval 10s
43+
--health-timeout 5s
44+
--health-retries 5
45+
3046
steps:
3147
- name: Checkout code
3248
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
@@ -70,5 +86,6 @@ jobs:
7086
- name: Run Tests
7187
env:
7288
XDEBUG_MODE: coverage
89+
DB_PORT: ${{ job.services.postgres.ports['5432'] }}
7390
run: |
7491
vendor/bin/pest --ci --parallel

.github/workflows/continuous-integration.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
id: setup
3232
run: |
3333
echo "PHP_VERSION=8.4" >> "$GITHUB_OUTPUT"
34-
echo "PHP_EXTENSIONS=mbstring,pdo,xml,ctype,fileinfo,json,curl,openssl,dom,zip" >> "$GITHUB_OUTPUT"
34+
echo "PHP_EXTENSIONS=mbstring,pdo,pdo_pgsql,xml,ctype,fileinfo,json,curl,openssl,dom,zip" >> "$GITHUB_OUTPUT"
3535
echo "PHP_INI_PROPERTIES=post_max_size=256M,upload_max_filesize=256M" >> "$GITHUB_OUTPUT"
3636
echo "COMPOSER_FLAGS=--prefer-dist --optimize-autoloader" >> "$GITHUB_OUTPUT"
3737

Makefile

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,22 @@ check: test-rector test-pint test-phpstan ## Run Pint, PHPStan with Rector in dr
5050

5151
.PHONY: test
5252
test: ## Run all tests
53-
@$(CURDIR)/vendor/bin/pest --parallel --compact
53+
@$(CURDIR)/vendor/bin/pest --compact
5454

5555
.PHONY: t
5656
t: test ## Alias for test
5757

5858
.PHONY: test-unit
5959
test-unit: ## Run unit tests
60-
@$(CURDIR)/vendor/bin/pest --parallel --compact --group=unit
60+
@$(CURDIR)/vendor/bin/pest --compact --group=unit
6161

6262
.PHONY: test-feature
6363
test-feature: ## Run feature tests
64-
@$(CURDIR)/vendor/bin/pest --parallel --compact --group=feature
64+
@$(CURDIR)/vendor/bin/pest --compact --group=feature
65+
66+
.PHONY: setup-test-db
67+
setup-test-db: ## Create the testing database for running tests
68+
@PGHOST=localhost PGUSER=postgres PGPASSWORD=postgres createdb test_he4rtbot 2>/dev/null || echo "Database test_he4rtbot already exists"
6569

6670
.PHONY: migrate-fresh
6771
migrate-fresh: ## Run migrations and seed the database
@@ -89,6 +93,10 @@ setup: ## Setup the project
8993
@php artisan storage:link --ansi
9094
@composer run-script ide-helper
9195

96+
.PHONY: import-db
97+
import-db: ## Import a PostgreSQL dump file (usage: make import-db file=path/to/dump)
98+
@PGHOST=localhost PGUSER=postgres PGPASSWORD=postgres pg_restore -x -O -cC -j 8 -d postgres $(file)
99+
92100
.PHONY: bot
93101
bot: ## Run the Discord bot
94102
@php artisan bot:boot

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# He4rtBot Discord API
1+
# he4rtdevs.com
22

33
<p align="center">
44
<a href="https://discord.gg/he4rt">

app-modules/activity/src/Actions/NewMessage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function persist(NewMessageDTO $messageDTO): void
2525
'tenant_id' => $messageDTO->tenantId,
2626
'provider' => $messageDTO->provider,
2727
'external_account_id' => $messageDTO->externalAccountId,
28-
'model_type' => User::class,
28+
'model_type' => (new User)->getMorphClass(),
2929
'username' => $messageDTO->providerUsername,
3030
]);
3131

app-modules/bot-discord/src/Events/MessageReceivedEvent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function handle(Message $message, Discord $discord): void
3232

3333
try {
3434
$tenantProvider = ExternalIdentity::query()
35-
->where('model_type', Tenant::class)
35+
->where('model_type', (new Tenant)->getMorphClass())
3636
->where('external_account_id', (string) $message->guild_id)
3737
->firstOrFail();
3838

app-modules/bot-discord/src/Events/WelcomeMember.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function handle(Member $member, Discord $discord): void
2525
$channelId = config('bot-discord.channels.auto-report');
2626

2727
$tenantProvider = ExternalIdentity::query()
28-
->where('model_type', Tenant::class)
28+
->where('model_type', (new Tenant)->getMorphClass())
2929
->where('external_account_id', (string) $member->guild_id)
3030
->firstOrFail();
3131

@@ -34,7 +34,7 @@ public function handle(Member $member, Discord $discord): void
3434
'tenant_id' => $tenantProvider->tenant_id,
3535
'provider' => $tenantProvider->provider,
3636
'external_account_id' => $member->user->id,
37-
'model_type' => User::class,
37+
'model_type' => (new User)->getMorphClass(),
3838
'username' => $member->user->username,
3939
'avatar' => $member->user->avatar,
4040
]);

app-modules/bot-discord/src/SlashCommands/AbstractSlashCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,21 @@ protected function getMemberProviderQuery(): Builder
4242
{
4343
return ExternalIdentity::query()
4444
->where('tenant_id', $this->tenantProvider->tenant_id)
45-
->where('model_type', User::class)
45+
->where('model_type', (new User)->getMorphClass())
4646
->where('provider', IdentityProvider::Discord);
4747
}
4848

4949
private function beforePipeline(Interaction $interaction): void
5050
{
5151
$this->tenantProvider = ExternalIdentity::query()
52-
->where('model_type', Tenant::class)
52+
->where('model_type', (new Tenant)->getMorphClass())
5353
->where('provider', IdentityProvider::Discord)
5454
->where('external_account_id', $interaction->guild_id)
5555
->first();
5656

5757
$this->memberProvider = ExternalIdentity::query()
5858
->where('tenant_id', $this->tenantProvider->tenant_id)
59-
->where('model_type', User::class)
59+
->where('model_type', (new User)->getMorphClass())
6060
->where('provider', IdentityProvider::Discord)
6161
->where('external_account_id', $interaction->user->id)
6262
->first();

app-modules/bot-discord/src/SlashCommands/IntroductionCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,15 @@ public function handle(Interaction $interaction): void
115115
private function persistData(Interaction $interaction, Collection $components): void
116116
{
117117
$tenantProvider = ExternalIdentity::query()
118-
->where('model_type', Tenant::class)
118+
->where('model_type', (new Tenant)->getMorphClass())
119119
->where('external_account_id', (string) $interaction->guild_id)
120120
->firstOrFail();
121121

122122
$userDto = ResolveUserProviderDTO::make([
123123
'tenant_id' => $tenantProvider->tenant_id,
124124
'provider' => $tenantProvider->provider,
125125
'external_account_id' => $interaction->user->id,
126-
'model_type' => User::class,
126+
'model_type' => (new User)->getMorphClass(),
127127
'username' => $interaction->user->username,
128128
'avatar' => $interaction->user->avatar,
129129
]);

0 commit comments

Comments
 (0)