Skip to content

Commit

Permalink
[support-mariadb-dumps-with-newline] Move CopyStatementParser to post…
Browse files Browse the repository at this point in the history
…gres struct
  • Loading branch information
josacar committed Dec 28, 2023
1 parent a224612 commit 8c4b42e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 46 deletions.
45 changes: 0 additions & 45 deletions src/triki/copy_statement_parser.cr

This file was deleted.

43 changes: 42 additions & 1 deletion src/triki/postgres.cr
Original file line number Diff line number Diff line change
@@ -1,8 +1,49 @@
class Triki
struct Postgres
include Triki::CopyStatementParser
include Triki::ConfigScaffoldGenerator

# Postgres uses COPY statements instead of INSERT and look like:
#
# COPY some_table (a, b, c, d) FROM stdin;
# 1 2 3 4
# 5 6 7 8
# \.
#
# This requires the parse methods to persist data (table name and
# column names) across multiple lines.
#
def parse(obfuscator, config, input_io, output_io)
current_table_name = String.new
current_columns = ColumnList.new
inside_copy_statement = false

input_io.each_line(chomp: false) do |line|
if parse_insert_statement(line)
raise RuntimeError.new("Cannot obfuscate Postgres dumps containing INSERT statements. Please use COPY statments.")
elsif table_data = parse_copy_statement(line)
inside_copy_statement = true

current_table_name = table_data["table_name"].as(String)
current_columns = table_data["column_names"].as(ColumnList)

if !config[current_table_name]
Log.warn { "Deprecated: #{current_table_name} was not specified in the config. A future release will cause this to be an error. Please specify the table definition or set it to :keep." }
end

output_io.print(line)
elsif line.match /^\\\.$/
inside_copy_statement = false

output_io.print(line)
elsif inside_copy_statement
obfuscated_line = obfuscator.obfuscate_bulk_insert_line(line, current_table_name, current_columns)
output_io.puts(obfuscated_line) unless obfuscated_line.empty?
else
output_io.print(line)
end
end
end

# Copy statements contain the column values tab separated like so:
# blah blah blah blah
# which we want to turn into:
Expand Down

0 comments on commit 8c4b42e

Please sign in to comment.