holland-backup/holland

#297 still has issues with python2

venix1 opened this issue · 5 comments

A recent upgrade to 1.1.20 produced the following

[WARNING] must be unicode, not str

The warning was tracked down to this line.

    with io.open(definitions_file, "w", encoding="utf-8") as sqlf:
        sqlf.write(invalid_views)`

Added here . A solution was to convert invalid_views to utf-8 before writing.

    with io.open(definitions_file, "w", encoding="utf-8") as sqlf:
        sqlf.write(invalid_views.decode('utf-8', 'ignore'))

Thanks for reporting this. I've been unable to reproduce this issue on Centos 7 with multiple different versions of Mysql/Mariadb. Can you please let me know what locale the system is set to?

[root@e317edbbd18d /]# python --version
Python 2.7.5
[root@e317edbbd18d /]# holland --version
1.1.20
[root@e317edbbd18d /]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
[root@e317edbbd18d /]# mysql --version
mysql  Ver 14.14 Distrib 5.7.28, for Linux (x86_64) using  EditLine wrapper

I'm able to duplicate it with the following test case.

import io

invalid_views = "--\n-- DDL of Invalid Views\n-- Created automatically by Holland\n--\n"
with io.open('test.txt', "w", encoding="utf-8") as sqlf:
  sqlf.write(invalid_views)

As for everything else, it matches yours.

# python --version
Python 2.7.5

# holland --version
1.1.20

# cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)

# mysql --version
mysql  Ver 14.14 Distrib 5.7.28, for Linux (x86_64) using  EditLine wrapper

# locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES=C
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

# localectl status
   System Locale: LANG=en_US.UTF-8
                  LC_MESSAGES=C
       VC Keymap: us
      X11 Layout: us

Something strange is happening. I get the error as you describe when running your code snippet.

[root@07a488274925 /]# cat test.py
#!/usr/bin/env python

import io

invalid_views = "--\n-- DDL of Invalid Views\n-- Created automatically by Holland\n--\n"
with io.open('test.txt', "w", encoding="utf-8") as sqlf:
  sqlf.write(invalid_views)
[root@07a488274925 /]# ./test.py
Traceback (most recent call last):
  File "./test.py", line 7, in <module>
    sqlf.write(invalid_views)
TypeError: must be unicode, not str

But holland still prints out the invalid_views.sql file without an error.

[root@07a488274925 /]# cat /var/spool/holland/mysqldump/20191230_161731/invalid_views.sql
--
-- DDL of Invalid Views
-- Created automatically by Holland
--
--
-- Current View: `test`.`v1`
--
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `test`.`v1` AS select `test`.`t1`.`a` AS `a` from `test`.`t1`;

Using codecs.open has the desired effect without having to decode invalid_views. It's actually being used for the same purpose elsewhere in the file and I didn't notice before.

[root@07a488274925 /]# cat test.py
#!/usr/bin/env python

import codecs

invalid_views = "--\n-- DDL of Invalid Views\n-- Created automatically by Holland\n--\n"
with codecs.open('test.txt', "wt", encoding="utf-8") as sqlf:
  sqlf.write(invalid_views)

I'm hesitant to release this without understanding what's going on. I'd like to figure out why the code works in holland in my test environment and not on the system you reported this for.

Where did codecs.open come from? The installed version of holland 1.1.20 uses the snippet provided indicating io.open, not codecs.open. Is it possible there are 2 versions of 1.1.20 that got released? The current python2 branch also shows io.open and not codecs.open

@venix1 Thanks for helping me figure this out. Looks like this failed when there are not invalid views. The output from mysql was fixing the encoding on the invalid_view string.

This should be fixed in holland 1.1.21