예제 #1
0
파일: dbupgrade.py 프로젝트: tjjh89017/maas
    def _perform_view_installation(cls, database):
        """Register all PL/pgSQL views.

        :attention: `database` argument is not used!
        """
        from maasserver import dbviews
        dbviews.register_all_views()
예제 #2
0
 def test_views_contain_valid_sql(self):
     # This is a positive test case. The view creation code is very simple,
     # and will just abort with an exception if the SQL is invalid. So all
     # we just need to make sure no execeptions are thrown when the views
     # are created.
     register_all_views()
예제 #3
0
 def test_each_view_can_be_used(self):
     register_all_views()
     for view_name, view_sql in _ALL_VIEWS.items():
         with connection.cursor() as cursor:
             cursor.execute("SELECT * from %s;" % view_name)
예제 #4
0
파일: resources.py 프로젝트: zhangrb/maas
    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