SET autocommit=0;

USE ccs2;

START TRANSACTION; 

SET NAMES latin1;
SET character_set_results = NULL;
SET sql_mode='STRICT_TRANS_TABLES';

/* drop statdata and statdesc tables */
drop table statdata;
drop table statdesc;

/* drop all existing foreign key constraints and indexes */
alter table rawdata drop foreign key FK3AC853D2F9EF45DB;
drop index FK3AC853D2F9EF45DB on rawdata;
drop index ts on rawdata;
drop index name on datadesc;
drop index name_2 on datadesc;
alter table datametadata drop foreign key FK306F20D9C36A5603;
drop index FK306F20D9C36A5603 on datametadata;
alter table plotdata drop foreign key FK87A0240BF9EF45DB;
drop index ts_plot on plotdata;
drop index FK87A0240BF9EF45DB on plotdata;

/* Rename / Remove columns */
ALTER TABLE rawdata CHANGE descr_id dataDescId bigint(20) default NULL;
ALTER TABLE rawdata CHANGE tstampmills time bigint(20) NOT NULL;

ALTER TABLE datadesc CHANGE srcSubsystem agentName varchar(255) NOT NULL;
ALTER TABLE datadesc CHANGE srcName dataName varchar(255) not null;
ALTER TABLE datadesc DROP COLUMN maxSamplingMillis;
ALTER TABLE datadesc DROP COLUMN preservationDelay;
ALTER TABLE datadesc DROP COLUMN name;

ALTER TABLE datametadata CHANGE rawDescr_id dataDescId bigint(20) default NULL;
ALTER TABLE datametadata CHANGE tstartmillis startTime bigint(20) NOT NULL;
ALTER TABLE datametadata CHANGE tstopmillis endTime bigint(20) NOT NULL;

ALTER TABLE plotdata CHANGE descr_id dataDescId bigint(20) default NULL;
ALTER TABLE plotdata CHANGE tstampmills time bigint(20) NOT NULL;

/* Renaming the tables*/
rename table rawdata to ccs_rawData;
rename table datadesc to ccs_dataDesc;
rename table datametadata to ccs_metaDataData;
rename table plotdata to ccs_plotData;

/* End of minimal migration script */

/* Creation of the new tables */

create table ccs_agentDesc (
    agentName varchar(255) not null,
    agentType varchar(255),
    primary key (agentName)
    );

create table ccs_agentSt_innerStDe_b (
    agentStateId bigint not null,
    innerStatesId bigint not null,
    primary key (agentStateId, innerStatesId)
    );

create table ccs_agentState (
    id bigint not null auto_increment,
    agentName varchar(255) not null,
    baseStateId bigint not null,
    primary key (id)
    );

create table ccs_alertDesc (
    id bigint not null auto_increment,
    alertDescription varchar(255),
    alertId varchar(255),
    agentName varchar(255) not null,
    primary key (id)
    );

create table ccs_baseState (
    id bigint not null auto_increment,
    alertState varchar(255),
    commandState varchar(255),
    configState varchar(255),
    operationalState varchar(255),
    phaseState varchar(255),
    primary key (id)
    );

create table ccs_clearedAlertData (
    id bigint not null auto_increment,
    time bigint not null,
    agentName varchar(255) not null,
    agentStateId bigint not null,
    alertDescId bigint not null,
    primary key (id)
    );

create table ccs_innerStateDesc (
    id bigint not null auto_increment,
    enumClassName varchar(255) not null,
    enumValue varchar(255) not null,
    agentName varchar(255) not null,
    primary key (id)
    );

create table ccs_raisedAlertData (
    id bigint not null auto_increment,
    time bigint not null,
    active bit,
    alertCause varchar(255),
    severity varchar(255),
    agentName varchar(255) not null,
    agentStateId bigint not null,
    alertDescId bigint not null,
    clearingAlertId bigint,
    primary key (id)
    );

create table ccs_statData (
    id bigint not null auto_increment,
    average double precision not null,
    maxval double precision,
    minval double precision,
    n integer not null,
    sum2 double precision not null,
    statDescId bigint,
    statTimeIntervalId bigint,
    primary key (id)
    );

create table ccs_statDesc (
    id bigint not null auto_increment,
    timeBinWidth bigint not null,
    dataDescId bigint,
    primary key (id)
    );

create table ccs_statTimeInterval (
    id bigint not null auto_increment,
    binWidth bigint not null,
    startTime bigint not null,
    primary key (id)
    );

/* Index creation */
create index ccs_agentSt_agentNa_i on ccs_agentState (agentName);
create index ccs_agentSt_baseStId_i on ccs_agentState (baseStateId);
create index ccs_baseSt_phaseSt_i on ccs_baseState (phaseState);
create index ccs_baseSt_operationalSt_i on ccs_baseState (operationalState);
create index ccs_baseSt_alertSt_i on ccs_baseState (alertState);
create index ccs_baseSt_commandSt_i on ccs_baseState (commandState);
create index ccs_baseSt_configSt_i on ccs_baseState (configState);
create index ccs_dataDe_agentNa_i on ccs_dataDesc (agentName);
create index ccs_metaDaDa_dataDeId_i on ccs_metaDataData (dataDescId);
create index ccs_metaDaDa_startTi_i on ccs_metaDataData (startTime);
create index ccs_metaDaDa_endTi_i on ccs_metaDataData (endTime);
create index ccs_plotDa_dataDeId_i on ccs_plotData (dataDescId);
create index ccs_plotDa_time_i on ccs_plotData (time);
create index ccs_rawDa_time_i on ccs_rawData (time);
create index ccs_rawDa_dataDeId_i on ccs_rawData (dataDescId);
create index ccs_statDa_statDeId_i on ccs_statData (statDescId);
create index ccs_statDe_dataDeId_i on ccs_statDesc (dataDescId);
create index ccs_statTiIn_startTi_i on ccs_statTimeInterval (startTime);
create index ccs_statTiIn_binWi_i on ccs_statTimeInterval (binWidth);
create index ccs_clearedAlDa_time_i on ccs_clearedAlertData (time);
create index ccs_raisedAlDa_time_i on ccs_raisedAlertData (time);

/* Unique constraints*/

alter table ccs_alertDesc 
    add constraint ccs_alertDe_agentNa_alertId_u unique (agentName, alertId);

alter table ccs_baseState 
    add constraint ccs_baseSt_phaseSt_operationalSt_commandSt_alertSt_configSt_u unique (phaseState, operationalState, commandState, alertState, configState);

alter table ccs_statDesc 
    add constraint ccs_statDe_dataDeId_timeBiWi_u unique (dataDescId, timeBinWidth);

alter table ccs_dataDesc 
    add constraint ccs_dataDe_agentNa_dataNa_u unique (agentName, dataName);

alter table ccs_innerStateDesc 
    add constraint ccs_innerStDe_agentNa_enumClNa_enumVa_u unique (agentName, enumClassName, enumValue);

alter table ccs_statTimeInterval 
    add constraint ccs_statTiIn_startTi_binWi_u unique (startTime, binWidth);

/* Foreign key constraints */

alter table ccs_agentSt_innerStDe_b 
    add constraint ccs_agentStStDe_innerStId_innerStDe_fk 
    foreign key (innerStatesId) 
    references ccs_innerStateDesc (id);

alter table ccs_agentSt_innerStDe_b 
    add constraint ccs_agentStStDe_agentStId_agentSt_fk 
    foreign key (agentStateId) 
    references ccs_agentState (id);

alter table ccs_agentState 
    add constraint ccs_agentSt_agentNa_agentDe_fk 
    foreign key (agentName) 
    references ccs_agentDesc (agentName);

alter table ccs_agentState 
    add constraint ccs_agentSt_baseStId_baseSt_fk 
    foreign key (baseStateId) 
    references ccs_baseState (id);

alter table ccs_alertDesc 
    add constraint ccs_alertDe_agentNa_agentDe_fk 
    foreign key (agentName) 
    references ccs_agentDesc (agentName);

alter table ccs_clearedAlertData 
    add constraint ccs_clearedAlDa_agentNa_agentDe_fk 
    foreign key (agentName) 
    references ccs_agentDesc (agentName);

alter table ccs_clearedAlertData 
    add constraint ccs_clearedAlDa_agentStId_agentSt_fk 
    foreign key (agentStateId) 
    references ccs_agentState (id);

alter table ccs_clearedAlertData 
    add constraint ccs_clearedAlDa_alertDeId_alertDe_fk 
    foreign key (alertDescId) 
    references ccs_alertDesc (id);

alter table ccs_innerStateDesc 
    add constraint ccs_innerStDe_agentNa_agentDe_fk 
    foreign key (agentName) 
    references ccs_agentDesc (agentName);

alter table ccs_metaDataData 
    add constraint ccs_metaDaDa_dataDeId_dataDe_fk 
    foreign key (dataDescId) 
    references ccs_dataDesc (id);

alter table ccs_plotData 
    add constraint ccs_plotDa_dataDeId_dataDe_fk 
    foreign key (dataDescId) 
    references ccs_dataDesc (id);

alter table ccs_raisedAlertData 
    add constraint ccs_raisedAlDa_agentNa_agentDe_fk 
    foreign key (agentName) 
    references ccs_agentDesc (agentName);

alter table ccs_raisedAlertData 
    add constraint ccs_raisedAlDa_agentStId_agentSt_fk 
    foreign key (agentStateId) 
    references ccs_agentState (id);

alter table ccs_raisedAlertData 
    add constraint ccs_raisedAlDa_alertDeId_alertDe_fk 
    foreign key (alertDescId) 
    references ccs_alertDesc (id);

alter table ccs_raisedAlertData 
    add constraint ccs_raisedAlDa_clearingAlId_clearedAlDa_fk 
    foreign key (clearingAlertId) 
    references ccs_clearedAlertData (id);

alter table ccs_rawData 
    add constraint ccs_rawDa_dataDeId_dataDe_fk 
    foreign key (dataDescId) 
    references ccs_dataDesc (id);

alter table ccs_statData 
    add constraint ccs_statDa_statDeId_statDe_fk 
    foreign key (statDescId) 
    references ccs_statDesc (id);
 
alter table ccs_statData 
    add constraint ccs_statDa_statTiInId_statTiIn_fk 
    foreign key (statTimeIntervalId) 
    references ccs_statTimeInterval (id);

alter table ccs_statDesc 
    add constraint ccs_statDe_dataDeId_dataDe_fk 
    foreign key (dataDescId) 
    references ccs_dataDesc (id);

COMMIT;

SET autocommit=1;