From 396e1cd80b6bbe66cf40841ca1ff24c79f1b9885 Mon Sep 17 00:00:00 2001
From: Anton Bagliy <taccessviolation@gmail.com>
Date: Sat, 17 Nov 2018 18:00:26 +0300
Subject: [PATCH] CHG: use int in study_groups.formid instead of enum; ADD:
 issue50 sql migration script #50

---
 .../alter_study_groups_formid_17.11.18.sql    | 74 +++++++++++++++++++
 db/postgresql/functions.sql                   |  2 +-
 db/postgresql/keys_and_index.sql              |  3 +
 db/postgresql/tables_and_views.sql            | 10 ++-
 db/postgresql/types.sql                       | 10 +--
 .../classes/Controller/Api/V0/Student.php     |  6 +-
 6 files changed, 94 insertions(+), 11 deletions(-)
 create mode 100644 db/postgresql/alter_study_groups_formid_17.11.18.sql

diff --git a/db/postgresql/alter_study_groups_formid_17.11.18.sql b/db/postgresql/alter_study_groups_formid_17.11.18.sql
new file mode 100644
index 000000000..50ca471de
--- /dev/null
+++ b/db/postgresql/alter_study_groups_formid_17.11.18.sql
@@ -0,0 +1,74 @@
+update study_groups set formid=1;
+
+ALTER TABLE public.study_groups ALTER COLUMN formid SET NOT NULL;
+ALTER TABLE public.study_groups ALTER COLUMN formid SET DEFAULT 1;
+
+ALTER TABLE public.study_groups DROP CONSTRAINT study_groups_facultyid_gradeid_groupnum_key;
+ALTER TABLE ONLY study_groups
+  ADD CONSTRAINT study_groups_facultyid_gradeid_groupnum_formid_key UNIQUE (facultyid, gradeid, groupnum, formid);
+
+ALTER TABLE ONLY study_groups
+  ADD CONSTRAINT study_groups_ibfk_4 FOREIGN KEY (formid) REFERENCES study_form(id);
+
+DROP FUNCTION IF EXISTS public.creategroup(pgradeid integer, pgroupnum integer, pspecname character varying, pfacultyid integer, pyear integer);
+CREATE OR REPLACE FUNCTION public.creategroup(pgradeid integer, pgroupnum integer, pspecname character varying, pfacultyid integer, pyear integer, pformid integer)
+  RETURNS integer
+LANGUAGE plpgsql
+AS $function$
+DECLARE vGroupID INT DEFAULT null;
+        vSpecId INT DEFAULT null;
+        vGroupYear INT DEFAULT null;
+        vIsSpecMatch BOOL DEFAULT null;
+
+BEGIN
+  --select public.creategroup(
+  --	:pgradeid,	-- put the pgradeid parameter value instead of 'pgradeid' (int4)
+  --	:pgroupnum,	-- put the pgroupnum parameter value instead of 'pgroupnum' (int4)
+  --	:pspecname,	-- put the pspecname parameter value instead of 'pspecname' (varchar)
+  --	:pfacultyid,	-- put the pfacultyid parameter value instead of 'pfacultyid' (int4)
+  --	:pyear 	-- put the pyear parameter value instead of 'pyear' (int4)
+  --);
+  -- create specialization vSpecId - UNIQUE (pSpecName,pFacultyID)
+  select specializations.id into vSpecId
+  from specializations
+  where specializations.facultyid=pFacultyID and specializations."name" like pSpecName;
+  if (vSpecId is null) then
+    INSERT INTO specializations (Name, Abbr, FacultyID)
+    VALUES  (pSpecName, NULL, pFacultyID)
+    returning id into vSpecId;
+  end if;
+
+
+  -- create group  vGroupID - UNIQUE (pGradeID, pGroupNum, pFacultyID)
+  select study_groups.id into vGroupID
+  from study_groups
+  where study_groups.gradeid=pgradeID
+        and study_groups.groupnum= pGroupNum
+        and study_groups.facultyid=pFacultyID
+        and study_groups.formid=pFormID;
+  if ( vGroupID  is null) then
+    INSERT INTO study_groups (GradeID, GroupNum, FacultyID, FormID)
+    VALUES (pGradeID, pGroupNum, pFacultyID, pFormID)
+    returning id into  vGroupID ;
+  end if;
+
+
+  SELECT groups_years.groupid, groups_years.specializationid = vSpecId
+  INTO vGroupYear, vIsSpecMatch
+  FROM groups_years
+  WHERE groups_years.groupid = vGroupID AND groups_years."year" = pYear
+  LIMIT 1;
+
+  IF  (vGroupYear is null) THEN
+    INSERT INTO groups_years (GroupID, Year, SpecializationID)
+    VALUES (vGroupID, pYear, vSpecId);
+  ELSEIF NOT vIsSpecMatch THEN
+    RETURN -1;
+  END IF;
+
+  RETURN vGroupID;
+  EXCEPTION
+  when others then
+    RETURN -1;
+END
+$function$;
\ No newline at end of file
diff --git a/db/postgresql/functions.sql b/db/postgresql/functions.sql
index 31a63b85e..011eef635 100644
--- a/db/postgresql/functions.sql
+++ b/db/postgresql/functions.sql
@@ -1141,7 +1141,7 @@ BEGIN
 END
 $function$;
 
- CREATE OR REPLACE FUNCTION public.creategroup(pgradeid integer, pgroupnum integer, pspecname character varying, pfacultyid integer, pyear integer, pformid fulltime_parttime_evening)
+ CREATE OR REPLACE FUNCTION public.creategroup(pgradeid integer, pgroupnum integer, pspecname character varying, pfacultyid integer, pyear integer, pformid integer)
  RETURNS integer
  LANGUAGE plpgsql
  AS $function$
diff --git a/db/postgresql/keys_and_index.sql b/db/postgresql/keys_and_index.sql
index 8885ac9fd..7a7d42e70 100644
--- a/db/postgresql/keys_and_index.sql
+++ b/db/postgresql/keys_and_index.sql
@@ -1527,6 +1527,9 @@ ALTER TABLE ONLY study_groups
     ADD CONSTRAINT study_groups_ibfk_3 FOREIGN KEY (facultyid) REFERENCES faculties(id);
 
 
+ALTER TABLE ONLY study_groups
+    ADD CONSTRAINT study_groups_ibfk_4 FOREIGN KEY (formid) REFERENCES study_form(id);
+
 --
 -- TOC entry 2636 (class 2606 OID 46417)
 -- Name: study_plans study_plans_fk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
diff --git a/db/postgresql/tables_and_views.sql b/db/postgresql/tables_and_views.sql
index 50581fecb..84242288f 100644
--- a/db/postgresql/tables_and_views.sql
+++ b/db/postgresql/tables_and_views.sql
@@ -936,7 +936,7 @@ CREATE TABLE study_groups (
     gradeid integer NOT NULL,
     groupnum integer NOT NULL,
     facultyid integer NOT NULL,
-    formid fulltime_parttime_evening DEFAULT 'fulltime' NOT NULL
+    formid integer DEFAULT 1 NOT NULL
 );
 
 
@@ -1294,4 +1294,10 @@ INSERT INTO lesson_types(id, lesson_type) values
 (1, 'лекция'),
 (2, 'лабораторная'),
 (3, 'семинар'),
-(4, 'практикум');
\ No newline at end of file
+(4, 'практикум');
+
+INSERT INTO public.study_form(id, formname) VALUES
+(1, 'очная'),
+(2, 'очно-заочная'),
+(3, 'заочная');
+
diff --git a/db/postgresql/types.sql b/db/postgresql/types.sql
index 7f19bd553..f9dceb22e 100644
--- a/db/postgresql/types.sql
+++ b/db/postgresql/types.sql
@@ -152,8 +152,8 @@ CREATE TYPE student_teacher_admin_dean AS ENUM (
 
 CREATE TYPE noukd_norate_full AS ENUM ('noukd', 'norate', 'full');
 
-CREATE TYPE fulltime_parttime_evening AS ENUM (
-    'fulltime',
-    'parttime',
-    'evening'
-);
\ No newline at end of file
+--CREATE TYPE fulltime_parttime_evening AS ENUM (
+--    'fulltime',
+--    'parttime',
+--    'evening'
+--);
\ No newline at end of file
diff --git a/~dev_rating/application/classes/Controller/Api/V0/Student.php b/~dev_rating/application/classes/Controller/Api/V0/Student.php
index 2de2701c7..8daa5c96b 100644
--- a/~dev_rating/application/classes/Controller/Api/V0/Student.php
+++ b/~dev_rating/application/classes/Controller/Api/V0/Student.php
@@ -143,15 +143,15 @@ class Controller_Api_V0_Student extends Controller_Handler_Api {
 
         switch ($recordBookData->form) {
             case 'Очная':
-                $recordBookData->form = 'fulltime';
+                $recordBookData->form = 1;
                 break;
 
             case 'Очно-заочная':
-                $recordBookData->form = 'parttime';
+                $recordBookData->form = 3;
                 break;
 
             case 'Заочная':
-                $recordBookData->form = 'evening';
+                $recordBookData->form = 2;
                 break;
 
             default:
-- 
GitLab