Skip to content

Commit

Permalink
siparis, irsaliye ve fatura hareket tablolarindaki enum tipinde olan …
Browse files Browse the repository at this point in the history
…status alanini tanimlanabilir hale getiriliyor
  • Loading branch information
mdpinar committed Jul 3, 2015
1 parent 7c79d62 commit db5ef91
Show file tree
Hide file tree
Showing 9 changed files with 576 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
package db.migration;

import java.sql.Connection;
import java.sql.Statement;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import utils.GlobalCons;

import com.googlecode.flyway.core.api.migration.jdbc.JdbcMigration;

/**
* Siparis, Irsaliye ve Fatura hareket tablolarindaki tipindeki Status alani version 1.0.11 ten once enum tipindeydi,
* bu sinif ile tanimlanabilir hale getirilip hareket tablolari icin lookup table yapisina (many-to-one) cevrildi.
*
* status_id integer,
*
* @author mdpinar
*/

public class V1_0_11__Changing_Status_data_type_in_Stock_Trans_tables implements JdbcMigration {

private final static Logger log = LoggerFactory.getLogger(V1_0_11__Changing_Status_data_type_in_Stock_Trans_tables.class);

@Override
public void migrate(Connection con) throws Exception {
String[] tables = {"order", "waybill", "invoice"};

Statement sta = con.createStatement();
for (String table : tables) {
log.info("Altering " + table + "_trans on " + GlobalCons.dbVendor + " to change Status field type is executing...");

if (GlobalCons.dbVendor.equals("h2")) executeScripts_H2(sta, table);
if (GlobalCons.dbVendor.equals("mysql")) executeScripts_MYSQL(sta, table);
if (GlobalCons.dbVendor.equals("postgresql")) executeScripts_POSTGRESQL(sta, table);
if (GlobalCons.dbVendor.equals("sqlserver")) executeScripts_SQLSERVER(sta, table);

executeScripts_ToAddNewStatus(sta, table);
executeScripts_ToChnageStatusColumnType(sta, table);

log.info("" + table + "_trans is altered.");
}
}

private void executeScripts_H2(Statement sta, String table) throws Exception {
StringBuilder sb = new StringBuilder();

sb.append("create table " + table + "_trans_status (");
sb.append(" id int auto_increment not null,");
sb.append(" parent_id int,");
sb.append(" name varchar(30) not null,");
sb.append(" insert_by varchar(20),");
sb.append(" insert_at datetime,");
sb.append(" update_by varchar(20),");
sb.append(" update_at datetime,");
sb.append(" is_active boolean default true,");
sb.append(" version int default 0,");
sb.append(" primary key (id)");
sb.append("); ");
sb.append("create index " + table + "_trans_status_ix1 on invoice_trans_status (workspace, name); ");
sb.append("create sequence " + table + "_trans_status_seq; ");

sb.append("create table " + table + "_trans_status_history (");
sb.append(" id int auto_increment not null,");
sb.append(" trans_time datetime,");
sb.append(" trans_id int not null,");
sb.append(" status_id int not null,");
sb.append(" username varchar(20),");
sb.append(" primary key (id)");
sb.append("); ");
sb.append("create sequence " + table + "_trans_status_history_seq; ");

sb.append("alter table " + table + "_trans_status add foreign key (parent_id) references " + table + "_trans_status (id); ");
sb.append("alter table " + table + "_trans_status_history add foreign key (trans_id) references " + table + "_trans (id); ");
sb.append("alter table " + table + "_trans_status_history add foreign key (status_id) references " + table + "_trans_status (id); ");


sta.executeUpdate("alter table " + table + "_trans add column t_status_id int");

sta.executeUpdate(sb.toString());
}

private void executeScripts_MYSQL(Statement sta, String table) throws Exception {
StringBuilder sb = new StringBuilder();

sb.append("create table " + table + "_trans_status (");
sb.append(" id int auto_increment not null,");
sb.append(" parent_id int,");
sb.append(" name varchar(30) not null,");
sb.append(" insert_by varchar(20),");
sb.append(" insert_at datetime,");
sb.append(" update_by varchar(20),");
sb.append(" update_at datetime,");
sb.append(" is_active tinyint(1) default 1,");
sb.append(" version int default 0,");
sb.append(" primary key (id)");
sb.append(") engine=innodb default charset=utf8; ");
sb.append("create index " + table + "_trans_status_ix1 on invoice_trans_status (workspace, name); ");

sb.append("create table " + table + "_trans_status_history (");
sb.append(" id int auto_increment not null,");
sb.append(" trans_time datetime,");
sb.append(" trans_id int not null,");
sb.append(" status_id int not null,");
sb.append(" username varchar(20),");
sb.append(" primary key (id)");
sb.append(") engine=innodb default charset=utf8; ");

sb.append("alter table " + table + "_trans_status add foreign key (parent_id) references " + table + "_trans_status (id); ");
sb.append("alter table " + table + "_trans_status_history add foreign key (trans_id) references " + table + "_trans (id); ");
sb.append("alter table " + table + "_trans_status_history add foreign key (status_id) references " + table + "_trans_status (id); ");

sta.executeUpdate(sb.toString());
}

private void executeScripts_POSTGRESQL(Statement sta, String table) throws Exception {
StringBuilder sb = new StringBuilder();

sb.append("create table " + table + "_trans_status (");
sb.append(" id serial not null,");
sb.append(" parent_id integer,");
sb.append(" name varchar(30) not null,");
sb.append(" insert_by varchar(20),");
sb.append(" insert_at timestamp,");
sb.append(" update_by varchar(20),");
sb.append(" update_at timestamp,");
sb.append(" is_active boolean default true,");
sb.append(" version integer default 0,");
sb.append(" primary key (id)");
sb.append("); ");
sb.append("create index " + table + "_trans_status_ix1 on invoice_trans_status (workspace, name); ");
sb.append("create sequence " + table + "_trans_status_seq; ");

sb.append("create table " + table + "_trans_status_history (");
sb.append(" id serial not null,");
sb.append(" trans_time timestamp,");
sb.append(" trans_id integer not null,");
sb.append(" status_id integer not null,");
sb.append(" username varchar(20),");
sb.append(" primary key (id)");
sb.append("); ");
sb.append("create sequence " + table + "_trans_status_history_seq; ");

sb.append("alter table " + table + "_trans_status add foreign key (parent_id) references " + table + "_trans_status (id); ");
sb.append("alter table " + table + "_trans_status_history add foreign key (trans_id) references " + table + "_trans (id); ");
sb.append("alter table " + table + "_trans_status_history add foreign key (status_id) references " + table + "_trans_status (id); ");

sta.executeUpdate(sb.toString());
}

private void executeScripts_SQLSERVER(Statement sta, String table) throws Exception {
StringBuilder sb = new StringBuilder();

sb.append("create table " + table + "_trans_status (");
sb.append(" id integer identity(1,1) primary key,");
sb.append(" parent_id integer,");
sb.append(" name varchar(30) not null,");
sb.append(" insert_by varchar(20),");
sb.append(" insert_at datetime,");
sb.append(" update_by varchar(20),");
sb.append(" update_at datetime,");
sb.append(" is_active boolean default true,");
sb.append(" version integer default 0");
sb.append("); ");
sb.append("create index " + table + "_trans_status_ix1 on invoice_trans_status (workspace, name); ");

sb.append("create table " + table + "_trans_status_history (");
sb.append(" id integer identity(1,1) primary key,");
sb.append(" trans_time datetime,");
sb.append(" trans_id integer not null,");
sb.append(" status_id integer not null,");
sb.append(" username varchar(20)");
sb.append("); ");

sb.append("alter table " + table + "_trans_status add foreign key (parent_id) references " + table + "_trans_status (id); ");
sb.append("alter table " + table + "_trans_status_history add foreign key (trans_id) references " + table + "_trans (id); ");
sb.append("alter table " + table + "_trans_status_history add foreign key (status_id) references " + table + "_trans_status (id); ");

sta.executeUpdate(sb.toString());
}

private void executeScripts_ToAddNewStatus(Statement sta, String table) throws Exception {
StringBuilder sb = new StringBuilder();

if (GlobalCons.dbVendor.equals("sqlserver")) {
sb.append("insert into " + table + "_trans_status (name) values ('Beklemede'); ");
sb.append("insert into " + table + "_trans_status (name, parent_id) values ('Kapandı', (select id from " + table + "_trans_status where name = 'Beklemede')); ");
} else {
sb.append("insert into " + table + "_trans_status (id, name) values (1, 'Beklemede'); ");
sb.append("insert into " + table + "_trans_status (id, name, parent_id) values (2, 'Kapandı', 1); ");
}

sta.executeUpdate(sb.toString());
}

private void executeScripts_ToChnageStatusColumnType(Statement sta, String table) throws Exception {
StringBuilder sb = new StringBuilder();

sb.append("alter table " + table + "_trans add column temp_status_id integer; ");
sb.append("alter table " + table + "_trans_detail add column temp_status_id integer; ");

sb.append("update " + table + "_trans set temp_status_id = 1 where status <> 'Kapandı'; ");
sb.append("update " + table + "_trans_detail set temp_status_id = 1 where status <> 'Kapandı'; ");

sb.append("update " + table + "_trans set temp_status_id = 2 where status = 'Kapandı'; ");
sb.append("update " + table + "_trans_detail set temp_status_id = 2 where status = 'Kapandı'; ");

sb.append("alter table " + table + "_trans drop column status; ");
sb.append("alter table " + table + "_trans_detail drop column status; ");

if (GlobalCons.dbVendor.equals("mysql")) {
sb.append("alter table " + table + "_trans change temp_status_id status_id int; ");
sb.append("alter table " + table + "_trans_detail change temp_status_id status_id int; ");
} else if (GlobalCons.dbVendor.equals("sqlserver")) {
sb.append("EXEC sp_rename " + table + "'_trans.temp_status_id', 'status_id', 'COLUMN'; ");
sb.append("EXEC sp_rename " + table + "'_trans_detail.temp_status_id', 'status_id', 'COLUMN'; ");
} else if (GlobalCons.dbVendor.equals("h2")) {
sb.append("alter table " + table + "_trans alter column temp_status_id rename to status_id; ");
sb.append("alter table " + table + "_trans_detail alter column temp_status_id rename to status_id; ");
} else {
sb.append("alter table " + table + "_trans rename column temp_status_id to status_id; ");
sb.append("alter table " + table + "_trans_detail rename column temp_status_id to status_id; ");
}

sb.append("alter table " + table + "_trans add foreign key (status_id) references " + table + "_trans_status (id); ");
sb.append("alter table " + table + "_trans_detail add foreign key (status_id) references " + table + "_trans_status (id); ");

sta.executeUpdate(sb.toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void migrate(Connection con) throws Exception {

if (GlobalCons.dbVendor.equals("mysql")) {
sta.executeUpdate("alter table contact change tc tc_kimlik varchar(11)");
} else if (GlobalCons.dbVendor.equals("mssql")) {
} else if (GlobalCons.dbVendor.equals("sqlserver")) {
sta.executeUpdate("EXEC sp_rename 'contact.tc', 'tc_kimlik_no', 'COLUMN';");
} else if (GlobalCons.dbVendor.equals("h2")) {
sta.executeUpdate("alter table contact alter column tc rename to tc_kimlik");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void migrate(Connection con) throws Exception {

if (GlobalCons.dbVendor.equals("mysql")) {
sta.executeUpdate("alter table admin_user_audit change descr description varchar(255)");
} else if (GlobalCons.dbVendor.equals("mssql")) {
} else if (GlobalCons.dbVendor.equals("sqlserver")) {
sta.executeUpdate("EXEC sp_rename 'admin_user_audit.descr', 'description', 'COLUMN';");
} else if (GlobalCons.dbVendor.equals("h2")) {
sta.executeUpdate("alter table admin_user_audit alter column descr rename to description");
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import play.Project._

name := "seyhan"

version := "1.0.9"
version := "1.0.11"

libraryDependencies ++= Seq(
jdbc,
Expand Down
89 changes: 89 additions & 0 deletions conf/evolutions/h2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,31 @@ create table invoice_trans_source (
create index invoice_trans_source_ix1 on invoice_trans_source (workspace, name);
create sequence invoice_trans_source_seq;

create table invoice_trans_status (
id int auto_increment not null,
parent_id int,
name varchar(30) not null,
insert_by varchar(20),
insert_at datetime,
update_by varchar(20),
update_at datetime,
is_active boolean default true,
version int default 0,
primary key (id)
);
create index invoice_trans_status_ix1 on invoice_trans_status (workspace, name);
create sequence invoice_trans_status_seq;

create table invoice_trans_status_history (
id int auto_increment not null,
trans_time datetime,
trans_id int not null,
status_id int not null,
username varchar(20),
primary key (id)
);
create sequence invoice_trans_status_history_seq;

create table invoice_trans_tax (
id int auto_increment not null,
tax_rate double default 0,
Expand Down Expand Up @@ -1036,6 +1061,32 @@ create table order_trans_source (
create index order_trans_source_ix1 on order_trans_source (workspace, name);
create sequence order_trans_source_seq;

create table order_trans_status (
id int auto_increment not null,
parent_id int,
name varchar(30) not null,
insert_by varchar(20),
insert_at datetime,
update_by varchar(20),
update_at datetime,
is_active boolean default true,
workspace int not null,
version int default 0,
primary key (id)
);
create index order_trans_status_ix1 on order_trans_status (workspace, name);
create sequence order_trans_status_seq;

create table order_trans_status_history (
id int auto_increment not null,
trans_time datetime,
trans_id int not null,
status_id int not null,
username varchar(20),
primary key (id)
);
create sequence order_trans_status_history_seq;

create table safe (
id int auto_increment not null,
name varchar(50) not null,
Expand Down Expand Up @@ -1776,6 +1827,32 @@ create table waybill_trans_source (
create index waybill_trans_source_ix1 on waybill_trans_source (workspace, name);
create sequence waybill_trans_source_seq;

create table waybill_trans_status (
id int auto_increment not null,
parent_id int,
name varchar(30) not null,
insert_by varchar(20),
insert_at datetime,
update_by varchar(20),
update_at datetime,
is_active boolean default true,
workspace int not null,
version int default 0,
primary key (id)
);
create index waybill_trans_status_ix1 on waybill_trans_status (workspace, name);
create sequence waybill_trans_status_seq;

create table waybill_trans_status_history (
id int auto_increment not null,
trans_time datetime,
trans_id int not null,
status_id int not null,
username varchar(20),
primary key (id)
);
create sequence waybill_trans_status_history_seq;

-- Temp tables

create table temp_contact_aging (
Expand Down Expand Up @@ -1876,6 +1953,8 @@ alter table invoice_trans_relation add foreign key (trans_id) references invoice
alter table invoice_trans_tax add foreign key (trans_id) references invoice_trans (id);
alter table invoice_trans_currency add foreign key (trans_id) references invoice_trans (id);

alter table invoice_trans_status add foreign key (parent_id) references invoice_trans_status (id);

alter table order_trans add foreign key (contact_id) references contact (id);
alter table order_trans add foreign key (depot_id) references stock_depot (id);
alter table order_trans add foreign key (trans_source_id) references order_trans_source (id);
Expand All @@ -1894,6 +1973,7 @@ alter table order_trans_detail add foreign key (private_code_id) references glob

alter table order_trans_factor add foreign key (trans_id) references order_trans (id);
alter table order_trans_factor add foreign key (factor_id) references stock_cost_factor (id);
alter table order_trans_status add foreign key (parent_id) references order_trans_status (id);

alter table sale_campaign add foreign key (stock_category_id) references stock_category (id);

Expand Down Expand Up @@ -1964,6 +2044,8 @@ alter table waybill_trans_factor add foreign key (trans_id) references waybill_t
alter table waybill_trans_factor add foreign key (factor_id) references stock_cost_factor (id);
alter table waybill_trans_relation add foreign key (trans_id) references waybill_trans (id);

alter table waybill_trans_status add foreign key (parent_id) references waybill_trans_status (id);

alter table admin_user_right add foreign key (user_role_id) references admin_user_role (id);
alter table admin_user add foreign key (user_group_id) references admin_user_group (id);

Expand Down Expand Up @@ -2120,4 +2202,11 @@ insert into admin_extra_fields (id, idno, distinction, name, is_required, is_act
insert into admin_extra_fields (id, idno, distinction, name, is_required, is_active) values (19, 8, 'stock', 'ek_alan8', false, false);
insert into admin_extra_fields (id, idno, distinction, name, is_required, is_active) values (20, 9, 'stock', 'ek_alan9', false, false);

insert into invoice_trans_status (id, name) values (1, 'Beklemede');
insert into order_trans_status (id, name) values (1, 'Beklemede');
insert into waybill_trans_status (id, name) values (1, 'Beklemede');
insert into invoice_trans_status (id, name, parent_id) values (2, 'Kapandı', 1);
insert into order_trans_status (id, name, parent_id) values (2, 'Kapandı', 1);
insert into waybill_trans_status (id, name, parent_id) values (2, 'Kapandı', 1);

COMMIT;
Loading

0 comments on commit db5ef91

Please sign in to comment.