Welcome Guest, Not a member yet? Register   Sign In
Migrations inside composer scripts
#1

Hello.
I have a composer package. It's like small cms (with db tables).
My target is to start migrations after package install/update from composer. In this case I use composer scripts. But there are no any autoloaded classes exist inside it.
composer.json:
Code:
{
    "name": "nwnpc/ci4-cms",
    "description": "Small seo friendly ci4 cms",
    "license": "MIT",
    "minimum-stability": "dev",
    "require": {
        "php": "^7.3 || ^8.0"
    },
    "autoload": {
        "psr-4": {
            "Velldoris\\": "src/"
        }
    },
    "scripts": "Velldoris\\Database\\Migration::install"
}


Velldoris\Database\Migration::install:
Code:
        try {
            $velldoris_app_config = config('Velldoris\\Config\\VelldorisApp');
            $db = \Config\Database::connect($velldoris_app_config->DBGroup);
            $db->getVersion();
        } catch (\Throwable $e) {
            print_r('Velldoris package installation error: ' . $e->getMessage());
            exit;
        }
   
        $migrations = service('Migrations', config('Config\Migrations'), $db);
        $migrations->setNamespace('Velldoris');
   
        try {
            $migrations->latest();
        } catch (\Throwable $e) {
            print_r('Velldoris package installation error: ' . $e->getMessage());
            exit;
        }


There is an error like "Codeigniter class not defined" while i try to init my method strict ("composer run-script post-install-cmd"). And it's not surprising.

Here are the solutions I see:
1) Load all project classes throught composer autoload.php
2) Use mysql dump instead of migrations (really?)
Reply
#2

I'm no composer expert, but can't you call "spark migrate -all"?
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply
#3

(This post was last modified: 10-16-2021, 03:23 PM by ARAmiss.)

I would like auto start migrations when "composer require ..." and "composer update". Not manual.
The composer allows to do this. For example:
https://getcomposer.org/doc/articles/scr...hp-scripts

But the command "spark migrate -all" can be called in the ROOTPATH. How can i call this from project root path?

The another way (similar to spark migrate) is to call controller method like this:
spark install index
spark update index

And this is the same question - how to call from root.



The way is to create own spark file in the package root. And then we can call it something like this:

Code:
    "scripts": {
        "post-package-install": [
            "@composer run-migrations"
        ],
        "run-migrations": [
            "@php spark -install -index"
        ]
    }


And how can we pass execution from our package spark to default /project-root-path/spark?
Reply
#4

(This post was last modified: 10-17-2021, 12:39 AM by ARAmiss.)

About spark. There is FCPATH definition inside it:
Code:
// Path to the front controller
define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR);


What if my public directory named not "public"? Any idea?
Reply
#5

(10-17-2021, 12:39 AM)ARAmiss Wrote: About spark. There is FCPATH definition inside it:
Code:
// Path to the front controller
define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR);


What if my public directory named not "public"? Any idea?
In that case you need to edit the spark script. There should be a better way to configure this, but at the moment I think it's the only way to do it.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply
#6

(This post was last modified: 10-17-2021, 06:56 AM by ARAmiss.)

Thank you.
Despite it is not very convenient to edit the spark file on each project, apparently this is the only option for now.

What about getting the root folder of the project from the composer scripts? Maybe someone has faced such a task?
And in general, how does anyone solve the task of starting migrations after install/update package from the composer? Or is it all done by hand from the command line?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB