# Courtesy of Rolf Schaufelberger <rs@plusw.de>
# {{{ AT_Types
CREATE TABLE AT_Types (
  id SERIAL,
  Name varchar(200) NOT NULL  ,
  Description varchar(255) NULL  ,
  DefaultAdmin integer NULL DEFAULT 0  ,
  Lifecycle varchar(32) NULL  ,
  Creator integer NOT NULL DEFAULT 0  ,
  Created timestamp NULL  ,
  LastUpdatedBy integer NOT NULL DEFAULT 0  ,
  LastUpdated timestamp NULL  ,
  Disabled integer NOT NULL DEFAULT 0 ,
  PRIMARY KEY (id)
) ;
CREATE UNIQUE INDEX AT_Types1 ON AT_Types (Name) ;
CREATE INDEX AT_Types2 ON AT_Types (Disabled) ;

# }}}

# {{{ Assets

CREATE TABLE AT_Assets (
  id SERIAL,
  Type integer NOT NULL DEFAULT 0  ,
  Name varchar(200) NOT NULL  ,
  Description varchar(255) NULL  ,
  Status varchar(64) NULL ,
  URI varchar(255),

  LastUpdatedBy integer NOT NULL DEFAULT 0  ,
  LastUpdated timestamp NULL  ,
  Creator integer NOT NULL DEFAULT 0  ,
  Created timestamp NULL  ,
  PRIMARY KEY (id)
) ;

CREATE INDEX AT_Assets1 ON AT_Assets (Type, Status) ;
CREATE INDEX AT_Assets2 ON AT_Assets (Name, Type, Status) ;

# }}}

# {{{ AT_IPs
CREATE TABLE AT_IPs (
  id SERIAL,
  IP char(15) NOT NULL  ,
  MAC char(12) NULL  ,
  Interface varchar(25) NULL DEFAULT 0  ,
  Asset integer NOT NULL DEFAULT 0  ,

  Creator integer NOT NULL DEFAULT 0  ,
  Created timestamp NULL  ,
  LastUpdatedBy integer NOT NULL DEFAULT 0  ,
  LastUpdated timestamp NULL  ,
  PRIMARY KEY (id)
) ;
CREATE UNIQUE INDEX AT_IPs1 ON AT_IPs (IP) ;
CREATE INDEX AT_IPs2 ON AT_IPs (Asset) ;

# }}}

# {{{ AT_Ports
CREATE TABLE AT_Ports (
  id SERIAL,
  Transport char(15) NOT NULL  ,
  Port char(12) NULL  ,
  IP integer NOT NULL DEFAULT 0  ,

  Creator integer NOT NULL DEFAULT 0  ,
  Created timestamp NULL  ,
  LastUpdatedBy integer NOT NULL DEFAULT 0  ,
  LastUpdated timestamp NULL  ,
  PRIMARY KEY (id)
) ;
CREATE INDEX AT_Ports1 ON AT_Ports (Port) ;
CREATE INDEX AT_Ports2 ON AT_Ports (Transport,Port) ;
CREATE INDEX AT_Ports3 ON AT_Ports (IP) ;

# }}}

--
-- Sequences for table SCRIPCONDITIONS
--

CREATE SEQUENCE at_scripconditions_id_seq;

CREATE TABLE AT_ScripConditions (
  id INTEGER DEFAULT nextval('at_scripconditions_id_seq'),
  Name varchar(200) NULL  ,
  Description varchar(255) NULL  ,
  ExecModule varchar(60) NULL  ,
  Argument varchar(255) NULL  ,
  ApplicableTransTypes varchar(60) NULL  ,

  Creator integer NOT NULL DEFAULT 0  ,
  Created TIMESTAMP NULL  ,
  LastUpdatedBy integer NOT NULL DEFAULT 0  ,
  LastUpdated TIMESTAMP NULL  ,
  PRIMARY KEY (id)

);

--
-- Sequences for table SCRIPS
--

CREATE SEQUENCE at_scrips_id_seq;

CREATE TABLE AT_Scrips (
  id INTEGER DEFAULT nextval('at_scrips_id_seq'),
  Description varchar(255),
  ScripCondition integer NOT NULL DEFAULT 0  ,
  ScripAction integer NOT NULL DEFAULT 0  ,
  ConditionRules text NULL  ,
  ActionRules text NULL  ,
  CustomIsApplicableCode text NULL  ,
  CustomPrepareCode text NULL  ,
  CustomCommitCode text NULL  ,
  Stage varchar(32) NULL  ,
  AssetType integer NOT NULL DEFAULT 0  ,
  Template integer NOT NULL DEFAULT 0  ,
  Creator integer NOT NULL DEFAULT 0  ,
  Created TIMESTAMP NULL  ,
  LastUpdatedBy integer NOT NULL DEFAULT 0  ,
  LastUpdated TIMESTAMP NULL  ,
  PRIMARY KEY (id)

);

--
-- Sequences for table SCRIPACTIONS
--

CREATE SEQUENCE at_scripactions_id_seq;

CREATE TABLE AT_ScripActions (
  id INTEGER DEFAULT nextval('at_scripactions_id_seq'),
  Name varchar(200) NULL  ,
  Description varchar(255) NULL  ,
  ExecModule varchar(60) NULL  ,
  Argument varchar(255) NULL  ,
  Creator integer NOT NULL DEFAULT 0  ,
  Created TIMESTAMP NULL  ,
  LastUpdatedBy integer NOT NULL DEFAULT 0  ,
  LastUpdated TIMESTAMP NULL  ,
  PRIMARY KEY (id)

);


--
-- Sequences for table TEMPLATES
--

CREATE SEQUENCE at_templates_id_seq;

CREATE TABLE AT_Templates (
  id INTEGER DEFAULT nextval('at_templates_id_seq'),
  AssetType integer NOT NULL DEFAULT 0 ,
  Name varchar(200) NOT NULL  ,
  Description varchar(255) NULL  ,
  Type varchar(16) NULL  ,
  Language varchar(16) NULL  ,
  TranslationOf integer NOT NULL DEFAULT 0  ,
  Content text NULL  ,
  LastUpdated TIMESTAMP NULL  ,
  LastUpdatedBy integer NOT NULL DEFAULT 0  ,
  Creator integer NOT NULL DEFAULT 0  ,
  Created TIMESTAMP NULL  ,
  PRIMARY KEY (id)

);


