Class: ShopifyToolkit::CommandLine

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/shopify_toolkit/command_line.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

RESERVED_COLUMN_NAMES =
%w[select type id]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/shopify_toolkit/command_line.rb', line 114

def self.exit_on_failure?
  true
end

Instance Method Details

#analyze(csv_path) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/shopify_toolkit/command_line.rb', line 25

def analyze(csv_path)
  csv_path = File.expand_path(csv_path)
  underscore = ->(string) { string.downcase.gsub(/[^a-z0-9]+/, "_").gsub(/(^_+|_+$)/, "") }
  csv = CSV.open(csv_path, liberal_parsing:true )
  header_to_column = -> { RESERVED_COLUMN_NAMES.include?(_1.to_s) ? "#{_1}_1" : _1 }
  headers = csv.shift.map(&underscore).map(&header_to_column)
  basename = File.basename csv_path
  database = "#{options[:tmp_dir]}/shopify-toolkit-analyze-#{underscore[basename]}.sqlite3"
  should_import = options[:force_import] || !File.exist?(database)
  to_record = ->(row) { headers.zip(row.each{ |c| c.delete!("\u0000") if String === c }).to_h.transform_keys(&header_to_column) }

  File.delete(database) if should_import && File.exist?(database)

  ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database:)

  if should_import
    puts "==> Importing #{csv_path} into #{database}"
    ActiveRecord::Schema.define do
      create_table :results, force: true do |t|
        t.json :data
        headers.each { |header| t.string header }
      end
      add_index :results, :import_result if headers.include?('import_result')
    end
    csv.each_slice(5000) { |rows| print "."; Result.insert_all(rows.map(&to_record)) }
    puts
  end

  puts "==> Starting console for #{basename}"
  require "irb"
  IRB.conf[:IRB_NAME] = basename
  Result.class_eval { binding.irb(show_code: false) }
end

#generate_migration(name) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/shopify_toolkit/command_line.rb', line 90

def generate_migration(name)
  require "./config/environment"
  migrations_dir = Rails.root.join("config/shopify/migrate")
  file_name = "#{Time.now.utc.strftime('%Y%m%d%H%M%S')}_#{name.underscore}.rb"

  if migrations_dir.entries.map(&:to_s).grep(/\A\d+_#{Regexp.escape name.underscore}\.rb\z/).any?
    raise Thor::Error, "Migration class already exists: #{file_name}"
  end

  create_file migrations_dir.join(file_name) do
    <<~RUBY
      class #{name.camelize} < ShopifyToolkit::Migration
        def up
          # Add your migration code here
        end

        def down
          # Add your rollback code here
        end
      end
    RUBY
  end
end

#migrateObject



60
61
62
63
# File 'lib/shopify_toolkit/command_line.rb', line 60

def migrate
  require "./config/environment"
  ::Shop.sole.with_shopify_session { ShopifyToolkit::Migrator.new.up }
end

#redoObject



72
73
74
75
# File 'lib/shopify_toolkit/command_line.rb', line 72

def redo
  require "./config/environment"
  ::Shop.sole.with_shopify_session { ShopifyToolkit::Migrator.new.redo }
end

#rollbackObject



66
67
68
69
# File 'lib/shopify_toolkit/command_line.rb', line 66

def rollback
  require "./config/environment"
  ::Shop.sole.with_shopify_session { ShopifyToolkit::Migrator.new.down }
end

#schema_dumpObject



84
85
86
87
# File 'lib/shopify_toolkit/command_line.rb', line 84

def schema_dump
  require "./config/environment"
  ::Shop.sole.with_shopify_session { ShopifyToolkit::Schema.dump! }
end

#schema_loadObject



78
79
80
81
# File 'lib/shopify_toolkit/command_line.rb', line 78

def schema_load
  require "./config/environment"
  ::Shop.sole.with_shopify_session { ShopifyToolkit::Schema.load! }
end