Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
a443f677
Commit
a443f677
authored
Jun 08, 2018
by
Micaël Bergeron
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
wip: making the fetch paginated
parent
ae91c723
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
10 deletions
+29
-10
lib/pseudonymizer/dumper.rb
lib/pseudonymizer/dumper.rb
+29
-10
No files found.
lib/pseudonymizer/dumper.rb
View file @
a443f677
...
...
@@ -4,6 +4,8 @@ require 'csv'
require
'yaml'
module
Pseudonymizer
PAGE_SIZE
=
10000
class
Anon
def
initialize
(
fields
)
@anon_fields
=
fields
...
...
@@ -43,7 +45,8 @@ module Pseudonymizer
new_tables
=
tables
.
map
do
|
k
,
v
|
@schema
[
k
]
=
{}
table_to_csv
(
k
,
v
[
"whitelist"
],
v
[
"pseudo"
])
table_to_schema
(
k
)
write_to_csv_file
(
k
,
table_page_results
(
k
,
v
[
'whitelist'
],
v
[
'pseudo'
]))
end
schema_to_yml
...
...
@@ -68,14 +71,33 @@ module Pseudonymizer
File
.
open
(
file_path
,
'w'
)
{
|
file
|
file
.
write
(
@output_files
.
to_json
)
}
end
def
table_to_csv
(
table
,
whitelist_columns
,
pseudonymity_columns
)
sql
=
"SELECT
#{
whitelist_columns
.
join
(
","
)
}
FROM
#{
table
}
;"
results
=
ActiveRecord
::
Base
.
connection
.
exec_query
(
sql
)
# yield every results, pagined, anonymized
def
table_page_results
(
table
,
whitelist_columns
,
pseudonymity_columns
)
anonymizer
=
Anon
.
new
(
pseudonymity_columns
)
page
=
0
Enumerator
.
new
do
|
yielder
|
loop
do
offset
=
page
*
PAGE_SIZE
sql
=
"SELECT
#{
whitelist_columns
.
join
(
","
)
}
FROM
#{
table
}
LIMIT
#{
PAGE_SIZE
}
OFFSET
#{
offset
}
;"
# a page of results
results
=
ActiveRecord
::
Base
.
connection
.
exec_query
(
sql
)
break
if
results
.
empty?
binding
.
pry
anonymizer
.
anonymize
(
results
).
each
{
|
result
|
yielder
<<
result
}
page
+=
1
end
end
end
def
table_to_schema
(
table
)
type_results
=
ActiveRecord
::
Base
.
connection
.
columns
(
table
)
type_results
=
type_results
.
select
do
|
c
|
@config
[
"tables"
][
table
][
"whitelist"
].
include?
(
c
.
name
)
end
type_results
=
type_results
.
map
do
|
c
|
data_type
=
c
.
sql_type
...
...
@@ -86,10 +108,6 @@ module Pseudonymizer
{
name:
c
.
name
,
data_type:
data_type
}
end
set_schema_column_types
(
table
,
type_results
)
return
if
results
.
empty?
anon
=
Anon
.
new
(
pseudonymity_columns
)
write_to_csv_file
(
table
,
anon
.
anonymize
(
results
))
end
def
set_schema_column_types
(
table
,
type_results
)
...
...
@@ -103,14 +121,15 @@ module Pseudonymizer
def
write_to_csv_file
(
title
,
contents
)
Rails
.
logger
.
info
"Writing
#{
title
}
..."
file_path
=
get_and_log_file_name
(
"csv"
,
title
)
binding
.
pry
column_names
=
contents
.
first
.
keys
contents
=
CSV
.
generate
do
|
csv
|
CSV
.
open
(
file_path
,
'w'
)
do
|
csv
|
csv
<<
column_names
contents
.
each
do
|
x
|
csv
<<
x
.
values
end
end
File
.
open
(
file_path
,
'w'
)
{
|
file
|
file
.
write
(
contents
)
}
file_path
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment