Class: ShopifyToolkit::Migrator
- Inherits:
-
Object
- Object
- ShopifyToolkit::Migrator
- Includes:
- AdminClient, MetafieldStatements, MetaobjectStatements
- Defined in:
- lib/shopify_toolkit/migrator.rb
Overview
:nodoc:
Defined Under Namespace
Classes: MigrationProxy
Constant Summary
Constants included from AdminClient
Instance Attribute Summary collapse
-
#migrated_versions ⇒ Object
readonly
Returns the value of attribute migrated_versions.
-
#migrations ⇒ Object
readonly
Returns the value of attribute migrations.
-
#migrations_paths ⇒ Object
Returns the value of attribute migrations_paths.
Instance Method Summary collapse
- #current_version ⇒ Object
- #down ⇒ Object
- #executed_migrations ⇒ Object
-
#initialize(migrations_paths: self.class.migrations_paths) ⇒ Migrator
constructor
A new instance of Migrator.
- #load_migrations ⇒ Object
- #migration_files ⇒ Object
- #parse_migration_filename(filename) ⇒ Object
- #query(query, **variables) ⇒ Object
- #read_or_create_metafield ⇒ Object
- #redo ⇒ Object
- #up ⇒ Object
- #update_metafield ⇒ Object
Methods included from MetaobjectStatements
#add_field_validations_to_metaobject, #apply_pending_field_validations, log_time
Methods included from MetaobjectUtilities
#convert_validations_types_to_gids, #get_metaobject_definition_gid, #get_metaobject_definition_type_by_gid, #is_metaobject_reference_type?
Methods included from AdminClient
#api_version, #handle_shopify_admin_client_errors, #shopify_admin_client
Methods included from ShopifyToolkit::Migration::Logging
#announce, #say, #say_with_time, #write
Methods included from MetafieldStatements
define, #get_metafield_gid, log_time
Constructor Details
#initialize(migrations_paths: self.class.migrations_paths) ⇒ Migrator
Returns a new instance of Migrator.
16 17 18 19 20 |
# File 'lib/shopify_toolkit/migrator.rb', line 16 def initialize(migrations_paths: self.class.migrations_paths) @migrations_paths = migrations_paths @migrated_versions = ["migrated_versions"] @migrations = load_migrations # [MigrationProxy<Migration1>, MigrationProxy<Migration2>, ...] end |
Instance Attribute Details
#migrated_versions ⇒ Object (readonly)
Returns the value of attribute migrated_versions.
14 15 16 |
# File 'lib/shopify_toolkit/migrator.rb', line 14 def migrated_versions @migrated_versions end |
#migrations ⇒ Object (readonly)
Returns the value of attribute migrations.
14 15 16 |
# File 'lib/shopify_toolkit/migrator.rb', line 14 def migrations @migrations end |
#migrations_paths ⇒ Object
Returns the value of attribute migrations_paths.
11 12 13 |
# File 'lib/shopify_toolkit/migrator.rb', line 11 def migrations_paths @migrations_paths end |
Instance Method Details
#current_version ⇒ Object
22 23 24 |
# File 'lib/shopify_toolkit/migrator.rb', line 22 def current_version migrated.max || 0 end |
#down ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/shopify_toolkit/migrator.rb', line 41 def down # For now we'll just rollback the last one executed_migrations.last(1).each do |migration| migration.migrate(:down) migrated_versions.delete(migration.version) end ensure end |
#executed_migrations ⇒ Object
37 38 39 |
# File 'lib/shopify_toolkit/migrator.rb', line 37 def executed_migrations migrations.select { migrated_versions.include?(_1.version) } end |
#load_migrations ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/shopify_toolkit/migrator.rb', line 113 def load_migrations migrations = migration_files.map do |file| version, name, scope = parse_migration_filename(file) raise "missing version #{file}" unless version raise "missing name #{file}" unless name version = version.to_i name = name.camelize MigrationProxy.new(name, version, file, scope) end migrations.sort_by(&:version) end |
#migration_files ⇒ Object
104 105 106 107 |
# File 'lib/shopify_toolkit/migrator.rb', line 104 def migration_files paths = Array(migrations_paths) Dir[*paths.flat_map { |path| "#{path}/**/[0-9]*_*.rb" }] end |
#parse_migration_filename(filename) ⇒ Object
109 110 111 |
# File 'lib/shopify_toolkit/migrator.rb', line 109 def parse_migration_filename(filename) File.basename(filename).scan(/\A([0-9]+)_([_a-z0-9]*)\.?([_a-z0-9]*)?\.rb\z/).first end |
#query(query, **variables) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/shopify_toolkit/migrator.rb', line 51 def query(query, **variables) shopify_admin_client .query(query:, variables:) .tap { handle_shopify_admin_client_errors(_1) } .body .dig("data") end |
#read_or_create_metafield ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/shopify_toolkit/migrator.rb', line 82 def namespace = :shopify_toolkit key = :migrations value = query( "query ShopMetafield($namespace: String!, $key: String!) {shop {metafield(namespace: $namespace, key: $key) {value}}}", namespace:, key:, ).dig("shop", "metafield", "value") if value.nil? :shop, key, :json, namespace:, name: "Migrations metadata" return { "migrated_versions" => [] } end JSON.parse(value) end |
#redo ⇒ Object
99 100 101 102 |
# File 'lib/shopify_toolkit/migrator.rb', line 99 def redo down up end |
#up ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/shopify_toolkit/migrator.rb', line 26 def up pending_migrations = migrations.reject { migrated_versions.include?(_1.version) } pending_migrations.each do |migration| migration.migrate(:up) migrated_versions << migration.version end ensure end |
#update_metafield ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/shopify_toolkit/migrator.rb', line 59 def namespace = :shopify_toolkit key = :migrations value = { "migrated_versions" => migrated_versions.to_a.sort }.to_json owner_id = query(%(query GetShopGId { shop { id } })).dig("shop", "id") query = " mutation metafieldsSet($metafields: [MetafieldsSetInput!]!) {\n metafieldsSet(metafields: $metafields) {\n userErrors {\n field\n message\n }\n }\n }\n GRAPHQL\n query(query, metafields: [{ namespace:, key:, ownerId: owner_id, value: }])\n\n nil\nend\n" |