# Copyright 2016 Yanis Guenane <yguenane@redhat.com>
# Author: Yanis Guenane <yguenane@redhat.com>
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from swiftbackmeup import databases


class PostgreSQL(databases.Database):

    def __init__(self, conf):
        super(PostgreSQL, self).__init__(conf)
        self.pg_dump_options = conf.get('pg_dump_options')
        self.command = self.build_command()

    def build_command(self):
        command = 'pg_dump'

        if self.pg_dump_options:
            command += ' %s' % self.pg_dump_options

        if self.user:
            command += ' -U %s' % self.user

        if self.host:
            command += ' -h %s' % self.host
        
        if self.database:
            command += ' %s' % self.database

        if self.password:
            self.env['PGPASSWORD'] = self.password

        return command

@@ -676,16 +676,185 @@
 tabases%0A
+from swiftbackmeup import exceptions%0A%0A%0A_PARAMS = %7B%0A  'data_only': '-a',%0A  'globals_only': '-g',%0A  'roles_only': '-r',%0A  'schema_only': '-s',%0A  'tablespaces_only': '-t'%0A%7D
 %0A%0Aclass 
@@ -1034,101 +1034,1247 @@
     
-self.command = self.build_command()%0A%0A    def build_command(self):%0A        command = 'pg_dump'
+# pg_dumpall parameter%0A        self.data_only = conf.get('data_only')%0A        self.globals_only = conf.get('globals_only')%0A        self.roles_only = conf.get('roles_only')%0A        self.schema_only = conf.get('schema_only')%0A        self.tablespaces_only = conf.get('tablespaces_only')%0A%0A        self.command = self.build_command()%0A%0A    def build_command(self):%0A%0A        if self.database == 'all':%0A            command = 'pg_dumpall'%0A        else:%0A            command = 'pg_dump'%0A%0A        # pg_dumpall *-only options management%0A        if self.globals_only and self.roles_only:%0A            raise exceptions.ConfigurationExceptions('%25s: options globals_only and roles_only cannot be used together' %25 self.database)%0A        elif self.globals_only and self.tablespaces_only:%0A            raise  exceptions.ConfigurationExceptions('%25s: options globals_only and tablespaces_only cannot be used together' %25 self.database)%0A        elif self.tablespaces_only and self.roles_only:%0A            raise  exceptions.ConfigurationExceptions('%25s: options tablespaces_only and roles_only cannot be used together' %25 self.database)%0A%0A        for param in _PARAMS.keys():%0A            if getattr(self, param, None):%0A                command += ' %25s' %25  _PARAMS%5Bparam%5D
 %0A%0A  
@@ -2302,16 +2302,47 @@
 _options
+ and not self.database == 'all'
 :%0A      
@@ -2554,16 +2554,47 @@
 database
+ and not self.database == 'all'
 :%0A      

Support pg_dumpall options