我们正在使用自定义任务增强db:create
和db:test:purge
任务,以创建架构并启用postgis
,uuid-ossp
和hstore
扩展,如下所示。< / p>
# lib/tasks/db_enhancements.rake
#
namespace :db do
desc 'create shared_extensions Schema'
task :extensions => :environment do
# Create Schema
ActiveRecord::Base.connection.execute 'CREATE SCHEMA IF NOT EXISTS shared_extensions;'
# Enable Hstore
ActiveRecord::Base.connection.execute 'CREATE EXTENSION IF NOT EXISTS HSTORE SCHEMA shared_extensions;'
# Enable UUID-OSSP
ActiveRecord::Base.connection.execute 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp" SCHEMA shared_extensions;'
# Enable Postgis
# Drop the postgis extension first
ActiveRecord::Base.connection.execute 'DROP EXTENSION "postgis"';
# Create postgis in shared_extensions
ActiveRecord::Base.connection.execute 'CREATE EXTENSION IF NOT EXISTS "postgis" SCHEMA shared_extensions;'
end
end
Rake::Task["db:create"].enhance do
Rake::Task["db:extensions"].invoke
end
Rake::Task["db:test:purge"].enhance do
Rake::Task["db:extensions"].invoke
end
上述任务在Rails 4.2.5中运行良好
rails db:create
任务应调用自定义任务,而后者又应创建shared_extensions
架构并启用扩展,如下所示。
test_application_test=# \dx
List of installed extensions
Name | Version | Schema | Description
-----------+---------+-------------------+---------------------------------------------------------------------
hstore | 1.3 | shared_extensions | data type for storing sets of (key, value) pairs
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
postgis | 2.2.1 | shared_extensions | PostGIS geometry, geography, and raster spatial types and functions
uuid-ossp | 1.0 | shared_extensions | generate universally unique identifiers (UUIDs)
在Rails 5.0.1 ----
中运行rails db:create
时,db:extensions
任务会在开发数据库中创建shared_extensions
模式,但不会在测试数据库中创建。另一方面,如果在运行rails db:create RAILS_ENV=test
之前运行rails db:create
,则会为测试数据库创建shared_extensions
架构。
如上所述,自定义任务在Rails 4.2.5中运行良好
答案 0 :(得分:0)
似乎rails db:create
正在发挥作用。我在GitHub rails / rails问题上创建了一个问题。 @rafaelfranca澄清说,在Rails 4.2 db:test:purge
中调用了db:create
,而不是在Rails 5.0中调用。{/ p>
目前,这是命令行的解决方法。
bin/rails db:environment:set RAILS_ENV=development
bin/rails db:drop
bin/rails db:create RAILS_ENV=test
bin/rails db:create
bin/rails db:migrate RAILS_ENV=test
bin/rails db:migrate