Skip to content

Type parsed twice with SQLite #436

@pointlessone

Description

@pointlessone

Describe the bug

A column type is parsed twice (successively) when the db is SQLite.

To Reproduce

#!/usr/bin/env ruby
# frozen_string_literal: true

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'sqlite3'
  gem 'rom-sql'
  gem 'dry-core'
  gem 'dry-auto_inject'
end

require 'rom'
require 'rom/sql'

rom_config = ROM::Configuration.new(:sql, 'sqlite::memory')
gateway = rom_config.default_gateway

migration = gateway.migration do
  change do
    create_table :items do
      primary_key :id
      column :tags, 'text', null: false
    end
  end
end

migration.apply(gateway.connection, :up)

module Structs
  class Item < ROM::Struct
  end
end

class Items < ROM::Relation[:sql]
  Tags = Types.define(Types::String) do
    input { |tags| ::JSON.dump(Array(tags)) }
    output { |json| Array(::JSON.parse(json)) }
  end

  schema :items, infer: true do
    attribute :tags, Tags
  end
end

rom_config.register_relation(Items)

container = Dry::Core::Container.new
container.register("persistence.rom", ROM.container(rom_config))

Deps = Dry::AutoInject(container)

class Repository < ROM::Repository::Root
  include Deps[container: "persistence.rom"]
  def find(id) = root.by_pk(id).one
end

class ItemRepo < Repository[:items]
  struct_namespace Structs
  commands :create #, update: :by_pk, delete: :by_pk
end

repo = ItemRepo.new

# binding.irb

repo.create(tags: ['foo'])

Expected behavior

I'd expect the type transformation to be applied once.

I was told that this works as expected with a pg db but fails with SQLite.

My environment

  • Affects my production application: NO
  • Ruby version: 3.4.1
  • OS: macOS
  • DB: SQLite

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions