diff --git a/scripts/setup/initialize-database b/scripts/setup/initialize-database index 0bf8355898..f5a8500889 100755 --- a/scripts/setup/initialize-database +++ b/scripts/setup/initialize-database @@ -32,17 +32,6 @@ cd "$THIS_DIR/../.." ./manage.py migrate --noinput ./manage.py createcachetable third_party_api_results -if ! ./manage.py initialize_production_database; then - set +x - echo - echo -e '\033[32mPopulating default database failed.' - echo "After you fix the problem, you will need to do the following before rerunning this:" - echo " * supervisorctl stop all # to stop all services that might be accessing the database" - echo " * scripts/setup/postgres-init-db # run as root to drop and re-create the database" - echo -e '\033[0m' - exit 1 -fi - # Check if the supervisor socket exists. If not, it could be: # # A) A normal installation went bad (supervisor hasn't started) diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 963f5496f4..2b05f12ac3 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -53,6 +53,7 @@ from zerver.lib.realm_logo import get_realm_logo_data from zerver.lib.retention import move_messages_to_archive from zerver.lib.send_email import send_email, FromAddress, send_email_to_admins, \ clear_scheduled_emails, clear_scheduled_invitation_emails +from zerver.lib.server_initialization import create_internal_realm, server_initialized from zerver.lib.storage import static_path from zerver.lib.stream_subscription import ( get_active_subscriptions_for_stream_id, @@ -3753,6 +3754,9 @@ def do_create_realm(string_id: str, name: str, emails_restricted_to_domains: Optional[bool]=None) -> Realm: if Realm.objects.filter(string_id=string_id).exists(): raise AssertionError("Realm %s already exists!" % (string_id,)) + if not server_initialized(): + logging.info("Server not yet initialized. Creating the internal realm first.") + create_internal_realm() kwargs = {} # type: Dict[str, Any] if emails_restricted_to_domains is not None: diff --git a/zerver/lib/server_initialization.py b/zerver/lib/server_initialization.py index 8b9da25ff9..0db5cb555c 100644 --- a/zerver/lib/server_initialization.py +++ b/zerver/lib/server_initialization.py @@ -1,6 +1,5 @@ from django.conf import settings -from zerver.lib.actions import do_change_is_admin from zerver.lib.bulk_create import bulk_create_users from zerver.models import Realm, UserProfile, email_to_username, get_client, \ get_system_bot @@ -8,9 +7,11 @@ from zerver.models import Realm, UserProfile, email_to_username, get_client, \ from typing import Iterable, Optional, Tuple def server_initialized() -> bool: - return Realm.objects.count() > 0 + return Realm.objects.exists() def create_internal_realm() -> None: + from zerver.lib.actions import do_change_is_admin + realm = Realm.objects.create(string_id=settings.SYSTEM_BOT_REALM) # Create the "website" and "API" clients: diff --git a/zerver/management/commands/initialize_production_database.py b/zerver/management/commands/initialize_production_database.py deleted file mode 100644 index cdaf5939ac..0000000000 --- a/zerver/management/commands/initialize_production_database.py +++ /dev/null @@ -1,29 +0,0 @@ -from argparse import ArgumentParser -from typing import Any - -from django.conf import settings -from django.core.management.base import BaseCommand - -from zerver.lib.server_initialization import create_internal_realm, server_initialized - -settings.TORNADO_SERVER = None - -class Command(BaseCommand): - help = "Populate system realm and bots for a Zulip production server" - - def add_arguments(self, parser: ArgumentParser) -> None: - parser.add_argument('--extra-users', - dest='extra_users', - type=int, - default=0, - help='The number of extra users to create') - - def handle(self, *args: Any, **options: Any) -> None: - if server_initialized(): - print("Database already initialized; doing nothing.") - return - create_internal_realm() - - self.stdout.write("Successfully populated database with initial data.\n") - self.stdout.write("Please run ./manage.py generate_realm_creation_link " - "to generate link for creating organization") diff --git a/zerver/tests/test_realm.py b/zerver/tests/test_realm.py index a55a627649..b635c1dedf 100644 --- a/zerver/tests/test_realm.py +++ b/zerver/tests/test_realm.py @@ -35,6 +35,12 @@ class RealmTest(ZulipTestCase): new_realm_name: str) -> None: self.assertEqual(user_profile.realm.name, new_realm_name) + def test_realm_creation_ensures_internal_realms(self) -> None: + with mock.patch("zerver.lib.actions.server_initialized", return_value=False): + with mock.patch("zerver.lib.actions.create_internal_realm") as mock_create_internal: + do_create_realm("testrealm", "Test Realm") + mock_create_internal.assert_called_once() + def test_do_set_realm_name_caching(self) -> None: """The main complicated thing about setting realm names is fighting the cache, and we start by populating the cache for Hamlet, and we end