bir*_*ird 7 ruby mysql ruby-on-rails apartment-gem
我需要在我的 Rails 应用程序中查看 SQL 和存储过程,所以我必须从 schema.rb 更改为 structure.sql。这是我的配置config/application.rb
config.active_record.schema_format = :sql
Run Code Online (Sandbox Code Playgroud)
但是当我在 seed.rb 中为实体创建新记录时
Company.create(
name: 'Google',
subdomain: 'google'
)
Run Code Online (Sandbox Code Playgroud)
发生错误
my_project/db/schema.rb 还不存在
我不知道是什么问题。我是否错过了 cofig 中的某些内容,某处仍然需要 schema.rb 或者我错过了一些 rake 任务命令?我刚用
rake db:migrate db:test:prepare
Run Code Online (Sandbox Code Playgroud)
关注这个博客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
Run Code Online (Sandbox Code Playgroud)
我尝试更改config.use_schemas 为false然后启用并设置config.use_sql为true但它仍然无法正常工作。也许它仅适用于 PosrtgreSQL。
那么,对 MySQL 有任何设置吗?
gem继承自create方法,mysql2_adapter如下所示:apartmentabstract_adapter
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
Run Code Online (Sandbox Code Playgroud)
该import_database_schema方法将抛出错误:FileNotFound ... db/schema.rb doesn't exist yet如果没有这样的文件schema.rb,所以我猜当您决定使用structure.sql而不是schema.rb删除它时。
我建议两种修复方法:
schema.rb,就可以了。use_schemas,因为这表明 gemapartment将使用postgresql schemas和mysql use,但是如果mysql你使用,则根本structure.sql不需要,对吧?import_database_schema所以你可以创建新的 mysql 适配器,如下所示:# 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
Run Code Online (Sandbox Code Playgroud)
然后加载该适配器,同时打开两者use_schemas和use_sql初始化程序配置
# config/initializers/apartment.rb
Apartment.configure do |config|
# ...
config.use_schemas = true
config.use_sql = true
end
# ...
Dir[File.join(Rails.root, "lib", "apartment", "adapters", "**/*.rb")].each {|r| require r}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
587 次 |
| 最近记录: |