Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Here's a script for backfilling routes & exchanges
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed Feb 6, 2016
1 parent 5b9af81 commit 3a8076e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
71 changes: 71 additions & 0 deletions backfill.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env python2 -u
"""Script for backfilling exchange status, route, and ref.
Symlink a directory of data at `./backfill` and then call like so:
[gratipay] $ run_dammit defaults.env local.env -c env/bin/python backfill.py
Data files should be one per network (named `samurai`, `stripe`, etc), as CSVs
with these columns:
user_id required Gratipay participant.id
username ignored
address optional defaults to 'fake-deadbeef'
exchange_id required Gratipay exchanges.id
status optional defaults to 'succeeded'
ref optional defaults to 'fake-beeffeed'
For successfully backfilled exchanges (and routes), the script outputs the same
CSV as was input, with optional fields filled in. The script is idempotent (the
faked address and ref are hashed from other input values).
"""
from __future__ import absolute_import, division, print_function, unicode_literals

import csv
import os
import sha
import sys
from os import path

from gratipay import wireup
from gratipay.models.exchange_route import ExchangeRoute
from gratipay.models.participant import Participant


BASE = path.dirname(__file__)


def fake(*a):
return 'fake-' + sha.new(''.join(map(str, a))).hexdigest()


def link(db, log, user_id, network, address, exchange_id, status, ref):
participant = Participant.from_id(user_id)
route = ExchangeRoute.from_network(participant, network)
if route is None:
route = ExchangeRoute.insert(participant, network, address)
db.run( "UPDATE exchanges SET status=%s, route=%s, ref=%s WHERE id=%s"
, (status, route.id, ref, exchange_id)
)
log(participant.id, participant.username, address, exchange_id, status, ref )


def main(db, log):
for network in os.listdir('backfill'):
data = csv.reader(open(path.join('backfill', network)))
for user_id, _, address, exchange_id, status, ref in data:
assert user_id
address = address or fake(user_id, network)
assert exchange_id
status = status or 'succeeded'
ref = ref or fake(user_id, network, exchange_id)

link(db, log, user_id, network, address, exchange_id, status, ref)


if __name__ == '__main__':
db = wireup.db(wireup.env())
writer = csv.writer(sys.stdout)
log = lambda *a: writer.writerow(a)
main(db, log)
4 changes: 4 additions & 0 deletions sql/branch.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TYPE payment_net ADD VALUE 'samurai';
ALTER TYPE payment_net ADD VALUE 'stripe';

ALTER TABLE exchanges ADD UNIQUE (ref);

0 comments on commit 3a8076e

Please sign in to comment.