staempfli/magento2-builder-tool

View on GitHub
build/xmlscripts/database.xml

Summary

Maintainability
Test Coverage
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * database.xml
 *
 * @copyright Copyright (c) 2016 Staempfli AG
 * @author    juan.alonso@staempfli.com
 */
-->
<project name="Database" default="..." basedir=".">

    <target name="database:properties"
            hidden="true">

        <property name="database.user" value="${project.name}"/>
        <property name="database.name" value="${project.name}"/>

        <if>
            <not>
                <isset property="database.password"/>
            </not>
            <then>
                <passwordGenerator outputPropertyName="database.password"/>
            </then>
        </if>
        <if>
            <not>
                <isset property="database.admin.username"/>
            </not>
            <then>
                <input propertyName="database.admin.username"
                       message="Database Admin Username: "/>
            </then>
        </if>
        <if>
            <not>
                <isset property="database.admin.password"/>
            </not>
            <then>
                <echo message="${database.admin.username}'s password:"/>
                <exec command="${phing.dir}/build/shellscripts/inputpassword.sh" outputProperty="database.admin.password"/>
            </then>
        </if>

        <property name="database.connection.command" value="mysql -h${database.host} -u${database.admin.username}"/>
        <if>
            <not>
                <equals arg1="${database.admin.password}" arg2=""/>
            </not>
            <then>
                <property name="database.connection.command" value="${database.connection.command} -p${database.admin.password}" override="true"/>
            </then>
        </if>
    </target>

    <target name="database:user:create"
            hidden="true"
            unless="skipDbUserCreation"
            logskipped="true">

        <exec command="${database.connection.command} -e '
        DROP USER IF EXISTS `${database.user}`@`${database.host}`;
        CREATE USER IF NOT EXISTS `${database.user}`@`${database.host}` IDENTIFIED BY &quot;${database.password}&quot;;
        GRANT ALL ON *.* TO `${database.user}`@`${database.host}`;'"
              checkreturn="true"
              logoutput="true"/>

    </target>

    <target name="database:create"
            hidden="true">

        <property name="command.create.database"
                  value="${bin.n98-magerun2} db:create --connection=${database.connection.type}"
                  override="true"/>
        <echo message="${command.create.database}"/>
        <exec command="${command.create.database}" checkreturn="true" logoutput="true"/>

    </target>

    <target name="database:drop"
            hidden="true">

        <property name="command.drop.database"
                  value="${bin.n98-magerun2} db:drop -f --connection=${database.connection.type}"
                  override="true"/>
        <echo message="${command.drop.database}"/>
        <exec command="${command.drop.database}" checkreturn="false" logoutput="true"/>

    </target>

    <target name="database:import"
            depends="database:drop,
            database:create"
            hidden="true">

        <if>
            <not>
                <and>
                    <isset property="dump.local.absolutePath"/>
                    <istrue value="${dump.local.absolutePath}"/>
                </and>
            </not>
            <then>
                <input message="DB dump filename" propertyName="dump.local.absolutePath"/>
            </then>
        </if>

        <exec command="echo ${dump.local.absolutePath} | awk -F . '{print $NF}'" outputProperty="dump.filename.extension"/>
        <if>
            <equals arg1="${dump.filename.extension}" arg2="gz"/>
            <then>
                <property name="compression.param" value='--compression="gzip"' override="true"/>
            </then>
            <else>
                <property name="compression.param" value="" override="true"/>
            </else>
        </if>

        <property name="command.import.dump"
                  value="${bin.n98-magerun2} db:import --connection=${database.connection.type} ${compression.param} ${dump.local.absolutePath}"
                  override="true"/>
        <echo message="${command.import.dump}"/>
        <exec command="${command.import.dump}"
              checkreturn="true"
              logoutput="true"/>

        <if>
            <equals arg1="${database.connection.type}" arg2="default"/>
            <then>
                <phingcall target="magento:setup:upgrade"/>
                <phingcall target="magento:config:set"/>
                <phingcall target="magento:cache:clean"/>
            </then>
        </if>

    </target>

</project>