Commit a80cc255 authored by Libor Peltan's avatar Libor Peltan Committed by Daniel Salzman

sql import: support for transactions

parent e26f6fc7
......@@ -31,6 +31,7 @@ import re
from subprocess import DEVNULL, PIPE, Popen
# globals
connection = None
soa_serial = int(time.time())
config = configparser.ConfigParser()
fix_absolute = False
......@@ -70,15 +71,15 @@ def remove_dot(s):
def fix_abs(name):
return remove_dot(name) + '.' if fix_absolute else name
def domain_get_records(domain):
def domain_get_records(domain, txn):
if str(domain).isdigit():
return == domain)
return == domain, connection=txn)
dn = remove_dot(domain)
return == Records.q.domain, == dn))
return == Records.q.domain, == dn), connection=txn)
def domain_id2name(domain):
return == domain)[0].name
def domain_id2name(domain, txn):
return == domain, connection=txn)[0].name
def get_config(key, default_val):
global config
......@@ -146,21 +147,21 @@ def print_record(record, outfile):
record = ("%s. %d %s %s\n" % (, record.ttl, t, content))
def print_domain(domain, change_type = 0):
def print_domain(domain, change_type = 0, txn = None):
global knotc_socket
dn = domain_id2name(domain) if str(domain).isdigit() else domain
dn = domain_id2name(domain, txn) if str(domain).isdigit() else domain
f = open(zone_storage(dn), "w")
for r in domain_get_records(domain):
for r in domain_get_records(domain, txn):
print_record(r, f)
if knotc_socket is not None:
knotc_send(change_type, dn)
print("Updated zone %s" % dn, file=sys.stderr)
def domain_from_change(change):
def domain_from_change(change, txn):
global knotc_socket
if change.type >= 0:
print_domain(, change.type)
print_domain(, change.type, txn)
dn =
......@@ -173,16 +174,26 @@ def domain_from_change(change):
print("Warning: removed zone '%s', but unspecified knotc socket." % dn, file=sys.stderr)
def process_changes(startwith):
global connection
processed = []
for ch in > startwith):
txn = connection.transaction()
for ch in > startwith, connection=txn):
domain_from_change(ch, txn)
if len(processed) > 0:
print("Processed up to change_id %d" % processed[-1], file=sys.stderr)
# TODO delete processed ?
def process_all():
global connection
txn = connection.transaction()
for d in
print_domain(, txn = txn)
def read_config_file(filename):
global config
......@@ -196,6 +207,7 @@ def main():
global knotc_socket
global soa_serial
global fix_absolute
global connection
argp = argparse.ArgumentParser(prog='dns_sql2zf', description="Export DNS records from Mysql or Postgres DB into zonefile.", epilog="(C) CZ.NIC, GPLv3") # TODO better epilog
argp.add_argument(dest='domains', metavar='zone', nargs='*', help='Zone to be exported.')
......@@ -232,8 +244,7 @@ def main():
if args.all:
for d in
if args.from_changes is not None:
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment