Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
StorageLeaf
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ProjectLeaf
StorageLeaf
Commits
ad6c776f
Commit
ad6c776f
authored
3 years ago
by
Robert Goldmann
Browse files
Options
Downloads
Patches
Plain Diff
#9 - optionally perform backup after clenaup
parent
9732e624
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
src/logic/database/DatabaseCleaner.py
+4
-2
4 additions, 2 deletions
src/logic/database/DatabaseCleaner.py
src/test/DatabaseCleanerTest.py
+23
-4
23 additions, 4 deletions
src/test/DatabaseCleanerTest.py
with
27 additions
and
6 deletions
src/logic/database/DatabaseCleaner.py
+
4
−
2
View file @
ad6c776f
...
...
@@ -14,8 +14,9 @@ LOGGER = logging.getLogger(Constants.APP_NAME)
class
DatabaseCleaner
:
MIN_DATE
=
datetime
(
year
=
1970
,
month
=
1
,
day
=
1
).
date
()
def
__init__
(
self
,
retentionPolicies
:
List
[
RetentionPolicy
]):
def
__init__
(
self
,
retentionPolicies
:
List
[
RetentionPolicy
]
,
forceBackupAfterCleanup
:
bool
):
self
.
_policies
=
retentionPolicies
self
.
_forceBackupAfterCleanup
=
forceBackupAfterCleanup
def
clean
(
self
,
db
:
Session
,
currentDate
:
datetime
.
date
):
LOGGER
.
info
(
'
Performing database cleanup...
'
)
...
...
@@ -33,7 +34,8 @@ class DatabaseCleaner:
LOGGER
.
info
(
'
Database cleanup done
'
)
# TODO: force backup?
if
self
.
_forceBackupAfterCleanup
:
Crud
.
BACKUP_SERVICE
.
backup
()
@staticmethod
def
_cleanup_measurements_for_sensor
(
sensor
:
Schemas
.
Sensor
,
db
:
Session
,
...
...
This diff is collapsed.
Click to expand it.
src/test/DatabaseCleanerTest.py
+
23
−
4
View file @
ad6c776f
...
...
@@ -220,7 +220,7 @@ class TestDatabaseCleaner(unittest.TestCase):
database
=
Mock
()
from
logic.database.DatabaseCleaner
import
DatabaseCleaner
DatabaseCleaner
([]).
clean
(
database
,
CURRENT_DATE_TIME
)
DatabaseCleaner
([]
,
False
).
clean
(
database
,
CURRENT_DATE_TIME
)
mockedCrud
.
get_measurements_for_sensor
.
assert_not_called
()
...
...
@@ -238,7 +238,7 @@ class TestDatabaseCleaner(unittest.TestCase):
from
logic.database.DatabaseCleaner
import
RetentionPolicy
policy
=
RetentionPolicy
(
numberOfMeasurementsPerDay
=
4
,
ageInDays
=
1
)
DatabaseCleaner
([
policy
]).
clean
(
database
,
datetime
(
year
=
2021
,
month
=
8
,
day
=
19
).
date
())
DatabaseCleaner
([
policy
]
,
False
).
clean
(
database
,
datetime
(
year
=
2021
,
month
=
8
,
day
=
19
).
date
())
mockedCrud
.
delete_multiple_measurements
.
assert_called_once_with
(
database
,
{
3
})
...
...
@@ -261,7 +261,7 @@ class TestDatabaseCleaner(unittest.TestCase):
from
logic.database.DatabaseCleaner
import
RetentionPolicy
policy
=
RetentionPolicy
(
numberOfMeasurementsPerDay
=
4
,
ageInDays
=
1
)
DatabaseCleaner
([
policy
]).
clean
(
database
,
datetime
(
year
=
2021
,
month
=
8
,
day
=
19
).
date
())
DatabaseCleaner
([
policy
]
,
False
).
clean
(
database
,
datetime
(
year
=
2021
,
month
=
8
,
day
=
19
).
date
())
calls
=
mockedCrud
.
delete_multiple_measurements
.
call_args_list
self
.
assertEqual
(
2
,
len
(
calls
))
...
...
@@ -284,9 +284,28 @@ class TestDatabaseCleaner(unittest.TestCase):
policy1
=
RetentionPolicy
(
numberOfMeasurementsPerDay
=
4
,
ageInDays
=
1
)
policy2
=
RetentionPolicy
(
numberOfMeasurementsPerDay
=
2
,
ageInDays
=
1
)
DatabaseCleaner
([
policy1
,
policy2
]).
clean
(
database
,
datetime
(
year
=
2021
,
month
=
8
,
day
=
19
).
date
())
DatabaseCleaner
([
policy1
,
policy2
]
,
False
).
clean
(
database
,
datetime
(
year
=
2021
,
month
=
8
,
day
=
19
).
date
())
calls
=
mockedCrud
.
delete_multiple_measurements
.
call_args_list
self
.
assertEqual
(
2
,
len
(
calls
))
self
.
assertEqual
((
database
,
{
3
}),
calls
[
0
].
args
)
self
.
assertEqual
((
database
,
{
3
,
4
}),
calls
[
1
].
args
)
def
test_forceBackupAfterCleanup
(
self
):
mockedCrud
=
Mock
()
with
patch
.
dict
(
'
sys.modules
'
,
**
{
'
logic.database.Crud
'
:
mockedCrud
}):
mockedCrud
.
DATE_FORMAT
=
'
%Y-%m-%d %H:%M:%S
'
mockedCrud
.
get_measurements_for_sensor
.
return_value
=
[]
mockedCrud
.
get_sensors
.
return_value
=
[
Schemas
.
Sensor
(
id
=
1
,
name
=
"
myTempSensor
"
,
type
=
"
temperature
"
,
device_id
=
1
)]
mockedCrud
.
get_first_measurement_for_sensor
.
return_value
=
self
.
FIRST_MEASUREMENT
database
=
Mock
()
from
logic.database.DatabaseCleaner
import
DatabaseCleaner
from
logic.database.DatabaseCleaner
import
RetentionPolicy
policy
=
RetentionPolicy
(
numberOfMeasurementsPerDay
=
4
,
ageInDays
=
1
)
DatabaseCleaner
([
policy
],
True
).
clean
(
database
,
datetime
(
year
=
2021
,
month
=
8
,
day
=
19
).
date
())
mockedCrud
.
BACKUP_SERVICE
.
backup
.
assert_called_once
()
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