在Rails 5.0.1 / PostgreSQL中增强数据库任务

时间:2017-01-09 05:30:06

标签: ruby-on-rails postgresql ruby-on-rails-5

我们正在使用自定义任务增强db:createdb:test:purge任务,以创建架构并启用postgisuuid-ossphstore扩展,如下所示。< / 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中运行良好

1 个答案:

答案 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