Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix write-hook compatiblity for 2.3.6 (import/export) #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions rethinkdb/_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ def export_table(db, table, directory, options, error_queue, progress_info, sind

writer = None

has_write_hooks = utils_common.check_minimum_version(options, '2.3.7', False)

try:
# -- get table info

Expand All @@ -248,13 +250,14 @@ def export_table(db, table, directory, options, error_queue, progress_info, sind

sindex_counter.value += len(table_info["indexes"])

table_info['write_hook'] = options.retryQuery(
'table write hook data %s.%s' % (db, table),
query.db(db).table(table).get_write_hook(),
run_options={'binary_format': 'raw'})
if has_write_hooks:
table_info['write_hook'] = options.retryQuery(
'table write hook data %s.%s' % (db, table),
query.db(db).table(table).get_write_hook(),
run_options={'binary_format': 'raw'})

if table_info['write_hook'] is not None:
hook_counter.value += 1
if table_info['write_hook'] is not None:
hook_counter.value += 1

with open(os.path.join(directory, db, table + '.info'), 'w') as info_file:
info_file.write(json.dumps(table_info) + "\n")
Expand Down
27 changes: 17 additions & 10 deletions rethinkdb/_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,13 @@ def restore_indexes(self, warning_queue):
exception_type, exception_class, trcback = sys.exc_info()
warning_queue.put((exception_type, exception_class, traceback.extract_tb(trcback), self._source.name))

self.query_runner(
"Write hook from: %s.%s" %
(self.db, self.table), query.db(
self.db).table(
self.table).get_write_hook())
try:
if self.write_hook:
if self.write_hook:
self.query_runner(
"Write hook from: %s.%s" %
(self.db, self.table), query.db(
self.db).table(
self.table).get_write_hook())
try:
self.query_runner(
"drop hook: %s.%s" % (self.db, self.table),
query.db(self.db).table(self.table).set_write_hook(None)
Expand All @@ -316,9 +316,9 @@ def restore_indexes(self, warning_queue):
"create hook: %s.%s:%s" % (self.db, self.table, self.write_hook),
query.db(self.db).table(self.table).set_write_hook(self.write_hook["function"])
)
except RuntimeError:
exception_type, exception_class, trcback = sys.exc_info()
warning_queue.put((exception_type, exception_class, traceback.extract_tb(trcback), self._source.name))
except RuntimeError:
exception_type, exception_class, trcback = sys.exc_info()
warning_queue.put((exception_type, exception_class, traceback.extract_tb(trcback), self._source.name))

def batches(self, batch_size=None, warning_queue=None):

Expand Down Expand Up @@ -1342,6 +1342,7 @@ def parse_sources(options, files_ignored=None):
def parse_info_file(path):
primary_key = None
indexes = []
write_hook = None
with open(path, 'r') as info_file:
metadata = json.load(info_file)
if "primary_key" in metadata:
Expand All @@ -1352,6 +1353,8 @@ def parse_info_file(path):
write_hook = metadata["write_hook"]
return primary_key, indexes, write_hook

has_write_hooks = utils_common.check_minimum_version(options, '2.3.7', False)

sources = set()
if files_ignored is None:
files_ignored = []
Expand Down Expand Up @@ -1385,6 +1388,8 @@ def parse_info_file(path):
indexes = info_indexes
if write_hook is None:
write_hook = info_write_hook
if write_hook and not has_write_hooks:
raise Exception('this RDB version doesn\'t support write-hooks')

sources.add(
table_type(
Expand Down Expand Up @@ -1449,6 +1454,8 @@ def parse_info_file(path):
files_ignored.append(os.path.join(root, filename))
else:
primary_key, indexes, write_hook = parse_info_file(info_path)
if write_hook and not has_write_hooks:
raise Exception('RDB versions below doesn\'t support write-hooks')

table_type = None
if ext == ".json":
Expand Down
7 changes: 5 additions & 2 deletions rethinkdb/utils_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def print_progress(ratio, indent=0, read=None, write=None):
sys.stdout.flush()


def check_minimum_version(options, minimum_version='1.6'):
def check_minimum_version(options, minimum_version='1.6', raise_exception=True):
minimum_version = distutils.version.LooseVersion(minimum_version)
version_string = options.retryQuery('get server version', query.db(
'rethinkdb').table('server_status')[0]['process']['version'])
Expand All @@ -135,7 +135,10 @@ def check_minimum_version(options, minimum_version='1.6'):
raise RuntimeError("invalid version string format: %s" % version_string)

if distutils.version.LooseVersion(matches.group('version')) < minimum_version:
raise RuntimeError("Incompatible version, expected >= %s got: %s" % (minimum_version, version_string))
if raise_exception:
raise RuntimeError("Incompatible version, expected >= %s got: %s" % (minimum_version, version_string))
return False
return True


DbTable = collections.namedtuple('DbTable', ['db', 'table'])
Expand Down