From bd01f54a560707f7afc5628a3f4932c1ce647a9a Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sun, 22 Feb 2015 21:04:29 +0100 Subject: [PATCH] Using liquibase for DDL and DML statements. --- MyBatis/MyBatis-Spring/README.txt | 28 ++- MyBatis/MyBatis-Spring/createdatabase.sh | 9 - MyBatis/MyBatis-Spring/pom.xml | 6 + .../src/main/resources/liquibase/ddlChangelog.xml | 63 ++++++ .../src/main/resources/liquibase/dmlChangelog.xml | 230 +++++++++++++++++++++ .../resources/liquibase/liquibaseChangeLogs.xml | 18 ++ .../src/main/resources/spring-config.xml | 5 + 7 files changed, 349 insertions(+), 10 deletions(-) delete mode 100755 MyBatis/MyBatis-Spring/createdatabase.sh create mode 100644 MyBatis/MyBatis-Spring/src/main/resources/liquibase/ddlChangelog.xml create mode 100644 MyBatis/MyBatis-Spring/src/main/resources/liquibase/dmlChangelog.xml create mode 100644 MyBatis/MyBatis-Spring/src/main/resources/liquibase/liquibaseChangeLogs.xml diff --git a/MyBatis/MyBatis-Spring/README.txt b/MyBatis/MyBatis-Spring/README.txt index 3511f09..2272363 100644 --- a/MyBatis/MyBatis-Spring/README.txt +++ b/MyBatis/MyBatis-Spring/README.txt @@ -1,4 +1,3 @@ -Create database with createdatabase.sh script. export M2_HOME=/opt/maven/apache-maven-2.2.1 PATH=$M2_HOME/bin:$PATH @@ -6,3 +5,30 @@ PATH=$M2_HOME/bin:$PATH mvn clean install -Dmaven.test.skip=true mvn dependency:sources mvn dependency:resolve -Dclassifier=javadoc + + +# I am using the mybatis-generator maven plugin. So, I need always a database (it would be better to use the Eclipse plugin) +mysql -uroot -proot -e "CREATE DATABASE mybatis_example DEFAULT CHARACTER SET utf8" + +mysql -uroot -proot -e "USE mybatis_example; CREATE TABLE ad (id SERIAL, company_id BIGINT, company_categ_id BIGINT, ad_gps BLOB, ad_mobile_image varchar(255), created_at TIMESTAMP NOT NULL, updated_at TIMESTAMP NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB, DEFAULT CHARSET=utf8, COLLATE=utf8_unicode_ci" + +mysql -uroot -proot -e "USE mybatis_example; CREATE TABLE ad_description (id SERIAL, laguage_id BIGINT NOT NULL, ad_id BIGINT(20) UNSIGNED NOT NULL, ad_name VARCHAR(255) NOT NULL, ad_description LONGTEXT, ad_mobile_text VARCHAR(500) NOT NULL, ad_link VARCHAR(3000) NOT NULL, PRIMARY KEY (id), INDEX(ad_id), FOREIGN KEY (ad_id) REFERENCES ad (id) ON DELETE CASCADE) ENGINE=InnoDB, DEFAULT CHARSET=utf8, COLLATE=utf8_unicode_ci" + +# Run mvn clean install (mybatis-generator plugin creates autogenerated code from tables) +mvn clean install + +# Drop database (we want to check liquibase is working with Spring) +mysql -uroot -proot -A -e "drop database mybatis_example" + +# Liquibase requires (AFAIK) of an existing schema +mysql -uroot -proot -e "CREATE DATABASE mybatis_example DEFAULT CHARACTER SET utf8" + +# Run TestMain.java :) + + +# Useful commands for liquibase that I used in order to create the changelog files from an existing schema. +# Changelog from DDL. Creates DDL changelog from current database (if schema was previously created without liquibase as above) +/opt/liquibase/liquibase --driver=com.mysql.jdbc.Driver --classpath=$HOME/.m2/repository/mysql/mysql-connector-java/5.1.9/mysql-connector-java-5.1.9.jar --logLevel=debug --changeLogFile=src/main/resources/liquibase/ddlChangelog.xml --url="jdbc:mysql://localhost/mybatis_example" --username=root --password=root generateChangeLog + +# Changelog for DML. Creates DML changelog from current database (if there are data) +/opt/liquibase/liquibase --driver=com.mysql.jdbc.Driver --classpath=$HOME/.m2/repository/mysql/mysql-connector-java/5.1.9/mysql-connector-java-5.1.9.jar --logLevel=debug --changeLogFile=src/main/resources/liquibase/dmlChangelog.xml --url="jdbc:mysql://localhost/mybatis_example" --username=root --password=root --diffTypes="data" generateChangeLog diff --git a/MyBatis/MyBatis-Spring/createdatabase.sh b/MyBatis/MyBatis-Spring/createdatabase.sh deleted file mode 100755 index ef2bb57..0000000 --- a/MyBatis/MyBatis-Spring/createdatabase.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash -mysql -uroot -proot -e "CREATE DATABASE mybatis_example DEFAULT CHARACTER SET utf8" - -mysql -uroot -proot -e "USE mybatis_example; CREATE TABLE ad (id SERIAL, company_id BIGINT, company_categ_id BIGINT, ad_gps BLOB, ad_mobile_image varchar(255), created_at TIMESTAMP NOT NULL, updated_at TIMESTAMP NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB, DEFAULT CHARSET=utf8, COLLATE=utf8_unicode_ci" - -# Por alguna extraña razon no pude poner el id de esta tabla como serial y no tiene ahora este id autoincrement -# Daba un error raro diciendo que no podia haber 2 claves con autoincrement en la misma tabla. Pero sí se puede... -# debo estar haciendo algo mal... :( -mysql -uroot -proot -e "USE mybatis_example; CREATE TABLE ad_description (id BIGINT(20) UNSIGNED NOT NULL, laguage_id BIGINT NOT NULL, ad_id SERIAL NOT NULL, ad_name VARCHAR(255) NOT NULL, ad_description LONGTEXT, ad_mobile_text VARCHAR(500) NOT NULL, ad_link VARCHAR(3000) NOT NULL, PRIMARY KEY (id), INDEX(ad_id), FOREIGN KEY (ad_id) REFERENCES ad (id) ON DELETE CASCADE) ENGINE=InnoDB, DEFAULT CHARSET=utf8, COLLATE=utf8_unicode_ci" diff --git a/MyBatis/MyBatis-Spring/pom.xml b/MyBatis/MyBatis-Spring/pom.xml index a4c386b..0471644 100644 --- a/MyBatis/MyBatis-Spring/pom.xml +++ b/MyBatis/MyBatis-Spring/pom.xml @@ -10,6 +10,7 @@ ${project.build.directory}/generated-sources/mybatis-generator/ UTF-8 + 3.3.0 @@ -57,6 +58,11 @@ cglib 2.2.2 + + org.liquibase + liquibase-core + ${liquibase.version} + diff --git a/MyBatis/MyBatis-Spring/src/main/resources/liquibase/ddlChangelog.xml b/MyBatis/MyBatis-Spring/src/main/resources/liquibase/ddlChangelog.xml new file mode 100644 index 0000000..8aef71d --- /dev/null +++ b/MyBatis/MyBatis-Spring/src/main/resources/liquibase/ddlChangelog.xml @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/MyBatis/MyBatis-Spring/src/main/resources/liquibase/dmlChangelog.xml b/MyBatis/MyBatis-Spring/src/main/resources/liquibase/dmlChangelog.xml new file mode 100644 index 0000000..13a4e12 --- /dev/null +++ b/MyBatis/MyBatis-Spring/src/main/resources/liquibase/dmlChangelog.xml @@ -0,0 +1,230 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/MyBatis/MyBatis-Spring/src/main/resources/liquibase/liquibaseChangeLogs.xml b/MyBatis/MyBatis-Spring/src/main/resources/liquibase/liquibaseChangeLogs.xml new file mode 100644 index 0000000..740f261 --- /dev/null +++ b/MyBatis/MyBatis-Spring/src/main/resources/liquibase/liquibaseChangeLogs.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + diff --git a/MyBatis/MyBatis-Spring/src/main/resources/spring-config.xml b/MyBatis/MyBatis-Spring/src/main/resources/spring-config.xml index 0fa5d05..27a2ea2 100644 --- a/MyBatis/MyBatis-Spring/src/main/resources/spring-config.xml +++ b/MyBatis/MyBatis-Spring/src/main/resources/spring-config.xml @@ -110,5 +110,10 @@ + + + + + -- 2.1.4