version: "3"

tasks:

  pull:
    desc: Pull remote Gitea DB into local DB
    aliases: [gp]
    silent: true
    cmds:
      - task db:reset
      - |
        ssh {{.REMOTE_USER}}@{{.REMOTE_HOST}} \
          "docker exec {{.GITEA_SERVICE}} pg_dump -U {{.GITEA_USER}} {{.GITEA_DB}}" | docker compose -p {{.PROJECT}} exec -T {{.DB_SERVICE_NAME}} sh -c "PGPASSWORD='{{.DB_PASSWORD}}' psql -U {{.DB_USER}} -d {{.DB_NAME}}"


  dump:
    desc: Dump remote Gitea DB to file
    aliases: [dump]
    silent: true
    vars:
      TEMP_DB_NAME: "temp_db_copy"
      SERVER: https://file.ma-al.pl
      TARGET_DIR: /maal_internal/nalu_b2b/dumps
      TMP_DIR: /tmp
      REMOTE_DB_USER: root
      REMOTE_DB_PASS: Maal12345678
      TOKEN: 
        sh: curl -s -X GET {{.SERVER}}/api/v2/user/token -u "{{.FILE_MAAL_PL_USER}}:{{.FILE_MAAL_PL_PASSWORD}}" | jq -r .access_token
    cmds:
      - >
        ssh {{.REMOTE_USER}}@{{.REMOTE_HOST}} "docker exec \$(docker ps -q -f name={{.REMOTE_DB_SERVICE}}) mariadb -u {{.REMOTE_DB_USER}} --password={{.REMOTE_DB_PASS}} -e 'CREATE DATABASE IF NOT EXISTS {{.TEMP_DB_NAME}}'"
      - >
        ssh {{.REMOTE_USER}}@{{.REMOTE_HOST}}
        "docker exec \$(docker ps -q -f name={{.REMOTE_DB_SERVICE}}) bash -c \"mariadb-dump --routines --events --triggers -u {{.REMOTE_DB_USER}} --password={{.REMOTE_DB_PASS}} {{.REMOTE_DB_NAME}} | sed 's/DEFINER[ ]*=[ ]*[^ ]*//' | mariadb -u {{.REMOTE_DB_USER}} --password={{.REMOTE_DB_PASS}} {{.TEMP_DB_NAME}}\""
      - >
        ssh {{.REMOTE_USER}}@{{.REMOTE_HOST}}
        "docker exec \$(docker ps -q -f name={{.REMOTE_DB_SERVICE}}) mariadb -u {{.REMOTE_DB_USER}} --password={{.REMOTE_DB_PASS}} {{.TEMP_DB_NAME}} -e \"
            UPDATE IGNORE ps_customer SET email = REGEXP_REPLACE(email, '@.*$', '@ma-al.com'); 
            UPDATE ps_configuration SET value = '${HOST_TO_SERVE}' WHERE name = 'PS_SHOP_DOMAIN';
            UPDATE ps_configuration SET value = '${HOST_TO_SERVE}' WHERE name = 'PS_SHOP_DOMAIN_SSL';
            UPDATE ps_configuration SET value = '${USE_SSL:-1}' WHERE name = 'PS_SSL_ENABLED';
            UPDATE ps_configuration SET value = '1' WHERE name = 'PS_SSL_ENABLED_EVERYWHERE';
            UPDATE ps_configuration SET value = '2' WHERE name = 'PS_MAIL_METHOD';
            UPDATE ps_configuration SET value = 'admin@ma-al.com' WHERE name = 'PS_SHOP_EMAIL';
            UPDATE ps_configuration SET value = 'maildev' WHERE name = 'PS_MAIL_SERVER';
            UPDATE ps_configuration SET value = '' WHERE name = 'PS_MAIL_USER';
            UPDATE ps_configuration SET value = '' WHERE name = 'PS_MAIL_PASSWD';
            UPDATE ps_configuration SET value = 'off' WHERE name = 'PS_MAIL_SMTP_ENCRYPTION';
            UPDATE ps_configuration SET value = '1025' WHERE name = 'PS_MAIL_SMTP_PORT';
            UPDATE ps_configuration SET value = 'admin@ma-al.com' WHERE name = 'MA_MERCHANT_MAILS';
            UPDATE ps_shop_url SET domain = '${HOST_TO_SERVE}', domain_ssl = '${HOST_TO_SERVE}' WHERE id_shop_url = '1';
            UPDATE ps_configuration SET value='0' WHERE (ps_configuration.name = 'PS_SMARTY_CACHE');
            UPDATE ps_configuration SET value='0' WHERE (ps_configuration.name = 'PS_CSS_THEME_CACHE');
            UPDATE ps_configuration SET value='0' WHERE (ps_configuration.name = 'PS_JS_THEME_CACHE');
        \""
      - >
        ssh {{.REMOTE_USER}}@{{.REMOTE_HOST}}
        "docker exec \$(docker ps -q -f name={{.REMOTE_DB_SERVICE}}) mariadb-dump --routines --events --triggers -u {{.REMOTE_DB_USER}} --password={{.REMOTE_DB_PASS}} {{.TEMP_DB_NAME}}" > {{.TMP_DIR}}/{{.DUMP_FILE_NAME}}
      - sed -i 's/DEFINER[ ]*=[ ]*[^ ]*//' {{.TMP_DIR}}/{{.DUMP_FILE_NAME}}
      - > 
        curl -X POST "{{.SERVER}}/api/v2/user/files/upload?path={{.TARGET_DIR}}/{{.DUMP_FILE_NAME}}&mkdir_parents=true" -H "Authorization: Bearer {{.TOKEN}}" -H "Content-Type: application/x-tar" --data-binary "@{{.TMP_DIR}}/{{.DUMP_FILE_NAME}}" 
      - rm -rf {{.TMP_DIR}}/{{.DUMP_FILE_NAME}}
      - > 
        ssh {{.REMOTE_USER}}@{{.REMOTE_HOST}} "docker exec \$(docker ps -q -f name={{.REMOTE_DB_SERVICE}}) mariadb -u {{.REMOTE_DB_USER}} --password={{.REMOTE_DB_PASS}} -e 'DROP DATABASE IF EXISTS {{.TEMP_DB_NAME}}'"    

