Browse Source

Homework 2 fix

Vsevolod Levitan 2 months ago
parent
commit
81b2238024
1 changed files with 17 additions and 2 deletions
  1. 17 2
      homework/1.sql

+ 17 - 2
homework/1.sql

@@ -1,3 +1,4 @@
+begin;
 DO $$ 
 begin
 
@@ -8,7 +9,7 @@ create table if not exists ranks (id serial primary key, name varchar(32));
 create table if not exists users (id serial primary key, name varchar(64), rank_id integer references ranks(id));
 
 -- Заполняем звания
-if exists(select from ranks where name = 'Рядовой' limit 1) then
+if exists(select from ranks limit 1) then
 raise notice 'Ranks values exist';
 else
 insert into ranks(name) values ('Рядовой');
@@ -38,6 +39,7 @@ if exists(select from information_schema.columns where table_name='measurement_b
 raise notice 'user_id column already exists in table measurement_batch';
 else
 alter table measurement_batch rename column username to user_id;
+delete from measurement_batch cascade;
 alter table measurement_batch alter column user_id type integer using null;
 end if;
 
@@ -58,14 +60,27 @@ end if;
 
 END $$;
 
+commit;
+
+begin;
+
 DO $$
 begin
 
 -- -- Измерение
-if (select count(id) from measurement_batch limit 1) > 0 then
+if exists(select id from measurement_batch limit 1) then
 raise notice 'Test batch exists';
 else
 insert into measurement_batch(start_period, user_id, pos_x, pos_y) values (now(), (select id from users limit 1), 69.00, 42.00);
 end if;
 
+-- Данные измерения
+if exists(select id from measurement_params limit 1) then 
+raise notice 'Test params exists';
+else
+insert into measurement_params(measurement_type_id, measurement_batch_id, height, temperature, pressure, wind_speed, wind_direction, bullet_speed) values (1, 1, 100, 15, 750, 0, 0, 0);
+end if;
+
 END$$;
+
+commit;