[英]Rails - copy a Table from one schema to another schema in same database
Following is my setups:以下是我的设置:
Ruby version: 3.0.1红宝石版本:3.0.1
Rails Version: 6.1.3.2 Rails 版本:6.1.3.2
postgres (PostgreSQL) 9.6.17 postgres (PostgreSQL) 9.6.17
I am using gem ros-apartment (2.9.0) to manage tenants.我正在使用 gem ros-apartment (2.9.0) 来管理租户。 This is what I have in my apartment.rb
这就是我在我的公寓里的东西.rb
config.tenant_names = -> { Tenant.pluck :app_name }
Problem statement: I have a set of data across tables in the admin tenant.问题陈述:我在管理租户中有一组跨表的数据。 The schema name for the admin tenant is
pq-admin
.管理员租户的架构名称是
pq-admin
。 Now whenever a new tenant is created, I need to take all data from selected tables from pq-admin
schema and then put in the newly created tenant with schema (say test-tenant1
).现在,每当创建新租户时,我都需要从
pq-admin
模式中的选定表中获取所有数据,然后将新创建的租户与模式一起放入(比如test-tenant1
)。
To achieve this, the following is what I have written at the tenant.rb为了实现这一点,以下是我在tenant.rb上写的
class Tenant < ApplicationRecord
after_create :create_apartment_tenant, :populate_data
after_destroy :delete_apartment_tenant
private
def create_apartment_tenant
Apartment::Tenant.create(app_name)
end
def delete_apartment_tenant
Apartment::Tenant.drop(app_name)
end
def populate_data
%w[
pc_core_packages
pc_core_preorders
pc_core_delivery_times
pc_core_dynamic_columns
pc_core_product_items
pc_core_dynamic_options
pc_core_dynamic_values
pc_core_specifications
pc_core_images
pc_core_prices
pc_core_product_trees
pc_core_read_models_product_trees
pc_core_read_models_product_items
].each do |table|
query = "INSERT INTO #{app_name}.#{table} SELECT * FROM #{ENV['ADMIN_TENANT']}.#{table};"
ActiveRecord::Base.connection.exec_query(query)
# ENV['ADMIN_TENANT'] = 'pq-admin'
# the generated query = "INSERT INTO test-tenant1.pc_core_packages SELECT * FROM pq-admin.pc_core_packages;"
end
end
end
The Following is the error I encountered.以下是我遇到的错误。 Need help to resolve above described problem.
需要帮助解决上述问题。
SQL (0.6ms) INSERT INTO test-tenant1.pc_core_packages SELECT * FROM pq-admin.pc_core_packages;
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: syntax error at or near "-"
LINE 1: INSERT INTO test-tenant1.pc_core_packages SELECT * FROM pq-...
^
from /Users/bhagawatadhikari/.rvm/gems/ruby-3.0.1/gems/activerecord-6.1.3.2/lib/active_record/connection_adapters/postgresql_adapter.rb:672:in `exec_params'
Caused by PG::SyntaxError: ERROR: syntax error at or near "-"
LINE 1: INSERT INTO test-tenant1.pc_core_packages SELECT * FROM pq-...
^
from /Users/bhagawatadhikari/.rvm/gems/ruby-3.0.1/gems/activerecord-6.1.3.2/lib/active_record/connection_adapters/postgresql_adapter.rb:672:in `exec_params'
Need to delimit the identifier when including a dash (hyphen) in a schema or table name.在架构或表名称中包含破折号(连字符)时需要分隔标识符。 Updating your query to...
正在将您的查询更新为...
"INSERT INTO \"#{app_name}.#{table}" SELECT * FROM \"#{ENV['ADMIN_TENANT']}.#{table}\";"
Should do the trick.应该做的伎俩。
See... what's the escape sequence for hyphen (-) in PostgreSQL看... PostgreSQL 中连字符 (-) 的转义序列是什么
Though it might be a better practice to just exclude the hyphen from names to begin with.尽管从名称中排除连字符可能是更好的做法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.