-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to trim auxiliary tables (#314)
- Loading branch information
1 parent
6f0c166
commit 3f13318
Showing
14 changed files
with
133 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import sys | ||
from datetime import datetime, timedelta | ||
from textwrap import dedent | ||
|
||
from datastore.shared.di import injector | ||
from datastore.shared.postgresql_backend import ConnectionHandler | ||
from datastore.writer.app import register_services | ||
|
||
|
||
def main(args: list[str] = []): | ||
""" | ||
Usage: python trim_collectionfield_tables.py [days=1] | ||
Trims all collectionfield tables by deleting all entries which are older than the given amount | ||
of days (which may be a floating point number). | ||
""" | ||
register_services() | ||
connection: ConnectionHandler = injector.get(ConnectionHandler) | ||
|
||
delta = float(args[1]) if len(args) > 1 else 1 | ||
threshold = datetime.now() - timedelta(days=delta) | ||
with connection.get_connection_context(): | ||
# delete collectionsfields which haven't been updated in the last 24 hours | ||
connection.execute( | ||
dedent( | ||
"""\ | ||
DELETE FROM collectionfields cf | ||
USING positions p | ||
WHERE cf.position = p.position AND p.timestamp < %s | ||
""" | ||
), | ||
[threshold], | ||
) | ||
# delete events_to_collectionfields from events older than 24 hours | ||
connection.execute( | ||
dedent( | ||
"""\ | ||
DELETE FROM events_to_collectionfields ecf | ||
USING events e, positions p | ||
WHERE ecf.event_id = e.id AND e.position = p.position AND p.timestamp < %s | ||
""" | ||
), | ||
[threshold], | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main(sys.argv)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import pytest | ||
|
||
from datastore.shared.postgresql_backend import setup_di as postgresql_setup_di | ||
from datastore.shared.services import setup_di as util_setup_di | ||
from datastore.writer import setup_di as writer_setup_di | ||
from datastore.writer.redis_backend import setup_di as redis_setup_di | ||
from tests import ( # noqa | ||
db_connection, | ||
db_cur, | ||
reset_db_data, | ||
reset_db_schema, | ||
reset_di, | ||
setup_db_connection, | ||
) | ||
|
||
|
||
# Application | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def setup_di(reset_di): # noqa | ||
util_setup_di() | ||
postgresql_setup_di() | ||
redis_setup_di() | ||
writer_setup_di() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from datetime import datetime, timedelta | ||
|
||
from cli.trim_collectionfield_tables import main as trim_collectionfield_tables | ||
from datastore.shared.di import injector | ||
|
||
|
||
def test_trim_collectionfield_tables(db_cur): | ||
in_time = datetime.now() - timedelta(hours=12) | ||
out_time = datetime.now() - timedelta(hours=36) | ||
db_cur.execute( | ||
"INSERT INTO positions (timestamp, user_id, migration_index) VALUES (%s, -1, -1), (%s, -1, -1)", | ||
[out_time, in_time], | ||
) | ||
db_cur.execute( | ||
"INSERT INTO events (position, fqid, type, weight) VALUES (1, 'a/1', 'create', 1), (2, 'a/2', 'create', 1)", | ||
[], | ||
) | ||
db_cur.execute( | ||
"INSERT INTO collectionfields (collectionfield, position) VALUES ('a/f', 1), ('a/g', 2)", | ||
[], | ||
) | ||
db_cur.execute( | ||
"INSERT INTO events_to_collectionfields VALUES (1, 1), (1, 2), (2, 2)", [] | ||
) | ||
db_cur.connection.commit() | ||
injector.provider_map.clear() # de-register services for testing purposes | ||
trim_collectionfield_tables() | ||
db_cur.execute("SELECT * FROM collectionfields") | ||
assert db_cur.fetchall() == [(2, "a/g", 2)] | ||
db_cur.execute("SELECT * FROM events_to_collectionfields") | ||
assert db_cur.fetchall() == [(2, 2)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters