Exemplo n.º 1
0
 def affected_models(self):
     """
 Returns a list of models which are cached in self.affected_caches
 """
     if self._models is None:
         from baph.db.models import get_models
         self._models = []
         for model in get_models():
             cache_alias = model._meta.cache_alias
             if cache_alias not in self.affected_caches:
                 continue
             self._models.append(model)
     return self._models
Exemplo n.º 2
0
Arquivo: utils.py Projeto: devhub/baph
 def affected_models(self):
   """
   Returns a list of models which are cached in self.affected_caches
   """
   if self._models is None:
     from baph.db.models import get_models
     self._models = []
     for model in get_models():
       cache_alias = model._meta.cache_alias
       if cache_alias not in self.affected_caches:
         continue
       self._models.append(model)
   return self._models
Exemplo n.º 3
0
def sort_dependencies(app_list):
    """Sort a list of app,modellist pairs into a single list of models.

    The single list of models is sorted so that any model with a natural key
    is serialized before a normal model, and any model with a natural key
    dependency has it's dependencies serialized first.
    """
    from baph.db.models import get_model, get_models

    models = set()
    for app, model_list in app_list:
        if model_list is None:
            model_list = get_models(app)

        for model in model_list:
            models.add(model)

    tables = orm.Base.metadata.sorted_tables
    model_list = sorted(models, key=lambda x: tables.index(x.__table__))

    return model_list
Exemplo n.º 4
0
def sort_dependencies(app_list):
    """Sort a list of app,modellist pairs into a single list of models.

    The single list of models is sorted so that any model with a natural key
    is serialized before a normal model, and any model with a natural key
    dependency has it's dependencies serialized first.
    """
    from baph.db.models import get_model, get_models

    models = set()
    for app, model_list in app_list:
        if model_list is None:
            model_list = get_models(app)

        for model in model_list:
            models.add(model)

    tables = orm.Base.metadata.sorted_tables
    model_list = sorted(models, key=lambda x: tables.index(x.__table__))

    return model_list
Exemplo n.º 5
0
    def handle_noargs(self, **options):

        verbosity = int(options.get('verbosity'))
        interactive = options.get('interactive')
        show_traceback = options.get('traceback')
        load_initial_data = options.get('load_initial_data')

        self.style = no_style()

        # Import the 'management' module within each installed app, to register
        # dispatcher events.
        for app_name in settings.INSTALLED_APPS:
            try:
                import_module('.management', app_name)
            except ImportError as exc:
                # This is slightly hackish. We want to ignore ImportErrors
                # if the "management" module itself is missing -- but we don't
                # want to ignore the exception if the management module exists
                # but raises an ImportError for some reason. The only way we
                # can do this is to check the text of the exception. Note that
                # we're a bit broad in how we check the text, because different
                # Python implementations may not use the same text.
                # CPython uses the text "No module named management"
                # PyPy uses "No module named myproject.myapp.management"
                msg = exc.args[0]
                if not msg.startswith(
                        'No module named') or 'management' not in msg:
                    raise

        db = options.get('database')
        engine = connections[db]
        default_schema = engine.url.database

        # the default db may not exist yet, so we remove it before connecting
        engine.url.database = None
        tmp_url = str(engine.url)
        engine.url.database = default_schema
        tmp_engine = create_engine(tmp_url)
        tmp_conn = tmp_engine.connect()
        existing_schemas = set(
            [s[0] for s in tmp_conn.execute('show databases')])
        if not default_schema in existing_schemas:
            tmp_engine.execute(CreateSchema(default_schema))
            existing_schemas.add(default_schema)

        # now reconnect with the default_db provided
        conn = engine.connect()
        Base.metadata.bind = engine

        if verbosity >= 3:
            self.stdout.write("Getting existing schemas...\n")
            for schema in existing_schemas:
                self.stdout.write("\t%s\n" % schema)
            else:
                self.stdout.write("\tNone\n")

        existing_tables = []
        if verbosity >= 1:
            self.stdout.write("Getting existing tables...\n")
        for schema in existing_schemas:
            for name in engine.engine.table_names(schema, connection=conn):
                existing_tables.append('%s.%s' % (schema, name))
                if verbosity >= 3:
                    self.stdout.write("\t%s.%s\n" % (schema, name))

        existing_models = []
        if verbosity >= 1:
            self.stdout.write("Getting existing models...\n")
        for cls_name, cls in Base._decl_class_registry.items():
            tablename = get_tablename(cls)
            if tablename and tablename in existing_tables:
                existing_models.append(cls)
                if verbosity >= 3:
                    self.stdout.write("\t%s\n" % cls)

        all_tables = []
        if verbosity >= 1:
            self.stdout.write("Getting required tables...\n")
        for table in Base.metadata.sorted_tables:
            tablename = get_tablename(table)
            all_tables.append(tablename)
            if verbosity >= 3:
                self.stdout.write("\t%s\n" % tablename)

        all_models = []
        if verbosity >= 1:
            self.stdout.write("Getting required models...\n")
        for app in get_apps():
            for model in get_models(app, include_auto_created=True):
                app_name = app.__name__.rsplit('.', 1)[0]
                all_models.append((app_name, model))
                if verbosity >= 3:
                    self.stdout.write("\t%s.%s\n" % (app_name, model))

        schema_manifest = set()
        table_manifest = set()
        if verbosity >= 1:
            self.stdout.write('Building manifest...\n')
        for app_name, model in all_models:
            tablename = get_tablename(model)
            if tablename in existing_tables:
                continue
            table_manifest.add((app_name, model))
            schema = tablename.rsplit('.', 1)[0]
            if schema not in existing_schemas:
                schema_manifest.add(schema)

        table_manifest = sorted(
            table_manifest,
            key=lambda x: all_tables.index(get_tablename(x[1])))

        if verbosity >= 3:
            print 'Schema Manifest:\n'
            for schema in schema_manifest:
                print '\t%s\n' % schema
            print 'Model/Table Manifest\n'
            for app_name, model in table_manifest:
                print '\t%s.%s (%s)\n' % (app_name, model._meta.object_name,
                                          get_tablename(model))

        # create any missing schemas
        if verbosity >= 1:
            self.stdout.write("Creating schemas ...\n")
        for schema in schema_manifest:
            if verbosity >= 3:
                self.stdout.write("\t%s\n" % schema)
            engine.execute(CreateSchema(schema))
            existing_schemas.add(schema)

        # create any missing tables
        created_models = set()
        if verbosity >= 1:
            self.stdout.write("Creating tables ...\n")
        for app_name, model in table_manifest:
            if verbosity >= 3:
                self.stdout.write("\tCreating table for model %s.%s\n" %
                                  (app_name, model._meta.object_name))
            tablename = get_tablename(model)
            if tablename not in existing_tables:
                model.__table__.create()
                existing_tables.append(tablename)
            existing_models.append(model)
            created_models.add(model)

        # Send the post_syncdb signal
        emit_post_sync_signal(created_models, verbosity, interactive, db)

        # Load initial_data fixtures (unless that has been disabled)
        if load_initial_data:
            call_command('loaddata',
                         'initial_data',
                         verbosity=verbosity,
                         database=db,
                         skip_validation=True)
Exemplo n.º 6
0
    def handle_noargs(self, **options):

        verbosity = int(options.get('verbosity'))
        interactive = options.get('interactive')
        show_traceback = options.get('traceback')
        load_initial_data = options.get('load_initial_data')

        self.style = no_style()

        # Import the 'management' module within each installed app, to register
        # dispatcher events.
        for app_name in settings.INSTALLED_APPS:
            try:
                import_module('.management', app_name)
            except ImportError as exc:
                # This is slightly hackish. We want to ignore ImportErrors
                # if the "management" module itself is missing -- but we don't
                # want to ignore the exception if the management module exists
                # but raises an ImportError for some reason. The only way we
                # can do this is to check the text of the exception. Note that
                # we're a bit broad in how we check the text, because different
                # Python implementations may not use the same text.
                # CPython uses the text "No module named management"
                # PyPy uses "No module named myproject.myapp.management"
                msg = exc.args[0]
                if not msg.startswith('No module named') or 'management' not in msg:
                    raise

        db = options.get('database')
        engine = connections[db]
        default_schema = engine.url.database

        # the default db may not exist yet, so we remove it before connecting
        engine.url.database = None
        tmp_url = str(engine.url)
        engine.url.database = default_schema
        tmp_engine = create_engine(tmp_url)
        tmp_conn = tmp_engine.connect()
        existing_schemas = set([s[0] for s in tmp_conn.execute('show databases')])
        if not default_schema in existing_schemas:
            tmp_engine.execute(CreateSchema(default_schema))
            existing_schemas.add(default_schema)

        # now reconnect with the default_db provided
        conn = engine.connect()
        Base.metadata.bind = engine
        
        if verbosity >= 3:
            self.stdout.write("Getting existing schemas...\n")
            for schema in existing_schemas:
                self.stdout.write("\t%s\n" % schema)
            else:
                self.stdout.write("\tNone\n")

        existing_tables = []
        if verbosity >= 1:
            self.stdout.write("Getting existing tables...\n")
        for schema in existing_schemas:
            for name in engine.engine.table_names(schema, connection=conn):
                existing_tables.append('%s.%s' % (schema,name))
                if verbosity >= 3:
                    self.stdout.write("\t%s.%s\n" % (schema,name))    

        existing_models = []
        if verbosity >= 1:
            self.stdout.write("Getting existing models...\n")
        for cls_name, cls in Base._decl_class_registry.items():
            tablename = get_tablename(cls)
            if tablename and tablename in existing_tables:
                existing_models.append(cls)
                if verbosity >= 3:
                    self.stdout.write("\t%s\n" % cls)

        all_tables = []
        if verbosity >= 1:
            self.stdout.write("Getting required tables...\n")
        for table in Base.metadata.sorted_tables:
            tablename = get_tablename(table)
            all_tables.append(tablename)
            if verbosity >= 3:
                self.stdout.write("\t%s\n" % tablename)

        all_models = []
        if verbosity >= 1:
            self.stdout.write("Getting required models...\n")
        for app in get_apps():
            for model in get_models(app, include_auto_created=True):
                app_name = app.__name__.rsplit('.',1)[0]
                all_models.append( (app_name, model) )
                if verbosity >= 3:
                    self.stdout.write("\t%s.%s\n" % (app_name,model))

        schema_manifest = set()
        table_manifest = set()
        if verbosity >= 1:
            self.stdout.write('Building manifest...\n')
        for app_name, model in all_models:
            tablename = get_tablename(model)
            if tablename in existing_tables:
                continue
            table_manifest.add( (app_name, model) )
            schema = tablename.rsplit('.',1)[0]
            if schema not in existing_schemas:
                schema_manifest.add(schema)

        table_manifest = sorted(table_manifest, key=lambda x: 
            all_tables.index(get_tablename(x[1])))

        if verbosity >= 3:
            print 'Schema Manifest:\n'
            for schema in schema_manifest:
                print '\t%s\n' % schema
            print 'Model/Table Manifest\n'
            for app_name, model in table_manifest:
                print '\t%s.%s (%s)\n' % (app_name, model._meta.object_name, 
                    get_tablename(model)) 

        # create any missing schemas
        if verbosity >= 1:
            self.stdout.write("Creating schemas ...\n")
        for schema in schema_manifest:
            if verbosity >= 3:
                self.stdout.write("\t%s\n" % schema)
            engine.execute(CreateSchema(schema))
            existing_schemas.add(schema)            

        # create any missing tables
        created_models = set()
        if verbosity >= 1:
            self.stdout.write("Creating tables ...\n")
        for app_name, model in table_manifest:
            if verbosity >= 3:
                self.stdout.write("\tCreating table for model %s.%s\n" 
                    % (app_name, model._meta.object_name))
            tablename = get_tablename(model)
            if tablename not in existing_tables:
                model.__table__.create()
                existing_tables.append(tablename)
            existing_models.append(model)
            created_models.add(model)

        # Send the post_syncdb signal
        emit_post_sync_signal(created_models, verbosity, interactive, db)

        # Load initial_data fixtures (unless that has been disabled)
        if load_initial_data:
            call_command('loaddata', 'initial_data', verbosity=verbosity,
                         database=db, skip_validation=True)
Exemplo n.º 7
0
Arquivo: flush.py Projeto: devhub/baph
    def handle_noargs(self, **options):
        #db = options.get('database', DEFAULT_DB_ALIAS)
        #connection = connections[db]
        verbosity = int(options.get('verbosity', 1))
        interactive = options.get('interactive')

        self.style = no_style()

        # Import the 'management' module within each installed app, to register
        # dispatcher events.
        for app_name in settings.INSTALLED_APPS:
            try:
                import_module('.management', app_name)
            except ImportError:
                pass

        #sql_list = sql_flush(self.style, connection, only_django=True)

        if interactive:
            confirm = raw_input("""You have requested a flush of the database.
This will IRREVERSIBLY DESTROY all data currently in the database,
and return each table to the state it was in after syncdb.
Are you sure you want to do this?

    Type 'yes' to continue, or 'no' to cancel: """)
        else:
            confirm = 'yes'

        if confirm == 'yes':
            session = orm.sessionmaker()
            session.expunge_all()
            try:
                session.execute('set foreign_key_checks=0')
                for table in reversed(Base.metadata.sorted_tables):
                    if table.info.get('preserve_during_flush', False):
                        continue
                    try:
                        session.execute(table.delete())
                    except Exception as e:
                        # table not present
                        pass
                session.flush()
            except Exception as e:
                session.rollback()
                raise CommandError('Could not flush the database')
            finally:
                session.execute('set foreign_key_checks=1')
                session.commit()

            # Emit the post sync signal. This allows individual
            # applications to respond as if the database had been
            # sync'd from scratch.
            all_models = []
            for app in get_apps():
                all_models.extend([
                    m for m in get_models(app, include_auto_created=True)
                ])
            emit_post_sync_signal(set(all_models), verbosity, interactive, None) 

            # Reinstall the initial_data fixture.
            if options.get('load_initial_data'):
                # Reinstall the initial_data fixture.
                call_command('loaddata', 'initial_data', **options)

        else:
            self.stdout.write("Flush cancelled.\n")
Exemplo n.º 8
0
    def handle_noargs(self, **options):
        #db = options.get('database', DEFAULT_DB_ALIAS)
        #connection = connections[db]
        verbosity = int(options.get('verbosity', 1))
        interactive = options.get('interactive')

        self.style = no_style()

        # Import the 'management' module within each installed app, to register
        # dispatcher events.
        for app_name in settings.INSTALLED_APPS:
            try:
                import_module('.management', app_name)
            except ImportError:
                pass

        #sql_list = sql_flush(self.style, connection, only_django=True)

        if interactive:
            confirm = raw_input("""You have requested a flush of the database.
This will IRREVERSIBLY DESTROY all data currently in the database,
and return each table to the state it was in after syncdb.
Are you sure you want to do this?

    Type 'yes' to continue, or 'no' to cancel: """)
        else:
            confirm = 'yes'

        if confirm == 'yes':
            session = orm.sessionmaker()
            session.expunge_all()
            try:
                session.execute('set foreign_key_checks=0')
                for table in reversed(Base.metadata.sorted_tables):
                    if table.info.get('preserve_during_flush', False):
                        continue
                    try:
                        session.execute(table.delete())
                    except Exception as e:
                        # table not present
                        pass
                session.flush()
            except Exception as e:
                session.rollback()
                raise CommandError('Could not flush the database')
            finally:
                session.execute('set foreign_key_checks=1')
                session.commit()

            # Emit the post sync signal. This allows individual
            # applications to respond as if the database had been
            # sync'd from scratch.
            all_models = []
            for app in get_apps():
                all_models.extend(
                    [m for m in get_models(app, include_auto_created=True)])
            emit_post_sync_signal(set(all_models), verbosity, interactive,
                                  None)

            # Reinstall the initial_data fixture.
            if options.get('load_initial_data'):
                # Reinstall the initial_data fixture.
                call_command('loaddata', 'initial_data', **options)

        else:
            self.stdout.write("Flush cancelled.\n")
Exemplo n.º 9
0
    def handle_noargs(self, **options):
        #db = options.get('database', DEFAULT_DB_ALIAS)
        #connection = connections[db]
        verbosity = int(options.get('verbosity', 1))
        interactive = options.get('interactive')

        self.style = no_style()

        # Import the 'management' module within each installed app, to register
        # dispatcher events.
        for app_name in settings.INSTALLED_APPS:
            try:
                import_module('.management', app_name)
            except ImportError:
                pass

        #sql_list = sql_flush(self.style, connection, only_django=True)

        if interactive:
            confirm = raw_input("""You have requested a flush of the database.
This will IRREVERSIBLY DESTROY all data currently in the database,
and return each table to the state it was in after syncdb.
Are you sure you want to do this?

    Type 'yes' to continue, or 'no' to cancel: """)
        else:
            confirm = 'yes'

        if confirm == 'yes':
            try:
                session = Session()
                for table in reversed(Base.metadata.sorted_tables):
                    if table.info.get('preserve_during_flush', False):
                        continue
                    try:
                        session.execute(table.delete())
                    except:
                        pass
                session.commit()
            except Exception, e:
                session.rollback()
                raise
                raise CommandError("""Database couldn't be flushed. Possible reasons:
  * The database isn't running or isn't configured correctly.
  * At least one of the expected database tables doesn't exist.
  * The SQL was invalid.
Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run.
The full error: %s""")

            # Emit the post sync signal. This allows individual
            # applications to respond as if the database had been
            # sync'd from scratch.
            all_models = []
            for app in get_apps():
                all_models.extend([
                    m for m in get_models(app, include_auto_created=True)
                ])
            emit_post_sync_signal(set(all_models), verbosity, interactive, None) 

            # Reinstall the initial_data fixture.
            if options.get('load_initial_data'):
                # Reinstall the initial_data fixture.
                call_command('loaddata', 'initial_data', **options)