sagelib

Load HRVHS Students Script

Oct 6th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Script for Loading HRVHS Patrons and adding Freshman/Grad Year Stat Cats--
  2.  
  3. BEGIN;
  4.  
  5. --DROP TABLE IF EXISTS hrvhs_freshman_load;
  6. --DROP TABLE IF EXISTS hrvhs_freshman_load_ids;
  7. --DROP TABLE IF EXISTS hrvhs_stat_cat_1;
  8. --DROP TABLE IF EXISTS hrvhs_stat_cat_2;
  9.  
  10.  
  11. -- Hints --
  12. -- test db perm group = 16, org_unit = 246
  13. -- statcat1 = Class Standing
  14. -- statcat2 = Grad Year
  15.  
  16.     --Create main student staging table.
  17.     CREATE TABLE hrvhs_freshman_load (
  18.         h_studentid text, -- Student ID # // ident_value
  19.         h_usrname text, -- will concat 'hrv+studentid' later
  20.         h_first_name text,
  21.         h_middle_name text,
  22.         h_last_name text,
  23.         h_dob date,
  24.         h_home_ou int DEFAULT 214, -- Hood River High School = 246
  25.         h_profile int DEFAULT 90, -- STUDENT - HIGH SCHOOL
  26.         h_ident_type int DEFAULT 2, -- Student ID
  27.         h_net_access_level int DEFAULT 2,
  28.         h_passwd text, -- student id
  29.         --
  30.         h_address_type text,
  31.         h_street1 text,
  32.         h_street2 text,
  33.         h_city text,
  34.         h_state text,
  35.         h_country text,
  36.         h_postal_code text,
  37.         --
  38.         h_statcat1 text,
  39.         h_statcat2 text
  40.     );
  41.  
  42.     --Copy records from your import text file
  43.     COPY hrvhs_freshman_load (
  44.         h_studentid, -- Student ID
  45.         h_first_name,
  46.         h_middle_name,
  47.         h_last_name,
  48.         h_dob,
  49.         --
  50.         h_address_type,
  51.         h_street1,
  52.         h_street2,
  53.         h_city,
  54.         h_state,
  55.         h_country,
  56.         h_postal_code,
  57.         --
  58.         h_statcat1,
  59.         h_statcat2
  60.         )
  61.         FROM '/home/bmills/hrvhs_freshman_load.csv' WITH CSV HEADER;
  62.  
  63.     --Create the usrname(barcode) entry from the student id (ident_value)
  64.     UPDATE hrvhs_freshman_load SET h_usrname='hrv'||h_studentid;
  65.    
  66.     -- Set password=student ID for now
  67.     UPDATE hrvhs_freshman_load SET h_passwd=h_studentid;
  68.  
  69.     --Insert records from the hrvhs_freshman_load table into the actor.usr table.
  70.     INSERT INTO actor.usr (
  71.         profile,
  72.         usrname,
  73.         passwd,
  74.         ident_type,
  75.         ident_value,
  76.         first_given_name,
  77.         family_name,
  78.         home_ou,
  79.         dob,
  80.         net_access_level)
  81.     SELECT
  82.         h_profile,
  83.         h_usrname,
  84.         h_passwd,
  85.         h_ident_type,
  86.         h_studentid,
  87.         h_first_name,
  88.         h_last_name,
  89.         h_home_ou,
  90.         h_dob,
  91.         h_net_access_level
  92.         FROM hrvhs_freshman_load;
  93.  
  94.         -- Make our lives easier and create a table of actor.usr ids of the incoming students for later consulation
  95.         CREATE TABLE hrvhs_freshman_load_ids(
  96.             h_actor_usr_id int,
  97.             h_usrname text,
  98.             h_studentid text,
  99.             h_create_time date DEFAULT now(),
  100.             h_class_and_year text DEFAULT '2020 - Freshman'
  101.         );
  102.         -- Load it up
  103.         INSERT INTO hrvhs_freshman_load_ids(h_actor_usr_id,h_usrname,h_studentid)
  104.         SELECT au.id, hrvhs.h_usrname,hrvhs.h_studentid
  105.         FROM hrvhs_freshman_load hrvhs
  106.         INNER JOIN actor.usr au
  107.         ON hrvhs.h_usrname=au.usrname;
  108.          
  109.     --Load barcodes into actor.card table from the usrname entries.
  110.     INSERT INTO actor.card (usr, barcode)
  111.         SELECT h_actor_usr_id,h_usrname
  112.         FROM hrvhs_freshman_load_ids;
  113.        
  114.     --Update actor.usr.card field with actor.card.id to associate active card with the user:
  115.     --Play it safe and only update when User ID is in the hrvhs id table
  116.     UPDATE actor.usr au
  117.         SET card = ac.id
  118.         FROM actor.card ac
  119.         WHERE ac.usr = au.id AND au.id IN (SELECT h_actor_usr_id FROM hrvhs_freshman_load_ids);
  120.  
  121.     --Update actor.usr_address from staging table.
  122.     INSERT INTO actor.usr_address (usr, street1, street2, city, state, country, post_code)
  123.         SELECT au.id,
  124.         hrvhs.h_street1,
  125.         hrvhs.h_street2,
  126.         hrvhs.h_city,
  127.         hrvhs.h_state,
  128.         hrvhs.h_country,
  129.         hrvhs.h_postal_code
  130.         FROM hrvhs_freshman_load hrvhs
  131.         INNER JOIN actor.usr au ON au.usrname=hrvhs.h_usrname;
  132.  
  133.  
  134.    --Update actor.usr mailing address with id from actor.usr_address table:
  135.     UPDATE actor.usr au
  136.         SET mailing_address = aua.id, billing_address = aua.id
  137.         FROM actor.usr_address aua
  138.         WHERE au.id = aua.usr
  139.             AND au.id IN (SELECT h_actor_usr_id FROM hrvhs_freshman_load_ids);
  140.        
  141.     --Insert Stat Cats (Freshman, 2020 Grad Date) -- "Freshman,43 | 2020,69"
  142.     --80 (Grad Year), 81 (Class) on test server
  143.    
  144.     -- Create Scratch Tables for load
  145.     -- Since I know of no nice way to do multiple inserts with one value dynamically changing
  146.    
  147.     --School Level--
  148.     CREATE TABLE hrvhs_stat_cat_1 (
  149.         stat_cat_entry text DEFAULT 'Freshman',
  150.         stat_cat int DEFAULT 43,
  151.         target_usr int
  152.     );
  153.     INSERT INTO hrvhs_stat_cat_1 (target_usr)
  154.     SELECT h_actor_usr_id FROM hrvhs_freshman_load_ids;
  155.    
  156.     INSERT INTO actor.stat_cat_entry_usr_map (stat_cat_entry,stat_cat,target_usr)
  157.     SELECT stat_cat_entry,stat_cat,target_usr FROM hrvhs_stat_cat_1;
  158.    
  159.     --Grad Year--
  160.     CREATE TABLE hrvhs_stat_cat_2 (
  161.         stat_cat_entry text DEFAULT '2020',
  162.         stat_cat int DEFAULT 69,
  163.         target_usr int
  164.     );
  165.     INSERT INTO hrvhs_stat_cat_2 (target_usr)
  166.     SELECT h_actor_usr_id FROM hrvhs_freshman_load_ids;
  167.    
  168.     INSERT INTO actor.stat_cat_entry_usr_map (stat_cat_entry,stat_cat,target_usr)
  169.     SELECT stat_cat_entry,stat_cat,target_usr FROM hrvhs_stat_cat_2;
  170.    
  171.     -- DONE! --
  172.  
  173.     COMMIT;
Add Comment
Please, Sign In to add comment