我需要在我的Rails应用程序中查看SQL和存储过程,因此我必须从schema.rb更改为structure.sql。这是config/application.rb
config.active_record.schema_format = :sql
但是当我在seed.rb
中为实体创建新记录时Company.create(
name: 'Google',
subdomain: 'google'
)
发生错误
my_project / db / schema.rb尚不存在
我不知道问题是什么。我是否错过了cofig中的某些内容,某处仍然需要schema.rb或者我错过了一些rake任务命令?我刚用过
rake db:migrate db:test:prepare
关注此博客https://rietta.com/blog/2013/11/28/rails-and-sql-views-for-a-report/#setting-up-the-sql-view
更新:
我使用apartment-gem
和Company
实体是租户。
这是apartment.rb
Apartment.configure do |config|
config.excluded_models = %w{ Company CommonField }
config.tenant_names = lambda{ Company.pluck(:subdomain) }
# ==> PostgreSQL only options
config.use_schemas = true
# Apartment can be forced to use raw SQL dumps instead of schema.rb for
# creating new schemas.
# Use this when you are using some extra features in PostgreSQL that can't
# be respresented in
# schema.rb, like materialized views etc. (only applies with use_schemas set
# to true).
# (Note: this option doesn't use db/structure.sql, it creates SQL dump by
# executing pg_dump)
#
# config.use_sql = false
# <== PostgreSQL only options
config.prepend_environment = !Rails.env.production?
end
我尝试将config.use_schemas
更改为false
,然后启用并将config.use_sql
设置为true
,但仍无效。也许它只适用于PosrtgreSQL。
那么,对MySQL有什么设置吗?
答案 0 :(得分:1)
gem mysql2_adapter
中的 apartment
从 abstract_adapter
扩展而来,它继承了 create method 如下:
def create(tenant)
run_callbacks :create do
create_tenant(tenant)
switch(tenant) do
import_database_schema
# Seed data if appropriate
seed_data if Apartment.seed_after_create
yield if block_given?
end
end
end
import_database_schema
方法会抛出错误:FileNotFound ... db/schema.rb doesn't exist yet
如果没有这样的文件 schema.rb
,所以我猜当你决定使用 structure.sql
而不是 {{1} } 你删除了它。
我建议两种修复方法:
schema.rb
,即它。schema.rb
,因为这表明 gem use_schemas
将使用 apartment
和 postgresql schemas
,但是
在 mysql use
的情况下,如果您使用 mysql
,根本不需要 structure.sql
,对吗?所以你可以像下面这样创建新的 mysql 适配器:import_database_schema
然后加载该适配器,同时在初始化配置中同时打开 # your-app/lib/apartment/adapters/mysql2_structure_adapter.rb
require 'apartment/adapters/mysql2_adapter'
module Apartment
module Tenant
def self.mysql2_adapter(config)
if Apartment.use_schemas
Apartment.use_sql ?
Adapters::Mysql2StructureAdapter.new(config) :
Adapters::Mysql2SchemaAdapter.new(config)
else
Adapters::Mysql2Adapter.new(config)
end
end
end
module Adapters
class Mysql2StructureAdapter < Mysql2SchemaAdapter
def create(tenant)
run_callbacks :create do
create_tenant(tenant)
switch(tenant) do
# no need to import schema
# import_database_schema
# Seed data if appropriate
seed_data if Apartment.seed_after_create
yield if block_given?
end
end
end
end
end
end
和 use_schemas
use_sql