awslabs / aws-support-tools

Tools and sample code provided by AWS Premium Support.

Home Page:https://aws.amazon.com/premiumsupport/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mysql_to_redshift_tablename_noquotes.py fails if COMMENT string has parentheses

alexandros-kyriakides opened this issue · comments

For the following string in the CREATE TABLE definition, the python code fails:

"currency" char(3) DEFAULT NULL COMMENT 'Currency (ISO 4217)',

On the first split, extra takes the value "DEFAULT NULL COMMENT 'Currency (ISO 4217)'"

Because extra contains a ")" the second split runs:
sql_type, extra = definition.strip().split(" ", 1)

But because this contains more than one ")", it results in Value Error: too many values to unpack, and it is handled by the exception handling code that results in sql_type taking the value of "char(3) default null comment 'currency (iso 4217)" which is then incorrectly parsed further down the code.

A solution is to remove the COMMENT part right from the start. Instead of removing it from extra, remove it from definition:

  # adding code to identify COMMENT portion in column definition and omitting the COMMENT                                                                                                                                                                                                                       
  # since Redshift does not support COMMENT in CREATE TABLE statement                                                                                                                                                                                                                                           
                commentindefinition = "COMMENT" in definition
                if commentindefinition:
                    definition = definition.strip().split("COMMENT")[0].strip()