Changeset - 93ac95d58972
[Not reviewed]
0 1 0
Lance Edgar (lance) - 7 years ago 2018-01-15 16:57:47
lance@edbob.org
Revert "Remove a couple steps when cloning Postgres database"

This reverts commit 3cfed219b44d1ce66068001304f2c51565676ba7.
1 file changed with 9 insertions and 7 deletions:
0 comments (0 inline, 0 general)
rattail/fablib/postgresql.py
Show inline comments
 
# -*- coding: utf-8; -*-
 
# -*- coding: utf-8 -*-
 
################################################################################
 
#
 
#  Rattail -- Retail Software Framework
 
#  Copyright © 2010-2018 Lance Edgar
 
#  Copyright © 2010-2017 Lance Edgar
 
#
 
#  This file is part of Rattail.
 
#
 
#  Rattail is free software: you can redistribute it and/or modify it under the
 
#  terms of the GNU General Public License as published by the Free Software
 
#  Foundation, either version 3 of the License, or (at your option) any later
 
#  version.
 
#
 
#  Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
 
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
#  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
#  details.
 
#
 
#  You should have received a copy of the GNU General Public License along with
 
#  Rattail.  If not, see <http://www.gnu.org/licenses/>.
 
#
 
################################################################################
 
"""
 
Fabric Library for PostgreSQL
 
"""
 

	
 
from __future__ import unicode_literals, absolute_import
 

	
 
import os
 
@@ -125,53 +125,55 @@ def create_db(name, owner=None, port=None, checkfirst=True):
 
        sudo(cmd, shell=False)
 

	
 

	
 
def create_schema(name, dbname, owner='rattail', port=None):
 
    """
 
    Create a schema within a PostgreSQL database.
 
    """
 
    sql_ = "create schema if not exists {} authorization {}".format(name, owner)
 
    sql(sql_, database=dbname, port=port)
 

	
 

	
 
def drop_db(name, checkfirst=True):
 
    """
 
    Drop a PostgreSQL database.
 
    """
 
    if not checkfirst or db_exists(name):
 
        sudo('sudo -u postgres dropdb {0}'.format(name), shell=False)
 

	
 

	
 
def download_db(name, destination=None):
 
    """
 
    Download a database from the "current" server.
 
    """
 
    if destination is None:
 
        destination = './{}.sql.gz'.format(name)
 
    sudo('sudo -u postgres pg_dump {0} > {0}.sql'.format(name))
 
    run('gzip --force {}.sql'.format(name))
 
    get('{}.sql.gz'.format(name), destination)
 
    run('rm {}.sql.gz'.format(name))
 
        destination = './{0}.sql.gz'.format(name)
 
    run('touch {0}.sql'.format(name))
 
    run('chmod 0666 {0}.sql'.format(name))
 
    sudo('sudo -u postgres pg_dump --file={0}.sql {0}'.format(name), shell=False)
 
    run('gzip --force {0}.sql'.format(name))
 
    get('{0}.sql.gz'.format(name), destination)
 
    run('rm {0}.sql.gz'.format(name))
 

	
 

	
 
def clone_db(name, owner, download, user='rattail', force=False, workdir=None):
 
    """
 
    Clone a database from a (presumably live) server
 

	
 
    :param name: Name of the database.
 

	
 
    :param owner: Username of the user who is to own the database.
 

	
 
    :param force: Whether the target database should be forcibly dropped, if it
 
       exists already.
 
    """
 
    if db_exists(name):
 
       if force:
 
           drop_db(name, checkfirst=False)
 
       else:
 
           abort("Database '{}' already exists!".format(name))
 

	
 
    create_db(name, owner=owner, checkfirst=False)
 

	
 
    # upload database dump to target server
 
    if workdir:
 
        curdir = os.getcwd()
0 comments (0 inline, 0 general)