File tree Expand file tree Collapse file tree 2 files changed +35
-1
lines changed Expand file tree Collapse file tree 2 files changed +35
-1
lines changed Original file line number Diff line number Diff line change 33 email : product-team@data-contracts.com
44description : All active customers of our product.
55version : 1
6+ retention :
7+ policy : customer
8+ timestamp : created
69
710columns :
811 - name : id
@@ -28,5 +31,5 @@ columns:
2831checks :
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
Original file line number Diff line number Diff line change 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' ]} `" )
You can’t perform that action at this time.
0 commit comments