Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add select_dump method #1

Open
Jaymon opened this issue Feb 26, 2015 · 0 comments
Open

add select_dump method #1

Jaymon opened this issue Feb 26, 2015 · 0 comments

Comments

@Jaymon
Copy link
Owner

Jaymon commented Feb 26, 2015

this method will allow you to specify the table you want to dump by creating a select query, that way you can only easily backup just a subset of the rows

def select_dump(self, table, queries, outfile=None, pipes=None):
        '''
        this uses a flow discussed here (in order to output only certain rows)
        http://dbaspot.com/postgresql/348627-pg_dump-t-give-where-condition.html
        '''
        if not outfile: outfile = self._get_outfile(table)
        if isinstance(queries, basestring): queries = [queries]
        if not queries: raise ValueError('no queries to run')
        if not table: raise ValueError('no table')

        args = []
        args += [self.db]

        temp_table = 'temp__{}'.format(table)
        # could do print statements using \qecho
        # http://stackoverflow.com/questions/10663520
        run_queries = [
            'DROP TABLE IF EXISTS {}'.format(temp_table),
            'CREATE TABLE {} (LIKE {})'.format(temp_table, table),
        ]
        # add all the queries that were passed in so our temp table gets populated with the data we want to keep
        for q in queries: run_queries.append('INSERT INTO {} {}'.format(temp_table, q))

        self._run_queries(run_queries)

        if not pipes: pipes = []
        pipes.append('sed -e "s/{}/{}/g"'.format(temp_table, table))

        self.table_dump(table=temp_table, pipes=pipes, outfile=outfile)

        self._run_queries(['DROP TABLE {}'.format(temp_table)])
        return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant