예제 #1
0
    def execute(self, properties, arguments):
        """
        Drop a database after checking if schema and environment are
        valid values. Also check that host is not on the block list.

        :type properties: system.Properties.Properties
        :param properties: The project properties
        :type arguments: dict
        :param arguments: This dict contains the plugin arguments:

            * **host**: The hostname to drop on;
            * **environment**: Environment to drop the database from.
        """
        prepared_args = self._validate_and_prepare(properties, arguments)

        host = prepared_args['host']
        environment = prepared_args['environment']
        databases = properties.get('databases')

        objects = properties.get('drop_objects')

        # retrieve the user credentials for this database project.
        users = properties.get('postgresql_users')

        # try to retrieve the users from the credentials file, when no users are configured in
        # myproject.json.
        if not users:
            # retrieve the name of this database project, introduced in version 1.0.12
            profile = PropertyHelper.get_profile(properties)
            if profile:
                users = profile.get('postgresql_users')

        # fail when no users are found. This means that they are not set in myproject.json or
        # credentials.json
        Fail.fail_on_no_users(users)

        for database in databases:
            print(
                "dropping database '{db}' on host '{host}' using environment '{env}'"
                .format(db=database, host=host, env=environment))

            executor = PropertyHelper.get_postgres_properties(
                users, host, database)

            connector = self.get_connector()

            for obj in objects:
                folder = File(
                    os.path.join(properties.get('plugin.dir'), 'postgresql',
                                 'drop', obj))
                ConnectionExecutor.execute(connector, executor, properties,
                                           folder)

            print("database '{}' dropped".format(database))
예제 #2
0
    def execute(self, properties, arguments):
        """
        Create a new database instance for the initial version.

        :type properties: system.Properties.Properties
        :param properties: The project properties
        :type arguments: dict
        :param arguments: This dict contains the plugin arguments:

            * **host**: The hostname where the database is running;
            * **environment**: The environment to create the database in (optional).
        """
        prepared_args = self._validate_and_prepare(properties, arguments)

        host = prepared_args['host']
        environment = prepared_args['environment']
        databases = properties.get('databases')

        objects = properties.get('create_objects')

        # retrieve the user credentials for this database project.
        users = properties.get('postgresql_users')

        # try to retrieve the users from the credentials file, when no users are configured in
        # myproject.json.
        if not users:
            # retrieve the name of this database project, introduced in version 1.0.12
            profile = PropertyHelper.get_profile(properties)
            if profile:
                users = profile.get('postgresql_users')

        # fail when no users are found. This means that they are not set in myproject.json or
        # credentials.json
        Fail.fail_on_no_users(users)

        connector = self.get_connector()
        create_dir = properties.get('create.dir')

        for database in databases:
            print("creating database '{db}' on host '{host}' using environment '{env}'".format(
                db=database, host=host, env=environment))

            executor = PropertyHelper.get_postgres_properties(users, host, database)

            for obj in objects:
                # global ddl objects
                folder = File(os.path.join(create_dir, database, 'ddl', obj))
                ConnectionExecutor.execute(connector, executor, properties, folder)

                # environment specific ddl objects
                folder = File(os.path.join(create_dir, database, 'ddl', obj, environment))
                ConnectionExecutor.execute(connector, executor, properties, folder)

            # global dat objects
            folder = File(os.path.join(create_dir, database, 'dat'))
            ConnectionExecutor.execute(connector, executor, properties, folder)

            # environment specific dat objects
            folder = File(os.path.join(create_dir, database, 'dat', environment))
            ConnectionExecutor.execute(connector, executor, properties, folder)

            print("database '{}' created.".format(database))