import attr
import logging
+import psycopg.errors
import subprocess
import tempfile
def schema_exists(cur, schema):
'''Does the schema exist?'''
- cur.execute('SELECT 1 FROM pg_namespace'
- ' WHERE nspname = %s',
- (schema,))
+ try:
+ cur.execute('SELECT 1 FROM pg_namespace'
+ ' WHERE nspname = %s',
+ (schema,))
+ except UnicodeEncodeError as ex:
+ raise copy_ex.InvalidSchemaError(
+ ex,
+ ("Data cannot be represented in the database"
+ " connection's client-side character encoding"),
+ (f'The schema name ({schema}) has character(s) that cannot'
+ ' be sent to the server'))
+ except psycopg.errors.UntranslatableCharacter as ex:
+ raise copy_ex.InvalidSchemaError(
+ ex,
+ ("Data cannot be represented in the"
+ " character encoding of the database"),
+ (f'The schema name ({schema}) has character(s) that cannot'
+ ' be represented in the database'))
+
return cur.fetchone() is not None