Advertisement
Guest User

Untitled

a guest
Feb 25th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. Db myapp_01
  2. _ my_customer_foo_schema
  3. _ my_customer_bar_schema
  4. Db myapp_02
  5. _ my_customer_foo_schema
  6. _ my_customer_bar_schema
  7.  
  8. import os
  9. import sys
  10. import pg
  11.  
  12. #Take the new schema name from the second cmd arguments (the first is the filename)
  13. newSchema = sys.argv[1]
  14. #Temp folder for the dumps
  15. dumpFile = '/test/dumps/' + str(newSchema) + '.sql'
  16. #Settings
  17. db_name = 'db_name'
  18. db_user = 'db_user'
  19. db_pass = 'db_pass'
  20. schema_as_template = 'schema_name'
  21.  
  22. #Connection
  23. pgConnect = pg.connect(dbname= db_name, host='localhost', user= db_user, passwd= db_pass)
  24. #Rename schema with the new name
  25. pgConnect.query("ALTER SCHEMA " + schema_as_template + " RENAME TO " + str(newSchema))
  26. #Dump it
  27. command = 'export PGPASSWORD="' + db_pass + '" && pg_dump -U ' + db_user + ' -n ' + str(newSchema) + ' ' + db_name + ' > ' + dumpFile
  28. os.system(command)
  29. #Rename back with its default name
  30. pgConnect.query("ALTER SCHEMA " + str(newSchema) + " RENAME TO " + schema_as_template)
  31. #Restore the previus dump to create the new schema
  32. restore = 'export PGPASSWORD="' + db_pass + '" && psql -U ' + db_user + ' -d ' + db_name + ' < ' + dumpFile
  33. os.system(restore)
  34. #Want to delete the dump file?
  35. os.remove(dumpFile)
  36. #Close connection
  37. pgConnect.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement