Code Monkey home page Code Monkey logo

Comments (12)

bithavoc avatar bithavoc commented on June 7, 2024 1

Recreating the schema_migrations table with INT works:

drop table schema_migrations;
create table schema_migrations(versions INT PRIMARY KEY);
rails db:schema:load

Success

from activerecord-cockroachdb-adapter.

bithavoc avatar bithavoc commented on June 7, 2024

Additionally, I'm unable to create a model in rails: generate_subscripts is used somewhere, my guess is using it to inspect the attributes of the table at runtime so it can insert the record:

FactoryGirl.create(:user)

ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR:  unknown function: generate_subscripts()
: SELECT a.attname
  FROM (
         SELECT indrelid, indkey, generate_subscripts(indkey, 1) idx
           FROM pg_index
          WHERE indrelid = '"users"'::regclass
            AND indisprimary
       ) i
  JOIN pg_attribute a
    ON a.attrelid = i.indrelid
   AND a.attnum = i.indkey[i.idx]
 ORDER BY i.idx

        from (irb):4

Edit: Yes, it's trying to inspect the primary keys, see https://github.com/rails/rails/blob/471bec310d1ecc50311f18b545df1d789d6bac0d/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb#L360

from activerecord-cockroachdb-adapter.

knz avatar knz commented on June 7, 2024

@jordanlewis plz file the following separate issues in the lang project:

  • insert int into string column (note: we may want to address this by pattern-matching the query instead of weird typing rule)
  • implement generate_subscripts

from activerecord-cockroachdb-adapter.

bithavoc avatar bithavoc commented on June 7, 2024

@knz this PR has the solution, can @jordanlewis review it? https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/20/files#diff-a6efa568aab40b05159b3148c29a54f9R200

from activerecord-cockroachdb-adapter.

Cikey avatar Cikey commented on June 7, 2024

The PR #20 didn't solve the initial issue for me. I could not run any rails tests or anything that required schema loading. Normal migrations using db:migrate are working fine.

Im using:

  • cockroachdb v2.0.1
  • cockroach adapter 0.2.3 (the readme still points to 0.2.2)
  • rails 5.2.0

Looks like, this line is the error source: https://github.com/rails/rails/blob/73d1f5f310e4dc3acb1ea502143d679f99931840/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb#L1057

The schema version like ActiveRecord::Schema.define(version: 2018_05_04_205847) do is always cast to an int. When executing the insert statement, the int value is not inside single quotes. Postgres allows automatic int to varchar conversions that cockroach seem to prevent: sqlfiddle

If i remove the version block from the schema.rb file, the error disappears.

I resolved the issue by monkeypatching the cockroachdb_adapter and 'fix' the assume_migrated_upto_version method as proof of concept:

module CockroachDb
    module SchemaFix
      def assume_migrated_upto_version(version, migrations_paths)
        migrations_paths = Array(migrations_paths)
        version = version.to_i
        sm_table = quote_table_name(ActiveRecord::SchemaMigration.table_name)

        migrated = ActiveRecord::SchemaMigration.all_versions.map(&:to_i)
        versions = migration_context.migration_files.map do |file|
          migration_context.parse_migration_filename(file).first.to_i
        end

        unless migrated.include?(version)
          execute "INSERT INTO #{sm_table} (version) VALUES ('#{quote(version)}')"
        end

        inserting = (versions - migrated).select { |v| v < version }
        if inserting.any?
          if (duplicate = inserting.detect { |v| inserting.count(v) > 1 })
            raise "Duplicate migration #{duplicate}. Please renumber your migrations to resolve the conflict."
          end
          if supports_multi_insert?
            execute insert_versions_sql(inserting)
          else
            inserting.each do |v|
              execute insert_versions_sql(v)
            end
          end
        end
      end
    end
end

require 'active_record/connection_adapters/cockroachdb_adapter'

ActiveRecord::ConnectionAdapters::CockroachDBAdapter.include(CockroachDb::SchemaFix)

Note the additional quotes around #{quote(version)} in execute "INSERT INTO #{sm_table} (version) VALUES ('#{quote(version)}')"

Im currently learning rails, so i don't know if this may break other thinks. By github search, this method seams only relevant to schema loading, so i hope its good. 🤔

I've seen that this adapter already has its own schema_statements.rb, so maybe we could add this method override to it?

from activerecord-cockroachdb-adapter.

knz avatar knz commented on June 7, 2024

Thanks for the hint!

from activerecord-cockroachdb-adapter.

Cikey avatar Cikey commented on June 7, 2024

I tried moving the monkeypatch to the cockroach/SchemaStatements.

Diff here

If this is an acceptable solution i could create a pull request i you want.

from activerecord-cockroachdb-adapter.

knz avatar knz commented on June 7, 2024

oh yes that would be nice

from activerecord-cockroachdb-adapter.

babelfish avatar babelfish commented on June 7, 2024

@jordanlewis Can we get a release with this fix?

from activerecord-cockroachdb-adapter.

eric-christian avatar eric-christian commented on June 7, 2024

@jordanlewis @knz any updates on this? What is blocking the next release? :)

from activerecord-cockroachdb-adapter.

knz avatar knz commented on June 7, 2024

@eric-christian thanks for the note. I think the PR #27 was merged successfully. @jordanlewis what does it take to make a "release"?

from activerecord-cockroachdb-adapter.

jordanlewis avatar jordanlewis commented on June 7, 2024

I think somebody needs to build and push the gem, and maybe update its version number. I can try to look at it. Sorry to everyone who's been waiting on this - we don't really have the bandwidth to maintain this gem.

from activerecord-cockroachdb-adapter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.