Skip to content

Commit

Permalink
Initialize database with a specific date
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-iohk committed May 20, 2024
1 parent 8bee574 commit c91302a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,12 @@ Create a database and relevant tables before first-time program execution using
# Initialize with current timestamp
invoke init-database

# Initialize with a specific timestamp
# Initialize with a specific unix timestamp
invoke init-database --batch-end-epoch <timestamp>

# Initialize with a specific date
invoke init-database --batch-end-epoch "2024-06-05 00:00:00"

# Initialize with a timestamp from n minutes ago
invoke init-database --mins-ago 20
```
Expand Down
11 changes: 10 additions & 1 deletion tasks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime, timedelta, timezone
import re
from invoke import task
import os
import psycopg2
Expand Down Expand Up @@ -75,7 +76,15 @@ def init_database(ctx, batch_end_epoch=None, mins_ago=None, override_empty=False
elif batch_end_epoch is None:
batch_end_epoch = datetime.now(timezone.utc).timestamp()
else:
batch_end_epoch = int(batch_end_epoch)
datetime_pattern = re.compile(r"^\d{4}-\d{2}-\d{2}.\d{2}:\d{2}:\d{2}.*$")
# Convert batch_end_epoch to a timestamp in utc if it is a datetime string
# use regex to check if it is of format 'YYYY-MM-DD HH:MM:SS'
if datetime_pattern.match(batch_end_epoch):
batch_end_epoch = datetime.fromisoformat(batch_end_epoch).timestamp()
print(f"Converted datetime string to timestamp: {batch_end_epoch}")
else:
batch_end_epoch = int(batch_end_epoch)
print(f"Using provided timestamp: {batch_end_epoch}")

# Check if the table is empty, if override_empty is False
should_insert = True
Expand Down

0 comments on commit c91302a

Please sign in to comment.