選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

62 行
1.4 KiB

  1. """
  2. Helpers for initializing and dropping schemas.
  3. """
  4. # stdlib imports
  5. import logging
  6. # antiorm imports
  7. from antipool import dbpool
  8. def reset_sql(schemas):
  9. """
  10. Drop and recreate the given schemas.
  11. """
  12. drop_sql(schemas)
  13. initialize_sql(schemas)
  14. def initialize_sql(schemas):
  15. """
  16. Insures that the given schemas are created.
  17. """
  18. conn, cursor = dbpool().connection(1)
  19. try:
  20. cursor.execute("""
  21. SELECT table_name FROM information_schema.tables
  22. """)
  23. tables = set(x[0] for x in cursor.fetchall())
  24. for table_name, schema in schemas:
  25. if table_name not in tables:
  26. logging.info('Creating SQL schema %s' % table_name)
  27. cursor.execute(schema)
  28. conn.commit()
  29. finally:
  30. conn.release()
  31. def drop_sql(schemas):
  32. """
  33. Drop the given list of schemes.
  34. """
  35. dbapi = dbpool().module()
  36. conn, cursor = dbpool().connection(1)
  37. try:
  38. cursor.execute("""
  39. SELECT table_name FROM information_schema.tables
  40. """)
  41. tables = set(x[0] for x in cursor.fetchall())
  42. names = [x for x, s in schemas if x in tables]
  43. for n in names:
  44. try:
  45. cursor.execute('DROP TABLE "%s" CASCADE' % n)
  46. conn.commit()
  47. except dbapi.Error:
  48. conn.rollback() # ignore errors due to dependencies.
  49. finally:
  50. conn.release()