mirror of
https://github.com/planetmint/planetmint.git
synced 2025-11-25 06:55:45 +00:00
fixed run_command_without_output config and removed faulty error handling
Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
parent
f424f4f175
commit
b69ca92c3c
@ -57,13 +57,12 @@ class TarantoolDBConnection(Connection):
|
|||||||
def run(self, query, only_data=True):
|
def run(self, query, only_data=True):
|
||||||
try:
|
try:
|
||||||
return query.run(self.conn).data if only_data else query.run(self.conn)
|
return query.run(self.conn).data if only_data else query.run(self.conn)
|
||||||
except tarantool.error.SchemaError:
|
|
||||||
return None
|
|
||||||
except tarantool.error.OperationalError as op_error:
|
except tarantool.error.OperationalError as op_error:
|
||||||
raise op_error
|
raise op_error
|
||||||
except tarantool.error.NetworkError as net_error:
|
except tarantool.error.NetworkError as net_error:
|
||||||
raise net_error
|
raise net_error
|
||||||
|
|
||||||
|
|
||||||
def get_connection(self):
|
def get_connection(self):
|
||||||
return self.conn
|
return self.conn
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
import tarantool
|
import tarantool
|
||||||
|
from planetmint.config import Config
|
||||||
from planetmint.backend.utils import module_dispatch_registrar
|
from planetmint.backend.utils import module_dispatch_registrar
|
||||||
from planetmint import backend
|
from planetmint import backend
|
||||||
from planetmint.backend.tarantool.connection import TarantoolDBConnection
|
from planetmint.backend.tarantool.connection import TarantoolDBConnection
|
||||||
@ -151,16 +152,10 @@ def drop_database(connection, not_used=None):
|
|||||||
for _space in SPACE_NAMES:
|
for _space in SPACE_NAMES:
|
||||||
try:
|
try:
|
||||||
cmd = SCHEMA_DROP_COMMANDS[_space].encode()
|
cmd = SCHEMA_DROP_COMMANDS[_space].encode()
|
||||||
_output = run_command_with_output(command=cmd)
|
run_command_with_output(command=cmd)
|
||||||
if "nil value" in _output:
|
|
||||||
raise tarantool.error.DatabaseError(f"Space '{_space}' does not exists.")
|
|
||||||
else:
|
|
||||||
print(f"Space '{_space}' was dropped succesfuly.")
|
print(f"Space '{_space}' was dropped succesfuly.")
|
||||||
except tarantool.error.DatabaseError as space_does_not_exists:
|
except Exception:
|
||||||
print(space_does_not_exists)
|
print(f"Unexpected error while trying to drop space '{_space}'")
|
||||||
|
|
||||||
# connection.drop_database()
|
|
||||||
|
|
||||||
|
|
||||||
@register_schema(TarantoolDBConnection)
|
@register_schema(TarantoolDBConnection)
|
||||||
def create_database(connection, dbname):
|
def create_database(connection, dbname):
|
||||||
@ -176,7 +171,7 @@ def create_database(connection, dbname):
|
|||||||
|
|
||||||
def run_command_with_output(command):
|
def run_command_with_output(command):
|
||||||
from subprocess import run
|
from subprocess import run
|
||||||
host_port = "%s:%s" % ("localhost", 3303)
|
host_port = "%s:%s" % (Config().get()["database"]["host"], Config().get()["database"]["port"])
|
||||||
output = run(["tarantoolctl", "connect", host_port],
|
output = run(["tarantoolctl", "connect", host_port],
|
||||||
input=command,
|
input=command,
|
||||||
capture_output=True).stderr
|
capture_output=True).stderr
|
||||||
@ -189,35 +184,28 @@ def create_tables(connection, dbname):
|
|||||||
for _space in SPACE_NAMES:
|
for _space in SPACE_NAMES:
|
||||||
try:
|
try:
|
||||||
cmd = SPACE_COMMANDS[_space].encode()
|
cmd = SPACE_COMMANDS[_space].encode()
|
||||||
_output = run_command_with_output(command=cmd)
|
run_command_with_output(command=cmd)
|
||||||
if "exists" in _output:
|
|
||||||
raise tarantool.error.SchemaError(f"Space '{_space}' already exists")
|
|
||||||
else:
|
|
||||||
print(f"Space '{_space}' created.")
|
print(f"Space '{_space}' created.")
|
||||||
except tarantool.error.SchemaError as exists_error:
|
except Exception:
|
||||||
print(exists_error)
|
print(f"Unexpected error while trying to create '{_space}'")
|
||||||
continue
|
|
||||||
create_schema(space_name=_space)
|
create_schema(space_name=_space)
|
||||||
create_indexes(space_name=_space)
|
create_indexes(space_name=_space)
|
||||||
|
|
||||||
|
|
||||||
def create_indexes(space_name):
|
def create_indexes(space_name):
|
||||||
try:
|
|
||||||
indexes = INDEX_COMMANDS[space_name]
|
indexes = INDEX_COMMANDS[space_name]
|
||||||
for index_name, index_cmd in indexes.items():
|
for index_name, index_cmd in indexes.items():
|
||||||
_output = run_command_with_output(command=index_cmd.encode())
|
try:
|
||||||
if "exists" in _output:
|
run_command_with_output(command=index_cmd.encode())
|
||||||
raise tarantool.error.SchemaError(f"Index {index_name} already exists.")
|
|
||||||
else:
|
|
||||||
print(f"Index '{index_name}' created succesfully.")
|
print(f"Index '{index_name}' created succesfully.")
|
||||||
except tarantool.error.SchemaError as exists_error:
|
except Exception:
|
||||||
print(exists_error)
|
print(f"Unexpected error while trying to create index '{index_name}'")
|
||||||
|
|
||||||
|
|
||||||
def create_schema(space_name):
|
def create_schema(space_name):
|
||||||
try:
|
try:
|
||||||
cmd = SCHEMA_COMMANDS[space_name].encode()
|
cmd = SCHEMA_COMMANDS[space_name].encode()
|
||||||
_output = run_command_with_output(command=cmd)
|
run_command_with_output(command=cmd)
|
||||||
print(f"Schema created for {space_name} succesfully.")
|
print(f"Schema created for {space_name} succesfully.")
|
||||||
except Exception as unexpected_error:
|
except Exception as unexpected_error:
|
||||||
print(f"Got unexpected error when creating index for '{space_name}' Space.\n {unexpected_error}")
|
print(f"Got unexpected error when creating index for '{space_name}' Space.\n {unexpected_error}")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user