pyramid_sqla / pyramid_sqla / paster_templates / pyramid_sqla / +package+ / scripts / create_db.py_tmpl

"""Create the application's database.

Run this once after installing the application::

    python -m {{package}}.scripts.create_db development.ini
"""
import logging.config
import sys

from pyramid.paster import get_app
import transaction

import {{package}}.models as models

def main():
    if len(sys.argv) != 2:
        sys.exit("Usage: python -m {{package}}.scripts.create_db INI_FILE")
    ini_file = sys.argv[1]
    logging.config.fileConfig(ini_file)
    log = logging.getLogger(__name__)
    app = get_app(ini_file, "{{project}}")
    #settings = app.registry.settings

    # Abort if any tables exist to prevent accidental overwriting
    for table in models.Base.metadata.sorted_tables:
        log.debug("checking if table '%s' exists", table.name)
        if table.exists():
            raise RuntimeError("database table '%s' exists" % table.name)

    # Create the tables
    models.Base.metadata.create_all()
    sess = models.Session()

    # Record 1
    #p = models.MyModel(id=1, name=u"Foo Bar")
    #sess.add(p)

    transaction.commit()

    

if __name__ == "__main__":  
    main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.