|
| 1 | +# Will export the Database zip file to /tmp/superset_database_exports with zip filename adjusted to include the database_id |
| 2 | +# Example zipfile: dashboard_#{database_id}_export_#{datestamp}.zip |
| 3 | +# |
| 4 | +# File will then be unziped and all files copied into the destination_path with the database_id as a subfolder |
| 5 | +# Optianally remove the dataset yaml files from the export |
| 6 | +# |
| 7 | +# Usage |
| 8 | +# Superset::Database::Export.new(database_id: 1, destination_path: '/tmp/superset_database_exports/').perform |
| 9 | + |
| 10 | +# Superset::Database::Export.new(database_id: 1, destination_path: '/tmp/superset_database_exports/', remove_dataset_yamls: true).perform |
| 11 | +# |
| 12 | + |
| 13 | +require 'superset/file_utilities' |
| 14 | + |
| 15 | +module Superset |
| 16 | + module Database |
| 17 | + class Export < Request |
| 18 | + include FileUtilities |
| 19 | + |
| 20 | + TMP_SUPERSET_DATABASE_PATH = '/tmp/superset_database_exports'.freeze |
| 21 | + |
| 22 | + attr_reader :database_id, :destination_path, :remove_dataset_yamls |
| 23 | + |
| 24 | + def initialize(database_id: , destination_path: , remove_dataset_yamls: true) |
| 25 | + @database_id = database_id |
| 26 | + @destination_path = destination_path.chomp('/') |
| 27 | + @remove_dataset_yamls = remove_dataset_yamls |
| 28 | + end |
| 29 | + |
| 30 | + def perform |
| 31 | + create_tmp_dir |
| 32 | + save_exported_zip_file |
| 33 | + unzip_files |
| 34 | + copy_export_files_to_destination_path |
| 35 | + |
| 36 | + Dir.glob("#{destination_path_with_db_id}/databases/*") |
| 37 | + end |
| 38 | + |
| 39 | + def response |
| 40 | + @response ||= client.call( |
| 41 | + :get, |
| 42 | + client.url(route), |
| 43 | + client.param_check(params) |
| 44 | + ) |
| 45 | + end |
| 46 | + |
| 47 | + def exported_zip_path |
| 48 | + @exported_zip_path ||= "#{tmp_uniq_database_path}/database_#{database_id}_export_#{datestamp}.zip" |
| 49 | + end |
| 50 | + |
| 51 | + private |
| 52 | + |
| 53 | + def params |
| 54 | + # The Swagger API interface indicates this endpoint should take an array of integers |
| 55 | + # however this does not work within the Swagger interface or when testing the API |
| 56 | + # Investigating the Superset GUI with Dev Tools shows that the format below is used |
| 57 | + |
| 58 | + { "q": "!(#{database_id})" } |
| 59 | + end |
| 60 | + |
| 61 | + def save_exported_zip_file |
| 62 | + File.open(exported_zip_path, 'wb') { |fp| fp.write(response.body) } |
| 63 | + end |
| 64 | + |
| 65 | + def unzip_files |
| 66 | + @extracted_files = unzip_file(exported_zip_path, tmp_uniq_database_path) |
| 67 | + remove_dataset_yaml_files if remove_dataset_yamls |
| 68 | + end |
| 69 | + |
| 70 | + def download_folder |
| 71 | + File.dirname(extracted_files[0]) |
| 72 | + end |
| 73 | + |
| 74 | + def destination_path_with_db_id |
| 75 | + @destination_path_with_db_id ||= File.join(destination_path, database_id.to_s) |
| 76 | + end |
| 77 | + |
| 78 | + def copy_export_files_to_destination_path |
| 79 | + FileUtils.mkdir_p(destination_path_with_db_id) unless File.directory?(destination_path_with_db_id) |
| 80 | + |
| 81 | + Dir.glob("#{download_folder}/*").each do |item| |
| 82 | + FileUtils.cp_r(item, destination_path_with_db_id) |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + def remove_dataset_yaml_files |
| 87 | + datasets_directories = Dir.glob( File.join(tmp_uniq_database_path, '/*/datasets') ) |
| 88 | + |
| 89 | + datasets_directories.each do |directory| |
| 90 | + FileUtils.rm_rf(directory) if Dir.exist?(directory) |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + def create_tmp_dir |
| 95 | + FileUtils.mkdir_p(tmp_uniq_database_path) unless File.directory?(tmp_uniq_database_path) |
| 96 | + end |
| 97 | + |
| 98 | + def tmp_uniq_database_path |
| 99 | + @tmp_uniq_database_path ||= File.join(TMP_SUPERSET_DATABASE_PATH, uuid) |
| 100 | + end |
| 101 | + |
| 102 | + def uuid |
| 103 | + SecureRandom.uuid |
| 104 | + end |
| 105 | + |
| 106 | + def extracted_files |
| 107 | + @extracted_files ||= [] |
| 108 | + end |
| 109 | + |
| 110 | + def route |
| 111 | + "database/export/" |
| 112 | + end |
| 113 | + |
| 114 | + def datestamp |
| 115 | + @datestamp ||= Time.now.strftime('%Y%m%d') |
| 116 | + end |
| 117 | + end |
| 118 | + end |
| 119 | +end |
0 commit comments