From c33bbf0de6d86d127aa574f341580ea4102ac114 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garcia=20Nu=C3=B1ez?= Date: Fri, 13 Dec 2019 18:26:55 +0100 Subject: [PATCH] =?UTF-8?q?Script=20co=20comprobaci=C3=B3n=20de=20existenc?= =?UTF-8?q?ia=20de=20objetos,=20para=20poder=20ejecutar=20despliegue=20aut?= =?UTF-8?q?om=C3=A1tico=20en=20base=20de=20datos.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2.database/01.CreateTables.sql | 118 ++++++++++++++++++++++----------- 1 file changed, 81 insertions(+), 37 deletions(-) diff --git a/2.database/01.CreateTables.sql b/2.database/01.CreateTables.sql index 145ec54..c28d5be 100644 --- a/2.database/01.CreateTables.sql +++ b/2.database/01.CreateTables.sql @@ -1,40 +1,88 @@ -/* -DROP TABLE myhealth.Administrator; -DROP TABLE myhealth.FamilyDoctor; -DROP TABLE myhealth.MedicalSpecialty; -DROP TABLE myhealth.MedicalTest; -DROP TABLE myhealth.Patient; -DROP TABLE myhealth.PrimaryHealthCareCenter; -DROP TABLE myhealth.Question; -DROP TABLE myhealth.Response; -DROP TABLE myhealth.SpecialistDoctor; -DROP TABLE myhealth.Visit; +DO +$$ +begin -DROP SEQUENCE myhealth.ProfesionalNumber; -DROP SEQUENCE myhealth.CodigoIdentificacionPaciente +IF NOT EXISTS(SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'myhealth') THEN + CREATE SCHEMA myhealth; +END IF; + +IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname='usrmyhealth') THEN + create user usrmyhealth with encrypted password 'myhealth.123'; +END IF; + +IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname='USER') THEN + create user "USER" with encrypted password 'PASSWORD'; +END IF; + +GRANT ALL ON SCHEMA myhealth TO "USER"; +GRANT ALL ON SCHEMA myhealth TO usrmyhealth; + +if exists (SELECT 1 FROM pg_tables WHERE schemaname='myhealth' and tablename='administrator') THEN + drop table myhealth.administrator; +end if; +if exists (SELECT 1 FROM pg_tables WHERE schemaname='myhealth' and tablename='familydoctor') THEN + drop table myhealth.familydoctor; +end if; +if exists (SELECT 1 FROM pg_tables WHERE schemaname='myhealth' and tablename='medicalspecialty') THEN + drop table myhealth.medicalspecialty; +end if; +if exists (SELECT 1 FROM pg_tables WHERE schemaname='myhealth' and tablename='medicaltest') THEN + drop table myhealth.medicaltest; +end if; +if exists (SELECT 1 FROM pg_tables WHERE schemaname='myhealth' and tablename='patient') THEN + drop table myhealth.patient; +end if; +if exists (SELECT 1 FROM pg_tables WHERE schemaname='myhealth' and tablename='primaryhealthcarecenter') THEN + drop table myhealth.primaryhealthcarecenter; +end if; +if exists (SELECT 1 FROM pg_tables WHERE schemaname='myhealth' and tablename='question') THEN + drop table myhealth.question; +end if; +if exists (SELECT 1 FROM pg_tables WHERE schemaname='myhealth' and tablename='response') THEN + drop table myhealth.response; +end if; +if exists (SELECT 1 FROM pg_tables WHERE schemaname='myhealth' and tablename='specialistdoctor') THEN + drop table myhealth.specialistdoctor; +end if; +if exists (SELECT 1 FROM pg_tables WHERE schemaname='myhealth' and tablename='visit') THEN + drop table myhealth.visit; +end if; + +/* +drop table myhealth.administrator; +drop table myhealth.familydoctor; +drop table myhealth.medicalspecialty; +drop table myhealth.medicaltest; +drop table myhealth.patient; +drop table myhealth.primaryhealthcarecenter; +drop table myhealth.question; +drop table myhealth.response; +drop table myhealth.specialistdoctor; +drop table myhealth.visit; */ +if exists (SELECT 1 FROM pg_sequences WHERE schemaname='myhealth' and sequencename='profesionalnumber') THEN + drop sequence myhealth.profesionalnumber; +end if; + +if exists (SELECT 1 FROM pg_sequences WHERE schemaname='myhealth' and sequencename='codigoidentificacionpaciente') THEN + drop sequence myhealth.codigoidentificacionpaciente; +end if; + CREATE SEQUENCE myhealth.ProfesionalNumber INCREMENT 1 START 1000 MINVALUE 1000 CACHE 1; -ALTER SEQUENCE myhealth.ProfesionalNumber OWNER TO "USER"; - CREATE SEQUENCE myhealth.CodigoIdentificacionPaciente INCREMENT 1 START 1000 MINVALUE 1000 CACHE 1; -ALTER SEQUENCE myhealth.CodigoIdentificacionPaciente OWNER TO "USER"; - - -- Table: myhealth.administrator - -- DROP TABLE myhealth.administrator; - CREATE TABLE myhealth.administrator ( email VARCHAR(120) COLLATE pg_catalog."default" NOT NULL, @@ -64,6 +112,9 @@ CREATE TABLE myhealth.familydoctor ) TABLESPACE pg_default; +CREATE UNIQUE INDEX family_doctor_professionaln_index + ON myhealth.familydoctor (professionalnumber); + ALTER TABLE myhealth.familydoctor OWNER to "USER"; @@ -122,24 +173,12 @@ CREATE TABLE myhealth.patient ) TABLESPACE pg_default; +CREATE UNIQUE INDEX patient_pic_index + ON myhealth.patient (personalIdentificationCode); + ALTER TABLE myhealth.patient OWNER to "USER"; --- Table: myhealth.location - --- DROP TABLE myhealth.location; - -CREATE TABLE myhealth.location -( - id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ), - name VARCHAR(100) COLLATE pg_catalog."default" NOT NULL, - CONSTRAINT primarylocation_pkey PRIMARY KEY (id) -) -TABLESPACE pg_default; - -ALTER TABLE myhealth.location - OWNER to "USER"; - -- Table: myhealth.primaryhealthcarecenter -- DROP TABLE myhealth.primaryhealthcarecenter; @@ -147,8 +186,8 @@ ALTER TABLE myhealth.location CREATE TABLE myhealth.primaryhealthcarecenter ( id integer NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ), - name character varying(150) COLLATE pg_catalog."default" NOT NULL, - location character varying(512) COLLATE pg_catalog."default", + name VARCHAR(150) COLLATE pg_catalog."default" NOT NULL, + location VARCHAR(512) COLLATE pg_catalog."default", CONSTRAINT primaryhealthcarecenter_pkey PRIMARY KEY (id) ) TABLESPACE pg_default; @@ -208,6 +247,9 @@ CREATE TABLE myhealth.specialistdoctor ) TABLESPACE pg_default; +CREATE UNIQUE INDEX specialistdoctor_professionaln_index + ON myhealth.specialistdoctor (professionalnumber); + ALTER TABLE myhealth.specialistdoctor OWNER to "USER"; @@ -231,3 +273,5 @@ TABLESPACE pg_default; ALTER TABLE myhealth.visit OWNER to "USER"; +END; +$$