database.py 884 B

123456789101112131415161718192021222324252627
  1. from sqlalchemy import create_engine
  2. from sqlalchemy.orm import scoped_session, sessionmaker
  3. from sqlalchemy.ext.declarative import declarative_base
  4. from gestor import configs
  5. db_str = configs.DATABASE_NAME
  6. #db_str = configs.DATABASE_NAME+"/"+configs.DATABASE_USERNAME+"/"+configs.DATABASE_PASSWORD
  7. engine = create_engine( db_str , convert_unicode=True )
  8. db_session = scoped_session(sessionmaker(autocommit=False,
  9. autoflush=False,
  10. bind=engine))
  11. Base = declarative_base()
  12. Base.query = db_session.query_property()
  13. def init_db():
  14. # import all modules here that might define models so that
  15. # they will be registered properly on the metadata. Otherwise
  16. # you will have to import them first before calling init_db()
  17. from gestor import models
  18. Base.metadata.create_all(bind=engine)