Python peewee.ProgrammingError() Examples

The following are 7 code examples of peewee.ProgrammingError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module peewee , or try the search function .
Example #1
Source File: models.py    From sentinel with MIT License 6 votes vote down vote up
def check_db_schema_version():
    """ Ensure DB schema is correct version. Drop tables if not. """
    db_schema_version = None

    try:
        db_schema_version = Setting.get(Setting.name == 'DB_SCHEMA_VERSION').value
    except (peewee.OperationalError, peewee.DoesNotExist, peewee.ProgrammingError) as e:
        printdbg("[info]: Can't get DB_SCHEMA_VERSION...")

    printdbg("[info]: SCHEMA_VERSION (code) = [%s]" % SCHEMA_VERSION)
    printdbg("[info]: DB_SCHEMA_VERSION = [%s]" % db_schema_version)
    if (SCHEMA_VERSION != db_schema_version):
        printdbg("[info]: Schema version mis-match. Syncing tables.")
        try:
            existing_table_names = db.get_tables()
            existing_models = [m for m in db_models() if m._meta.db_table in existing_table_names]
            if (existing_models):
                printdbg("[info]: Dropping tables...")
                db.drop_tables(existing_models, safe=False, cascade=False)
        except (peewee.InternalError, peewee.OperationalError, peewee.ProgrammingError) as e:
            print("[error] Could not drop tables: %s" % e) 
Example #2
Source File: conftest.py    From social-relay with GNU Affero General Public License v3.0 5 votes vote down vote up
def app(request):
    from social_relay import app, database
    try:
        drop_all_tables(database)
    except (ProgrammingError, OperationalError):
        pass
    create_all_tables(database)

    def drop_db():
        drop_all_tables(database)

    request.addfinalizer(drop_db)

    return app 
Example #3
Source File: database.py    From mqtt-pwn with GNU General Public License v3.0 5 votes vote down vote up
def create_tables(db, tables):
    """ Creates the given tables """

    try:
        db.create_tables(tables)
    except ProgrammingError:
        pass 
Example #4
Source File: 005_packagefile_basename_unique.py    From pypi-server with MIT License 5 votes vote down vote up
def add_uniquie_basename_index(migrator, db):
    try:
        migrate(
            migrator.add_index('packagefile', ('basename',), True)
        )
    except (OperationalError, ProgrammingError):
        pass 
Example #5
Source File: test.py    From peewee-db-evolve with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_drop_table(self):
    class SomeModel(pw.Model):
      some_field = pw.CharField(null=True)
      class Meta:
        database = self.db
    self.evolve_and_check_noop()
    SomeModel.create(some_field='woot')
    peeweedbevolve.clear()
    self.evolve_and_check_noop()
    with self.assertRaises(pw.ProgrammingError):
      SomeModel.create(some_field='woot2') # fails because table isn't there 
Example #6
Source File: test.py    From peewee-db-evolve with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_ignore_new_model(self):
    class SomeModel(pw.Model):
      some_field = pw.CharField(null=True)
      class Meta:
        database = self.db
        evolve = False
    self.evolve_and_check_noop()
    with self.assertRaises(pw.ProgrammingError):
      # should fail because table does not exist
      SomeModel.create(some_field='woot') 
Example #7
Source File: models.py    From sentinel with MIT License 4 votes vote down vote up
def check_db_sane():
    """ Ensure DB tables exist, create them if they don't. """
    check_db_schema_version()

    missing_table_models = []

    for model in db_models():
        if not getattr(model, 'table_exists')():
            missing_table_models.append(model)
            printdbg("[warning]: Table for %s (%s) doesn't exist in DB." % (model, model._meta.db_table))

    if missing_table_models:
        printdbg("[warning]: Missing database tables. Auto-creating tables.")
        try:
            db.create_tables(missing_table_models, safe=True)
        except (peewee.InternalError, peewee.OperationalError, peewee.ProgrammingError) as e:
            print("[error] Could not create tables: %s" % e)

    update_schema_version()
    purge_invalid_amounts()