【问题标题】:django-tenant-schemas: Migrate data from shared schema to multi tenant schemadjango-tenant-schemas:将数据从共享模式迁移到多租户模式
【发布时间】:2016-05-10 00:13:39
【问题描述】:

如何使用 django-tenant-schemas 将数据从共享模式迁移到多租户模式?

我们有一个 saas,最初没有使用 django-tenant-schemas,而是使用共享数据库、共享模式方法。我们现在发现了 django-tenant-schemas 并认为这是正确的方法。

现在的问题是如何将数据从单一的public 架构迁移到独立的租户架构。

django-tenant-schemas 文档说明如下:

注意:如果这是您第一次使用,您的数据库应该是空的 运行这个命令。

在我现有的应用程序中,我有一个租户表,所有其他模型都有 ForeignKey,但所有内容都在公共架构中。我使用south 迁移。现在我需要将所有这些租户数据迁移到单独的模式。如何做同样的事情?

【问题讨论】:

  • 你找到方法了吗?

标签: python django django-models multi-tenant django-south


【解决方案1】:

这样做有一个技巧。首先,您应该了解 psql 转储和加载命令。

步骤:

  1. 您以前的 django 应用程序包含一个数据库,其中所有表都在 public 模式中。使用pg_dump 命令将其转储到pgsql 文件中。比如说database.pgsql
  2. 编写一个 shell 脚本,首先将这个转储加载到一个临时数据库,比如说temp_db
  3. 由于它将转储到的架构是公共架构,请将架构名称更改为首选架构,以使用租户架构实现的数据库中的首选架构,比如tenant_xyz
  4. 再次转储,这次是架构 tenant_xyztenant_xyz.pgsql
  5. tenant_xyz 架构转储加载到租户架构实现的数据库中,比如说 postgres
  6. 现在,已加载转储的tenant_xyz 架构将在首选架构内的postgres 数据库中可用。

脚本如下:

psql -U postgres -c "DROP DATABASE IF EXISTS temp_db"
psql -U postgres -c "CREATE DATABASE temp_db"
psql -U postgres temp_db < database.pgsql
psql -U postgres -d temp_db -c "ALTER SCHEMA public RENAME TO teanat_xyz"
pg_dump -U postgres -d temp_db -n tenant_xyz > tenant_xyz.pgsql
psql -U postgres -c "DROP DATABASE temp_db"
psql -U postgres -c "DROP SCHEMA IF EXISTS tenant_xyz CASCADE"
psql -U postgres postgres < tenant_xyz.pgsql

【讨论】:

    猜你喜欢
    • 2020-08-01
    • 2016-10-30
    • 1970-01-01
    • 2019-05-18
    • 2014-01-16
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 2022-11-10
    相关资源
    最近更新 更多