sourcebroker / deployer-extended-database

Deployer tasks to manage database synchronization between application instances.
MIT License
39 stars 13 forks source link

The db: pull command uses the same path on staging and local #18

Closed nonzod closed 2 years ago

nonzod commented 2 years ago

my config:

...

host('local')
  ->hostname('localhost')
  ->set('deploy_path', getcwd())
  ->set('db_databases', [
    'database_default' => [
      get('local_db')
    ]
  ]);

host('staging')
  ->hostname(get('staging_host'))
  ->user('myuser') 
  ->port(22922)  
  ->set('deploy_path', get('staging_path'))
  ->set('branch', get('staging_branch'))
  ->set('db_databases', [
    'database_default' => [
      get('staging_db')
    ]
  ]);

at ./vendor/bin/dep db:pull staging -vvv

[localhost] < ➤ Executing task db:download
[localhost] < [staging] > [ -d /<staging_path>/.dep/database/dumps ] || mkdir -p /<staging_path>/.dep/database/dumps
[localhost] < [localhost] > [ -d /<staging_path>/.dep/database/dumps ] || mkdir -p /<staging_path>/.dep/database/dumps
[localhost] < [localhost] < mkdir: impossibile creare la directory "/<staging_path>": Permission denied

Attempt to create the staging_path (get('staging_path')) directory locally instead of using local path (getcwd()). The .env files are set in both environments with INSTANCE="local" for localhost and INSTANCE="staging" for staging.

kszymukowicz commented 2 years ago

I have too little info to give you answer. For example is sourceborker/deployer-instance installed and loaded? If yes then you need to debug vendor/deployer-extended-database/deployer/db/config/set.php

set('db_storage_path_local', function () {
    if (Configuration::getLocalHost()->getConfig()->get('db_storage_path_relative', false) == false) {
        $dbStoragePathLocal = Configuration::getLocalHost()->getConfig()->get('deploy_path') . '/.dep/database/dumps';
    } else {
        $dbStoragePathLocal = Configuration::getLocalHost()->getConfig()->get('deploy_path') . '/'
            . Configuration::getLocalHost()->getConfig()->get('db_storage_path_relative');
    }
    runLocally('[ -d ' . $dbStoragePathLocal . ' ] || mkdir -p ' . $dbStoragePathLocal);
    return $dbStoragePathLocal;
});
nonzod commented 2 years ago

Thank you for the answer. Yes is installed and loaded.

composer.json

 "require-dev": {
        "deployer/deployer": "^6.8",
        "drupal/devel": "^4.1",
        "drupal/drupal-extension": "^4.1",
        "drupal/twig_xdebug": "^1.2",
        "kint-php/kint": "^3.3",
        "sourcebroker/deployer-extended-database": "^13.0"
    }

deployer.php

namespace Deployer;

require 'recipe/drupal8.php';

new \SourceBroker\DeployerLoader\Load([
  ['path' => 'vendor/sourcebroker/deployer-instance/deployer'],
  ['path' => 'vendor/sourcebroker/deployer-extended-database/deployer'],
]);

and yes, vendor/deployer-extended-database/deployer/db/config/set.php contains

// Returns path to store database dumps on local stage.
set('db_storage_path_local', function () {
    if (Configuration::getLocalHost()->getConfig()->get('db_storage_path_relative', false) == false) {
        $dbStoragePathLocal = Configuration::getLocalHost()->getConfig()->get('deploy_path') . '/.dep/database/dumps';
    } else {
        $dbStoragePathLocal = Configuration::getLocalHost()->getConfig()->get('deploy_path') . '/'
            . Configuration::getLocalHost()->getConfig()->get('db_storage_path_relative');
    }
    runLocally('[ -d ' . $dbStoragePathLocal . ' ] || mkdir -p ' . $dbStoragePathLocal);
    return $dbStoragePathLocal;
});
nonzod commented 2 years ago

Found the problem. Thanks to you suggestion I've noticed that Configuration::getLocalHost() use the default_stage value. My default_stage is 'staging', changing to 'local' everythings works. It's not intuitive but it works.

kszymukowicz commented 2 years ago

You should not change default_stage yourself. It is set automatically based on .env INSTANCE value!

Look on vendor/sourcebroker/deployer-instance/deployer/config/set.php

<?php

namespace Deployer;

use RuntimeException;
use SourceBroker\DeployerInstance\Env;

set('default_stage', function () {
    $env = new Env();
    $env->load();
    $instance = $env->get('INSTANCE');
    if ($instance === null) {
        throw new RuntimeException(
            'INSTANCE var is no set.',
            1602784218
        );
    }
    return $instance;
});

set('argument_stage', function () {
    return input()->getArgument('stage');
});
nonzod commented 2 years ago

Ok but this is in contrast with the official documentation:

https://deployer.org/docs/6.x/configuration#default_stage

kszymukowicz commented 2 years ago

I do not understand why is this in contrast. The default_stage is set for you based on .env data.

kszymukowicz commented 2 years ago

Ok - I see what you mean.

In fact I never used "default_stage" in the meaning it was created for. I always do deploy with the given instance/stage.

So always "dep deploy prod" and never "dep deploy".

kszymukowicz commented 2 years ago

Without such trick most of commands would be much more complicated.

Examples:

So because I no not use "default_stage" for doing deploys then I use "default_stage" for simplifying database operations.

nonzod commented 2 years ago

Ok, all clear. I think it is important to write this in the documentation.

thank you @kszymukowicz !