2021-04-26 20:07:00 +02:00
#coding:utf-8
2022-01-25 22:55:48 +01:00
"""
ID : issue - 5733
ISSUE : 5733
TITLE : Support GENERATED ALWAYS identity columns and OVERRIDE clause
DESCRIPTION :
JIRA : CORE - 5463
2022-02-02 15:46:19 +01:00
FBTEST : bugs . core_5463
2023-07-18 10:54:37 +02:00
NOTES :
[ 18.07 .2023 ] pzotov
Adjusted STDERR text after changes related to fixed gh - 7638
( " OVERRIDING USER VALUE should be allowed for GENERATED ALWAYS AS IDENTITY. " ) , see :
https : / / github . com / FirebirdSQL / firebird / commit / 844631 b0ee473fd94dab61558f146a8268c0967b
https : / / github . com / FirebirdSQL / firebird / commit / 43 c5426b48b2647c795143fcaa4a8e20c57fc9c8
Old text :
" OVERRIDING SYSTEM VALUE should be used to override the value of an identity column defined as ' GENERATED ALWAYS ' in table/view ... "
New text ( after #7638 was fixed):
" OVERRIDING clause should be used when an identity column defined as ' GENERATED ALWAYS ' is present in the INSERT ' s field list for table table/view ... "
2022-01-25 22:55:48 +01:00
"""
2021-04-26 20:07:00 +02:00
2022-01-25 22:55:48 +01:00
import pytest
from firebird . qa import *
2021-04-26 20:07:00 +02:00
2022-01-25 22:55:48 +01:00
db = db_factory ( )
2021-04-26 20:07:00 +02:00
2022-01-25 22:55:48 +01:00
test_script = """
2021-04-26 20:07:00 +02:00
set list on ;
recreate table test_always (
id_always int generated ALWAYS as identity ( start with 11 increment 22 ) unique
) ;
recreate table test_default (
id_default int generated BY DEFAULT as identity ( start with - 11 increment - 22 ) unique
) ;
insert into test_default default values returning id_default ; - - expected : - 33
insert into test_always default values returning id_always ; - - expected : 33
2022-01-25 22:55:48 +01:00
- - Comments taken from doc \\sql . extensions \\README . identity_columns . txt
2021-04-26 20:07:00 +02:00
- - == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == ==
- - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
- - Identity columns are implicitly NOT NULL
- - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
- - Statement failed , SQLSTATE = 42000
2023-07-18 10:54:37 +02:00
- - : : : NB : : : Error text was changed for following statement .
- - 1. For FB 5. x - since 28 - JUN - 2023 ( old text was up to build 5.0 .0 .1088 , new appeared since 5.0 .0 .1093 )
- - 2. For FB 4. x - since 08 - JUL - 2023 ( old text was up to build 4.0 .3 .2958 , new appeared since 4.0 .3 .2963 )
- - OLD TEXT : OVERRIDING SYSTEM VALUE should be used to override the value of an identity column defined as ' GENERATED ALWAYS ' in table / view TEST_ALWAYS
- - NEW TEXT : OVERRIDING clause should be used when an identity column defined as ' GENERATED ALWAYS ' is present in the INSERT ' s field list for table table/view TEST_ALWAYS
2021-04-26 20:07:00 +02:00
insert into test_always ( id_always ) values ( null ) ;
- - Statement failed , SQLSTATE = 23000
- - validation error for column " TEST " . " id_default " , value " *** null *** "
insert into test_default ( id_default ) values ( null ) ;
- - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
- - BY DEFAULT identity columns can be overriden in INSERT statements
- - just specifying the value in the values list
- - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
insert into test_default ( id_default ) values ( - 7654321 ) returning id_default ;
- - insert into test_default default values returning id_default ; == > - 99 !
- - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
- - However , for ALWAYS identity columns that is not allowed .
2022-01-25 22:55:48 +01:00
- - To use the value passed in the INSERT statement for an ALWAYS column , you should pass
2021-04-26 20:07:00 +02:00
- - OVERRIDING SYSTEM VALUE :
- - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
- - 1 ) check that OVERRIDING SYSTEM VALUE clause does not affect on NOT_NULL constraint :
- - Statement failed , SQLSTATE = 23000
- - validation error for column " test_always " . " ID_ALWAYS " , value " *** null *** "
insert into test_always ( id_always ) overriding system value values ( null ) returning id_always ;
- - 2 ) check ability to override system value by providing our own :
insert into test_always ( id_always ) overriding system value values ( 7654321 ) returning id_always ;
- - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2022-01-25 22:55:48 +01:00
- - OVERRIDING also supports a subclause to be used with BY DEFAULT columns ,
2021-04-26 20:07:00 +02:00
- - to ignore the value passed in INSERT and use the defined sequence :
- - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
- - 1 ) check that attempt to pass NULL into VALUES list has no effect :
insert into test_default ( id_default ) overriding user value values ( null ) returning id_default ; - - expected : - 99
- - 2 ) check ability to pass allowed value but it also must be overriden by default one :
insert into test_default ( id_default ) overriding user value values ( - 7654322 ) returning id_default ; - - expected : - 121
2021-12-22 20:23:11 +01:00
"""
2021-04-26 20:07:00 +02:00
2023-07-18 10:54:37 +02:00
act = isql_act ( ' db ' , test_script )
2021-04-26 20:07:00 +02:00
2022-01-25 22:55:48 +01:00
expected_stdout = """
2021-04-26 20:07:00 +02:00
ID_DEFAULT - 11
ID_ALWAYS 11
ID_DEFAULT - 7654321
ID_ALWAYS 7654321
ID_DEFAULT - 33
ID_DEFAULT - 55
2021-12-22 20:23:11 +01:00
"""
2022-01-25 22:55:48 +01:00
expected_stderr = """
2021-04-26 20:07:00 +02:00
Statement failed , SQLSTATE = 42000
2023-07-18 10:54:37 +02:00
OVERRIDING clause should be used when an identity column defined as ' GENERATED ALWAYS ' is present in the INSERT ' s field list for table table/view TEST_ALWAYS
2021-04-26 20:07:00 +02:00
Statement failed , SQLSTATE = 23000
validation error for column " TEST_DEFAULT " . " ID_DEFAULT " , value " *** null *** "
Statement failed , SQLSTATE = 23000
validation error for column " TEST_ALWAYS " . " ID_ALWAYS " , value " *** null *** "
2021-12-22 20:23:11 +01:00
"""
2021-04-26 20:07:00 +02:00
@pytest.mark.version ( ' >=4.0 ' )
2022-01-25 22:55:48 +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