Commit fbc4b711 authored by Bruce Momjian's avatar Bruce Momjian

The attached patch adds regression tests for setting and removing

default values using ALTER TABLE, on both views and tables.

(You'll need to apply the default-values-for-views patch that I sent
to -patches earlier for the regression tests to pass.)

Neil Conway <neilconway@rogers.com>
parent 7f459808
......@@ -681,3 +681,59 @@ insert into child (a, b) values (NULL, 'foo');
ERROR: ExecAppend: Fail to add null value in not null attribute a
drop table child;
drop table parent;
-- test setting and removing default values
create table def_test (
c1 int4 default 5,
c2 text default 'initial_default'
);
insert into def_test default values;
alter table def_test alter column c1 drop default;
insert into def_test default values;
alter table def_test alter column c2 drop default;
insert into def_test default values;
alter table def_test alter column c1 set default 10;
alter table def_test alter column c2 set default 'new_default';
insert into def_test default values;
select * from def_test;
c1 | c2
----+-----------------
5 | initial_default
| initial_default
|
10 | new_default
(4 rows)
-- set defaults to an incorrect type: this should fail
alter table def_test alter column c1 set default 'wrong_datatype';
ERROR: pg_atoi: error in "wrong_datatype": can't parse "wrong_datatype"
alter table def_test alter column c2 set default 20;
-- set defaults on a non-existent column: this should fail
alter table def_test alter column c3 set default 30;
ERROR: ALTER TABLE: relation "def_test" has no column "c3"
-- set defaults on views: we need to create a view, add a rule
-- to allow insertions into it, and then alter the view to add
-- a default
create view def_view_test as select * from def_test;
create rule def_view_test_ins as
on insert to def_view_test
do instead insert into def_test select new.*;
insert into def_view_test default values;
alter table def_view_test alter column c1 set default 45;
insert into def_view_test default values;
alter table def_view_test alter column c2 set default 'view_default';
insert into def_view_test default values;
select * from def_view_test;
c1 | c2
----+-----------------
5 | initial_default
| initial_default
|
10 | new_default
|
45 |
45 | view_default
(7 rows)
drop rule def_view_test_ins;
drop view def_view_test;
drop table def_test;
......@@ -532,3 +532,42 @@ insert into child (a, b) values (NULL, 'foo');
drop table child;
drop table parent;
-- test setting and removing default values
create table def_test (
c1 int4 default 5,
c2 text default 'initial_default'
);
insert into def_test default values;
alter table def_test alter column c1 drop default;
insert into def_test default values;
alter table def_test alter column c2 drop default;
insert into def_test default values;
alter table def_test alter column c1 set default 10;
alter table def_test alter column c2 set default 'new_default';
insert into def_test default values;
select * from def_test;
-- set defaults to an incorrect type: this should fail
alter table def_test alter column c1 set default 'wrong_datatype';
alter table def_test alter column c2 set default 20;
-- set defaults on a non-existent column: this should fail
alter table def_test alter column c3 set default 30;
-- set defaults on views: we need to create a view, add a rule
-- to allow insertions into it, and then alter the view to add
-- a default
create view def_view_test as select * from def_test;
create rule def_view_test_ins as
on insert to def_view_test
do instead insert into def_test select new.*;
insert into def_view_test default values;
alter table def_view_test alter column c1 set default 45;
insert into def_view_test default values;
alter table def_view_test alter column c2 set default 'view_default';
insert into def_view_test default values;
select * from def_view_test;
drop rule def_view_test_ins;
drop view def_view_test;
drop table def_test;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment