def drop_database(): """Drops an existing database using PostgreSQL command line clients""" nav_opts = ConnectionParameters.from_config() postgres_opts = ConnectionParameters.for_postgres_user() postgres_opts.export(os.environ) print "Dropping database %s" % nav_opts.dbname trap_and_die(subprocess.CalledProcessError, "Failed to drop database %s" % nav_opts.dbname, check_call, ["dropdb", nav_opts.dbname])
def drop_database(): """Drops an existing database using PostgreSQL command line clients""" nav_opts = ConnectionParameters.from_config() postgres_opts = ConnectionParameters.for_postgres_user() postgres_opts.export(os.environ) print("Dropping database %s" % nav_opts.dbname) trap_and_die(subprocess.CalledProcessError, "Failed to drop database %s" % nav_opts.dbname, check_call, ["dropdb", nav_opts.dbname])
def restore_from_dump(filename): """Restores a NAV database from an SQL dump produced by pg_dump""" postgres_opts = ConnectionParameters.for_postgres_user() postgres_opts.export(os.environ) nav_opts = ConnectionParameters.from_config() print("Restoring database %s from file %s" % (nav_opts.dbname, filename)) trap_and_die( subprocess.CalledProcessError, "Failed to restore database %s from file %s" % (nav_opts.dbname, filename), check_call, ["psql", "--quiet", "-f", filename, nav_opts.dbname])
def restore_from_dump(filename): """Restores a NAV database from an SQL dump produced by pg_dump""" postgres_opts = ConnectionParameters.for_postgres_user() postgres_opts.export(os.environ) nav_opts = ConnectionParameters.from_config() print "Restoring database %s from file %s" % (nav_opts.dbname, filename) trap_and_die( subprocess.CalledProcessError, "Failed to restore database %s from file %s" % (nav_opts.dbname, filename), check_call, ["psql", "--quiet", "-f", filename, nav_opts.dbname])
def verify_hstore_extension(self): """ Installs the hstore extension to dbname if not already present. Installation takes place in the namespace mentioned first in the search path, which should be the manage namespace in NAV's case. """ postgres_opts = ConnectionParameters.for_postgres_user() postgres_opts.export(os.environ) self.cursor.execute( "SELECT COUNT(*) FROM pg_extension WHERE extname='hstore'") count, = self.cursor.fetchone() if count > 0: return print("Creating hstore extension in database {0}".format( self.connect_options.dbname)) trap_and_die( subprocess.CalledProcessError, "Failed to install the hstore extension, maybe you need " "to run as the postgres superuser?", check_call, [ "psql", "--quiet", "-c", "CREATE EXTENSION hstore " "WITH SCHEMA manage;", self.connect_options.dbname ])
def __init__(self, resource_module, apply_out_of_order_changes=False): self.resource_module = resource_module self.connection = None self.cursor = None self.connect_options = ConnectionParameters.from_config() self.apply_out_of_order_changes = apply_out_of_order_changes self.finder = ChangeScriptFinder(self.resource_module)
def __init__(self, sql_dir, apply_out_of_order_changes=False): self.sql_dir = sql_dir self.connection = None self.cursor = None self.connect_options = ConnectionParameters.from_config() self.apply_out_of_order_changes = apply_out_of_order_changes self.finder = ChangeScriptFinder(self.sql_dir)
def verify_hstore_extension(self): """ Installs the hstore extension to dbname if not already present. Installation takes place in the namespace mentioned first in the search path, which should be the manage namespace in NAV's case. """ postgres_opts = ConnectionParameters.for_postgres_user() postgres_opts.export(os.environ) self.cursor.execute( "SELECT COUNT(*) FROM pg_extension WHERE extname='hstore'") count, = self.cursor.fetchone() if count > 0: return print "Creating hstore extension in database {0}".format( self.connect_options.dbname) trap_and_die(subprocess.CalledProcessError, "Failed to install the hstore extension, maybe you need " "to run as the postgres superuser?", check_call, ["psql", "--quiet", "-c", "CREATE EXTENSION hstore;", self.connect_options.dbname])
def __init__(self, sql_dir, apply_out_of_order_changes=False): self.sql_dir = sql_dir self.connection = None self.cursor = None self.connect_options = ConnectionParameters.from_config() self.apply_out_of_order_changes = apply_out_of_order_changes self.finder = ChangeScriptFinder(self.sql_dir)
def create_database(): """Create a database using PostgreSQL command line clients""" nav_opts = ConnectionParameters.from_config() postgres_opts = ConnectionParameters.for_postgres_user() postgres_opts.export(os.environ) if not user_exists(nav_opts.user): create_user(nav_opts.user, nav_opts.password) print "Creating database %s owned by %s" % (nav_opts.dbname, nav_opts.user) trap_and_die(subprocess.CalledProcessError, "Failed creating database %s" % nav_opts.dbname, check_call, ["createdb", "--owner=%s" % nav_opts.user, "--encoding=utf-8", nav_opts.dbname]) install_pl_pgsql(nav_opts.dbname)
def export_pgvars(): """Exports NAV's db config as PG* environment variables""" params = ConnectionParameters.from_config() params.export(os.environ)
def verify_password_is_configured(): """Verifies that a password has been configured in db.conf""" opts = ConnectionParameters.from_config() if not opts.password: die("No password configured for %s user in db.conf" % opts.user)
def export_pgvars(): """Exports NAV's db config as PG* environment variables""" params = ConnectionParameters.from_config() params.export(os.environ)
def verify_password_is_configured(): """Verifies that a password has been configured in db.conf""" opts = ConnectionParameters.from_config() if not opts.password: die("No password configured for %s user in db.conf" % opts.user)