Esempio n. 1
0
 def _boolean_input(self, question, default=None):
     result = input("%s " % question)
     if not result and default is not None:
         return default
     while len(result) < 1 or result[0].lower() not in "yn":
         result = input("Please answer yes or no: ")
     return result[0].lower() == "y"
Esempio n. 2
0
 def _choice_input(self, question, choices):
     print(question)
     for i, choice in enumerate(choices):
         print(" %s) %s" % (i + 1, choice))
     result = input("Select an option: ")
     while True:
         try:
             value = int(result)
             if 0 < value <= len(choices):
                 return value
         except ValueError:
             pass
         result = input("Please select a valid option: ")
Esempio n. 3
0
    def _create_test_db(self, verbosity, autoclobber, keepdb=False):
        test_database_name = self._get_test_db_name()

        if keepdb:
            return test_database_name
        if not self.connection.is_in_memory_db(test_database_name):
            # Erase the old test database
            if verbosity >= 1:
                print("Destroying old test database for alias %s..." %
                      (self._get_database_display_str(verbosity,
                                                      test_database_name), ))
            if os.access(test_database_name, os.F_OK):
                if not autoclobber:
                    confirm = input(
                        "Type 'yes' if you would like to try deleting the test "
                        "database '%s', or 'no' to cancel: " %
                        test_database_name)
                if autoclobber or confirm == 'yes':
                    try:
                        os.remove(test_database_name)
                    except Exception as e:
                        sys.stderr.write(
                            "Got an error deleting the old test database: %s\n"
                            % e)
                        sys.exit(2)
                else:
                    print("Tests cancelled.")
                    sys.exit(1)
        return test_database_name
Esempio n. 4
0
    def handle(self, **options):
        database = options['database']
        connection = connections[database]
        verbosity = options['verbosity']
        interactive = options['interactive']
        # The following are stealth options used by Django's internals.
        reset_sequences = options.get('reset_sequences', True)
        allow_cascade = options.get('allow_cascade', False)
        inhibit_post_migrate = options.get('inhibit_post_migrate', False)

        self.style = no_style()

        # Import the 'management' module within each installed app, to register
        # dispatcher events.
        for app_config in apps.get_app_configs():
            try:
                import_module('.management', app_config.name)
            except ImportError:
                pass

        sql_list = sql_flush(self.style, connection, only_django=True,
                             reset_sequences=reset_sequences,
                             allow_cascade=allow_cascade)

        if interactive:
            confirm = input("""You have requested a flush of the database.
This will IRREVERSIBLY DESTROY all data currently in the %r database,
and return each table to an empty state.
Are you sure you want to do this?

    Type 'yes' to continue, or 'no' to cancel: """ % connection.settings_dict['NAME'])
        else:
            confirm = 'yes'

        if confirm == 'yes':
            try:
                with transaction.atomic(using=database,
                                        savepoint=connection.features.can_rollback_ddl):
                    with connection.cursor() as cursor:
                        for sql in sql_list:
                            cursor.execute(sql)
            except Exception as e:
                new_msg = (
                    "Database %s couldn't be flushed. Possible reasons:\n"
                    "  * The database isn't running or isn't configured correctly.\n"
                    "  * At least one of the expected database tables doesn't exist.\n"
                    "  * The SQL was invalid.\n"
                    "Hint: Look at the output of 'django-admin sqlflush'. "
                    "That's the SQL this command wasn't able to run.\n"
                    "The full error: %s") % (connection.settings_dict['NAME'], e)
                six.reraise(CommandError, CommandError(new_msg), sys.exc_info()[2])

            # Empty sql_list may signify an empty database and post_migrate would then crash
            if sql_list and not inhibit_post_migrate:
                # Emit the post migrate signal. This allows individual applications to
                # respond as if the database had been migrated from scratch.
                emit_post_migrate_signal(verbosity, interactive, database)
        else:
            self.stdout.write("Flush cancelled.\n")
Esempio n. 5
0
    def _ask_default(self, default=''):
        """
        Prompt for a default value.

        The ``default`` argument allows providing a custom default value (as a
        string) which will be shown to the user and used as the return value
        if the user doesn't provide any other input.
        """
        print("Please enter the default value now, as valid Python")
        if default:
            print("You can accept the default '{}' by pressing 'Enter' or you "
                  "can provide another value.".format(default))
        print(
            "The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now"
        )
        print("Type 'exit' to exit this prompt")
        while True:
            if default:
                prompt = "[default: {}] >>> ".format(default)
            else:
                prompt = ">>> "
            if six.PY3:
                # Six does not correctly abstract over the fact that
                # py3 input returns a unicode string, while py2 raw_input
                # returns a bytestring.
                code = input(prompt)
            else:
                code = input(prompt).decode(sys.stdin.encoding)
            if not code and default:
                code = default
            if not code:
                print(
                    "Please enter some code, or 'exit' (with no quotes) to exit."
                )
            elif code == "exit":
                sys.exit(1)
            else:
                try:
                    return eval(code, {}, {
                        "datetime": datetime_safe,
                        "timezone": timezone
                    })
                except (SyntaxError, NameError) as e:
                    print("Invalid input: %s" % e)
Esempio n. 6
0
    def _create_test_db(self, verbosity, autoclobber, keepdb=False):
        """
        Internal implementation - creates the test db tables.
        """
        suffix = self.sql_table_creation_suffix()

        test_database_name = self._get_test_db_name()

        qn = self.connection.ops.quote_name

        # Create the test database and connect to it.
        with self._nodb_connection.cursor() as cursor:
            try:
                cursor.execute("CREATE DATABASE %s %s" %
                               (qn(test_database_name), suffix))
            except Exception as e:
                # if we want to keep the db, then no need to do any of the below,
                # just return and skip it all.
                if keepdb:
                    return test_database_name

                sys.stderr.write(
                    "Got an error creating the test database: %s\n" % e)
                if not autoclobber:
                    confirm = input(
                        "Type 'yes' if you would like to try deleting the test "
                        "database '%s', or 'no' to cancel: " %
                        test_database_name)
                if autoclobber or confirm == 'yes':
                    try:
                        if verbosity >= 1:
                            print(
                                "Destroying old test database for alias %s..."
                                % (self._get_database_display_str(
                                    verbosity, test_database_name), ))
                        cursor.execute("DROP DATABASE %s" %
                                       qn(test_database_name))
                        cursor.execute("CREATE DATABASE %s %s" %
                                       (qn(test_database_name), suffix))
                    except Exception as e:
                        sys.stderr.write(
                            "Got an error recreating the test database: %s\n" %
                            e)
                        sys.exit(2)
                else:
                    print("Tests cancelled.")
                    sys.exit(1)

        return test_database_name
Esempio n. 7
0
 def _handle_objects_preventing_db_destruction(self, cursor, parameters,
                                               verbosity, autoclobber):
     # There are objects in the test tablespace which prevent dropping it
     # The easy fix is to drop the test user -- but are we allowed to do so?
     print(
         "There are objects in the old test database which prevent its destruction."
     )
     print(
         "If they belong to the test user, deleting the user will allow the test "
         "database to be recreated.")
     print(
         "Otherwise, you will need to find and remove each of these objects, "
         "or use a different tablespace.\n")
     if self._test_user_create():
         if not autoclobber:
             confirm = input("Type 'yes' to delete user %s: " %
                             parameters['user'])
         if autoclobber or confirm == 'yes':
             try:
                 if verbosity >= 1:
                     print("Destroying old test user...")
                 self._destroy_test_user(cursor, parameters, verbosity)
             except Exception as e:
                 sys.stderr.write(
                     "Got an error destroying the test user: %s\n" % e)
                 sys.exit(2)
             try:
                 if verbosity >= 1:
                     print(
                         "Destroying old test database for alias '%s'..." %
                         self.connection.alias)
                 self._execute_test_db_destruction(cursor, parameters,
                                                   verbosity)
             except Exception as e:
                 sys.stderr.write(
                     "Got an error destroying the test database: %s\n" % e)
                 sys.exit(2)
         else:
             print("Tests cancelled -- test database cannot be recreated.")
             sys.exit(1)
     else:
         print("Django is configured to use pre-existing test user '%s',"
               " and will not attempt to delete it.\n" % parameters['user'])
         print("Tests cancelled -- test database cannot be recreated.")
         sys.exit(1)
Esempio n. 8
0
    def _create_test_db(self, verbosity=1, autoclobber=False, keepdb=False):
        parameters = self._get_test_db_params()
        cursor = self._maindb_connection.cursor()
        if self._test_database_create():
            try:
                self._execute_test_db_creation(cursor, parameters, verbosity,
                                               keepdb)
            except Exception as e:
                # if we want to keep the db, then no need to do any of the below,
                # just return and skip it all.
                if keepdb:
                    return
                sys.stderr.write(
                    "Got an error creating the test database: %s\n" % e)
                if not autoclobber:
                    confirm = input(
                        "It appears the test database, %s, already exists. "
                        "Type 'yes' to delete it, or 'no' to cancel: " %
                        parameters['user'])
                if autoclobber or confirm == 'yes':
                    if verbosity >= 1:
                        print(
                            "Destroying old test database for alias '%s'..." %
                            self.connection.alias)
                    try:
                        self._execute_test_db_destruction(
                            cursor, parameters, verbosity)
                    except DatabaseError as e:
                        if 'ORA-29857' in str(e):
                            self._handle_objects_preventing_db_destruction(
                                cursor, parameters, verbosity, autoclobber)
                        else:
                            # Ran into a database error that isn't about leftover objects in the tablespace
                            sys.stderr.write(
                                "Got an error destroying the old test database: %s\n"
                                % e)
                            sys.exit(2)
                    except Exception as e:
                        sys.stderr.write(
                            "Got an error destroying the old test database: %s\n"
                            % e)
                        sys.exit(2)
                    try:
                        self._execute_test_db_creation(cursor, parameters,
                                                       verbosity, keepdb)
                    except Exception as e:
                        sys.stderr.write(
                            "Got an error recreating the test database: %s\n" %
                            e)
                        sys.exit(2)
                else:
                    print("Tests cancelled.")
                    sys.exit(1)

        if self._test_user_create():
            if verbosity >= 1:
                print("Creating test user...")
            try:
                self._create_test_user(cursor, parameters, verbosity, keepdb)
            except Exception as e:
                # If we want to keep the db, then we want to also keep the user.
                if keepdb:
                    return
                sys.stderr.write("Got an error creating the test user: %s\n" %
                                 e)
                if not autoclobber:
                    confirm = input(
                        "It appears the test user, %s, already exists. Type "
                        "'yes' to delete it, or 'no' to cancel: " %
                        parameters['user'])
                if autoclobber or confirm == 'yes':
                    try:
                        if verbosity >= 1:
                            print("Destroying old test user...")
                        self._destroy_test_user(cursor, parameters, verbosity)
                        if verbosity >= 1:
                            print("Creating test user...")
                        self._create_test_user(cursor, parameters, verbosity,
                                               keepdb)
                    except Exception as e:
                        sys.stderr.write(
                            "Got an error recreating the test user: %s\n" % e)
                        sys.exit(2)
                else:
                    print("Tests cancelled.")
                    sys.exit(1)

        self._maindb_connection.close(
        )  # done with main user -- test user and tablespaces created
        self._switch_to_test_user(parameters)
        return self.connection.settings_dict['NAME']