Skip to content

Instantly share code, notes, and snippets.

@mtbottens
Last active February 27, 2024 11:38
Show Gist options
  • Save mtbottens/c7daaca302032db27b09ce93b9c84214 to your computer and use it in GitHub Desktop.
Save mtbottens/c7daaca302032db27b09ce93b9c84214 to your computer and use it in GitHub Desktop.
Easily update, import and sanitize magento2 cloud databases
function m2-update-database() {
# USAGE
# m2-update-database 'Magento Cloud Project Title' 'http://project.dev/'
# $1 = Project Title
# $2 = Local URL
# Get the project ID by the project title
PROJECT_ID=$(magento-cloud project:list --title=$1 --pipe)
GIT_BRANCH_NAME=$(git branch | grep \* | cut -d ' ' -f2)
# Dump the DB
magento-cloud db:dump --project=$PROJECT_ID --environment=$GIT_BRANCH_NAME -f dump.sql
# Import the DB
php bin/n98-magerun2 db:import dump.sql
# Sanitize the DB
php bin/n98-magerun2 config:set web/unsecure/base_url $2
php bin/n98-magerun2 config:set web/secure/base_url $2
php bin/n98-magerun2 config:delete web/unsecure/base_media_url
php bin/n98-magerun2 config:delete web/secure/base_media_url
php bin/n98-magerun2 config:set dev/css/merge_css_files 0
php bin/n98-magerun2 config:set dev/css/minify_files 0
php bin/n98-magerun2 config:set dev/js/merge_files 0
php bin/n98-magerun2 config:set dev/js/minify_files 0
php bin/n98-magerun2 config:delete dev/static/sign
php bin/n98-magerun2 config:delete catalog/search/engine
php bin/n98-magerun2 config:delete web/unsecure/base_static_url
php bin/n98-magerun2 config:delete web/secure/base_static_url
php bin/n98-magerun2 config:set web/secure/use_in_frontend 0
php bin/n98-magerun2 config:set web/secure/use_in_adminhtml 0
php bin/n98-magerun2 config:delete web/cookie/cookie_domain --all
# Delete the dumped database
rm dump.sql
php bin/n98-magerun2 cache:flush
php bin/n98-magerun2 indexer:reindex
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment