The rake is interrupted because the undefined `inet 'method for sqlite db

My Rails application uses two databases: first postgresql and second -sqlite, the latter for a test environment. I used the Devise gem, which uses the inet data type to process IP information. It works well for postgresql, but has some errors for sqlite. I installed gem postgres_ext and added it to the Gemfile, but this did not solve the problem. The following is a snippet of the log:

$ RAILS_ENV=test rake db:setup (in /user/project) db/test.sqlite3 already exists -- enable_extension("plpgsql") -> 0.0030s -- create_table("comments", {:force=>:cascade}) -> 0.3368s -- add_index("comments", ["commentable_id"], {:name=>"index_comments_on_commentable_id", :using=>:btree}) -> 0.1680s -- add_index("comments", ["commentable_type"], {:name=>"index_comments_on_commentable_type", :using=>:btree}) -> 0.1898s -- add_index("comments", ["user_id"], {:name=>"index_comments_on_user_id", :using=>:btree}) -> 0.1568s -- create_table("contacts", {:force=>:cascade}) -> 0.2803s -- create_table("delayed_jobs", {:force=>:cascade}) -> 0.2818s -- add_index("delayed_jobs", ["priority", "run_at"], {:name=>"delayed_jobs_priority", :using=>:btree}) -> 0.1677s -- create_table("exercises", {:force=>:cascade}) -> 0.3360s -- create_table("languages", {:force=>:cascade}) -> 0.3025s -- create_table("levels", {:force=>:cascade}) -> 0.2692s -- create_table("solutions", {:force=>:cascade}) -> 0.3262s -- create_table("users", {:force=>:cascade}) rake aborted! NoMethodError: undefined method `inet' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x00000007563ec8> /home/mtczerwinski/Projekty/ruby/railsilla/db/schema.rb:104:in `block (2 levels) in <top (required)>' 

etc.

Can you tell me what I'm doing wrong?

+5
source share
3 answers

PostgreSQL has its own inet type :

8.9.1. internet

The inet type contains the IPv4 or IPv6 host address and, possibly, its subnet, all in one field. [...]

but SQLite does not. Apparently your users table has an inet column, so part of your schema depends on PostgreSQL.

Using a different database for testing than you use for development and production is as bad as using different databases for development and production. ActiveRecord offers very little portability, so switch your test environment to PostgreSQL.

+14
source

You can replace it with a "string" type, and it will work

 # pg # t.inet :current_sign_in_ip # t.inet :last_sign_in_ip # mysql t.string :current_sign_in_ip t.string :last_sign_in_ip 
+12
source

My CI / CD runs some tests to see that the build is correct, and I cannot use the production database for these tests. It makes no sense to lose functionality and convert everything to a string just because I want to verify that the application is deployed correctly. So when I deploy, I copy the modified database.yml file, which overrides the test:

 test: adapter: sqlite3 database: db/test.sqlite3 pool: 5 timeout: 5000 

This allows me to test locally and in Travis using the appropriate database (just like in production) To avoid sqlite problems, I override .inet inside the migration (only if the database is SQLIte3).

  # Redefine inet to string for SQLite3 if t.class.to_s.include? "::SQLite3" t.define_singleton_method(:inet) do |*args| t.string *args end end 
0
source

Source: https://habr.com/ru/post/1210693/


All Articles