Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlite: enable foreign key constraints by default #54777

Merged
merged 1 commit into from
Oct 6, 2024

Conversation

tniessen
Copy link
Member

@tniessen tniessen commented Sep 4, 2024

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.
@tniessen tniessen added the sqlite Issues and PRs related to the SQLite subsystem. label Sep 4, 2024
@nodejs-github-bot nodejs-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
Copy link

codecov bot commented Sep 5, 2024

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 lines Patch % Lines
src/node_sqlite.cc 80.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 lines Coverage Δ
src/node_sqlite.h 0.00% <ø> (ø)
src/node_sqlite.cc 84.00% <80.00%> (+<0.01%) ⬆️

... and 34 files with indirect coverage changes

Copy link
Contributor

@cjihrig cjihrig left a comment

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
Member Author

tniessen commented Sep 5, 2024

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).

@@ -21,7 +21,8 @@ class DatabaseSync : public BaseObject {
DatabaseSync(Environment* env,
v8::Local<v8::Object> object,
v8::Local<v8::String> location,
bool open);
bool open,
bool enable_foreign_keys_on_open);
Copy link
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
Member Author

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.

@tniessen tniessen added the request-ci Add this label to start a Jenkins CI on a PR. label Oct 6, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Oct 6, 2024
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@tniessen tniessen added the commit-queue Add this label to land a pull request using GitHub Actions. label Oct 6, 2024
@nodejs-github-bot nodejs-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:main Oct 6, 2024
70 checks passed
@nodejs-github-bot
Copy link
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>
@aduh95 aduh95 mentioned this pull request Oct 9, 2024
louwers added a commit to louwers/node that referenced this pull request Nov 2, 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: nodejs#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>
@marco-ippolito marco-ippolito added the dont-land-on-v20.x PRs that should not land on the v20.x-staging branch and should not be released in v20.x. label Nov 16, 2024
Sign up for free to 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++. dont-land-on-v20.x PRs that should not land on the v20.x-staging branch and should not be released in v20.x. needs-ci PRs that need a full CI run. sqlite Issues and PRs related to the SQLite subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants