Skip to content

Instantly share code, notes, and snippets.

@mrdotb
Created November 29, 2023 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrdotb/6c3ab23360dc05e96d6b3197c5080e98 to your computer and use it in GitHub Desktop.
Save mrdotb/6c3ab23360dc05e96d6b3197c5080e98 to your computer and use it in GitHub Desktop.
Flow step transaction error log + doc issue

Flow Step transaction error log + doc issue

Application.put_env(:ash, :validate_api_resource_inclusion?, false)
Application.put_env(:ash, :validate_api_config_inclusion?, false)
Mix.install([:ash, :faker], consolidate_protocols: false)

Stuff

defmodule Doc.User do
  use Ash.Resource, data_layer: Ash.DataLayer.Mnesia

  mnesia do
    table(:doc_user)
  end

  code_interface do
    define_for(Doc.Api)

    define(:create)
  end

  actions do
    defaults([:create, :update])
  end

  attributes do
    uuid_primary_key(:id)
    attribute(:first_name, :string)
    attribute(:last_name, :string)
  end
end

defmodule Doc.Api do
  use Ash.Api

  resources do
    resource(Doc.User)
  end
end
:mnesia.create_schema([Node.self()])
:mnesia.create_table(:doc_user, attributes: [:id, :first_name, :last_name])
:mnesia.start()
Ash.DataLayer.Mnesia.start(Doc.Api)
Doc.User.create(%{first_name: Faker.Person.first_name(), last_name: Faker.Person.last_name()})
defmodule Doc.TestFlow1 do
  use Ash.Flow

  flow do
    api(Doc.Api)
  end

  steps do
    transaction :create_users do
      create :create_a, Doc.User, :create do
        input(%{
          first_name: Faker.Person.first_name(),
          last_name: Faker.Person.last_name()
        })
      end

      create :create_b, Doc.User, :create do
        input(%{
          first_name: Faker.Person.first_name(),
          last_name: Faker.Person.last_name()
        })
      end
    end
  end
end
# Produce a error log but result is valid and complete
Doc.TestFlow1.run()
defmodule Doc.TestFlow2 do
  use Ash.Flow

  flow do
    api(Doc.Api)
  end

  steps do
    transaction :create_users, Doc.User do
      # touches_resources [Doc.User]
      create :create_a, Doc.User, :create do
        input(%{
          first_name: Faker.Person.first_name(),
          last_name: Faker.Person.last_name()
        })
      end

      create :create_b, Doc.User, :create do
        input(%{
          first_name: Faker.Person.first_name(),
          last_name: Faker.Person.last_name()
        })
      end
    end
  end
end
# Seems to be the correct way
Doc.TestFlow2.run()

This piece of code does not work it's just to see if this is the correct way to do things

defmodule Doc.TestFlow3 do
  use Ash.Flow

  flow do
    api(Doc.Api)
  end

  steps do
    transaction :create_user_and_book do
      touches_resources([Doc.User, Doc.Book])

      create :create_user, Doc.User, :create do
        input(%{
          first_name: Faker.Person.first_name(),
          last_name: Faker.Person.last_name()
        })
      end

      create :create_book, Doc.Book, :create do
        input(%{
          user_id: path(result(:create_user), :id),
          title: Faker.StarWars.character()
        })
      end
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment