Skip to content

non-utf8 charset support for mysql #32

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ The configuration file uses following kind of syntax:

```YAML
config:
charset: iso-8859-1 # defaults to utf-8 if missing, only affects mysqldump
addons:
- some.other.package
- yet.another.package
Expand Down
12 changes: 12 additions & 0 deletions database_sanitizer/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
SKIP_ROWS_CONFIG_VALUE = "skip_rows"
MYSQLDUMP_DEFAULT_PARAMETERS = ["--single-transaction"]
PG_DUMP_DEFAULT_PARAMETERS = []
CHARSET_DEFAULT = "utf-8"


class ConfigurationError(ValueError):
Expand All @@ -31,6 +32,7 @@ def __init__(self):
self.addon_packages = []
self.mysqldump_params = []
self.pg_dump_params = []
self.charset = ""

@classmethod
def from_file(cls, filename):
Expand Down Expand Up @@ -73,6 +75,16 @@ def load(self, config_data):
self.load_sanitizers(config_data)
self.load_dump_extra_parameters(config_data)

charset = config_data.get("config",{}).get("charset", CHARSET_DEFAULT)
if not isinstance(charset, str):
raise ConfigurationError(
"'config' is %s instead of str" % (
type(charset),
),
)

self.charset = charset

def load_dump_extra_parameters(self, config_data):
"""
Loads extra parameters for mysqldump and/or pg_dump CLI usage. These
Expand Down
2 changes: 1 addition & 1 deletion database_sanitizer/dump/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def sanitize_from_stream(stream, config):
of the values stored in the database.
:type config: database_sanitizer.config.Configuration|None
"""
for line in io.TextIOWrapper(stream, encoding="utf-8"):
for line in io.TextIOWrapper(stream, encoding=config.charset):
# Eat the trailing new line.
line = line.rstrip("\n")

Expand Down