Class: ShopifyToolkit::Migration

Inherits:
Object
  • Object
show all
Includes:
AdminClient, MetafieldStatements, MetaobjectStatements, Logging
Defined in:
lib/shopify_toolkit/migration.rb

Defined Under Namespace

Modules: Logging Classes: IrreversibleMigration

Constant Summary

Constants included from AdminClient

AdminClient::API_VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#say, #say_with_time, #write

Methods included from MetaobjectStatements

define, #get_metaobject_definition_gid, log_time, #update_metaobject_definition

Methods included from AdminClient

#api_version, #handle_shopify_admin_client_errors, #shopify_admin_client

Methods included from MetafieldStatements

define, #get_metafield_gid, log_time

Constructor Details

#initialize(name, version) ⇒ Migration

Returns a new instance of Migration.



30
31
32
33
# File 'lib/shopify_toolkit/migration.rb', line 30

def initialize(name, version)
  @name    = name
  @version = version
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



28
29
30
# File 'lib/shopify_toolkit/migration.rb', line 28

def name
  @name
end

#versionObject (readonly)

Returns the value of attribute version.



28
29
30
# File 'lib/shopify_toolkit/migration.rb', line 28

def version
  @version
end

Class Method Details

.[](api_version) ⇒ Object



22
23
24
25
26
# File 'lib/shopify_toolkit/migration.rb', line 22

def self.[](api_version)
  klass = Class.new(self)
  klass.const_set(:API_VERSION, api_version)
  klass
end

Instance Method Details

#announce(message) ⇒ Object



35
36
37
# File 'lib/shopify_toolkit/migration.rb', line 35

def announce(message)
  super "#{version} #{name}: #{message}"
end

#downObject



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

def down
  raise IrreversibleMigration
end

#loggerObject



14
15
16
17
18
19
20
# File 'lib/shopify_toolkit/migration.rb', line 14

def logger
  @logger ||= ActiveSupport::Logger.new(STDOUT).tap do |logger|
    logger.formatter = proc do |severity, datetime, progname, msg|
      "#{severity}: #{msg}\n"
    end
  end
end

#migrate(direction) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/shopify_toolkit/migration.rb', line 39

def migrate(direction)
  case direction
  when :up
    announce("migrating")
    time_elapsed = ActiveSupport::Benchmark.realtime { up }
    announce("migrated (%.4fs)" % time_elapsed)

  when :down
    announce("reverting")
    time_elapsed = ActiveSupport::Benchmark.realtime { down }
    announce("reverted (%.4fs)" % time_elapsed)

  else
    raise ArgumentError, "Unknown migration direction: #{direction}"
  end
end

#upObject



56
57
58
# File 'lib/shopify_toolkit/migration.rb', line 56

def up
  # Implement in subclass
end