Skip to content

Commit

Permalink
Remove PKs from FOR PORTION OF updates.
Browse files Browse the repository at this point in the history
We already removed GENERATED columns, as well as those owning a
sequence, but that didn't help with uuid primary keys for example.  So
now we just remove all columns of the primary key and that should handle
every situation.
  • Loading branch information
Vik Fearing committed Aug 7, 2019
1 parent 6237b1d commit cd0269f
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 87 deletions.
59 changes: 34 additions & 25 deletions expected/for_portion_of.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@ FROM pg_settings WHERE name = 'server_version_num';
f | f
(1 row)

/*
* Create a sequence to test non-serial primary keys. This actually tests
* things like uuid primary keys, but makes for reproducible test cases.
*/
CREATE SEQUENCE pricing_seq;
CREATE TABLE pricing (id1 bigserial,
id2 bigint GENERATED ALWAYS AS IDENTITY,
id3 bigint GENERATED ALWAYS AS (id1 + id2) STORED,
id2 bigint PRIMARY KEY DEFAULT nextval('pricing_seq'),
id3 bigint GENERATED ALWAYS AS IDENTITY,
id4 bigint GENERATED ALWAYS AS (id1 + id2) STORED,
product text, min_quantity integer, max_quantity integer, price numeric);
CREATE TABLE pricing (id1 bigserial,
id2 bigint GENERATED ALWAYS AS IDENTITY,
id2 bigint PRIMARY KEY DEFAULT nextval('pricing_seq'),
id3 bigint GENERATED ALWAYS AS IDENTITY,
product text, min_quantity integer, max_quantity integer, price numeric);
ERROR: relation "pricing" already exists
CREATE TABLE pricing (id1 bigserial,
id2 bigint PRIMARY KEY DEFAULT nextval('pricing_seq'),
product text, min_quantity integer, max_quantity integer, price numeric);
ERROR: relation "pricing" already exists
SELECT periods.add_period('pricing', 'quantities', 'min_quantity', 'max_quantity');
Expand All @@ -38,53 +46,53 @@ TABLE periods.for_portion_views;
/* Test UPDATE FOR PORTION */
INSERT INTO pricing (product, min_quantity, max_quantity, price) VALUES ('Trinket', 1, 20, 200);
TABLE pricing ORDER BY min_quantity;
id1 | id2 | id3 | product | min_quantity | max_quantity | price
-----+-----+-----+---------+--------------+--------------+-------
1 | 1 | 2 | Trinket | 1 | 20 | 200
id1 | id2 | id3 | id4 | product | min_quantity | max_quantity | price
-----+-----+-----+-----+---------+--------------+--------------+-------
1 | 1 | 1 | 2 | Trinket | 1 | 20 | 200
(1 row)

-- UPDATE fully preceding
UPDATE pricing__for_portion_of_quantities SET min_quantity = 0, max_quantity = 1, price = 0;
TABLE pricing ORDER BY min_quantity;
id1 | id2 | id3 | product | min_quantity | max_quantity | price
-----+-----+-----+---------+--------------+--------------+-------
1 | 1 | 2 | Trinket | 1 | 20 | 200
id1 | id2 | id3 | id4 | product | min_quantity | max_quantity | price
-----+-----+-----+-----+---------+--------------+--------------+-------
1 | 1 | 1 | 2 | Trinket | 1 | 20 | 200
(1 row)

-- UPDATE fully succeeding
UPDATE pricing__for_portion_of_quantities SET min_quantity = 30, max_quantity = 50, price = 0;
TABLE pricing ORDER BY min_quantity;
id1 | id2 | id3 | product | min_quantity | max_quantity | price
-----+-----+-----+---------+--------------+--------------+-------
1 | 1 | 2 | Trinket | 1 | 20 | 200
id1 | id2 | id3 | id4 | product | min_quantity | max_quantity | price
-----+-----+-----+-----+---------+--------------+--------------+-------
1 | 1 | 1 | 2 | Trinket | 1 | 20 | 200
(1 row)

-- UPDATE fully surrounding
UPDATE pricing__for_portion_of_quantities SET min_quantity = 0, max_quantity = 100, price = 100;
TABLE pricing ORDER BY min_quantity;
id1 | id2 | id3 | product | min_quantity | max_quantity | price
-----+-----+-----+---------+--------------+--------------+-------
1 | 1 | 2 | Trinket | 1 | 20 | 100
id1 | id2 | id3 | id4 | product | min_quantity | max_quantity | price
-----+-----+-----+-----+---------+--------------+--------------+-------
1 | 1 | 1 | 2 | Trinket | 1 | 20 | 100
(1 row)

-- UPDATE portion
UPDATE pricing__for_portion_of_quantities SET min_quantity = 10, max_quantity = 20, price = 80;
TABLE pricing ORDER BY min_quantity;
id1 | id2 | id3 | product | min_quantity | max_quantity | price
-----+-----+-----+---------+--------------+--------------+-------
2 | 2 | 4 | Trinket | 1 | 10 | 100
1 | 1 | 2 | Trinket | 10 | 20 | 80
id1 | id2 | id3 | id4 | product | min_quantity | max_quantity | price
-----+-----+-----+-----+---------+--------------+--------------+-------
2 | 2 | 2 | 4 | Trinket | 1 | 10 | 100
1 | 1 | 1 | 2 | Trinket | 10 | 20 | 80
(2 rows)

-- UPDATE portion of multiple rows
UPDATE pricing__for_portion_of_quantities SET min_quantity = 5, max_quantity = 15, price = 90;
TABLE pricing ORDER BY min_quantity;
id1 | id2 | id3 | product | min_quantity | max_quantity | price
-----+-----+-----+---------+--------------+--------------+-------
3 | 3 | 6 | Trinket | 1 | 5 | 100
2 | 2 | 4 | Trinket | 5 | 10 | 90
1 | 1 | 2 | Trinket | 10 | 15 | 90
4 | 4 | 8 | Trinket | 15 | 20 | 80
id1 | id2 | id3 | id4 | product | min_quantity | max_quantity | price
-----+-----+-----+-----+---------+--------------+--------------+-------
3 | 3 | 3 | 6 | Trinket | 1 | 5 | 100
2 | 2 | 2 | 4 | Trinket | 5 | 10 | 90
1 | 1 | 1 | 2 | Trinket | 10 | 15 | 90
4 | 4 | 4 | 8 | Trinket | 15 | 20 | 80
(4 rows)

-- If we drop the period (without CASCADE) then the FOR PORTION views should be
Expand Down Expand Up @@ -132,6 +140,7 @@ TABLE periods.for_portion_views;
(0 rows)

DROP TABLE pricing;
DROP SEQUENCE pricing_seq;
/* Make sure we handle nulls correctly */
CREATE TABLE portions (col1 text, col2 text, col3 text, s integer, e integer);
SELECT periods.add_period('portions', 'p', 's', 'e');
Expand Down
61 changes: 35 additions & 26 deletions expected/for_portion_of_1.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,25 @@ FROM pg_settings WHERE name = 'server_version_num';
f | t
(1 row)

/*
* Create a sequence to test non-serial primary keys. This actually tests
* things like uuid primary keys, but makes for reproducible test cases.
*/
CREATE SEQUENCE pricing_seq;
CREATE TABLE pricing (id1 bigserial,
id2 bigint GENERATED ALWAYS AS IDENTITY,
id3 bigint GENERATED ALWAYS AS (id1 + id2) STORED,
id2 bigint PRIMARY KEY DEFAULT nextval('pricing_seq'),
id3 bigint GENERATED ALWAYS AS IDENTITY,
id4 bigint GENERATED ALWAYS AS (id1 + id2) STORED,
product text, min_quantity integer, max_quantity integer, price numeric);
ERROR: syntax error at or near "("
LINE 3: ... id3 bigint GENERATED ALWAYS AS (id1 + id2...
LINE 4: ... id4 bigint GENERATED ALWAYS AS (id1 + id2...
^
CREATE TABLE pricing (id1 bigserial,
id2 bigint GENERATED ALWAYS AS IDENTITY,
id2 bigint PRIMARY KEY DEFAULT nextval('pricing_seq'),
id3 bigint GENERATED ALWAYS AS IDENTITY,
product text, min_quantity integer, max_quantity integer, price numeric);
CREATE TABLE pricing (id1 bigserial,
id2 bigint PRIMARY KEY DEFAULT nextval('pricing_seq'),
product text, min_quantity integer, max_quantity integer, price numeric);
ERROR: relation "pricing" already exists
SELECT periods.add_period('pricing', 'quantities', 'min_quantity', 'max_quantity');
Expand All @@ -40,53 +48,53 @@ TABLE periods.for_portion_views;
/* Test UPDATE FOR PORTION */
INSERT INTO pricing (product, min_quantity, max_quantity, price) VALUES ('Trinket', 1, 20, 200);
TABLE pricing ORDER BY min_quantity;
id1 | id2 | product | min_quantity | max_quantity | price
-----+-----+---------+--------------+--------------+-------
1 | 1 | Trinket | 1 | 20 | 200
id1 | id2 | id3 | product | min_quantity | max_quantity | price
-----+-----+-----+---------+--------------+--------------+-------
1 | 1 | 1 | Trinket | 1 | 20 | 200
(1 row)

-- UPDATE fully preceding
UPDATE pricing__for_portion_of_quantities SET min_quantity = 0, max_quantity = 1, price = 0;
TABLE pricing ORDER BY min_quantity;
id1 | id2 | product | min_quantity | max_quantity | price
-----+-----+---------+--------------+--------------+-------
1 | 1 | Trinket | 1 | 20 | 200
id1 | id2 | id3 | product | min_quantity | max_quantity | price
-----+-----+-----+---------+--------------+--------------+-------
1 | 1 | 1 | Trinket | 1 | 20 | 200
(1 row)

-- UPDATE fully succeeding
UPDATE pricing__for_portion_of_quantities SET min_quantity = 30, max_quantity = 50, price = 0;
TABLE pricing ORDER BY min_quantity;
id1 | id2 | product | min_quantity | max_quantity | price
-----+-----+---------+--------------+--------------+-------
1 | 1 | Trinket | 1 | 20 | 200
id1 | id2 | id3 | product | min_quantity | max_quantity | price
-----+-----+-----+---------+--------------+--------------+-------
1 | 1 | 1 | Trinket | 1 | 20 | 200
(1 row)

-- UPDATE fully surrounding
UPDATE pricing__for_portion_of_quantities SET min_quantity = 0, max_quantity = 100, price = 100;
TABLE pricing ORDER BY min_quantity;
id1 | id2 | product | min_quantity | max_quantity | price
-----+-----+---------+--------------+--------------+-------
1 | 1 | Trinket | 1 | 20 | 100
id1 | id2 | id3 | product | min_quantity | max_quantity | price
-----+-----+-----+---------+--------------+--------------+-------
1 | 1 | 1 | Trinket | 1 | 20 | 100
(1 row)

-- UPDATE portion
UPDATE pricing__for_portion_of_quantities SET min_quantity = 10, max_quantity = 20, price = 80;
TABLE pricing ORDER BY min_quantity;
id1 | id2 | product | min_quantity | max_quantity | price
-----+-----+---------+--------------+--------------+-------
2 | 2 | Trinket | 1 | 10 | 100
1 | 1 | Trinket | 10 | 20 | 80
id1 | id2 | id3 | product | min_quantity | max_quantity | price
-----+-----+-----+---------+--------------+--------------+-------
2 | 2 | 2 | Trinket | 1 | 10 | 100
1 | 1 | 1 | Trinket | 10 | 20 | 80
(2 rows)

-- UPDATE portion of multiple rows
UPDATE pricing__for_portion_of_quantities SET min_quantity = 5, max_quantity = 15, price = 90;
TABLE pricing ORDER BY min_quantity;
id1 | id2 | product | min_quantity | max_quantity | price
-----+-----+---------+--------------+--------------+-------
3 | 3 | Trinket | 1 | 5 | 100
2 | 2 | Trinket | 5 | 10 | 90
1 | 1 | Trinket | 10 | 15 | 90
4 | 4 | Trinket | 15 | 20 | 80
id1 | id2 | id3 | product | min_quantity | max_quantity | price
-----+-----+-----+---------+--------------+--------------+-------
3 | 3 | 3 | Trinket | 1 | 5 | 100
2 | 2 | 2 | Trinket | 5 | 10 | 90
1 | 1 | 1 | Trinket | 10 | 15 | 90
4 | 4 | 4 | Trinket | 15 | 20 | 80
(4 rows)

-- If we drop the period (without CASCADE) then the FOR PORTION views should be
Expand Down Expand Up @@ -134,6 +142,7 @@ TABLE periods.for_portion_views;
(0 rows)

DROP TABLE pricing;
DROP SEQUENCE pricing_seq;
/* Make sure we handle nulls correctly */
CREATE TABLE portions (col1 text, col2 text, col3 text, s integer, e integer);
SELECT periods.add_period('portions', 'p', 's', 'e');
Expand Down
63 changes: 36 additions & 27 deletions expected/for_portion_of_2.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@ FROM pg_settings WHERE name = 'server_version_num';
t | t
(1 row)

/*
* Create a sequence to test non-serial primary keys. This actually tests
* things like uuid primary keys, but makes for reproducible test cases.
*/
CREATE SEQUENCE pricing_seq;
CREATE TABLE pricing (id1 bigserial,
id2 bigint GENERATED ALWAYS AS IDENTITY,
id3 bigint GENERATED ALWAYS AS (id1 + id2) STORED,
id2 bigint PRIMARY KEY DEFAULT nextval('pricing_seq'),
id3 bigint GENERATED ALWAYS AS IDENTITY,
id4 bigint GENERATED ALWAYS AS (id1 + id2) STORED,
product text, min_quantity integer, max_quantity integer, price numeric);
ERROR: syntax error at or near "GENERATED"
LINE 2: id2 bigint GENERATED ALWAYS AS IDENTIT...
LINE 3: id3 bigint GENERATED ALWAYS AS IDENTIT...
^
CREATE TABLE pricing (id1 bigserial,
id2 bigint GENERATED ALWAYS AS IDENTITY,
id2 bigint PRIMARY KEY DEFAULT nextval('pricing_seq'),
id3 bigint GENERATED ALWAYS AS IDENTITY,
product text, min_quantity integer, max_quantity integer, price numeric);
ERROR: syntax error at or near "GENERATED"
LINE 2: id2 bigint GENERATED ALWAYS AS IDENTIT...
LINE 3: id3 bigint GENERATED ALWAYS AS IDENTIT...
^
CREATE TABLE pricing (id1 bigserial,
id2 bigint PRIMARY KEY DEFAULT nextval('pricing_seq'),
product text, min_quantity integer, max_quantity integer, price numeric);
SELECT periods.add_period('pricing', 'quantities', 'min_quantity', 'max_quantity');
add_period
Expand All @@ -42,53 +50,53 @@ TABLE periods.for_portion_views;
/* Test UPDATE FOR PORTION */
INSERT INTO pricing (product, min_quantity, max_quantity, price) VALUES ('Trinket', 1, 20, 200);
TABLE pricing ORDER BY min_quantity;
id1 | product | min_quantity | max_quantity | price
-----+---------+--------------+--------------+-------
1 | Trinket | 1 | 20 | 200
id1 | id2 | product | min_quantity | max_quantity | price
-----+-----+---------+--------------+--------------+-------
1 | 1 | Trinket | 1 | 20 | 200
(1 row)

-- UPDATE fully preceding
UPDATE pricing__for_portion_of_quantities SET min_quantity = 0, max_quantity = 1, price = 0;
TABLE pricing ORDER BY min_quantity;
id1 | product | min_quantity | max_quantity | price
-----+---------+--------------+--------------+-------
1 | Trinket | 1 | 20 | 200
id1 | id2 | product | min_quantity | max_quantity | price
-----+-----+---------+--------------+--------------+-------
1 | 1 | Trinket | 1 | 20 | 200
(1 row)

-- UPDATE fully succeeding
UPDATE pricing__for_portion_of_quantities SET min_quantity = 30, max_quantity = 50, price = 0;
TABLE pricing ORDER BY min_quantity;
id1 | product | min_quantity | max_quantity | price
-----+---------+--------------+--------------+-------
1 | Trinket | 1 | 20 | 200
id1 | id2 | product | min_quantity | max_quantity | price
-----+-----+---------+--------------+--------------+-------
1 | 1 | Trinket | 1 | 20 | 200
(1 row)

-- UPDATE fully surrounding
UPDATE pricing__for_portion_of_quantities SET min_quantity = 0, max_quantity = 100, price = 100;
TABLE pricing ORDER BY min_quantity;
id1 | product | min_quantity | max_quantity | price
-----+---------+--------------+--------------+-------
1 | Trinket | 1 | 20 | 100
id1 | id2 | product | min_quantity | max_quantity | price
-----+-----+---------+--------------+--------------+-------
1 | 1 | Trinket | 1 | 20 | 100
(1 row)

-- UPDATE portion
UPDATE pricing__for_portion_of_quantities SET min_quantity = 10, max_quantity = 20, price = 80;
TABLE pricing ORDER BY min_quantity;
id1 | product | min_quantity | max_quantity | price
-----+---------+--------------+--------------+-------
2 | Trinket | 1 | 10 | 100
1 | Trinket | 10 | 20 | 80
id1 | id2 | product | min_quantity | max_quantity | price
-----+-----+---------+--------------+--------------+-------
2 | 2 | Trinket | 1 | 10 | 100
1 | 1 | Trinket | 10 | 20 | 80
(2 rows)

-- UPDATE portion of multiple rows
UPDATE pricing__for_portion_of_quantities SET min_quantity = 5, max_quantity = 15, price = 90;
TABLE pricing ORDER BY min_quantity;
id1 | product | min_quantity | max_quantity | price
-----+---------+--------------+--------------+-------
3 | Trinket | 1 | 5 | 100
2 | Trinket | 5 | 10 | 90
1 | Trinket | 10 | 15 | 90
4 | Trinket | 15 | 20 | 80
id1 | id2 | product | min_quantity | max_quantity | price
-----+-----+---------+--------------+--------------+-------
3 | 3 | Trinket | 1 | 5 | 100
2 | 2 | Trinket | 5 | 10 | 90
1 | 1 | Trinket | 10 | 15 | 90
4 | 4 | Trinket | 15 | 20 | 80
(4 rows)

-- If we drop the period (without CASCADE) then the FOR PORTION views should be
Expand Down Expand Up @@ -136,6 +144,7 @@ TABLE periods.for_portion_views;
(0 rows)

DROP TABLE pricing;
DROP SEQUENCE pricing_seq;
/* Make sure we handle nulls correctly */
CREATE TABLE portions (col1 text, col2 text, col3 text, s integer, e integer);
SELECT periods.add_period('portions', 'p', 's', 'e');
Expand Down
Loading

0 comments on commit cd0269f

Please sign in to comment.