lando icon indicating copy to clipboard operation
lando copied to clipboard

lamp/lemp db-import database option

Open drjayvee opened this issue 4 years ago • 2 comments

In the lamp recipe, there's a tooling command db-import.

While it does provide a --host|-h option, it unfortunately doesn't have a --database|-d option to specify which database to use for the import.

The documentation for db-import seems to hint that this is (or at some point was) possible:

# Import a file into an auxiliary second database called 'db2'
# with a db called `dataz`

The sql-import.sh helper does have some code related to this feature:

# Get type-specific config
if [[ ${POSTGRES_DB} != '' ]]; then
  DATABASE=${POSTGRES_DB:-database}
  # ...
else
  DATABASE=${MYSQL_DATABASE:-database}
  # ...
fi

# ...

# Build DB specific connection string
if [[ ${POSTGRES_DB} != '' ]]; then
  CMD="psql postgresql://$USER@$HOST:$PORT/$DATABASE"
else
  CMD="mysql -h $HOST -P $PORT -u $USER ${LANDO_EXTRA_DB_IMPORT_ARGS}"
fi

I've tried lando db-import --database name file.sql, but no cigar. The command always defaults to using the database lamp.

drjayvee avatar Jul 01 '21 11:07 drjayvee

In my .lando.yml file I've overridden that tool like so:

tooling:
  'db-import <file>': disabled
  'db-import <file> [<database>]':
    service: database
    description: Imports a dump file into a database service
    cmd: /app/scripts/sql-import.sh

Then you can handle this argument with your replacement script. E.g.:

DATABASE=${2:-${DRUPAL_DATABASE_NAME:-drupal}}

phil-s avatar Oct 27 '21 23:10 phil-s

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions and please check out this if you are wondering why we auto close issues.

stale[bot] avatar Apr 27 '22 21:04 stale[bot]

We haven't heard anything here for about a year so we are automatically closing this issue to keep things tidy. If this is in error then please post in this thread and request the issue be reopened!

stale[bot] avatar May 22 '23 23:05 stale[bot]

The documentation noted in the original issue still exists:

# Import a file into an auxiliary second database called 'db2'
# with a db called `dataz`

Is it possible to specify a database for import?

markdorison avatar May 25 '23 18:05 markdorison

Is it possible to specify a database for import?

Three comments above yours there's an example of doing that.

It would be nice if the default tool provided this option, though.

phil-s avatar May 25 '23 23:05 phil-s

@phil-s It's still confusing that the official documentation references something that may (or may not?) be supported. If we can get confirmation that it is not supported, then I'd be happy to submit a PR to update the docs.

That said, I appreciate you responding! Is your suggestion to copy the existing import script and then modify it in some way with the snippet you shared?

markdorison avatar May 26 '23 01:05 markdorison

Yes, if the standard script works for you otherwise then a small tweak to that will probably do the trick. I'm not using that script myself, but I presume it's the one I can see in ~/.lando/scripts/sql-import.sh which does this:

if [[ ${POSTGRES_DB} != '' ]]; then
  DATABASE=${POSTGRES_DB:-database}
  PORT=${LANDO_DB_IMPORT_PORT:-5432}
  USER=${LANDO_DB_IMPORT_USER:-postgres}
else
  DATABASE=${MYSQL_DATABASE:-database}
  PORT=${LANDO_DB_IMPORT_PORT:-3306}
  USER=${LANDO_DB_IMPORT_USER:-root}
fi

If you copy that script, it's probably simplest just to append the following to that code:

if [ -n "$2" ]; then
  DATABASE=$2
fi

phil-s avatar May 26 '23 01:05 phil-s

@phil-s Thanks I will give this a shot!

markdorison avatar May 26 '23 01:05 markdorison