Skip to content

sqlite: enable foreign key constraints by default - #54777

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
tniessen:sqlite-foreign-keys
Oct 6, 2024
Merged

sqlite: enable foreign key constraints by default#54777
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
tniessen:sqlite-foreign-keys

Conversation

@tniessen

Copy link
Copy Markdown
Member

For historical reasons and to maintain compatibility with legacy database schemas, SQLite does not enable foreign key constraints by default. For new applications, however, this behavior is undesirable. Currently, any application that wishes to use foreign keys must use

PRAGMA foreign_keys =ON;

to explicitly enable enforcement of such constraints.

This commit changes the behavior of the SQLite API built into Node.js to enable foreign key constraints by default. This behavior can be overridden by users to maintain compatibility with legacy database schemas.

I decided against using the compile-time option to set the default behavior to avoid limiting compatibility with third-party code that links against node's copy of SQLite.

For historical reasons and to maintain compatibibility with legacy
database schemas, SQLite does not enable foreign key constraints by
default. For new applications, however, this behavior is undesirable.
Currently, any application that wishes to use foreign keys must use
PRAGMA foreign_keys = ON;
to explicitly enable enforcement of such constraints.
This commit changes the behavior of the SQLite API built into Node.js
to enable foreign key constraints by default. This behavior can be
overridden by users to maintain compatibility with legacy database
schemas.
@tniessentniessen added the sqlite Issues and PRs related to the SQLite subsystem. label Sep 4, 2024
@nodejs-github-botnodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Sep 4, 2024
@codecov

codecovBot commented Sep 5, 2024

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 80.00000% with 4 lines in your changes missing coverage. Please review.

Project coverage is 87.61%. Comparing base (80a989f) to head (2df5ab9).
Report is 653 commits behind head on main.

Files with missing linesPatch %Lines
src/node_sqlite.cc80.00%1 Missing and 3 partials ⚠️
Additional details and impacted files
@@ Coverage Diff @@## main #54777 +/- ##
==========================================
- Coverage 87.62% 87.61% -0.02% 
==========================================
Files 650 650 Lines 182878 182957 +79 Branches 35387 35403 +16 ==========================================
+ Hits 160242 160289 +47 - Misses 15921 15923 +2 - Partials 6715 6745 +30 
Files with missing linesCoverage Δ
src/node_sqlite.h0.00% <ø> (ø)
src/node_sqlite.cc84.00% <80.00%> (+<0.01%)⬆️

... and 34 files with indirect coverage changes

@cjihrigcjihrig left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Do you think we should do something similar for WAL mode?

@tniessen

Copy link
Copy Markdown
MemberAuthor

I personally don't think so. WAL mode is often preferable but also has significant disadvantages, and the SQLite developers recommend adjusting other configuration options as well when using WAL mode (e.g., PRAGMA synchronous).

Comment threadsrc/node_sqlite.h
v8::Local<v8::String> location,
bool open);
bool open,
bool enable_foreign_keys_on_open);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non-blocking nit: totally not a fan of bool arguments like this. Would much prefer having either an options struct or enums for this purpose.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasnell I agree, I am planning to add at least one more option and to then wrap the *_on_open options in a struct.

@tniessentniessen added the request-ci Add this label to start a Jenkins CI on a PR. label Oct 6, 2024
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Oct 6, 2024
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@tniessentniessen added the commit-queue Add this label to land a pull request using GitHub Actions. label Oct 6, 2024
@nodejs-github-botnodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Oct 6, 2024
@nodejs-github-bot
nodejs-github-bot merged commit a49abec into nodejs:mainOct 6, 2024
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in a49abec

aduh95 pushed a commit that referenced this pull request Oct 9, 2024
For historical reasons and to maintain compatibibility with legacy
database schemas, SQLite does not enable foreign key constraints by
default. For new applications, however, this behavior is undesirable.
Currently, any application that wishes to use foreign keys must use
PRAGMA foreign_keys = ON;
to explicitly enable enforcement of such constraints.
This commit changes the behavior of the SQLite API built into Node.js
to enable foreign key constraints by default. This behavior can be
overridden by users to maintain compatibility with legacy database
schemas.
PR-URL: #54777
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@aduh95aduh95 mentioned this pull request Oct 9, 2024
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++Issues and PRs that require attention from people who are familiar with C++.needs-ciPRs that need a full CI run.sqliteIssues and PRs related to the SQLite subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants

@tniessen@nodejs-github-bot@jasnell@anonrig@targos@cjihrig@atlowChemi@marco-ippolito