PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/api/scripts/create_database.py

https://gitlab.com/fdemian/Shelob
Python | 38 lines | 27 code | 10 blank | 1 comment | 0 complexity | 9ad96a15264efb2c7bf416f0df2703b7 MD5 | raw file
  1. from sqlalchemy import create_engine
  2. from tornado.options import define, options, parse_config_file
  3. from os import path
  4. config_file = '../../config.ini'
  5. config_file_path = path.join(path.dirname(__file__), config_file)
  6. # Get the database URL from the configuration file.
  7. def get_database_url():
  8. define('database_user', type=str, group='application', help='Database name.')
  9. define('database_port', type=str, group='application', help='Database port.')
  10. define('database_password', type=str, group='application', help='Database password.')
  11. parse_config_file(config_file_path)
  12. user = options.database_user
  13. port = options.database_port
  14. password = options.database_password
  15. return 'postgresql+psycopg2://' + user + ":" + password + "@localhost:" + port
  16. def get_database_name():
  17. define('database_name', type=str, group='application', help='Database name.')
  18. parse_config_file(config_file_path)
  19. return options.database_name
  20. def create_database():
  21. connection_string = get_database_url()
  22. database_name = get_database_name()
  23. engine = create_engine(connection_string)
  24. conn = engine.connect()
  25. conn.execute("commit")
  26. conn.execute("create database " + database_name)
  27. conn.close()
  28. create_database()