On Tue, Jul 31, 2018 at 3:12 AM, Amin M <mukhaima...@gmail.com> wrote:
> Thanks Mike, really helped me out.
>
> I've done the following, in env.py
>
> import sys
> import os
>
> project_root = os.path.abspath('./')  # get Cherrypy root
> sys.path.insert(0, project_root)  # add it to Path, so we can import models,
> and read Cherrypy configurations
>
>
> def get_models():
>     """
>     get cherrypy models (SQLAlchemy)
>     :return:
>     """
>     from models.data_model import BASE
>     from models.comment import Comment
>     from models.file import File
>     from models.user import User
>     return [BASE.metadata]
>
>
> def get_db_configurations():
>     """
>     read database configurations from cherrypy
>     :return:
>     """
>     from helpers.config import config_data
>
>     sqlalchemy_url = '%s://%s:%s@%s:%s/%s' % (
>         config_data['database']['type'],
>         config_data['database']['user'],
>         config_data['database']['password'],
>         config_data['database']['host'],
>         config_data['database']['port'],
>         config_data['database']['db'],
>     )
>
>     return sqlalchemy_url
>
>
> Works like a charm, though I would love to know more about
> "now for the import above to work, your Python application needs to be
> part of the Python environment.  Usually folks build their web
> application with a setup.py file and then they install it into a local
> virtual environment.  If you aren't doing that, you may have to alter
> your PYTHONPATH environment variable outside, or use sys.path inside
> env.py in order to add where it can locate your models for import.
> "
>
> Could you point me to documentations or examples of this? I only know what a
> venv is.

make your application into a package, here's an overview:
http://python-packaging.readthedocs.io/en/latest/




>
> Thanks a lot.
>
> On Monday, July 30, 2018 at 9:35:17 PM UTC+3, Mike Bayer wrote:
>>
>> the env.py needs to things to work:
>>
>> 1. a way of connecting to the database.   if you just want to give it
>> a database URL that is fixed, you can stick that in alembic.ini under
>> sqlalchemy.url and you are done.  If you want to use cherrypy's
>> configurational facilities, then you'd need to add code to env.py that
>> calls upon cherrypy's configuration.   But just putting the database
>> URL into alembic.ini is a quick way just to get started.
>>
>> 2. for autogenerate to work (which is optional, but everyone wants it)
>> you have to "import" your model into it, like "from myapp.models
>> import BASE", then refer to BASE.metadata which is set up as the
>> "target_metadata" for autogenerate:
>>
>> from myapp.models import BASE
>> target_metadata = BASE.metadata
>>
>> now for the import above to work, your Python application needs to be
>> part of the Python environment.  Usually folks build their web
>> application with a setup.py file and then they install it into a local
>> virtual environment.  If you aren't doing that, you may have to alter
>> your PYTHONPATH environment variable outside, or use sys.path inside
>> env.py in order to add where it can locate your models for import.
>>
>>
>> You might want to ask on cherrypy's mailing list for more specifics on
>> these two points, they should be able to tell you.
>>
>>
>>
>>
>>
>> On Mon, Jul 30, 2018 at 7:30 AM, Amin M <mukha...@gmail.com> wrote:
>> > hello, I have a cherrypy application with the following structure
>> > controllers/
>> > models/
>> > views/
>> > app.py
>> >
>> >
>> > and my models exist in models folder.
>> > My BASE model contains the following:
>> >
>> > from sqlalchemy import create_engine
>> > from sqlalchemy.ext.declarative import declarative_base
>> > from sqlalchemy.orm import scoped_session, sessionmaker
>> >
>> > from helpers.config import config_data
>> >
>> > # base ORM model
>> > BASE = declarative_base()
>> > ENGINE = create_engine('%s://%s:%s@%s:%s/%s' %
>> >                        (
>> >                            config_data['database']['type'],
>> >                            config_data['database']['user'],
>> >                            config_data['database']['password'],
>> >                            config_data['database']['host'],
>> >                            config_data['database']['port'],
>> >                            config_data['database']['db'],
>> >                        ), pool_recycle=7200,
>> > echo=config_data["debug_sql"]
>> >                        )
>> > DB_SESSION = scoped_session(sessionmaker(bind=ENGINE))
>> >
>> >
>> > And my models extend this class, like below:
>> >
>> > from sqlalchemy import Column, Integer, String
>> >
>> > from helpers.config import config_data
>> > from models.data_model import BASE, DB_SESSION
>> >
>> > class User(BASE):
>> >     __tablename__ = 'users'
>> >     id = Column(Integer, nullable=False, primary_key=True)
>> >     username = Column(String(200), nullable=False)
>> >     email = Column(String(255))
>> >     avatar_path = Column(String(2080))
>> >     role = Column(Integer,
>> >                   nullable=False,
>> >                   )
>> >
>> >
>> > I have about 10 model classes (sql alchemy), and I want to integrate
>> > alembic
>> > with them, so whenever I edit / add new columns, it will trigger a diff
>> > between current database structure and new changes, how do you suggest I
>> > proceed with this?
>> > I've done a
>> >
>> > pip install alembic
>> > alembic init alembic
>> >
>> > and it created a folder for me, I am not sure what to edit in env.py to
>> > read
>> > my models.
>> > Thanks
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "sqlalchemy-alembic" group.
>> > To unsubscribe from this group and stop receiving emails from it, send
>> > an
>> > email to sqlalchemy-alembic+unsubscr...@googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>
>
> On Monday, July 30, 2018 at 9:35:17 PM UTC+3, Mike Bayer wrote:
>>
>> the env.py needs to things to work:
>>
>> 1. a way of connecting to the database.   if you just want to give it
>> a database URL that is fixed, you can stick that in alembic.ini under
>> sqlalchemy.url and you are done.  If you want to use cherrypy's
>> configurational facilities, then you'd need to add code to env.py that
>> calls upon cherrypy's configuration.   But just putting the database
>> URL into alembic.ini is a quick way just to get started.
>>
>> 2. for autogenerate to work (which is optional, but everyone wants it)
>> you have to "import" your model into it, like "from myapp.models
>> import BASE", then refer to BASE.metadata which is set up as the
>> "target_metadata" for autogenerate:
>>
>> from myapp.models import BASE
>> target_metadata = BASE.metadata
>>
>> now for the import above to work, your Python application needs to be
>> part of the Python environment.  Usually folks build their web
>> application with a setup.py file and then they install it into a local
>> virtual environment.  If you aren't doing that, you may have to alter
>> your PYTHONPATH environment variable outside, or use sys.path inside
>> env.py in order to add where it can locate your models for import.
>>
>>
>> You might want to ask on cherrypy's mailing list for more specifics on
>> these two points, they should be able to tell you.
>>
>>
>>
>>
>>
>> On Mon, Jul 30, 2018 at 7:30 AM, Amin M <mukha...@gmail.com> wrote:
>> > hello, I have a cherrypy application with the following structure
>> > controllers/
>> > models/
>> > views/
>> > app.py
>> >
>> >
>> > and my models exist in models folder.
>> > My BASE model contains the following:
>> >
>> > from sqlalchemy import create_engine
>> > from sqlalchemy.ext.declarative import declarative_base
>> > from sqlalchemy.orm import scoped_session, sessionmaker
>> >
>> > from helpers.config import config_data
>> >
>> > # base ORM model
>> > BASE = declarative_base()
>> > ENGINE = create_engine('%s://%s:%s@%s:%s/%s' %
>> >                        (
>> >                            config_data['database']['type'],
>> >                            config_data['database']['user'],
>> >                            config_data['database']['password'],
>> >                            config_data['database']['host'],
>> >                            config_data['database']['port'],
>> >                            config_data['database']['db'],
>> >                        ), pool_recycle=7200,
>> > echo=config_data["debug_sql"]
>> >                        )
>> > DB_SESSION = scoped_session(sessionmaker(bind=ENGINE))
>> >
>> >
>> > And my models extend this class, like below:
>> >
>> > from sqlalchemy import Column, Integer, String
>> >
>> > from helpers.config import config_data
>> > from models.data_model import BASE, DB_SESSION
>> >
>> > class User(BASE):
>> >     __tablename__ = 'users'
>> >     id = Column(Integer, nullable=False, primary_key=True)
>> >     username = Column(String(200), nullable=False)
>> >     email = Column(String(255))
>> >     avatar_path = Column(String(2080))
>> >     role = Column(Integer,
>> >                   nullable=False,
>> >                   )
>> >
>> >
>> > I have about 10 model classes (sql alchemy), and I want to integrate
>> > alembic
>> > with them, so whenever I edit / add new columns, it will trigger a diff
>> > between current database structure and new changes, how do you suggest I
>> > proceed with this?
>> > I've done a
>> >
>> > pip install alembic
>> > alembic init alembic
>> >
>> > and it created a folder for me, I am not sure what to edit in env.py to
>> > read
>> > my models.
>> > Thanks
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "sqlalchemy-alembic" group.
>> > To unsubscribe from this group and stop receiving emails from it, send
>> > an
>> > email to sqlalchemy-alembic+unsubscr...@googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy-alembic" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy-alembic+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy-alembic" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy-alembic+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to