Skip to content

Commit b204070

Browse files
committed
Implement retention policy
1 parent 68c43a9 commit b204070

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

customers.contract.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ owner:
33
email: product-team@data-contracts.com
44
description: All active customers of our product.
55
version: 1
6+
retention:
7+
policy: customer
8+
timestamp: created
69

710
columns:
811
- name: id
@@ -28,5 +31,5 @@ columns:
2831
checks:
2932
- type: metric_expression
3033
metric: retention_policy
31-
expression_sql: COUNT(CASE WHEN created < NOW() - interval '3 year' THEN 1 END)
34+
expression_sql: COUNT(CASE WHEN created < NOW() - interval '3 years' THEN 1 END)
3235
must_be: 0

retention.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
3+
import yaml
4+
import psycopg2
5+
6+
# Static lookup for the retention policy
7+
retention_policy_lookup = {
8+
'customer': '3 years',
9+
'employee': '5 years',
10+
}
11+
12+
# Open and validate the data contract
13+
with open("./customers.contract.yml", "r") as stream:
14+
contents = stream.read()
15+
contract = yaml.safe_load(contents)
16+
17+
if 'retention' not in contract or 'policy' not in contract['retention'] or 'timestamp' not in contract['retention']:
18+
raise Exception(f"Retention policy not defined in the `{contract['dataset']}` contract")
19+
20+
# Generate some SQL to apply the retention policy
21+
conn = psycopg2.connect("dbname=postgres user=postgres host='localhost' password='secret' port='5432'")
22+
cur = conn.cursor()
23+
cur.execute(f"""
24+
DELETE FROM {contract['dataset']}
25+
WHERE
26+
{contract['retention']['timestamp']} < NOW() - interval '{retention_policy_lookup[contract['retention']['policy']]}'
27+
""")
28+
conn.commit()
29+
30+
# Print if successful
31+
print(f"Retention policy applied to `{contract['dataset']}`")

0 commit comments

Comments
 (0)