RAILS_ENV =开发rake db:create不创建开发数据库

时间:2018-06-06 12:44:05

标签: ruby-on-rails rake

我在docker容器上运行的rails应用程序上遇到了一个奇怪的问题。我运行RAILS_ENV=development db:drop db:create并尝试创建/删除 test数据库。

Rails 5.0.0

我的config / database.yml是

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: appname-api_development
  username: appname
  password: appname
  host: <%= ENV['DOCKER_DB_HOST'] || 'localhost' %>

test:
  <<: *default
  database: appname-api_test<%= ENV['TEST_ENV_NUMBER'] %>
  username: appname
  password: appname
  host: <%= ENV['DOCKER_DB_HOST'] || 'localhost' %>

这就是我得到的:

root@9176b57db829:/appname# RAILS_ENV=development rake db:drop db:create
Dropped database 'db'
Dropped database 'appname-api_test'
Created database 'db'
Created database 'appname-api_test'

这是在docker容器上运行的,并且没有为RAILS_ENVRACK_ENV或类似的东西设置env。

这是我的docker-compose.yml

version: '2'

services:
  db:
    image: postgres
    restart: always
    environment:
      POSTGRES_USER: appname
      POSTGRES_PASSWORD: appname
    volumes:
      - "${HOME}/.postgres-data:/var/lib/postgresql/data"
  redis:
    image: redis:3.2
  api:
    build: .
    environment:
      DOCKER_DB_HOST: 'db'
      REDIS_PROVIDER: 'redis://redis:6379/1'
      REDIS_URL: 'redis://redis:6379/1'
      SMTP_HOST: 'localhost:3000'
    # command: bundle exec rails s -b "0.0.0.0"
    volumes:
      - "${PWD}:/appname"
    ports:
      - "3000:3000"
      - "4000:4000"
    depends_on:
      - db
      - redis
    networks:
      default:
        aliases:
          - api

这是我的config/environments/development.rb文件:

# frozen_string_literal: true
Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  config.action_cable.url = ENV["ACTIONCABLE_URL"]
  config.action_cable.allowed_request_origins = [ ENV["FRONTEND_ORIGIN"] ]

  # Show full error reports.
  config.consider_all_requests_local = true

  # Enable/disable caching. By default caching is disabled.
  if Rails.root.join("tmp/caching-dev.txt").exist?
    config.action_controller.perform_caching = true

    config.cache_store = :memory_store
    config.public_file_server.headers = {
      "Cache-Control" => "public, max-age=172800",
    }
  else
    config.action_controller.perform_caching = false

    config.cache_store = :null_store
  end

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = false

  config.action_mailer.perform_caching = false

  config.action_mailer.delivery_method = :letter_opener

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise an error on page load if there are pending migrations.
  config.active_record.migration_error = :page_load

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = true

  # Suppress logger output for asset requests.
  config.assets.quiet = true

  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true

  # Uses Guard and LiveReload to reload html/css changes automatically
  config.middleware.insert_after ActionDispatch::Static, Rack::LiveReload
end

0 个答案:

没有答案