Add the SQLITE_CHANGESETAPPLY_FKNOACTION flag to sqlite3session.h, for passing to sqlite3changeset_apply_v2() to cause all foreign key constraints to behave as if they were declared NO ACTION.

FossilOrigin-Name: fc9f82ea084159eaf3dd1757b96d17d1201b00c4e06455a7dcd8067172b25f28
This commit is contained in:
dan 2023-10-20 17:06:39 +00:00
parent 90e4a3b7fc
commit 17c3408e0e
8 changed files with 133 additions and 15 deletions

View File

@ -0,0 +1,88 @@
# 2023 October 20
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.
#
if {![info exists testdir]} {
set testdir [file join [file dirname [info script]] .. .. test]
}
source [file join [file dirname [info script]] session_common.tcl]
source $testdir/tester.tcl
ifcapable !session {finish_test; return}
set testprefix sessionnoact
do_execsql_test 1.0 {
CREATE TABLE p1(a INTEGER PRIMARY KEY, b, c UNIQUE);
INSERT INTO p1 VALUES(1, 1, 'one');
INSERT INTO p1 VALUES(2, 2, 'two');
INSERT INTO p1 VALUES(3, 3, 'three');
INSERT INTO p1 VALUES(4, 4, 'four');
}
db_save
set C [changeset_from_sql {
DELETE FROM p1 WHERE a=2;
UPDATE p1 SET c='six' WHERE a=3;
INSERT INTO p1 VALUES(5, 5, 'two');
INSERT INTO p1 VALUES(6, 6, 'three');
}]
db_restore_and_reopen
do_execsql_test 1.1 {
CREATE TABLE c1(x INTEGER PRIMARY KEY, y,
FOREIGN KEY(y) REFERENCES p1(c) ON DELETE CASCADE ON UPDATE SET NULL
);
INSERT INTO c1 VALUES(10, 'one');
INSERT INTO c1 VALUES(20, 'two');
INSERT INTO c1 VALUES(30, 'three');
INSERT INTO c1 VALUES(40, 'four');
}
db_save
do_execsql_test 1.2 {
PRAGMA foreign_keys = 1;
}
set ::nConflict 0
proc conflict {args} {
incr ::nConflict
return "OMIT"
}
sqlite3changeset_apply_v2 db $C conflict
do_execsql_test 1.3 {
SELECT * FROM c1
} {
10 one
30 {}
40 four
}
db_restore_and_reopen
sqlite3changeset_apply_v2 -noaction db $C conflict
do_execsql_test 1.4 {
SELECT * FROM c1
} {
10 one
20 two
30 three
40 four
}
finish_test

View File

@ -5322,11 +5322,22 @@ int sqlite3changeset_apply_v2(
sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
int bInv = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
int rc = sessionChangesetStart(&pIter, 0, 0, nChangeset, pChangeset, bInv, 1);
u64 savedFlag = db->flags & SQLITE_FkNoAction;
if( flags & SQLITE_CHANGESETAPPLY_FKNOACTION ){
db->flags |= ((u64)SQLITE_FkNoAction);
}
if( rc==SQLITE_OK ){
rc = sessionChangesetApply(
db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
);
}
if( (flags & SQLITE_CHANGESETAPPLY_FKNOACTION) && savedFlag==0 ){
assert( db->flags & SQLITE_FkNoAction );
db->flags &= ((u64)SQLITE_FkNoAction);
}
return rc;
}

View File

@ -1318,10 +1318,17 @@ int sqlite3changeset_apply_v2(
** <li>an insert change if all fields of the conflicting row match
** the row being inserted.
** </ul>
**
** <dt>SQLITE_CHANGESETAPPLY_FKNOACTION <dd>
** If this flag it set, then all foreign key constraints in the target
** database behave as if they were declared with "ON UPDATE NO ACTION ON
** DELETE NO ACTION", even if they are actually CASCADE, RESTRICT, SET NULL
** or SET DEFAULT.
*/
#define SQLITE_CHANGESETAPPLY_NOSAVEPOINT 0x0001
#define SQLITE_CHANGESETAPPLY_INVERT 0x0002
#define SQLITE_CHANGESETAPPLY_IGNORENOOP 0x0004
#define SQLITE_CHANGESETAPPLY_FKNOACTION 0x0008
/*
** CAPI3REF: Constants Passed To The Conflict Handler

View File

@ -812,9 +812,12 @@ static int SQLITE_TCLAPI testSqlite3changesetApply(
while( objc>1 ){
const char *z1 = Tcl_GetString(objv[1]);
int n = strlen(z1);
if( n>1 && n<=12 && 0==sqlite3_strnicmp("-nosavepoint", z1, n) ){
if( n>3 && n<=12 && 0==sqlite3_strnicmp("-nosavepoint", z1, n) ){
flags |= SQLITE_CHANGESETAPPLY_NOSAVEPOINT;
}
else if( n>3 && n<=9 && 0==sqlite3_strnicmp("-noaction", z1, n) ){
flags |= SQLITE_CHANGESETAPPLY_FKNOACTION;
}
else if( n>2 && n<=7 && 0==sqlite3_strnicmp("-invert", z1, n) ){
flags |= SQLITE_CHANGESETAPPLY_INVERT;
}

View File

@ -1,5 +1,5 @@
C Simplifications\sand\soptimizations\sto\sthe\sExpr\sobject\sduplication\slogic.\nThe\s5x\smultiplier\scrutch\sfrom\s[f371e4c0f8ea73ae]\sis\sstill\spresent.\s\sMore\nfixes\sare\sstill\sneeded.
D 2023-10-20T15:47:30.360
C Add\sthe\sSQLITE_CHANGESETAPPLY_FKNOACTION\sflag\sto\ssqlite3session.h,\sfor\spassing\sto\ssqlite3changeset_apply_v2()\sto\scause\sall\sforeign\skey\sconstraints\sto\sbehave\sas\sif\sthey\swere\sdeclared\sNO\sACTION.
D 2023-10-20T17:06:39.472
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@ -544,6 +544,7 @@ F ext/session/sessionfault2.test b0d6a7c1d7398a7e800d84657404909c7d385965ea8576d
F ext/session/sessionfault3.test 7c7547202775de268f3fe6f074c4d0d165151829710b4e64f90d4a01645ba9e7
F ext/session/sessioninvert.test 04075517a9497a80d39c495ba6b44f3982c7371129b89e2c52219819bc105a25
F ext/session/sessionmem.test f2a735db84a3e9e19f571033b725b0b2daf847f3f28b1da55a0c1a4e74f1de09
F ext/session/sessionnoact.test 61d9ec369e5851fd7326c79ebb76df0b3dd7b21c0e2cb6d554eea3351b0a57de
F ext/session/sessionnoop.test a9366a36a95ef85f8a3687856ebef46983df399541174cb1ede2ee53b8011bc7
F ext/session/sessionnoop2.test de4672dce88464396ec9f30ed08c6c01643a69c53ae540fadbbf6d30642d64e8
F ext/session/sessionrebase.test 702378bdcb5062f1106e74457beca8797d09c113a81768734a58b197b5b334e2
@ -551,9 +552,9 @@ F ext/session/sessionrowid.test 85187c2f1b38861a5844868126f69f9ec62223a03449a98a
F ext/session/sessionsize.test 8fcf4685993c3dbaa46a24183940ab9f5aa9ed0d23e5fb63bfffbdb56134b795
F ext/session/sessionstat1.test b039e38e2ba83767b464baf39b297cc0b1cc6f3292255cb467ea7e12d0d0280c
F ext/session/sessionwor.test 6fd9a2256442cebde5b2284936ae9e0d54bde692d0f5fd009ecef8511f4cf3fc
F ext/session/sqlite3session.c ea28d07a280dc82efd4e98417a10b582058dc11a0f2a6ac3b194aa20d233d758
F ext/session/sqlite3session.h 4d1f69f1d8bfd4798e8f6431de301d17bb2e4097de2f77ca4dad494bb6c60dc0
F ext/session/test_session.c be0610a8ab717c3f9c898576d8b4c43e5eb75384a47fed0a0b22768067552778
F ext/session/sqlite3session.c 6da2db8b2d525a6fe0b6f451743dcb098f609fcc3733aa044406f2d28068aeb2
F ext/session/sqlite3session.h 4cf19a51975746d7cff2fdd74db8b769c570958e1c3639ac150d824ac1553b3e
F ext/session/test_session.c 73e9921205633a08c1497770beecf3dcbc9b826dd4358f7f8152f706eaac5d71
F ext/userauth/sqlite3userauth.h 7f3ea8c4686db8e40b0a0e7a8e0b00fac13aa7a3
F ext/userauth/user-auth.txt e6641021a9210364665fe625d067617d03f27b04
F ext/userauth/userauth.c 7f00cded7dcaa5d47f54539b290a43d2e59f4b1eb5f447545fa865f002fc80cb
@ -670,7 +671,7 @@ F src/dbstat.c 3b677254d512fcafd4d0b341bf267b38b235ccfddbef24f9154e19360fa22e43
F src/delete.c cb766727c78e715f9fb7ec8a7d03658ed2a3016343ca687acfcec9083cdca500
F src/expr.c 1755a8386bd8bb49b5c21cb33682608be33c07ce5df686eb2aa76983bc3a9520
F src/fault.c 460f3e55994363812d9d60844b2a6de88826e007
F src/fkey.c a7fcbf7e66d14dbb73cf49f31489ebf66d0e6006c62b95246924a3bae9f37b36
F src/fkey.c 360a9b644efc9e05746e0b5b6ccb4760fd039d287ebdf090723b9c97f6d163d9
F src/func.c e8d7b3587a225f4f1116f720b72090511fe9feb936e960bd26a053cea6a17a63
F src/global.c 29f56a330ed9d1b5cd9b79ac0ca36f97ac3afc730ff8bfa987b0db9e559d684d
F src/hash.c 9ee4269fb1d6632a6fecfb9479c93a1f29271bddbbaf215dd60420bcb80c7220
@ -723,7 +724,7 @@ F src/shell.c.in cf0a3387c5bb05ca2fe6073fa7df21aaa11e749ca5b3846b80b586a447e728a
F src/sqlite.h.in 60e5698417e674f44be17d5ceeb4c2d395ccedba18d6de05435dec0135d9eff1
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
F src/sqlite3ext.h 2f30b2671f4c03cd27a43f039e11251391066c97d11385f5f963bb40b03038ac
F src/sqliteInt.h 849004dd6f4315e05a8ec892999b0972484eb5438d122adc9444ed854bd905ce
F src/sqliteInt.h cf6646e8694a63749096e1f086767a2c1920dca9848ec2dbe9f7bfb961d322ef
F src/sqliteLimit.h 33b1c9baba578d34efe7dfdb43193b366111cdf41476b1e82699e14c11ee1fb6
F src/status.c 160c445d7d28c984a0eae38c144f6419311ed3eace59b44ac6dafc20db4af749
F src/table.c 0f141b58a16de7e2fbe81c308379e7279f4c6b50eb08efeec5892794a0ba30d1
@ -2133,8 +2134,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P a0cf7e24f928183866ac54f0d6cd83c859d487a19dc87572ab0188d90d2ff87d
R 984c0c092b0039888c79b896b5903542
U drh
Z 2bc926f7992297727e315207ed48e625
P 56142a78163b755f16afc05201f623a7a19d9a4b0620a67f7fa20d2a965a288d
R 570cd03c70d6d0573d22a0c9506a275a
U dan
Z 570f4c231d8132d7b5260691ca2afd34
# Remove this line to create a well-formed Fossil manifest.

View File

@ -1 +1 @@
56142a78163b755f16afc05201f623a7a19d9a4b0620a67f7fa20d2a965a288d
fc9f82ea084159eaf3dd1757b96d17d1201b00c4e06455a7dcd8067172b25f28

View File

@ -853,7 +853,7 @@ static int fkParentIsModified(
*/
static int isSetNullAction(Parse *pParse, FKey *pFKey){
Parse *pTop = sqlite3ParseToplevel(pParse);
if( pTop->pTriggerPrg ){
if( pTop->pTriggerPrg && 0==(pTop->db->flags & SQLITE_FkNoAction) ){
Trigger *p = pTop->pTriggerPrg->pTrigger;
if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
|| (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
@ -1052,6 +1052,8 @@ void sqlite3FkCheck(
}
if( regOld!=0 ){
int eAction = pFKey->aAction[aChange!=0];
if( (db->flags & SQLITE_FkNoAction) ) eAction = OE_None;
fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
/* If this is a deferred FK constraint, or a CASCADE or SET NULL
** action applies, then any foreign key violations caused by
@ -1167,7 +1169,11 @@ int sqlite3FkRequired(
/* Check if any parent key columns are being modified. */
for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
if( fkParentIsModified(pTab, p, aChange, chngRowid) ){
if( p->aAction[1]!=OE_None ) return 2;
if( (pParse->db->flags & SQLITE_FkNoAction)==0
&& p->aAction[1]!=OE_None
){
return 2;
}
bHaveFK = 1;
}
}
@ -1217,6 +1223,7 @@ static Trigger *fkActionTrigger(
int iAction = (pChanges!=0); /* 1 for UPDATE, 0 for DELETE */
action = pFKey->aAction[iAction];
if( (db->flags & SQLITE_FkNoAction) ) action = OE_None;
if( action==OE_Restrict && (db->flags & SQLITE_DeferFKs) ){
return 0;
}

View File

@ -1845,6 +1845,7 @@ struct sqlite3 {
/* the count using a callback. */
#define SQLITE_CorruptRdOnly HI(0x00002) /* Prohibit writes due to error */
#define SQLITE_ReadUncommit HI(0x00004) /* READ UNCOMMITTED in shared-cache */
#define SQLITE_FkNoAction HI(0x00008) /* Treat all FK as NO ACTION */
/* Flags used only if debugging */
#ifdef SQLITE_DEBUG