Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Multiple Schema migrates #31

Open
WilliamHH opened this issue Aug 12, 2021 · 3 comments
Open

Question: Multiple Schema migrates #31

WilliamHH opened this issue Aug 12, 2021 · 3 comments

Comments

@WilliamHH
Copy link

Thank you for this work ... I'm testing in an app at the moment with good success so far.
QUESTION: Is it possible to do multi schema updates at the same time for POSTGRESQL... e.g. TENANT_NAME=*?

@mik3y
Copy link
Owner

mik3y commented Aug 13, 2021

Hey there, thanks for sharing your feedback.

Is it possible to do multi schema updates at the same time for POSTGRESQL... e.g. TENANT_NAME=*?

No, there's no support for wildcard / "all tenant" operations. (The library isn't aware of how many tenants you have.) You would have to script this.

@WilliamHH
Copy link
Author

WilliamHH commented Aug 14, 2021 via email

@WilliamHH
Copy link
Author

Approx one year later and now working [a hobby project] to find a method for multi-schema migrations. With step assistance from various forums, I think the following script may assist db-multitenant users. I've done some basic testing but am not yet fully confident given my limited experience. Your trained eye will be appreciated.

commands/all_tenant_migrate.py

"""
A command to assist db-multitenant in mass migration of model changes.
Not tested to use when initially setting up a fresh Schema.

COMMAND : python manage.py all_tenant_migrate
"""

from django.conf import settings
from django.core.management.base import BaseCommand
from subprocess import Popen
from sys import stdout, stdin, stderr
import os
import psycopg2
import signal
import time

class Command(BaseCommand):

help = 'Run single makemigrations and migrate in sequence for all tenant schemas'

def handle(self, *args, **kwargs):

	# get all tenant names from schema list in database  
	conn = psycopg2.connect(
	host=settings.GET_SECRET('DATABASE_HOST'),
	database=settings.GET_SECRET('DATABASE_NAME'),
	user=settings.GET_SECRET('DATABASE_USER'),
	password=settings.GET_SECRET('DB_USER_PASSWORD'),)


	cursor = conn.cursor()
	cursor.execute("SELECT schema_name FROM information_schema.schemata WHERE schema_name NOT IN ('pg_toast','pg_catalog','information_schema', 'public');")
	tenants =[]
	tenants = cursor.fetchall() #values_list('tenant_name',flat=True)
	tenant_list = [list[0] for list in tenants]

	conn.commit()
	cursor.close()
	conn.close()	

	first_schema = tenant_list[0] # set a schema to run the leading 'makemigrations'
	
	commands = [f'TENANT_NAME={first_schema} python manage.py makemigrations ',]
	for schema in tenant_list:
		commands += f'TENANT_NAME={schema} python manage.py migrate',

	# OPTIONAL : commands.append('python manage.py runserver')

	proc_list = []

	for command in commands:
	    print("$ " + command)
	    proc = Popen(command, shell=True, stdin=stdin,
	                 stdout=stdout, stderr=stderr)
	    proc_list.append(proc)
	    time.sleep(2)
	
	for proc in proc_list:
	    os.kill(proc.pid, signal.SIGINT)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants