mailarchive of the ptxdist mailing list
 help / color / mirror / Atom feed
From: Bruno Thomsen <bruno.thomsen@gmail.com>
To: ptxdist@pengutronix.de, Bruno Thomsen <bruno.thomsen@gmail.com>,
	Bruno Thomsen <bth@kamstrup.com>
Subject: Re: [ptxdist] [PATCH] sqlite: enable foreign keys by default
Date: Fri, 21 Feb 2020 12:20:33 +0100	[thread overview]
Message-ID: <CAH+2xPDzysTPKfj+JEQyC3Ggg0sUcUtQSH=91H=z8Nf3gJipFg@mail.gmail.com> (raw)
In-Reply-To: <20200221072308.GB4597@pengutronix.de>

Hi Michael

Den fre. 21. feb. 2020 kl. 08.23 skrev Michael Olbrich
<m.olbrich@pengutronix.de>:
>
> On Sat, Feb 15, 2020 at 01:52:05PM +0100, Bruno Thomsen wrote:
> > Support for foreign_keys pragma was already enabled at
> > compile-time, but it was not enabled by default on new
> > database connections. It's still possible to turn off
> > enforcement at runtime.
> > This makes it easier to work on databases that uses
> > this feature without causing them to end up in an
> > invalid state.
>
> Hmm, what's the impact of this? What are the performance implications? And
> what's the default in other distros? I mean, if the default is generally
> off, then I don't expect any existing software to disable this.

Fedora 31 has =0, I don't know about other distros,
but you can test it fast with an in-memory database.

$ sqlite3
sqlite> pragma foreign_keys;
1
sqlite> .exit

You will only be affected by this change if your database schema contain
"FOREIGN KEY" SQL syntax. Features is already compiled into the binary
so there will not be any performance implications, and software does not
need to disable it to work.

If this change breaks your software it's because you had a bug or a bad
database access design. This feature is used when you have multiple
tables in a database that relates to each other and you what to ensure
consistency between tables.

Example:

dummy.sql
---------->8----------
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE device(
  address INTEGER PRIMARY KEY,
  description TEXT
);
INSERT INTO device VALUES(1001,"Lorem ipsum dolor sit amet,
consectetuer adipiscing elit");
INSERT INTO device VALUES(2002,"Typi non habent claritatem insitam");
CREATE TABLE event(
  address INTEGER,
  data BLOB NOT NULL,
  FOREIGN KEY(address) REFERENCES device(address) ON DELETE CASCADE
);
INSERT INTO event VALUES(1001,X'AABBCCDDEEFF');
INSERT INTO event VALUES(1001,X'112233445566');
INSERT INTO event VALUES(2002,X'112233445566778899');
INSERT INTO event VALUES(2002,X'112233445566101010');
COMMIT;
---------->8----------

Load database schema with dummy data.
$ sqlite3 device.sqlite < dummy.sql

Calling this command can result in 2 states in the database depending on
foreign_keys pragma.
$ sqlite3 device.sqlite "DELETE FROM device WHERE address=1001;"

foreign_keys=on result in cascade delete on event table:
---------->8----------
$ sqlite3 device.sqlite .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE device(
  address INTEGER PRIMARY KEY,
  description TEXT
);
INSERT INTO device VALUES(2002,'Typi non habent claritatem insitam');
CREATE TABLE event(
  address INTEGER,
  data BLOB NOT NULL,
  FOREIGN KEY(address) REFERENCES device(address) ON DELETE CASCADE
);
INSERT INTO event VALUES(2002,X'112233445566778899');
INSERT INTO event VALUES(2002,X'112233445566101010');
COMMIT;
---------->8----------

foreign_keys=off result in dangling rows in event table (inconsistency):
---------->8----------
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE device(
  address INTEGER PRIMARY KEY,
  description TEXT
);
INSERT INTO device VALUES(2002,"Typi non habent claritatem insitam");
CREATE TABLE event(
  address INTEGER,
  data BLOB NOT NULL,
  FOREIGN KEY(address) REFERENCES device(address) ON DELETE CASCADE
);
INSERT INTO event VALUES(1001,X'AABBCCDDEEFF');
INSERT INTO event VALUES(1001,X'112233445566');
INSERT INTO event VALUES(2002,X'112233445566778899');
INSERT INTO event VALUES(2002,X'112233445566101010');
COMMIT;
---------->8----------

The patch could be changed to a new menuconfig option, e.g.
1) Text field with extra compile defines
2) Specific config parameter
3) Generic config parameter (enable modern features)

What kind of change do you prefer? So I don't have to keep a forked edition :)

/Bruno

_______________________________________________
ptxdist mailing list
ptxdist@pengutronix.de

  reply	other threads:[~2020-02-21 11:20 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-15 12:52 Bruno Thomsen
2020-02-21  7:23 ` Michael Olbrich
2020-02-21 11:20   ` Bruno Thomsen [this message]
2020-02-21 11:41     ` Uwe Kleine-König

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+2xPDzysTPKfj+JEQyC3Ggg0sUcUtQSH=91H=z8Nf3gJipFg@mail.gmail.com' \
    --to=bruno.thomsen@gmail.com \
    --cc=bth@kamstrup.com \
    --cc=ptxdist@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox