2021-04-26 20:07:00 +02:00
#coding:utf-8
2022-01-24 20:27:02 +01:00
"""
ID : issue - 5032
ISSUE : 5032
TITLE : Inconsistencies with ALTER DOMAIN and ALTER TABLE with DROP NOT NULL and PRIMARY KEYs
DESCRIPTION :
JIRA : CORE - 4725
2022-02-02 15:46:19 +01:00
FBTEST : bugs . core_4725
2022-01-24 20:27:02 +01:00
"""
2021-04-26 20:07:00 +02:00
2022-01-24 20:27:02 +01:00
import pytest
from firebird . qa import *
2021-04-26 20:07:00 +02:00
2022-01-24 20:27:02 +01:00
db = db_factory ( )
2021-04-26 20:07:00 +02:00
2022-01-24 20:27:02 +01:00
test_script = """
2021-04-26 20:07:00 +02:00
- - Tests that manipulates with NULL fields / domains and check results :
- - CORE - 1518 Adding a non - null restricted column to a populated table renders the table inconsistent
- - CORE - 4453 ( Regression : NOT NULL constraint , declared in domain , does not work )
- - CORE - 4725 ( Inconsistencies with ALTER DOMAIN and ALTER TABLE with DROP NOT NULL and PRIMARY KEYs )
- - CORE - 4733 ( Command " Alter table <T> alter TYPE <C> <DOMAIN_WITH_NOT_NULL " does not verifies data in column < C > and makes incorrect assignments in < C > to ZERO / JULIAN_DATE / ASCII ( 0 ) for types INT , TIMESTAMP and VARCHAR )
set list on ;
set heading off ;
recreate table test00 ( x integer ) ;
recreate table test01 ( x integer ) ;
recreate table test02 ( x integer ) ;
recreate table test03 ( x integer ) ;
recreate table test04 ( x integer ) ;
recreate table test05 ( x integer ) ;
recreate table test06 ( x integer ) ;
commit ;
set term ^ ;
execute block as
begin
begin execute statement ' drop domain dm_01 ' ; when any do begin end end
begin execute statement ' drop domain dm_02 ' ; when any do begin end end
begin execute statement ' drop domain dm_03 ' ; when any do begin end end
begin execute statement ' drop domain dm_04 ' ; when any do begin end end
begin execute statement ' drop domain dm_05 ' ; when any do begin end end
begin execute statement ' drop domain dm_06 ' ; when any do begin end end
end
^
2022-01-24 20:27:02 +01:00
set term ; ^
2021-04-26 20:07:00 +02:00
commit ;
create domain dm_01 integer ;
create domain dm_02 integer not null ;
create domain dm_03 integer not null ;
create domain dm_04 integer not null ;
create domain dm_05 integer ;
create domain dm_06 integer ;
commit ;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2022-01-24 20:27:02 +01:00
2021-04-26 20:07:00 +02:00
recreate table test00 ( x integer ) ;
alter table test00 alter x set not null , add constraint t_pk primary key ( x ) ;
commit ;
- - should produce " SQLSTATE = 27000 / Column used in a PRIMARY constraint must be NOT NULL. " :
alter table test00 alter x drop not null ; - - correct error
commit ;
select ' After try to drop NN on FIELD, NN was added by ALTER <C> SET NOT NULL ' info_00
from rdb $ database ;
show table test00 ;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2022-01-24 20:27:02 +01:00
2021-04-26 20:07:00 +02:00
recreate table test01 ( x dm_01 not null ) ;
alter table test01 add constraint test01_pk primary key ( x ) ;
commit ;
- - should produce " SQLSTATE = 27000 / Column used in a PRIMARY constraint must be NOT NULL. " :
alter table test01 alter x drop not null ; - - correct error
commit ;
select ' After try to drop NN on FIELD, NN was added directly in CREATE TABLE (<C> not null) ' info_01
from rdb $ database ;
show table test01 ;
2022-01-24 20:27:02 +01:00
2021-04-26 20:07:00 +02:00
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2022-01-24 20:27:02 +01:00
2021-04-26 20:07:00 +02:00
recreate table test02 ( x dm_02 ) ;
alter table test02 add constraint test02_pk primary key ( x ) ;
commit ;
- - : : : ! ! ! : : :
- - This statement will NOT produce any error or warning but field will remain NOT null .
- - Confirmed by ASF in core - 4725 22.04 .2015
alter table test02 alter x drop not null ; - - correct pass
commit ;
select ' After try to drop NN on FIELD, NN was inherited from DOMAIN ' info_02
from rdb $ database ;
2022-01-24 20:27:02 +01:00
2021-04-26 20:07:00 +02:00
show table test02 ;
2022-01-24 20:27:02 +01:00
2021-04-26 20:07:00 +02:00
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2022-01-24 20:27:02 +01:00
2021-04-26 20:07:00 +02:00
recreate table test03 ( x dm_03 ) ;
alter table test03 add constraint test03_pk primary key ( x ) ;
commit ;
- - should produce " SQLSTATE = 42000 / Domain used in the PRIMARY KEY constraint of table TEST03 must be NOT NULL "
alter domain dm_03 drop not null ; - - incorrect pass ! ! !
commit ;
select ' After try to drop NN on DOMAIN but dependent table exists ' info_03
from rdb $ database ;
show domain dm_03 ;
show table test03 ;
2022-01-24 20:27:02 +01:00
2021-04-26 20:07:00 +02:00
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2022-01-24 20:27:02 +01:00
2021-04-26 20:07:00 +02:00
recreate table test04 ( x dm_04 not null ) ;
alter table test04 add constraint test04_pk primary key ( x ) ;
commit ;
2022-01-24 20:27:02 +01:00
- - : : : ! ! ! : : :
2021-04-26 20:07:00 +02:00
- - This statement will NOT produce any error or warning but field will remain NOT null .
- - Confirmed by ASF in core - 4725 22.04 .2015
alter table test04 alter x drop not null ; - - ASF : " incorrect error !!! " / / though it seems strange to me . . . : - /
commit ;
select ' After try to drop NN on FIELD based on not-null domain, but NN was also specified in the field DDL ' info_04
from rdb $ database ;
show table test04 ;
2022-01-24 20:27:02 +01:00
2021-04-26 20:07:00 +02:00
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2022-01-24 20:27:02 +01:00
2021-04-26 20:07:00 +02:00
recreate table test05 ( x dm_05 ) ;
commit ;
insert into test05 values ( 1 ) ;
insert into test05 values ( null ) ;
insert into test05 values ( 3 ) ;
commit ;
- - Should produce " SQLSTATE = 22006 / Cannot make field X of table TEST05 NOT NULL because there are NULLs present "
alter domain dm_05 set not null ;
commit ;
select ' After try to set NN on DOMAIN when at least one table exists with NULL in its data ' info_05
from rdb $ database ;
show domain dm_05 ;
2022-01-24 20:27:02 +01:00
2021-04-26 20:07:00 +02:00
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2022-01-24 20:27:02 +01:00
2021-04-26 20:07:00 +02:00
recreate table test06 ( x dm_06 ) ;
commit ;
insert into test06 values ( 1 ) ;
insert into test06 values ( 2 ) ;
insert into test06 values ( 3 ) ;
commit ;
- - Should pass :
alter domain dm_06 set not null ;
commit ;
select ' After try to set NN on DOMAIN when NO table exists with NULL in its data ' info_06
from rdb $ database ;
show domain dm_06 ;
- - Should produce " SQLSTATE = 23000 / validation error for column " TEST06 " . " X " , value " * * * null * * * " "
update test06 set x = null where x = 2 ;
2021-12-22 20:23:11 +01:00
"""
2021-04-26 20:07:00 +02:00
2022-01-24 20:27:02 +01:00
act = isql_act ( ' db ' , test_script )
2021-04-26 20:07:00 +02:00
2022-01-24 20:27:02 +01:00
expected_stdout = """
2021-04-26 20:07:00 +02:00
INFO_00 After try to drop NN on FIELD , NN was added by ALTER < C > SET NOT NULL
X INTEGER Not Null
CONSTRAINT T_PK :
Primary key ( X )
2022-01-24 20:27:02 +01:00
2021-04-26 20:07:00 +02:00
INFO_01 After try to drop NN on FIELD , NN was added directly in CREATE TABLE ( < C > not null )
X ( DM_01 ) INTEGER Not Null
CONSTRAINT TEST01_PK :
Primary key ( X )
2022-01-24 20:27:02 +01:00
2021-04-26 20:07:00 +02:00
INFO_02 After try to drop NN on FIELD , NN was inherited from DOMAIN
X ( DM_02 ) INTEGER Not Null
CONSTRAINT TEST02_PK :
Primary key ( X )
2022-01-24 20:27:02 +01:00
2021-04-26 20:07:00 +02:00
INFO_03 After try to drop NN on DOMAIN but dependent table exists
DM_03 INTEGER Not Null
2022-01-24 20:27:02 +01:00
X ( DM_03 ) INTEGER Not Null
2021-04-26 20:07:00 +02:00
CONSTRAINT TEST03_PK :
Primary key ( X )
2022-01-24 20:27:02 +01:00
2021-04-26 20:07:00 +02:00
INFO_04 After try to drop NN on FIELD based on not - null domain , but NN was also specified in the field DDL
X ( DM_04 ) INTEGER Not Null
CONSTRAINT TEST04_PK :
Primary key ( X )
2022-01-24 20:27:02 +01:00
2021-04-26 20:07:00 +02:00
INFO_05 After try to set NN on DOMAIN when at least one table exists with NULL in its data
DM_05 INTEGER Nullable
2022-01-24 20:27:02 +01:00
2021-04-26 20:07:00 +02:00
INFO_06 After try to set NN on DOMAIN when NO table exists with NULL in its data
DM_06 INTEGER Not Null
2021-12-22 20:23:11 +01:00
"""
2022-01-24 20:27:02 +01:00
expected_stderr = """
2021-04-26 20:07:00 +02:00
Statement failed , SQLSTATE = 27000
unsuccessful metadata update
- ALTER TABLE TEST00 failed
- action cancelled by trigger ( 2 ) to preserve data integrity
- Column used in a PRIMARY constraint must be NOT NULL .
Statement failed , SQLSTATE = 27000
unsuccessful metadata update
- ALTER TABLE TEST01 failed
- action cancelled by trigger ( 2 ) to preserve data integrity
- Column used in a PRIMARY constraint must be NOT NULL .
Statement failed , SQLSTATE = 42000
unsuccessful metadata update
- ALTER DOMAIN DM_03 failed
- Domain used in the PRIMARY KEY constraint of table TEST03 must be NOT NULL
Statement failed , SQLSTATE = 22006
unsuccessful metadata update
- Cannot make field X of table TEST05 NOT NULL because there are NULLs present
Statement failed , SQLSTATE = 23000
validation error for column " TEST06 " . " X " , value " *** null *** "
2021-12-22 20:23:11 +01:00
"""
2021-04-26 20:07:00 +02:00
@pytest.mark.version ( ' >=3.0 ' )
2022-01-24 20:27:02 +01:00
def test_1 ( act : Action ) :
act . expected_stdout = expected_stdout
act . expected_stderr = expected_stderr
act . execute ( )
assert ( act . clean_stderr == act . clean_expected_stderr and
act . clean_stdout == act . clean_expected_stdout )
2021-04-26 20:07:00 +02:00