Esempio n. 1
0
    def _drop_all_views(cls, database):
        """Register all PL/pgSQL views.

        :attention: `database` argument is not used!
        """
        from maasserver import dbviews
        dbviews.drop_all_views()
Esempio n. 2
0
    def _make(self, cluster):
        # Ensure that Django is initialised.
        import django
        django.setup()

        # Import other modules without risk of toy throwing from Django.
        from django.conf import settings
        from django.core.management import call_command
        from maasserver import dbviews
        from maasserver import triggers

        # For each database, create a ${name}_test database.
        databases = DjangoDatabases(
            database for database in settings.DATABASES.values()
            if database["HOST"] == cluster.datadir)

        created = set()
        with cluster.connect() as conn:
            with conn.cursor() as cursor:
                for database in databases:
                    dbname = database["NAME"] + "_test"
                    stmt = "CREATE DATABASE %s" % dbname
                    try:
                        cursor.execute(stmt)
                    except psycopg2.ProgrammingError as error:
                        if error.pgcode != DUPLICATE_DATABASE:
                            raise
                    else:
                        created.add(dbname)
                        debug("Created {dbname}; statement: {stmt}",
                              dbname=dbname,
                              stmt=stmt)
                    database["NAME"] = dbname

        # Attempt to populate these databases from a dumped database script.
        # This is *much* faster than falling back on Django's migrations.
        for database in databases:
            dbname = database["NAME"]
            if dbname in created:
                initial = here.joinpath("initial.%s.sql" % dbname)
                if initial.is_file():
                    cluster.execute("psql", "--quiet", "--single-transaction",
                                    "--set=ON_ERROR_STOP=1", "--dbname",
                                    dbname, "--output", os.devnull, "--file",
                                    str(initial))

        # First, drop any views that may already exist. We don't want views
        # that that depend on a particular schema to prevent schema changes
        # due to the dependency. The views will be recreated at the end of
        # this process.
        dbviews.drop_all_views()

        # Apply all current migrations. We use `migrate` here instead of
        # `dbupgrade` because we don't need everything that the latter
        # provides, and, more importantly, we need it to run in-process so
        # that it sees the effects of our settings changes.
        call_command("migrate", interactive=False)

        # Install all database functions, triggers, and views.
        triggers.register_all_triggers()
        dbviews.register_all_views()

        # Ensure that there are no sessions from Django.
        close_all_connections()

        return databases