Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Package registry
Container registry
Operate
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
videoag
backend
Commits
a9b7e0fb
Commit
a9b7e0fb
authored
5 months ago
by
Simon Künzel
Browse files
Options
Downloads
Patches
Plain Diff
Rework when drift check is skipped
parent
bce9836c
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
api/config/api_example_config.py
+5
-0
5 additions, 0 deletions
api/config/api_example_config.py
common_py/src/videoag_common/database/database.py
+22
-15
22 additions, 15 deletions
common_py/src/videoag_common/database/database.py
with
27 additions
and
15 deletions
api/config/api_example_config.py
+
5
−
0
View file @
a9b7e0fb
...
@@ -66,8 +66,13 @@ DATABASE = {
...
@@ -66,8 +66,13 @@ DATABASE = {
"
user
"
:
"
videoag
"
,
"
user
"
:
"
videoag
"
,
"
password
"
:
"
videoag
"
,
"
password
"
:
"
videoag
"
,
"
database
"
:
"
videoag
"
,
"
database
"
:
"
videoag
"
,
"
connect_args
"
:
{},
"
check_drift
"
:
True
,
"
auto_migration
"
:
True
,
"
auto_migration
"
:
True
,
"
ignore_no_connection
"
:
False
,
"
ignore_no_connection
"
:
False
,
"
skip_drift_check_when_no_connection
"
:
False
,
"
max_read_attempts
"
:
2
,
"
max_write_attempts
"
:
2
},
},
"
log_all_statements
"
:
True
# TODO
"
log_all_statements
"
:
True
# TODO
}
}
...
...
This diff is collapsed.
Click to expand it.
common_py/src/videoag_common/database/database.py
+
22
−
15
View file @
a9b7e0fb
...
@@ -44,9 +44,9 @@ class Database:
...
@@ -44,9 +44,9 @@ class Database:
check_drift
=
dict_get_check_type
(
engine_config
,
"
check_drift
"
,
bool
,
True
)
check_drift
=
dict_get_check_type
(
engine_config
,
"
check_drift
"
,
bool
,
True
)
auto_migration
=
dict_get_check_type
(
engine_config
,
"
auto_migration
"
,
bool
,
False
)
auto_migration
=
dict_get_check_type
(
engine_config
,
"
auto_migration
"
,
bool
,
False
)
ignore
_no_connection
=
dict_get_check_type
(
engine_config
,
"
ignore
_no_connection
"
,
bool
,
False
)
skip_drift_check_when
_no_connection
=
dict_get_check_type
(
engine_config
,
"
skip_drift_check_when
_no_connection
"
,
bool
,
False
)
if
os
.
environ
.
get
(
"
API_
IGNORE_NO_DB
_CONNECTION
"
,
"
false
"
).
lower
()
==
"
true
"
:
if
os
.
environ
.
get
(
"
API_
SKIP_DRIFT_CHECK_WHEN_NO
_CONNECTION
"
,
"
false
"
).
lower
()
==
"
true
"
:
ignore
_no_connection
=
True
skip_drift_check_when
_no_connection
=
True
self
.
_max_read_attempts
=
dict_get_check_type
(
engine_config
,
"
max_read_attempts
"
,
int
,
2
)
self
.
_max_read_attempts
=
dict_get_check_type
(
engine_config
,
"
max_read_attempts
"
,
int
,
2
)
self
.
_max_write_attempts
=
dict_get_check_type
(
engine_config
,
"
max_write_attempts
"
,
int
,
2
)
self
.
_max_write_attempts
=
dict_get_check_type
(
engine_config
,
"
max_write_attempts
"
,
int
,
2
)
...
@@ -81,18 +81,7 @@ class Database:
...
@@ -81,18 +81,7 @@ class Database:
)
)
if
check_drift
:
if
check_drift
:
# Ensure objects are loaded
_startup_check_drift
(
auto_migration
,
skip_drift_check_when_no_connection
)
import
videoag_common.objects
drifted
=
False
try
:
drifted
=
not
check_for_drift_and_migrate
(
Base
.
metadata
,
self
.
_engine
,
auto_migration
)
except
Exception
as
e
:
if
not
ignore_no_connection
:
raise
e
print
(
f
"
Exception while checking for drift. ignore_no_connection is set. Exception:
{
e
}
"
)
if
drifted
:
raise
Exception
(
"
Database schema has drifted!
"
)
if
config
.
get
(
"
log_all_statements
"
,
False
):
if
config
.
get
(
"
log_all_statements
"
,
False
):
import
logging
import
logging
...
@@ -101,6 +90,24 @@ class Database:
...
@@ -101,6 +90,24 @@ class Database:
logger
.
addHandler
(
handler
)
logger
.
addHandler
(
handler
)
logger
.
setLevel
(
logging
.
INFO
)
logger
.
setLevel
(
logging
.
INFO
)
def
_startup_check_drift
(
self
,
auto_migration
:
bool
,
skip_drift_check_when_no_connection
:
bool
):
if
skip_drift_check_when_no_connection
:
session
=
SessionDb
(
bind
=
self
.
_write_engines_by_level
[
TransactionIsolationLevel
.
REPEATABLE_READ
])
try
:
with
session
.
begin
():
session
.
execute
(
sqlachemy
.
text
(
"
SELECT 1
"
))
except
Exception
as
e
:
print
(
f
"
Unable to connect with database. Skipping drift check because
"
f
"
skip_drift_check_when_no_connection is set. Exception:
{
e
}
"
)
return
# Ensure objects are loaded
import
videoag_common.objects
drifted
=
not
check_for_drift_and_migrate
(
Base
.
metadata
,
self
.
_engine
,
auto_migration
)
if
drifted
:
raise
Exception
(
"
Database schema has drifted!
"
)
def
query_all_and_expunge
(
self
,
stmt
:
sqlalchemy
.
Select
[
_T
])
->
Sequence
[
_T
]:
def
query_all_and_expunge
(
self
,
stmt
:
sqlalchemy
.
Select
[
_T
])
->
Sequence
[
_T
]:
def
_trans
(
session
:
SessionDb
):
def
_trans
(
session
:
SessionDb
):
res
=
session
.
scalars
(
stmt
).
all
()
res
=
session
.
scalars
(
stmt
).
all
()
...
...
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