diff --git a/.lando.yml b/.lando.yml index 302a146..bb34128 100644 --- a/.lando.yml +++ b/.lando.yml @@ -17,6 +17,10 @@ services: database: type: mariadb:10.5.23 portforward: 32778 + creds: + user: pressbooks_oss_user + password: secretpassword + database: pressbooks_oss redis: type: redis:5.0 portforward: 6380 @@ -32,6 +36,8 @@ services: - scripts/pressbooks_required_libraries.sh build: - composer install + run_as_root: + - bash /app/scripts/prepare_test_environment.sh mailhog: hogfrom: - appserver @@ -51,7 +57,7 @@ tooling: install-tests: description: Install test requirements cmd: - - appserver: scripts/prepare_tests_environment.sh + - appserver: scripts/prepare_test_environment.sh test: description: Run all Unit Tests cmd: @@ -68,3 +74,11 @@ tooling: description: Run tests by filter cmd: - appserver: composer run filter_test + db-import-custom: + description: Import a SQL dump into the database defined in .env + cmd: + - appserver: scripts/import_db.sh + user: root +events: + post-start: + - database: bash /app/scripts/import_db.sh /app/pb_local_db.sql diff --git a/README.md b/README.md index bc72c28..88db020 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ This repository uses Lando/Docker to provision a local instance of Pressbooks fo ```bash lando start ``` - During the build process, you may be asked for an installation folder for Prince: + This will create all the services needed to install a local instance of Pressbooks and import a sample database. During the build process, you may be asked for an installation folder for Prince: ```bash Install directory This is the directory in which Prince 20220930 will be installed. @@ -45,20 +45,14 @@ This repository uses Lando/Docker to provision a local instance of Pressbooks fo [/usr]: ``` Press `Enter` to accept the default directory. -8. Import the prepared sample database included in this repo: - ```bash - lando db-import pb_local_db.sql - ``` -9. Install Pressbooks testing utilities - ```bash - lando install-tests - ``` -10. [Optional] Tell your host machine to trust the default Lando Certificate Authority by following these instructions: https://docs.lando.dev/core/v3/security.html#trusting-the-ca +8. [Optional] Tell your host machine to trust the default Lando Certificate Authority by following these instructions: https://docs.lando.dev/core/v3/security.html#trusting-the-ca ### Web access Once you have completed these steps, you should be able to use Pressbooks locally by visiting `http://pressbooks.test` or `https://pressbooks.test`. ### Running tests +Everything needed to run unit tests will be provided when you run `lando start`. You can re-install the Pressbooks test suite by running `lando install-tests`. + You can run tests inside your Lando instance with the following commands: `lando test` (this is a shortcut which runs the core Pressbooks unit tests inside your container) @@ -79,13 +73,14 @@ You can set up access to your database in your IDE by creating a new MariaDB con 2. In PHPStorm, open the `Database` menu (on the right side of the IDE), click the `+` button and add a new `MariaDB` connection. 3. Enter the following connection data: - The `host` and `port` values obtained by running `lando info` earlier - - user: wordpress - - password: wordpress - - database: wordpress + - user: pressbooks_oss_user + - password: secretpassword + - database: pressbooks_oss ### Notes - The sample database includes a single empty public book and a single super admin user with a username / password of `admin / admin`. - The `.env.example` file provides some additional environment variables which can be used with your local Pressbooks installation but are commented out by default. If you wish to install the optional PB MathJax service, you can do so following the instructions here: https://github.com/pressbooks/pb-mathjax?tab=readme-ov-file#installation. Once you've launched the service, you can uncomment the relevant line in your local `.env` file. Similar sample `.env` variables are provided for optional DocRaptor, Sentry, Redis, and Algolia integrations. +- You can force a reimport of the sample DB by running `lando db-import-custom pb_local_db.sql --force` - `lando info` provides a list of all the services and their ports. - You can install or update dependencies in the container or any repo by navigating to the desired location and running `lando composer install` or `lando composer update`. - For SSH access to the appserver you can run: `lando ssh` or `lando ssh -u root` (if you wish to access the appserver as the root user) diff --git a/composer.json b/composer.json index 89c01a7..5d5d57e 100644 --- a/composer.json +++ b/composer.json @@ -41,7 +41,7 @@ } ], "require": { - "php": ">=8.1", + "php": ">=8.2", "composer/installers": "^2.2", "owlsdepartment/multisite-url-fixer": "dev-main", "pressbooks/pressbooks": "dev-dev", @@ -55,9 +55,8 @@ "pressbooks/pressbooks-saml-sso": "dev-dev", "roots/bedrock-autoloader": "^1.0", "roots/bedrock-disallow-indexing": "^2.0", - "roots/wordpress": "^6.4", + "roots/wordpress": "^6.8", "roots/wp-config": "1.0.0", - "roots/wp-password-bcrypt": "1.1.0", "vlucas/phpdotenv": "^5.6" }, "require-dev": { diff --git a/scripts/import_db.sh b/scripts/import_db.sh new file mode 100755 index 0000000..8dac289 --- /dev/null +++ b/scripts/import_db.sh @@ -0,0 +1,63 @@ +#!/bin/bash +# Safe automatic DB import for Lando with optional force import +# Usage: +# ./import_db.sh /app/pb_local_db.sql # safe import, skips if DB exists +# ./import_db.sh --force /app/pb_local_db.sql # force re-import + +FORCE=false +SQL_FILE="" + +# Parse arguments +for arg in "$@"; do + case $arg in + --force|-f) + FORCE=true + shift + ;; + *) + SQL_FILE="$arg" + shift + ;; + esac +done + +if [ -z "$SQL_FILE" ]; then + echo "Usage: $0 [--force|-f] " + exit 1 +fi + +# Create .env from .env.example if .env doesn't exist +if [ ! -f /app/.env ]; then + cp /app/.env.example /app/.env + echo "Created .env from .env.example" +fi + +# Load environment variables from .env +set -a +source /app/.env +set +a + +if [ "$FORCE" = false ]; then + # Check if WordPress tables exist or if database is empty + WP_TABLES=$(mysql -h "$DB_HOST" -P 3306 -u "$DB_USER" -p"$DB_PASSWORD" "$DB_NAME" -e "SHOW TABLES LIKE 'wp_%';" 2>/dev/null | wc -l) + + if [ "$WP_TABLES" -gt 0 ]; then + echo "Database '$DB_NAME' contains WordPress tables. Skipping import." + exit 0 + fi +fi + +echo "Database '$DB_NAME' will be created/imported..." + +# Drop and recreate DB if force is true +if [ "$FORCE" = true ]; then + mysql -h "$DB_HOST" -P 3306 -u "$DB_USER" -p"$DB_PASSWORD" -e "DROP DATABASE IF EXISTS $DB_NAME;" +fi + +# Ensure database exists +mysql -h "$DB_HOST" -P 3306 -u "$DB_USER" -p"$DB_PASSWORD" -e "CREATE DATABASE IF NOT EXISTS $DB_NAME;" + +# Import SQL +mysql -h "$DB_HOST" -P 3306 -u "$DB_USER" -p"$DB_PASSWORD" "$DB_NAME" < "$SQL_FILE" + +echo "Import complete!" diff --git a/scripts/prepare_tests_environment.sh b/scripts/prepare_test_environment.sh similarity index 100% rename from scripts/prepare_tests_environment.sh rename to scripts/prepare_test_environment.sh diff --git a/scripts/pressbooks_required_libraries.sh b/scripts/pressbooks_required_libraries.sh index 40f911b..a95488e 100755 --- a/scripts/pressbooks_required_libraries.sh +++ b/scripts/pressbooks_required_libraries.sh @@ -32,7 +32,7 @@ else fi # Install latest version of Node -curl -sL "https://deb.nodesource.com/setup_18.x" | bash - \ +curl -sL "https://deb.nodesource.com/setup_22.x" | bash - \ && apt-get install -y nodejs # install missing xsl extension