From 235266ec53f620d94a080ed7ae8e77eaef6dbb3c Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 16 Jan 2018 21:26:59 +0100 Subject: Added basic database migration functionality --- bin/migrate | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100755 bin/migrate (limited to 'bin') diff --git a/bin/migrate b/bin/migrate new file mode 100755 index 00000000..20ae2a93 --- /dev/null +++ b/bin/migrate @@ -0,0 +1,21 @@ +#!/usr/bin/env php +register(MigrationServiceProvider::class); + +/** @var Migrate $migration */ +$migration = $app->get('db.migration'); +$migration->setOutput(function ($text) { echo $text . PHP_EOL; }); +$migration->run($baseDir, Migrate::UP); -- cgit v1.2.3-54-g00ecf From 2f41b9e4418def9b69cf237312bc592364585025 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 7 Aug 2018 16:47:47 +0200 Subject: Moved deploy.sh to bin folder --- .gitlab-ci.yml | 6 ++--- README.md | 7 +++--- bin/deploy.sh | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ deploy.sh | 72 ---------------------------------------------------------- 4 files changed, 79 insertions(+), 78 deletions(-) create mode 100755 bin/deploy.sh delete mode 100755 deploy.sh (limited to 'bin') diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 8018543b..250bf0db 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -76,7 +76,7 @@ test:7.1: # Install project and dependencies - &deployment_dependencies |- - chmod +x ./deploy.sh + chmod +x ./bin/deploy.sh apt update && apt install -yqq rsync openssh-client /usr/local/bin/composer --no-ansi install --no-dev /usr/local/bin/composer --no-ansi dump-autoload --optimize @@ -95,7 +95,7 @@ deploy_staging: - *deployment_ssh - *deployment_dependencies # Deploy to server - - ./deploy.sh -r "${STAGING_REMOTE}" -p "${STAGING_REMOTE_PATH}" -i "${CI_JOB_ID}-${CI_COMMIT_SHA}" + - ./bin/deploy.sh -r "${STAGING_REMOTE}" -p "${STAGING_REMOTE_PATH}" -i "${CI_JOB_ID}-${CI_COMMIT_SHA}" deploy_production: <<: *deploy_definition @@ -112,4 +112,4 @@ deploy_production: - *deployment_ssh - *deployment_dependencies # Deploy to server - - ./deploy.sh -r "${PRODUCTION_REMOTE}" -p "${PRODUCTION_REMOTE_PATH}" -i "${CI_JOB_ID}-${CI_COMMIT_SHA}" + - ./bin/deploy.sh -r "${PRODUCTION_REMOTE}" -p "${PRODUCTION_REMOTE_PATH}" -i "${CI_JOB_ID}-${CI_COMMIT_SHA}" diff --git a/README.md b/README.md index 662d994c..d56603a5 100644 --- a/README.md +++ b/README.md @@ -73,10 +73,11 @@ PRODUCTION_REMOTE # Same as STAGING_REMOTE but for the production environm PRODUCTION_REMOTE_PATH # Same as STAGING_REMOTE_PATH but for the production environment ``` -#### deploy.sh -The `deploy.sh` script can be used to deploy the engelsystem. It uses rsync to deploy the application to a server over ssh. +#### Scripts +##### bin/deploy.sh +The `bin/deploy.sh` script can be used to deploy the engelsystem. It uses rsync to deploy the application to a server over ssh. -For usage see `./deploy.sh -h` +For usage see `./bin/deploy.sh -h` ### Codestyle Please ensure that your pull requests follow [PSR-2](http://www.php-fig.org/psr/psr-2/) and [PSR-4](http://www.php-fig.org/psr/psr-4/). diff --git a/bin/deploy.sh b/bin/deploy.sh new file mode 100755 index 00000000..f1569a61 --- /dev/null +++ b/bin/deploy.sh @@ -0,0 +1,72 @@ +#!/usr/bin/env bash + +set -e + +remote_host= +remote_path= +deploy_id= + +show_help(){ + echo "Usage: $0 [OPTION]..." >&2 + echo "Deploys a software with rsync over ssh" >&2 + echo "The options -i, -p and -r are required" >&2 + echo "" >&2 + echo " -h Display this help screen" >&2 + echo " -i Sets the id, a unique name for the deployment" >&2 + echo " -p Define the base path on the server" >&2 + echo " -r The remote server name and user" >&2 +} + +while getopts ":hi:p:r:" opt; do + case ${opt} in + h) + show_help + exit 1 + ;; + r) + remote_host="$OPTARG" + ;; + p) + remote_path="$OPTARG" + ;; + i) + deploy_id="$OPTARG" + ;; + \?) + echo "Invalid option: -$OPTARG" >&2 + exit 1 + ;; + :) + echo "The option -$OPTARG requires an argument" >&2 + exit 1 + ;; + esac +done + +if [ -z "${remote_host}" ] || [ -z "${remote_path}" ] || [ -z "${deploy_id}" ]; then + show_help + exit 1 +fi + +echo "syncing ${PWD}/ to ${remote_host}:${remote_path}/${deploy_id}/" + +rsync -vAax --exclude '.git*' --exclude .composer/ \ + -e "ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" \ + ./ "${remote_host}:${remote_path}/${deploy_id}/" + +ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "${remote_host}" " + set -e + + if [[ -f \"${remote_path}/current/config/config.php\" ]]; then + echo \"Config backup\" + cp \"${remote_path}/current/config/config.php\" \"${deploy_id}-config.php\" + fi + + echo \"Changing symlink\" + unlink \"${remote_path}/current\" && ln -s \"${remote_path}/${deploy_id}\" \"${remote_path}/current\" + + if [[ -f \"${deploy_id}-config.php\" ]]; then + echo \"Restoring config\" + cp \"${deploy_id}-config.php\" \"${remote_path}/current/config/config.php\" + fi +" diff --git a/deploy.sh b/deploy.sh deleted file mode 100755 index f1569a61..00000000 --- a/deploy.sh +++ /dev/null @@ -1,72 +0,0 @@ -#!/usr/bin/env bash - -set -e - -remote_host= -remote_path= -deploy_id= - -show_help(){ - echo "Usage: $0 [OPTION]..." >&2 - echo "Deploys a software with rsync over ssh" >&2 - echo "The options -i, -p and -r are required" >&2 - echo "" >&2 - echo " -h Display this help screen" >&2 - echo " -i Sets the id, a unique name for the deployment" >&2 - echo " -p Define the base path on the server" >&2 - echo " -r The remote server name and user" >&2 -} - -while getopts ":hi:p:r:" opt; do - case ${opt} in - h) - show_help - exit 1 - ;; - r) - remote_host="$OPTARG" - ;; - p) - remote_path="$OPTARG" - ;; - i) - deploy_id="$OPTARG" - ;; - \?) - echo "Invalid option: -$OPTARG" >&2 - exit 1 - ;; - :) - echo "The option -$OPTARG requires an argument" >&2 - exit 1 - ;; - esac -done - -if [ -z "${remote_host}" ] || [ -z "${remote_path}" ] || [ -z "${deploy_id}" ]; then - show_help - exit 1 -fi - -echo "syncing ${PWD}/ to ${remote_host}:${remote_path}/${deploy_id}/" - -rsync -vAax --exclude '.git*' --exclude .composer/ \ - -e "ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" \ - ./ "${remote_host}:${remote_path}/${deploy_id}/" - -ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "${remote_host}" " - set -e - - if [[ -f \"${remote_path}/current/config/config.php\" ]]; then - echo \"Config backup\" - cp \"${remote_path}/current/config/config.php\" \"${deploy_id}-config.php\" - fi - - echo \"Changing symlink\" - unlink \"${remote_path}/current\" && ln -s \"${remote_path}/${deploy_id}\" \"${remote_path}/current\" - - if [[ -f \"${deploy_id}-config.php\" ]]; then - echo \"Restoring config\" - cp \"${deploy_id}-config.php\" \"${remote_path}/current/config/config.php\" - fi -" -- cgit v1.2.3-54-g00ecf From 2bebbeb1919e1d370ac5c0668e0db5ea63e73292 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Thu, 30 Aug 2018 16:07:50 +0200 Subject: Installation: Added migrations for install.sql and update.sql files --- .gitlab-ci.yml | 9 ++-- README.md | 7 ++- bin/migrate | 19 ++++++- .../2018_01_01_000001_import_install_sql.php | 59 ++++++++++++++++++++++ .../2018_01_01_000002_import_update_sql.php | 27 ++++++++++ includes/autoload.php | 3 +- 6 files changed, 116 insertions(+), 8 deletions(-) create mode 100644 db/migrations/2018_01_01_000001_import_install_sql.php create mode 100644 db/migrations/2018_01_01_000002_import_update_sql.php (limited to 'bin') diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 70e1d59b..c15a59db 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -29,15 +29,14 @@ before_script: - &before_install_xdebug |- pecl install xdebug docker-php-ext-enable xdebug - # MySQL DB - - &before_setup_mysql |- - apt install -yqq mariadb-client - mysql -h "$MYSQL_HOST" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" "$MYSQL_DATABASE" < db/install.sql - mysql -h "$MYSQL_HOST" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" "$MYSQL_DATABASE" < db/update.sql # Install Composer - &before_install_composer |- curl -sS https://getcomposer.org/installer | php -- --no-ansi --install-dir /usr/local/bin/ --filename composer composer --no-ansi install + # MySQL DB + - &before_setup_mysql |- + chmod +x ./bin/migrate + ./bin/migrate # Install Node.js and Yarn - &before_install_yarn |- apt -yqq install gnupg2 apt-transport-https diff --git a/README.md b/README.md index afcc3462..960d6b6d 100644 --- a/README.md +++ b/README.md @@ -41,8 +41,8 @@ To report bugs use [engelsystem/issues](https://github.com/engelsystem/engelsyst * Recommended: Directory Listing should be disabled. * There must a be MySQL database created with a user who has full rights to that database. - * It must be created by the ```db/install.sql``` and ```db/update.sql``` files. * If necessary, create a ```config/config.php``` to override values from ```config/config.default.php```. + * To import the database the ```bin/migrate``` script has to be called. * In the browser, login with credentials ```admin```:```asdfasdf``` and change the password. Engelsystem can now be used. @@ -109,5 +109,10 @@ The `bin/deploy.sh` script can be used to deploy the engelsystem. It uses rsync For usage see `./bin/deploy.sh -h` +##### bin/migrate +The `bin/migrate` script can be used to import and update the database of the engelsystem. + +For more information on how to use it call `bin/migrate help` + ### Codestyle Please ensure that your pull requests follow [PSR-2](http://www.php-fig.org/psr/psr-2/) and [PSR-4](http://www.php-fig.org/psr/psr-4/). diff --git a/bin/migrate b/bin/migrate index 20ae2a93..ab3598d4 100755 --- a/bin/migrate +++ b/bin/migrate @@ -18,4 +18,21 @@ $app->register(MigrationServiceProvider::class); /** @var Migrate $migration */ $migration = $app->get('db.migration'); $migration->setOutput(function ($text) { echo $text . PHP_EOL; }); -$migration->run($baseDir, Migrate::UP); + +if (isset($argv[1]) && strtolower($argv[1]) == 'help') { + echo PHP_EOL . 'Usage: ' . $argv[1] . ' [up|down] [one-step]' . PHP_EOL . PHP_EOL; + exit; +} + +$method = Migrate::UP; +if (isset($argv[1]) && strtolower($argv[1]) == 'down') { + $argv = array_values($argv); + $method = Migrate::DOWN; +} + +$oneStep = false; +if (isset($argv[2]) && strtolower($argv[2]) == 'one-step') { + $oneStep = true; +} + +$migration->run($baseDir, $method, $oneStep); diff --git a/db/migrations/2018_01_01_000001_import_install_sql.php b/db/migrations/2018_01_01_000001_import_install_sql.php new file mode 100644 index 00000000..bd7ec7ae --- /dev/null +++ b/db/migrations/2018_01_01_000001_import_install_sql.php @@ -0,0 +1,59 @@ +oldTables as $table) { + if ($this->schema->hasTable($table)) { + return; + } + } + + $sql = file_get_contents(__DIR__ . '/../install.sql'); + $this->schema->getConnection()->unprepared($sql); + } + + + /** + * Reverse the migration + */ + public + function down() + { + $this->schema->getConnection()->statement('SET FOREIGN_KEY_CHECKS=0;'); + + foreach ($this->oldTables as $table) { + if ($this->schema->hasTable($table)) { + $this->schema->dropIfExists($table); + } + } + } +} diff --git a/db/migrations/2018_01_01_000002_import_update_sql.php b/db/migrations/2018_01_01_000002_import_update_sql.php new file mode 100644 index 00000000..58687d6c --- /dev/null +++ b/db/migrations/2018_01_01_000002_import_update_sql.php @@ -0,0 +1,27 @@ +schema->hasTable('UserWorkLog')) { + return; + } + + $sql = file_get_contents(__DIR__ . '/../update.sql'); + $this->schema->getConnection()->unprepared($sql); + } + + /** + * Reverse the migration + */ + public function down() + { + $this->schema->dropIfExists('UserWorkLog'); + } +} diff --git a/includes/autoload.php b/includes/autoload.php index 0cd9d355..f2e90b16 100644 --- a/includes/autoload.php +++ b/includes/autoload.php @@ -2,7 +2,8 @@ // Check for autoloader if (!is_readable(__DIR__ . '/../vendor/autoload.php')) { - die('Please run composer.phar install'); + echo 'Please run composer.phar install'; + exit(1); } // Include composer autoloader -- cgit v1.2.3-54-g00ecf