Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
uffd2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Operate
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
thies
uffd2
Commits
e3366e35
Commit
e3366e35
authored
3 years ago
by
Julian
Browse files
Options
Downloads
Patches
Plain Diff
Enable foreign key support for SQLite
parent
1a8960d4
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
uffd/__init__.py
+3
-1
3 additions, 1 deletion
uffd/__init__.py
uffd/database.py
+18
-1
18 additions, 1 deletion
uffd/database.py
with
21 additions
and
2 deletions
uffd/__init__.py
+
3
−
1
View file @
e3366e35
...
...
@@ -13,7 +13,7 @@ except ImportError:
from
werkzeug.exceptions
import
InternalServerError
,
Forbidden
from
flask_migrate
import
Migrate
from
uffd.database
import
db
,
SQLAlchemyJSON
from
uffd.database
import
db
,
SQLAlchemyJSON
,
customize_db_engine
from
uffd.template_helper
import
register_template_helper
from
uffd.navbar
import
setup_navbar
from
uffd.secure_redirect
import
secure_local_redirect
...
...
@@ -79,6 +79,8 @@ def create_app(test_config=None): # pylint: disable=too-many-locals,too-many-sta
db
.
init_app
(
app
)
Migrate
(
app
,
db
,
render_as_batch
=
True
,
directory
=
os
.
path
.
join
(
app
.
root_path
,
'
migrations
'
))
with
app
.
app_context
():
customize_db_engine
(
db
.
engine
)
for
module
in
[
user
,
selfservice
,
role
,
mail
,
session
,
csrf
,
mfa
,
oauth2
,
services
,
rolemod
,
api
,
signup
,
invite
]:
for
bp
in
module
.
bp
:
...
...
This diff is collapsed.
Click to expand it.
uffd/database.py
+
18
−
1
View file @
e3366e35
from
collections
import
OrderedDict
from
sqlalchemy
import
MetaData
from
sqlalchemy
import
MetaData
,
event
from
flask_sqlalchemy
import
SQLAlchemy
from
flask.json
import
JSONEncoder
...
...
@@ -15,6 +15,23 @@ metadata = MetaData(naming_convention=convention)
db
=
SQLAlchemy
(
metadata
=
metadata
)
def
enable_sqlite_foreign_key_support
(
dbapi_connection
,
connection_record
):
# pylint: disable=unused-argument
cursor
=
dbapi_connection
.
cursor
()
cursor
.
execute
(
'
PRAGMA foreign_keys=ON
'
)
cursor
.
close
()
# We want to enable SQLite foreign key support for app and test code, but not
# for migrations.
# The common way to add the handler to the Engine class (so it applies to all
# instances) would also affect the migrations. With flask_sqlalchemy v2.4 and
# newer we could overwrite SQLAlchemy.create_engine and add our handler there.
# However Debian Buster and Bullseye ship v2.1, so we do this here and call
# this function in create_app.
def
customize_db_engine
(
engine
):
if
engine
.
name
==
'
sqlite
'
:
event
.
listen
(
engine
,
'
connect
'
,
enable_sqlite_foreign_key_support
)
class
SQLAlchemyJSON
(
JSONEncoder
):
def
default
(
self
,
o
):
if
isinstance
(
o
,
db
.
Model
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment