Skip to content

Commit

Permalink
untested stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
tobixen committed Mar 12, 2016
1 parent 66c3ce1 commit f89d74f
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions sync-script/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,29 @@ def sql(conn, sql, params=[]):
def pg_conn(database):
return psycopg2.connect(database=database)

def updates_from_new_to_old():
old_conn = pg_conn('memberdb')
new_conn = pg_conn('drywall')
last_run = sql(old_conn, "select started from member_sync where is_current and op='upd_new2old'")[0]
updated_rows = sql(new_conn, "select member.created_at,member_number,coalesce(first_name, '')||' '||coalesce(last_name, ''),coalesce(date_birth,date '1900-01-01'+(year_birth-1900)*interval '1 year'),address,zip,email,municipality,county,(select max(payment_date) from payment where user_id=login_user.id and purpose=1) from login_user join member on user_id=login_user.id where exists (select * from payment where user_id=login_user.id and purpose=1 and created_at>%s and payment_date>=%s) and not exists (select * from payment where user_id=login_user.id and payment_date<%s and purpose=1)", [last_run, last_run, last_run])

## TODO! this is not tested!
def inserts_new_to_old():
def inserts_from_new_to_old():
old_conn = pg_conn('memberdb')
new_conn = pg_conn('drywall')
last_run = sql(old_conn, "select started from member_sync where is_current and op='ins_new2old'")[0]
new_rows = sql(new_conn, "select member_number,first_name||' '||last_name,coalesce(date_birth,date '1900-01-01'+(year_birth-1900)*interval '1 year'),address,zip,email,municipality,county,(select max(payment_date) from payment where user_id=login_user.id and purpose=1 and payment_status>=40) from login_user join member on user_id=login_user.id where exists (select * from payment where user_id=login_user.id and purpose=1 and created_at>%s and payment_date>=%s) and not exists (select * from payment where user_id=login_user.id and payment_date<%s and purpose=1)", [last_run, last_run])
new_rows = sql(new_conn, "select member.created_at,member_number,coalesce(first_name, '')||' '||coalesce(last_name, ''),coalesce(date_birth,date '1900-01-01'+(year_birth-1900)*interval '1 year'),address,zip,email,municipality,county,(select max(payment_date) from payment where user_id=login_user.id and purpose=1) from login_user join member on user_id=login_user.id where exists (select * from payment where user_id=login_user.id and purpose=1 and created_at>%s and payment_date>=%s) and not exists (select * from payment where user_id=login_user.id and payment_date<%s and purpose=1)", [last_run, last_run, last_run])
cnt=0
for row in new_rows:
sql(old_conn, "insert into members (mnr,navn,fdato,adresse,postnummer,epost,kommune,fylke,innmeldt) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", params=list(row))
sql(old_conn, "update members set postnummer=(select post.poststed from post where post.postnummer=members.postnummer) where created_at=now()") ## will this work?
exists = sql(old_conn, "select * from members where mnr=%s", params=[row[1]])
if exists:
continue
sql(old_conn, "insert into members (created_at,mnr,navn,fdato,adresse,postnummer,epost,kommune,fylke,innmeldt) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", params=list(row))
sql(old_conn, "update members set postnummer=(select post.poststed from post where post.postnummer||''=members.postnummer) where created_at=now()") ## will this work?
sql(old_conn, "insert into member_sync (op, started, finished, num_rows) values ('ins_new2old', now(), %s, %s)", params=[datetime.datetime.now(), len(new_rows)])
old_conn.commit()
new_conn.commit()

def inserts_old_to_new():
def inserts_from_old_to_new():
old_conn = pg_conn('memberdb')
new_conn = pg_conn('drywall')
last_run = sql(old_conn, "select started from member_sync where is_current and op='ins_old2new'")[0]
Expand All @@ -49,15 +56,16 @@ def inserts_old_to_new():

## historiske kontingentinnbetalinger
if row[14]:
sql(new_conn, "insert into payment (payment_date, purpose, user_id, payment_status) values (%s, 1, %s, 80)", params=['2013-01-01', user_id])
sql(new_conn, "insert into payment (payment_date, purpose, user_id) values (%s, 1, %s)", params=['2013-01-01', user_id])
if row[15]:
sql(new_conn, "insert into payment (payment_date, purpose, user_id, payment_status) values (%s, 1, %s, 80)", params=['2014-01-01', user_id])
sql(new_conn, "insert into payment (payment_date, purpose, user_id) values (%s, 1, %s)", params=['2014-01-01', user_id])
if row[16]:
sql(new_conn, "insert into payment (payment_date, purpose, user_id, payment_status) values (%s, 1, %s, 80)", params=['2015-01-01', user_id])
sql(new_conn, "insert into payment (payment_date, purpose, user_id) values (%s, 1, %s)", params=['2015-01-01', user_id])
sql(old_conn, "insert into member_sync (op, started, finished, num_rows) values ('ins_old2new', now(), %s, %s)", params=[datetime.datetime.now(), len(new_rows)])
new_conn.commit()
old_conn.commit()



inserts_from_old_to_new()
inserts_from_new_to_old()

0 comments on commit f89d74f

Please sign in to comment.