Class: DataDuck::Commands

Inherits:
Object
  • Object
show all
Defined in:
lib/dataduck/commands.rb

Defined Under Namespace

Classes: Namespace

Class Method Summary collapse

Class Method Details

.acceptable_commandsObject



36
37
38
# File 'lib/dataduck/commands.rb', line 36

def self.acceptable_commands
  ['c', 'console', 'd', 'dbconsole', 'etl', 'quickstart', 'recreate', 'show']
end

.cObject



58
59
60
# File 'lib/dataduck/commands.rb', line 58

def self.c
  self.console
end

.consoleObject



62
63
64
65
66
# File 'lib/dataduck/commands.rb', line 62

def self.console
  require "irb"
  ARGV.clear
  IRB.start
end

.d(where = "destination") ⇒ Object



68
69
70
# File 'lib/dataduck/commands.rb', line 68

def self.d(where = "destination")
  self.dbconsole(where)
end

.dbconsole(where = "destination") ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dataduck/commands.rb', line 72

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(*table_names_underscore) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/dataduck/commands.rb', line 97

def self.etl(*table_names_underscore)
  if table_names_underscore.length == 0
    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

  etl = nil
  if table_names_underscore.length == 1 && table_names_underscore[0] == "all"
    etl = ETL.new(destinations: [only_destination], autoload_tables: true)
  else
    tables = []
    table_names_underscore.each do |table_name|
      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 "Table class #{ table_name_camelized } must inherit from DataDuck::Table"
      end
      table = table_class.new
      tables << table
    end
    etl = ETL.new({
        destinations: [only_destination],
        autoload_tables: false,
        tables: tables
    })
  end
  etl.process!
  
  if etl.errored?
    exit(1)
  end
end

.helpObject



133
134
135
136
# File 'lib/dataduck/commands.rb', line 133

def self.help
  puts "Usage: dataduck commandname"
  puts "Commands: #{ acceptable_commands.sort.join(' ') }"
end

.prompt_choices(choices = []) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dataduck/commands.rb', line 21

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

.quickstartObject



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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/dataduck/commands.rb', line 193

def self.quickstart
  puts "Welcome to DataDuck!"
  puts "This quickstart wizard will help you set up DataDuck."

  puts "What is your work email address?"
  email = STDIN.gets.strip
  self.quickstart_register_email(email)

  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("source1", {
      '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|
    begin
      DataDuck::Commands.quickstart_create_table(table_name, db_source)
    rescue
    end
  end

  config_obj = {
    'users' => {
        email => {
            'admin' => true
        }
    },
    'sources' => {
      'source1' => {
        'type' => db_type.to_s,
        'host' => source_host,
        'database' => source_database,
        'port' => source_port,
        'username' => source_username,
      }
    },
    'destinations' => {
      'destination1' => {
        'type'  => 'redshift',
        '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',
      }
    }
  }
  DataDuck::Commands.quickstart_save_file("#{ DataDuck.project_root }/config/base.yml", config_obj.to_yaml)

  DataDuck::Commands.quickstart_save_file("#{ DataDuck.project_root }/.env", """
destination1_aws_key=AWS_KEY_GOES_HERE
destination1_aws_secret=AWS_SECRET_GOES_HERE
destination1_password=REDSHIFT_PASSWORD_GOES_HERE
source1_password=#{ source_password }
""".strip)

  DataDuck::Commands.quickstart_update_gitignore

  puts "Quickstart complete!"
  puts "You still need to edit your .env and config/base.yml files with your AWS and Redshift credentials."
  puts "Run your ETL with: dataduck etl all"
  puts "For more help, visit http://dataducketl.com/docs"
end

.quickstart_create_schedule_configObject



327
328
329
330
331
332
# File 'lib/dataduck/commands.rb', line 327

def self.quickstart_create_schedule_config
  namespace = Namespace.new
  template = File.open("#{ DataDuck.gem_root }/lib/templates/quickstart/schedule.rb.erb", 'r').read
  result = ERB.new(template).result(namespace.get_binding)
  DataDuck::Commands.quickstart_save_file("#{ DataDuck.project_root }/config/schedule.rb", result)
end

.quickstart_create_table(table_name, db) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/dataduck/commands.rb', line 307

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_register_email(email) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/dataduck/commands.rb', line 173

def self.quickstart_register_email(email)
  registration_data = {
      email: email,
      version: DataDuck::VERSION,
      source: "quickstart"
  }

  request = Typhoeus::Request.new(
      "dataducketl.com/api/v1/register",
      method: :post,
      body: registration_data,
      timeout: 30,
      connecttimeout: 10,
  )

  hydra = Typhoeus::Hydra.new
  hydra.queue(request)
  hydra.run
end

.quickstart_save_file(output_path_full, contents) ⇒ Object



334
335
336
337
338
339
340
341
342
# File 'lib/dataduck/commands.rb', line 334

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_update_gitignoreObject



298
299
300
301
302
303
304
305
# File 'lib/dataduck/commands.rb', line 298

def self.quickstart_update_gitignore
  main_gitignore_path = "#{ DataDuck.project_root }/.gitignore"
  FileUtils.touch(main_gitignore_path)
  output = File.open(main_gitignore_path, "w")
  output << ".DS_Store\n"
  output << ".env\n"
  output.close
end

.recreate(table_name) ⇒ Object



138
139
140
141
142
143
144
145
146
147
# File 'lib/dataduck/commands.rb', line 138

def self.recreate(table_name)
  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 "Table class #{ table_name_camelized } must inherit from DataDuck::Table"
  end
  table = table_class.new
  table.recreate!(DataDuck::Destination.only_destination)
end

.route_command(args) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dataduck/commands.rb', line 40

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

  begin
    DataDuck::Commands.public_send(command, *args[1..-1])
  rescue => err
    DataDuck::Logs.error(err)
  end
end

.show(table_name = nil) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/dataduck/commands.rb', line 149

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 "Table class #{ table_name_camelized } must inherit from DataDuck::Table"
    end

    table = table_class.new
    table.show
  end
end