-- PostgreSQL Database creation script

CREATE DATABASE sympa;

-- Connect to DB 
\connect sympa

DROP TABLE user_table;
CREATE TABLE user_table (
  	email_user          	varchar (100) NOT NULL,
  	gecos_user          	varchar (150),
	cookie_delay_user       int4,
        password_user		varchar (40),
        lang_user               varchar (10),
	attributes_user		varchar (255),
	data_user	        varchar (255),
	CONSTRAINT ind_user PRIMARY KEY (email_user)
);

DROP TABLE subscriber_table;
CREATE TABLE subscriber_table (
  	list_subscriber       	varchar (50) NOT NULL,
	user_subscriber		varchar (100) NOT NULL,
  	custom_attribute_subscriber varchar (500),
	robot_subscriber	varchar (80) NOT NULL,
	date_subscriber		timestamp with time zone NOT NULL,
	update_subscriber 	timestamp with time zone,
	visibility_subscriber	varchar (20),
	reception_subscriber	varchar (20),
	topics_subscriber	varchar (200),
	bounce_subscriber	varchar (35),
	bounce_score_subscriber int4,
	bounce_address_subscriber	varchar (100),
	comment_subscriber	varchar (150),
	subscribed_subscriber 	smallint,
	included_subscriber 	smallint,
	include_sources_subscriber varchar(50),
	CONSTRAINT ind_subscriber PRIMARY KEY (robot_subscriber,list_subscriber,user_subscriber)
);
CREATE INDEX subscriber_idx ON subscriber_table (robot_subscriber,list_subscriber,user_subscriber);
CREATE INDEX subscriber_idx2 ON subscriber_table (user_subscriber);


DROP TABLE admin_table;
CREATE TABLE admin_table (
	list_admin 		varchar(50) NOT NULL,
 	user_admin 		varchar(100) NOT NULL,
 	robot_admin 		varchar(80) NOT NULL,
	role_admin 		varchar(15) NOT NULL,
	date_admin 		timestamp with time zone NOT NULL,
	update_admin 		timestamp with time zone,
	reception_admin 	varchar(20),
	visibility_admin 	varchar(20),
	comment_admin 		varchar(150),
	subscribed_admin  	smallint,
	included_admin  	smallint,
	include_sources_admin  	varchar(50),
	info_admin   		varchar(150),
	profile_admin  		varchar(15),
        CONSTRAINT ind_admin PRIMARY KEY (robot_admin, list_admin, role_admin, user_admin)
);
CREATE	INDEX admin_idx ON admin_table(robot_admin, list_admin, role_admin, user_admin);
CREATE	INDEX admin_idx2 ON admin_table(user_admin);
 
DROP TABLE netidmap_table;
CREATE TABLE netidmap_table (
        netid_netidmap              varchar (100) NOT NULL,
	serviceid_netidmap	    varchar (100) NOT NULL,
	robot_netidmap	            varchar (80) NOT NULL,
        email_netidmap              varchar (100),
        CONSTRAINT ind_netidmap PRIMARY KEY (netid_netidmap, serviceid_netidmap, robot_netidmap)
);
CREATE	INDEX netidmap_idx ON netidmap_table(netid_netidmap, serviceid_netidmap, robot_netidmap);

DROP TABLE logs_table;
CREATE TABLE logs_table (
	id_logs			bigint NOT NULL,
	date_logs		int4 NOT NULL,
	robot_logs		varchar (80),
	list_logs		varchar (50),
	action_logs		varchar (50) NOT NULL,
	parameters_logs		varchar (100),
	target_email_logs	varchar (100),
	user_email_logs		varchar (100),
	msg_id_logs		varchar (255),
	status_logs		varchar (10) NOT NULL,
	error_type_logs		varchar (150),
	client_logs		varchar (100),
	daemon_logs		varchar (10) NOT NULL,
        CONSTRAINT ind_logs PRIMARY KEY (id_logs)
);
CREATE	INDEX logs_idx ON logs_table(id_logs);

DROP TABLE session_table;
CREATE TABLE session_table (
	id_session			int8 NOT NULL,
	start_date_session		int4 NOT NULL,
	date_session		        int4 NOT NULL,
	remote_addr_session		varchar(60),
	robot_session		        varchar(80),
	email_session		        varchar(100),
	hit_session     	        int4,
	data_session		        varchar(255),
	CONSTRAINT ind_session PRIMARY KEY (id_session)
);
CREATE	INDEX session_idx ON session_table(id_session);
