Class: DataDuck::Commands
- Inherits:
-
Object
- Object
- DataDuck::Commands
- Defined in:
- lib/dataduck/commands.rb
Defined Under Namespace
Classes: Namespace
Class Method Summary collapse
- .acceptable_commands ⇒ Object
- .console ⇒ Object
- .dbconsole(where = "destination") ⇒ Object
- .etl(what = nil) ⇒ Object
- .help ⇒ Object
- .prompt_choices(choices = []) ⇒ Object
- .quickstart ⇒ Object
- .quickstart_create_table(table_name, db) ⇒ Object
- .quickstart_save_file(output_path_full, contents) ⇒ Object
- .quickstart_save_main ⇒ Object
- .quickstart_update_gitignore ⇒ Object
- .route_command(args) ⇒ Object
- .show(table_name = nil) ⇒ Object
Class Method Details
.acceptable_commands ⇒ Object
34 35 36 |
# File 'lib/dataduck/commands.rb', line 34 def self.acceptable_commands ['console', 'dbconsole', 'etl', 'quickstart', 'show'] end |
.console ⇒ Object
52 53 54 55 56 |
# File 'lib/dataduck/commands.rb', line 52 def self.console require "irb" ARGV.clear IRB.start end |
.dbconsole(where = "destination") ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/dataduck/commands.rb', line 58 def self.dbconsole(where = "destination") which_database = nil if where == "destination" which_database = DataDuck::Destination.only_destination elsif where == "source" which_database = DataDuck::Source.only_source else found_source = DataDuck::Source.source(where, true) found_destination = DataDuck::Destination.destination(where, true) if found_source && found_destination raise ArgumentError.new("Ambiguous call to dbconsole for #{ where } since there is both a source and destination named #{ where }.") end which_database = found_source if found_source which_database = found_destination if found_destination end if which_database.nil? raise ArgumentError.new("Could not find database '#{ where }'") end puts "Connecting to #{ where }..." which_database.dbconsole end |
.etl(what = nil) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/dataduck/commands.rb', line 83 def self.etl(what = nil) if what.nil? puts "You need to specify a table name or 'all'. Usage: dataduck etl all OR datduck etl my_table_name" return end only_destination = DataDuck::Destination.only_destination if what == "all" etl = ETL.new(destinations: [only_destination], autoload_tables: true) etl.process! else table_name_camelized = DataDuck::Util.underscore_to_camelcase(what) require DataDuck.project_root + "/src/tables/#{ what }.rb" table_class = Object.const_get(table_name_camelized) if !(table_class <= DataDuck::Table) raise Exception.new("Table class #{ table_name_camelized } must inherit from DataDuck::Table") end table = table_class.new etl = ETL.new(destinations: [only_destination], autoload_tables: false, tables: [table]) etl.process_table!(table) end end |
.help ⇒ Object
108 109 110 111 |
# File 'lib/dataduck/commands.rb', line 108 def self.help puts "Usage: dataduck commandname" puts "Commands: #{ acceptable_commands.sort.join(' ') }" end |
.prompt_choices(choices = []) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/dataduck/commands.rb', line 19 def self.prompt_choices(choices = []) while true print "Enter a number 0 - #{ choices.length - 1}\n" choices.each_with_index do |choice, idx| choice_name = choice.is_a?(String) ? choice : choice[1] print "#{ idx }: #{ choice_name }\n" end choice = STDIN.gets.strip.to_i if 0 <= choice && choice < choices.length selected = choices[choice] return selected.is_a?(String) ? selected : selected[0] end end end |
.quickstart ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/dataduck/commands.rb', line 137 def self.quickstart puts "Welcome to DataDuck!" puts "This quickstart wizard will help you set up DataDuck." puts "What kind of database would you like to source from?" db_type = prompt_choices([ [:mysql, "MySQL"], [:postgresql, "PostgreSQL"], [:other, "other"], ]) if db_type == :other puts "You've selected 'other'. Unfortunately, those are the only choices supported at the moment. Contact us at DataDuckETL.com to request support for your database." exit end puts "Enter the source hostname:" source_host = STDIN.gets.strip puts "Enter the name of the database when connecting to #{ source_host }:" source_database = STDIN.gets.strip puts "Enter the source's port:" source_port = STDIN.gets.strip.to_i puts "Enter the username:" source_username = STDIN.gets.strip puts "Enter the password:" source_password = STDIN.noecho(&:gets).chomp db_class = { mysql: DataDuck::MysqlSource, postgresql: DataDuck::PostgresqlSource, }[db_type] db_source = db_class.new({ 'db_type' => db_type.to_s, 'host' => source_host, 'database' => source_database, 'port' => source_port, 'username' => source_username, 'password' => source_password, }) puts "Connecting to source database..." table_names = db_source.table_names puts "Connection successful. Detected #{ table_names.length } tables." puts "Creating scaffolding..." table_names.each do |table_name| DataDuck::Commands.quickstart_create_table(table_name, db_source) end config_obj = { 'sources' => { 'my_database' => { 'type' => db_type.to_s, 'host' => source_host, 'database' => source_database, 'port' => source_port, 'username' => source_username, 'password' => source_password, } }, 'destinations' => { 'my_destination' => { 'type' => 'redshift', 'aws_key' => 'YOUR_AWS_KEY', 'aws_secret' => 'YOUR_AWS_SECRET', 's3_bucket' => 'YOUR_BUCKET', 's3_region' => 'YOUR_BUCKET_REGION', 'host' => 'redshift.somekeygoeshere.us-west-2.redshift.amazonaws.com', 'port' => 5439, 'database' => 'main', 'schema' => 'public', 'username' => 'YOUR_UESRNAME', 'password' => 'YOUR_PASSWORD', } } } DataDuck::Commands.quickstart_save_file("#{ DataDuck.project_root }/config/secret/#{ DataDuck.environment }.yml", config_obj.to_yaml) DataDuck::Commands.quickstart_save_main DataDuck::Commands.quickstart_update_gitignore puts "Quickstart complete!" puts "You still need to edit your config/secret/*.yml file with your AWS and Redshift credentials." puts "Run your ETL with: ruby src/main.rb" end |
.quickstart_create_table(table_name, db) ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/dataduck/commands.rb', line 238 def self.quickstart_create_table(table_name, db) columns = [] schema = db.schema(table_name) schema.each do |property_schema| property_name = property_schema[0] property_type = property_schema[1][:type] commented_out = ['ssn', 'socialsecurity', 'password', 'encrypted_password', 'salt', 'password_salt', 'pw'].include?(property_name.to_s.downcase) columns << [property_name.to_s, property_type.to_s, commented_out] end columns.sort! { |a, b| a[0] <=> b[0] } table_name = table_name.to_s.downcase table_name_camelcased = table_name.split('_').collect(&:capitalize).join namespace = Namespace.new(table_name_camelcased: table_name_camelcased, table_name: table_name, columns: columns) template = File.open("#{ DataDuck.gem_root }/lib/templates/quickstart/table.rb.erb", 'r').read result = ERB.new(template).result(namespace.get_binding) DataDuck::Commands.quickstart_save_file("#{ DataDuck.project_root }/src/tables/#{ table_name }.rb", result) end |
.quickstart_save_file(output_path_full, contents) ⇒ Object
258 259 260 261 262 263 264 265 266 |
# File 'lib/dataduck/commands.rb', line 258 def self.quickstart_save_file(output_path_full, contents) *output_path, output_filename = output_path_full.split('/') output_path = output_path.join("/") FileUtils::mkdir_p(output_path) output = File.open(output_path_full, "w") output << contents output.close end |
.quickstart_save_main ⇒ Object
268 269 270 271 272 273 |
# File 'lib/dataduck/commands.rb', line 268 def self.quickstart_save_main namespace = Namespace.new template = File.open("#{ DataDuck.gem_root }/lib/templates/quickstart/main.rb.erb", 'r').read result = ERB.new(template).result(namespace.get_binding) DataDuck::Commands.quickstart_save_file("#{ DataDuck.project_root }/src/main.rb", result) end |
.quickstart_update_gitignore ⇒ Object
227 228 229 230 231 232 233 234 235 236 |
# File 'lib/dataduck/commands.rb', line 227 def self.quickstart_update_gitignore main_gitignore_path = "#{ DataDuck.project_root }/.gitignore" FileUtils.touch(main_gitignore_path) secret_gitignore_path = "#{ DataDuck.project_root }/config/secret/.gitignore" FileUtils.touch(secret_gitignore_path) output = File.open(secret_gitignore_path, "w") output << '[^.]*' output.close end |
.route_command(args) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/dataduck/commands.rb', line 38 def self.route_command(args) if args.length == 0 return DataDuck::Commands.help end command = args[0] if !Commands.acceptable_commands.include?(command) puts "No such command: #{ command }" return DataDuck::Commands.help end DataDuck::Commands.public_send(command, *args[1..-1]) end |
.show(table_name = nil) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/dataduck/commands.rb', line 113 def self.show(table_name = nil) if table_name.nil? Dir[DataDuck.project_root + "/src/tables/*.rb"].each do |file| table_name_underscores = file.split("/").last.gsub(".rb", "") table_name_camelized = DataDuck::Util.underscore_to_camelcase(table_name_underscores) require file table = Object.const_get(table_name_camelized) if table <= DataDuck::Table puts table_name_underscores end end else table_name_camelized = DataDuck::Util.underscore_to_camelcase(table_name) require DataDuck.project_root + "/src/tables/#{ table_name }.rb" table_class = Object.const_get(table_name_camelized) if !(table_class <= DataDuck::Table) raise Exception.new("Table class #{ table_name_camelized } must inherit from DataDuck::Table") end table = table_class.new table.show end end |