database/build.xml
author Juan Pablo Aroztegi <juanpablo.aroztegi@openbravo.com>
Tue, 29 Jan 2008 11:08:22 +0000
changeset 378 5c93458e2916
parent 360 fa3488152476
child 436 c1318c4753b3
permissions -rw-r--r--
[1881705] Force file eol to LF (UNIX)
<?xml version="1.0" encoding="UTF-8" ?>
<!--
 *************************************************************************
 * The contents of this file are subject to the Openbravo  Public  License
 * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
 * Version 1.1  with a permitted attribution clause; you may not  use this
 * file except in compliance with the License. You  may  obtain  a copy of
 * the License at http://www.openbravo.com/legal/license.html 
 * Software distributed under the License  is  distributed  on  an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific  language  governing  rights  and  limitations
 * under the License. 
 * The Original Code is Openbravo ERP. 
 * The Initial Developer of the Original Code is Openbravo SL 
 * All portions are Copyright (C) 2001-2006 Openbravo SL 
 * All Rights Reserved. 
 * Contributor(s):  ______________________________________.
 ************************************************************************
-->
<!--
List of targets:

create.database: creates the database from scratch.
update.database: updates structure and data in database.
clean.database.ORACLE: Drop database from Oracle.
clean.database.POSTGRE: Drop database from PostgreSQL.
ORACLE.structure: Creates the database in Oracle.
POSTGRE.structure: Creates the database in PostgreSql.
prepare.database: Call to the specific rdbms structure.
create.database.structure: Create the database structure from xml files.
update.database.structure: Updates the database structure from xml files.
create.database.script: Creates the database script in the script directory.
update.database.script: Updates the database script in the script directory.
export.database.structure: Exports the database structure in the xml's files.
import.database.data: Imports the data into the database.
export.database.data: Exports the data from database to xml's files.
import.database.sampledata: Imports the database sample data from xml's files.
export.database.sampledata: Exports the database sample data to xml's files.
save.database.model: Saves the model in the database.
database.postcreate.ORACLE: Executes post creation operation in Oracle database.
database.postcreate.POSTGRE: Executes post creation operation in PostgreSQL database.
compare.database.structure: Generates a diff script.
-->
<project name="openbravo" default="create.database" basedir=".">
  <property environment="env" />
  <property name="database.file" value="${base.source}/database" />
  <condition property="bbdd.owner.url" value="${bbdd.url}/${bbdd.sid}" else="${bbdd.url}">
    <and>
      <equals arg1="${bbdd.rdbms}" arg2="POSTGRE"/>
    </and>
  </condition>

  <path id="runtime-classpath">
    <fileset dir="./lib">
      <include name="**/*.jar" />
    </fileset>
  </path>

  <taskdef name="createdatabase"
           classname="org.openbravo.ddlutils.task.CreateDatabase">
    <classpath refid="runtime-classpath" />
  </taskdef>
  <taskdef name="createscript"
           classname="org.openbravo.ddlutils.task.CreateXML2SQL">
    <classpath refid="runtime-classpath" />
  </taskdef>
  <taskdef name="alterdatabase"
           classname="org.openbravo.ddlutils.task.AlterDatabase">
    <classpath refid="runtime-classpath" />
  </taskdef>
  <taskdef name="alterscript"
           classname="org.openbravo.ddlutils.task.AlterXML2SQL">
    <classpath refid="runtime-classpath" />
  </taskdef>
  <taskdef name="exportdatabase"
           classname="org.openbravo.ddlutils.task.ExportDatabase">
    <classpath refid="runtime-classpath" />
  </taskdef>
  <taskdef name="importdata"
           classname="org.openbravo.ddlutils.task.ImportDataXML">
    <classpath refid="runtime-classpath" />
  </taskdef>
  <taskdef name="exportdata"
           classname="org.openbravo.ddlutils.task.ExportDataXML">
    <classpath refid="runtime-classpath" />
  </taskdef>
  <taskdef name="savemodel"
           classname="org.openbravo.ddlutils.task.SaveDatabase">
    <classpath refid="runtime-classpath" />
  </taskdef>
  <taskdef name="comparedatabase"
           classname="org.openbravo.ddlutils.task.CompareDatabase">
    <classpath refid="runtime-classpath" />
  </taskdef>

  <target name="create.database">
    <antcall target="clean.database.${bbdd.rdbms}"/>
    <chmod dir="${base.source}/database" perm="755" type="both" />
    <antcall target="prepare.database"/>
    <antcall target="create.database.structure"/>
    <antcall target="import.database.data" />
    <antcall target="import.database.sampledata" />
  </target>

    <target name="update.database">
    <antcall target="update.database.structure"/>
        <antcall target="import.database.data" />      
    </target>

  <target name="clean.database.ORACLE">
    <sql driver="${bbdd.driver}"
         url="${bbdd.url}"
         userid="${bbdd.systemUser}"
         password="${bbdd.systemPassword}"
         onerror="continue">
      <classpath>
        <fileset dir="${base.lib}">
          <include name="**/*.jar">
          </include>
        </fileset>
      </classpath>
      <transaction>
        DROP USER ${bbdd.user} CASCADE;
      </transaction>
    </sql>
  </target>

  <target name="clean.database.POSTGRE">
    <sql driver="${bbdd.driver}"
         url="${bbdd.url}"
         userid="${bbdd.systemUser}"
         password="${bbdd.systemPassword}"
         onerror="continue"
         autocommit="true">
      <classpath>
        <fileset dir="${base.lib}">
          <include name="**/*.jar">
          </include>
        </fileset>
      </classpath>
      <transaction>
        DROP DATABASE ${bbdd.sid};
      </transaction>
    </sql>
    <sql driver="${bbdd.driver}"
         url="${bbdd.url}"
         userid="${bbdd.systemUser}"
         password="${bbdd.systemPassword}"
         onerror="continue"
         autocommit="true">
      <classpath>
        <fileset dir="${base.lib}">
          <include name="**/*.jar">
          </include>
        </fileset>
      </classpath>
      <transaction>
        DROP ROLE ${bbdd.user};
      </transaction>
    </sql>
  </target>

  <target name="ORACLE.structure">
    <sql driver="${bbdd.driver}"
         url="${bbdd.url}"
         userid="${bbdd.systemUser}"
         password="${bbdd.systemPassword}"
         delimiter="/">
      <classpath>
        <fileset dir="${base.lib}">
          <include name="**/*.jar">
          </include>
        </fileset>
      </classpath>
      <transaction>
        --CHEKING IF USER EXISTS
        DECLARE
          v_count NUMBER;
        BEGIN
          SELECT COUNT(*) INTO v_count FROM DBA_USERS WHERE USERNAME = '${bbdd.user}';
          IF (v_count > 0) THEN
            RAISE_APPLICATION_ERROR(-20100, '${bbdd.user} ALREADY EXISTS. SELECT ANOTHER USER OR DROP IT FIRST');
          END IF;
        END;
        /
      </transaction>

    </sql>
    <sql driver="${bbdd.driver}"
         url="${bbdd.url}"
         userid="${bbdd.systemUser}"
         password="${bbdd.systemPassword}"
         onerror="continue">
      <classpath>
        <fileset dir="${base.lib}">
          <include name="**/*.jar">
          </include>
        </fileset>
      </classpath>
      <transaction>
        --UPDATING OPEN CURSORS NUMBER
        ALTER SYSTEM SET OPEN_CURSORS = 10000 SCOPE=BOTH;
      </transaction>
    </sql>
    <sql driver="${bbdd.driver}"
         url="${bbdd.url}"
         userid="${bbdd.systemUser}"
         password="${bbdd.systemPassword}"
         onerror="continue">
      <classpath>
        <fileset dir="${base.lib}">
          <include name="**/*.jar">
          </include>
        </fileset>
      </classpath>
      <transaction>
        --CREATING USER
        CREATE USER ${bbdd.user} IDENTIFIED BY ${bbdd.password}
        DEFAULT TABLESPACE USERS
        TEMPORARY TABLESPACE TEMP;

        --GRANTING USER
        GRANT DBA TO ${bbdd.user};
      </transaction>
    </sql>
  </target>

  <target name="POSTGRE.structure">
    <sql driver="${bbdd.driver}"
         url="${bbdd.url}"
         userid="${bbdd.systemUser}"
         password="${bbdd.systemPassword}"
         onerror="continue"
         autocommit="true">
      <classpath>
        <fileset dir="${base.lib}">
          <include name="**/*.jar">
          </include>
        </fileset>
      </classpath>
      <transaction>
        --CREATING USER
        CREATE ROLE ${bbdd.user} LOGIN PASSWORD '${bbdd.password}'
               SUPERUSER CREATEDB CREATEROLE
               VALID UNTIL 'infinity';
        
        UPDATE pg_authid SET rolcatupdate=true WHERE rolname='${bbdd.user}';
      </transaction>
    </sql>
    <sql driver="${bbdd.driver}"
         url="${bbdd.url}"
         userid="${bbdd.systemUser}"
         password="${bbdd.systemPassword}"
         onerror="continue"
         autocommit="true">
      <classpath>
        <fileset dir="${base.lib}">
          <include name="**/*.jar">
          </include>
        </fileset>
      </classpath>
      <transaction>
        CREATE DATABASE ${bbdd.sid}
                  WITH ENCODING='UTF8'
                  OWNER=${bbdd.user};
      </transaction>
    </sql>
  </target>

    <target name="prepare.database">
        <antcall target="${bbdd.rdbms}.structure" />
    </target>   

    <target name="create.database.structure">
        <createdatabase driver="${bbdd.driver}"
                        url="${bbdd.owner.url}"
                        user="${bbdd.user}"
                        password="${bbdd.password}"
                        model="model"
                        object="${bbdd.object}"
                        dropfirst="false"
                        failonerror="false"
                        verbosity="${bbdd.verbosity}" />
        <antcall target="database.poststructure.${bbdd.rdbms}" />
    </target>

    <target name="update.database.structure">
        <alterdatabase driver="${bbdd.driver}"
                       url="${bbdd.owner.url}"
                       user="${bbdd.user}"
                       password="${bbdd.password}"
                       excludeobjects="com.openbravo.db.OpenbravoExcludeFilter"
                       model="model"
                       object="${bbdd.object}"
                       failonerror="false"
                       verbosity="${bbdd.verbosity}" />
        <antcall target="database.poststructure.${bbdd.rdbms}" />
    </target>   

    <target name="create.database.script">
    <createscript driver="${bbdd.driver}"
                      url="${bbdd.owner.url}"
                      user="${bbdd.user}"
                      password="${bbdd.password}"
                      model="model"
                      object="${bbdd.object}"
                      output="${bbdd.outputscript}"
                      dropfirst="false"
                      verbosity="${bbdd.verbosity}" />
  </target>

    <target name="update.database.script">
    <alterscript driver="${bbdd.driver}"
                      url="${bbdd.owner.url}"
                      user="${bbdd.user}"
                      password="${bbdd.password}"
                      excludeobjects="com.openbravo.db.OpenbravoExcludeFilter"
                      model="model"
                      object="${bbdd.object}"
                      output="${bbdd.outputscript}"
                      verbosity="${bbdd.verbosity}" />
  </target>

    <target name="export.database.structure">
        <exportdatabase driver="${bbdd.driver}"
                        url="${bbdd.owner.url}"
                        user="${bbdd.user}"
                        password="${bbdd.password}"
                        excludeobjects="com.openbravo.db.OpenbravoExcludeFilter"
                        model="model"
                        verbosity="${bbdd.verbosity}" />
    </target>

  <target name="import.database.data">
      <antcall target="database.preimport.${bbdd.rdbms}" />
      <importdata driver="${bbdd.driver}"
                  url="${bbdd.owner.url}"
                  user="${bbdd.user}"
                  filter="com.openbravo.db.OpenbravoMetadataFilter"   
                  password="${bbdd.password}"
                  excludeobjects="com.openbravo.db.OpenbravoExcludeFilter"
                  input="sourcedata"
                  verbosity="${bbdd.verbosity}" />
      <antcall target="database.postimport.${bbdd.rdbms}" />
      <antcall target="database.postcreate.${bbdd.rdbms}" />
  </target>

  <target name="export.database.data">
      <exportdata driver="${bbdd.driver}"
                  url="${bbdd.owner.url}"
                  user="${bbdd.user}"
                  password="${bbdd.password}"
                  excludeobjects="com.openbravo.db.OpenbravoExcludeFilter"
                  filter="com.openbravo.db.OpenbravoMetadataFilter"   
                  output="sourcedata"
                  verbosity="${bbdd.verbosity}" />
  </target> 

  <target name="import.database.sampledata">
      <antcall target="database.preimport.${bbdd.rdbms}" />
      <importdata driver="${bbdd.driver}"
                  url="${bbdd.owner.url}"
                  user="${bbdd.user}"
                  password="${bbdd.password}"
                  excludeobjects="com.openbravo.db.OpenbravoExcludeFilter"
                  input="sampledata"
                  verbosity="${bbdd.verbosity}" />
      <antcall target="database.postimport.${bbdd.rdbms}" />
      <antcall target="database.postcreate.${bbdd.rdbms}" />
  </target>

  <target name="export.database.sampledata">
      <exportdata driver="${bbdd.driver}"
                  url="${bbdd.owner.url}"
                  user="${bbdd.user}"
                  password="${bbdd.password}"
                  excludeobjects="com.openbravo.db.OpenbravoExcludeFilter"
                  filter="com.openbravo.db.OpenbravoDataFilter"   
                  output="sampledata"
                  verbosity="${bbdd.verbosity}" />
  </target> 

  <target name="save.database.model">
    <savemodel driver="${bbdd.driver}"
                      url="${bbdd.owner.url}"
                      user="${bbdd.user}"
                      password="${bbdd.password}"
                  model="model"
                  verbosity="${bbdd.verbosity}" />
  </target>

  <target name="database.postcreate.ORACLE">
    <sql driver="${bbdd.driver}"
         url="${bbdd.owner.url}"
         userid="${bbdd.user}"
         password="${bbdd.password}"
         onerror="continue"
         autocommit="true">
        <classpath>
            <fileset dir="${base.lib}">
                <include name="**/*.jar">
                </include>
            </fileset>
        </classpath>
        <transaction>
          DELETE FROM AD_SYSTEM;
          INSERT INTO AD_SYSTEM (AD_SYSTEM_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY, NAME, SUPPORTUNITS, TAD_RECORDRANGE, TAD_RECORDRANGE_INFO, TAD_LEVEL, TAD_PARENT_VERSION, TAD_TRANSACTIONALRANGE, TAD_THEME)
          VALUES (0, 0, 0, 'Y', NOW(), 0, NOW(), 0, '?', 0, 20, 100, ${development.environment.id}, 'dev', 1, 'Default');
          CALL AD_UPDATE_SEQUENCE();
          CALL AD_UPDATE_ACCESS();
        </transaction>
    </sql>
  </target>

  <target name="database.postcreate.POSTGRE">
    <sql driver="${bbdd.driver}"
         url="${bbdd.owner.url}"
         userid="${bbdd.user}"
         password="${bbdd.password}"
         onerror="continue"
         autocommit="true">
        <classpath>
            <fileset dir="${base.lib}">
                <include name="**/*.jar">
                </include>
            </fileset>
        </classpath>
        <transaction>
          DELETE FROM AD_SYSTEM;
          INSERT INTO AD_SYSTEM (AD_SYSTEM_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY, NAME, SUPPORTUNITS, TAD_RECORDRANGE, TAD_RECORDRANGE_INFO, TAD_LEVEL, TAD_PARENT_VERSION, TAD_TRANSACTIONALRANGE, TAD_THEME)
          VALUES (0, 0, 0, 'Y', NOW(), 0, NOW(), 0, '?', 0, 20, 100, ${development.environment.id}, 'dev', 1, 'Default');
          SELECT AD_UPDATE_SEQUENCE();
          SELECT AD_UPDATE_ACCESS();
        </transaction>
    </sql>
  </target>

    <target name="compare.database.structure">
        <comparedatabase driver="${bbdd.driver}"
                        url="${bbdd.owner.url}"
                        user="${bbdd.user}"
                        password="${bbdd.password}"
                        model="model"
                        verbosity="${bbdd.verbosity}" />
    </target>

  <target name="database.preimport.ORACLE">
  </target>
  <target name="database.postimport.ORACLE">
  </target>
  <target name="database.preimport.POSTGRE">
    <sql driver="${bbdd.driver}"
         url="${bbdd.owner.url}"
         userid="${bbdd.user}"
         password="${bbdd.password}"
         onerror="continue"
         autocommit="true">
        <classpath>
            <fileset dir="${base.lib}">
                <include name="**/*.jar">
                </include>
            </fileset>
        </classpath>
        <transaction>
            UPDATE pg_trigger SET tgenabled = FALSE where tgisconstraint='f' or tgisconstraint='t';
        </transaction>
    </sql>
  </target>
  <target name="database.postimport.POSTGRE">
    <sql driver="${bbdd.driver}"
         url="${bbdd.owner.url}"
         userid="${bbdd.user}"
         password="${bbdd.password}"
         onerror="continue"
         autocommit="true">
        <classpath>
            <fileset dir="${base.lib}">
                <include name="**/*.jar">
                </include>
            </fileset>
        </classpath>
        <transaction>
            UPDATE pg_trigger SET tgenabled = TRUE where tgisconstraint='f' or tgisconstraint='t';
        </transaction>
    </sql>
  </target>
  <target name="database.poststructure.ORACLE">
    <sql driver="${bbdd.driver}"
         url="${bbdd.owner.url}"
         userid="${bbdd.user}"
         password="${bbdd.password}"
         onerror="continue"
         autocommit="true">
        <classpath>
            <fileset dir="${base.lib}">
                <include name="**/*.jar">
                </include>
            </fileset>
        </classpath>
        <transaction>
            PURGE RECYCLEBIN;
        </transaction>
    </sql>
  </target>
  <target name="database.poststructure.POSTGRE">
  </target>

</project>