CodeIgniter Forums
AbuseIPDB Module - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Addins (https://forum.codeigniter.com/forumdisplay.php?fid=34)
+--- Thread: AbuseIPDB Module (/showthread.php?tid=93095)



AbuseIPDB Module - grimpirate - 07-02-2025

I'm working on a module for CodeIgniter4 to provide ip blocking: https://github.com/grimpirate/abuseipdb

I've run into an issue where I have a migration located under modules/AbuseIpdb/Database/Migrations.

After completing the setup and creating the SQLite database (php spark db:create abuseipdb --ext db) I attempt: php spark migrate

It's my understanding from the docs that migrations are auto-detected and run across every namespace. However, in practice, that did not occur. My migrations do not run. If I move the file from modules/AbuseIpdb/Database/Migrations into app/Database/Migrations and run the migration then it works (even without changing the namespace).

What am I doing wrong here?


RE: AbuseIPDB Module - InsiteFX - 07-02-2025

I use Lonnie's Shield migrations setup for my modules and it works fine.

Take a look at how Lonnie does it in Shield for a module.

Also check out your app/Config/Modules.php file.


RE: AbuseIPDB Module - grimpirate - 07-02-2025

Replicating shield's structure won't change what's occurring here. The underlying issue is that a migration is failing to process within a module. app/Config/Modules.php is unchanged (nor should it require any modification).

The only things that should be required to have a database migration in a module work are:
  1. Specify the psr4 namespace (Modules\AbuseIpdb) in app/Config/Autoload.php (which I've done)
  2. Create a migration within the module i.e. modules/AbuseIpdb/Database/Migrations/2025-07-02-193200_CreateAbuseIpdbConfidenceTable.php (which I've done)
From the command line I get the following:

Code:
notebook@hbfnc:/var/www$ php spark migrate         

CodeIgniter v4.6.1 Command Line Tool - Server Time: 2025-07-03 01:58:27 UTC-04:00

Running all new migrations...
Migrations complete.

notebook@hbfnc:/var/www$ php spark migrate:status

CodeIgniter v4.6.1 Command Line Tool - Server Time: 2025-07-03 01:58:34 UTC-04:00

+----------------------+-------------------+--------------------------------+---------+---------------------+-------+
| Namespace            | Version          | Filename                      | Group  | Migrated On        | Batch |
+----------------------+-------------------+--------------------------------+---------+---------------------+-------+
| Modules\AbuseIpdb    | 2025-07-02-193200 | CreateAbuseIpdbConfidenceTable | ---    | ---                | ---  |
| CodeIgniter\Shield  | 2020-12-28-223112 | create_auth_tables            | default | 2025-04-18 15:03:29 | 1    |
| CodeIgniter\Settings | 2021-07-04-041948 | CreateSettingsTable            | default | 2025-04-18 15:03:29 | 1    |
| CodeIgniter\Settings | 2021-11-14-143905 | AddContextColumn              | default | 2025-04-18 15:03:29 | 1    |
+----------------------+-------------------+--------------------------------+---------+---------------------+-------+

Which shows that the namespace is correct and that the migration table is being detected, but the migration isn't being processed.

You can see a full demonstration of the project here that clearly shows the lack of creation of the 'confidence' table within the abuseipdb.db sqlite3 database.

If I mv the migration file into app/Database/Migrations and run the migrations:

Code:
notebook@phkh2:/var/www$ php spark migrate     

CodeIgniter v4.6.1 Command Line Tool - Server Time: 2025-07-03 02:35:14 UTC-04:00

Running all new migrations...
        Running: (App) 2025-07-02-193200_Modules\AbuseIpdb\Database\Migrations\CreateAbuseIpdbConfidenceTable
Migrations complete.

notebook@phkh2:/var/www$ php spark  migrate:status

CodeIgniter v4.6.1 Command Line Tool - Server Time: 2025-07-03 02:36:56 UTC-04:00

+----------------------+-------------------+--------------------------------+---------+---------------------+-------+
| Namespace            | Version          | Filename                      | Group  | Migrated On        | Batch |
+----------------------+-------------------+--------------------------------+---------+---------------------+-------+
| App                  | 2025-07-02-193200 | CreateAbuseIpdbConfidenceTable | default | 2025-07-03 02:35:15 | 2    |
| CodeIgniter\Shield  | 2020-12-28-223112 | create_auth_tables            | default | 2025-04-18 15:03:29 | 1    |
| CodeIgniter\Settings | 2021-07-04-041948 | CreateSettingsTable            | default | 2025-04-18 15:03:29 | 1    |
| CodeIgniter\Settings | 2021-11-14-143905 | AddContextColumn              | default | 2025-04-18 15:03:29 | 1    |
+----------------------+-------------------+--------------------------------+---------+---------------------+-------+

Two things are notable here (apart from the obvious that there's nothing wrong with the migration file itself):
  1. The Namespace is listed as App, despite the file clearly designating it as namespace Modules\AbuseIpdb\Database\Migrations;
  2. The Group is listed as default, but it should be abuseipdb which is what is specified in the migration file
The module is now working as intended, demonstrated here. This seems like a bug but I don't discount that I may have missed something and am hoping someone more knowledgeable can point it out.


RE: AbuseIPDB Module - InsiteFX - 07-03-2025

You can also use spark migrate --all to include migrations from all namespaces.

PHP Code:
For Unix:

php spark migrate test -n Acme\\Blog

For Windows:

php spark migrate test -n Acme\Blog 


Try that and see if it works.

If that doe's not work then I would report it on GitHub as a bug.


RE: AbuseIPDB Module - grimpirate - 07-03-2025

Thank you for the help InsiteFX. The nuances of performing a migration on the command line leave much to be desired.

My observations for the curious:

Code:
php spark migrate

Will not perform any migration

Code:
php spark migrate --all

Will perform the migrations from all namespaces but will designate the Group as default (should be picked up from within the migration file in my opinion)

Code:
php spark migrate -n Modules\\AbuseIpdb -g abuseipdb

Will perform the migration as expected (only the relevant namespace) and assign the appropriate group.


RE: AbuseIPDB Module - InsiteFX - 07-03-2025

Yes I have to agree with you on nuances of performing a migration on the command line.

Maybe someone will get the time to look into it.