python - django-租户模式 : Migrate data from shared schema to multi tenant schema

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

如何使用 django-tenant-schema 将数据从共享模式迁移到 Multi-Tenancy 模式?

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

现在的问题是如何从单个 public 迁移数据架构到独立租户架构。
django-tenant-schemas文档说明如下:

Note: your database should be empty if this is the first time you’re running this command.



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

最佳答案

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

脚步:

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

  • 脚本如下:
    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
    

    关于python - django-租户模式 : Migrate data from shared schema to multi tenant schema,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35132316/

    相关文章:

    javascript - 如何使用 javascript 和 django 设置两列布局

    相关对象的 Django 查询集,在对原始模型进行预过滤之后

    Django - 如何防止创建数据库外键约束

    python - 让 Python 3.3 和 numpy 工作起来很困难

    python - 如何初始化特定name_scope下的局部变量

    python - 如何将此 SQL 转换为 Django 查询?

    python - 使用 inspectdb 时未生成 models.py

    django - 如何在Django admin中显示没有值的值?

    python - 在tox+pytest中使用自己的包

    python - 有 PyMedia 的替代品吗