简体   繁体   English

Rake正在中止,因为sqlite db的未定义方法`inet'

[英]Rake is aborting because undefined method `inet' for sqlite db

My Rails application uses two databases, first is postgresql and second -sqlite, the last for test environment. My Rails应用程序使用两个数据库,第一个是postgresql,第二个是-sqlite,最后一个是测试环境。 I used Devise gem which uses inet data type to carring infomation about IP. 我使用了Devise gem,它使用inet数据类型来提供有关IP的信息。 It works well for postgresql, but couses some errors for sqlite. 它适用于postgresql,但是为sqlite提供了一些错误。 I had installed a gem postgres_ext and added it to Gemfile, but it didn't solve the problem. 我已经安装了一个gem postgres_ext并将其添加到Gemfile中,但它没有解决问题。 Below is a piece of 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)>'

and so on. 等等。

Can you tell me what I am doing wrong ? 你能告诉我我做错了什么吗?

PostgreSQL has a native inet type : PostgreSQL有一个本机inet类型

8.9.1. 8.9.1。 inet INET

The inet type holds an IPv4 or IPv6 host address, and optionally its subnet, all in one field. inet类型在一个字段中保存IPv4或IPv6主机地址,并且可选地包含其子网。 [...] [...]

but SQLite doesn't. 但SQLite没有。 Apparently your users table has an inet column in it so that part of your schema is PostgreSQL-specific. 显然,您的users表中有一个inet列,因此部分模式是PostgreSQL特有的。

Using a different database for testing than you use for development and production is just as bad an idea as using different databases for development and production. 使用不同的数据库进行测试而不是用于开发和生产,这与使用不同的数据库进行开发和生产一样糟糕。 ActiveRecord offers very little in the way of portability so switch your test environment to PostgreSQL too. ActiveRecord提供的可移植性很少,所以也将测试环境切换到PostgreSQL。

You can replace it with "string" type and it will work 你可以用“string”类型替换它,它会起作用

# 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

My CI/CD fires some tests to see that the build is correct, and I can't use the production database for those tests. 我的CI / CD会激活一些测试,看看构建是否正确,我不能将生产数据库用于这些测试。 It doesn't make sense to lose functionality and convert everything to string just because I want to test that the application has been deployed properly. 丢失功能并将所有内容转换为字符串是没有意义的,因为我想测试应用程序是否已正确部署。 So when I deploy I copy a modified database.yml that redefines test to: 因此,当我部署时,我复制了一个修改过的database.yml,它将test重新定义为:

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

This allows me to test locally and in Travis using a proper database (the same than in production) To avoid problems with sqlite, I redefine .inet inside the migration (only if the database is SQLIte3). 这允许我使用适当的数据库在本地和Travis中进行测试(与生产相同)为了避免sqlite的问题,我在迁移中重新定义.inet(仅当数据库是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM