def initial_create_databases(): """Create databases and/or db users.""" databases = config('databases') # TODO: Currently we only do the 'default' database # TODO: And we don't copy it from production to staging yet. for dbname in databases: if dbname != 'default': print("Skipped database '%s'." % (dbname,)) continue dbinfo = databases[dbname] if is_production_database(dbname): warning = "Database '{name}' on '{host} is a production database." print(red(warning.format(name=dbinfo['NAME'], host=['HOST']))) if confirm("Create user %s on %s?" % (dbinfo['USER'], dbinfo['HOST']), default=False): print(green("You will be prompted for a password. " + "The site wants to use %s .") % (dbinfo['PASSWORD'],)) print(green("\nIf it asks for another password, then that is " + "postgres'\npassword. On the staging server, that " + "is 'postgres'.\nIt might be different elsewhere.\n")) # Use warn-only so that the script doesn't halt if the user # exists already with settings(warn_only=True): local('createuser -h {host} -U postgres --pwprompt {user}'. format(host=dbinfo['HOST'], user=dbinfo['USER'])) create_individual_database(dbname)
def drop_database(dbname='default'): """Drop database.""" db = config('databases').get(dbname, None) if db is None: return if is_production_database(dbname): print(red("Database '{name}' on '{host}' is a production database!". format(name=db['NAME'], host=db['HOST']))) if confirm("Drop database %s on %s?" % ( db['NAME'], db['HOST']), default=False): cmd = 'dropdb -h {host} -U postgres {database}' with settings(warn_only=True): # Warn only so we don't abort if it doesn't exist yet local(cmd.format(host=db['HOST'], database=db['NAME']))