Example #1
0
 def test_sql_delete(self):
     app_config = apps.get_app_config('commands_sql_migrations')
     with self.assertRaises(CommandError):
         sql_delete(app_config,
                    no_style(),
                    connections[DEFAULT_DB_ALIAS],
                    close_connection=False)
def get_delete_sql(*plugin_models):
    from django.conf import settings
    from django.core.management.color import no_style
    style = no_style()
    from django.core.management import sql
    from django.db import models

#    # Insert app in installed apps
#    old_inst_apps = settings.INSTALLED_APPS
#    inst_apps = list(old_inst_apps)
#    inst_apps.append("PyLucid.system.PyLucidPlugins")
#    settings.INSTALLED_APPS = inst_apps

    models.loading.register_models("PyLucidPlugins", *plugin_models)

    # get all delete statements for the given App
    app = models.get_app("PyLucidPlugins")
    statements = sql.sql_delete(app, style)

    #cleanup
    app_models = models.loading.cache.app_models
    del(app_models["PyLucidPlugins"])
#    settings.INSTALLED_APPS = old_inst_apps

    return statements
Example #3
0
    def _drop_tables(self):
        """
        This is only important for development, if we create a new model and
        change it.
        Should be not used in productive environment!
        """
        print
        print "drop tables:"
        print "-"*80
        from django.db import connection
        from django.db.models import get_app

        app = get_app("PyLucid")

        from django.core.management import sql
        from django.core.management.color import no_style
        statements = sql.sql_delete(app, no_style())

        cursor = connection.cursor()
        for statement in statements:
            for table_name in self.DROP_TABLES:
                if table_name in statement:
                    print "Delete table '%s' (%s):" % (table_name, statement),
                    try:
                        cursor.execute(statement)
                    except Exception, e:
                        print "Error:", e
                    else:
                        print "OK"
Example #4
0
    def remove_seo_tables(self):
        from django.core.management.sql import sql_delete
        from django.db import connection
        from django.core.management.color import no_style
        from rollyourown.seo import models as seo_models

        try:
            sql_list = sql_delete(seo_models, no_style(), connection) 
        except TypeError:
            sql_list = sql_delete(seo_models, no_style())
        cursor = connection.cursor()
        try:
            for sql in sql_list:
                cursor.execute(sql)
        except Exception, e:
            transaction.rollback_unless_managed()
Example #5
0
    def handle_app(self, app, **options):
        db = options.get('database', DEFAULT_DB_ALIAS)
        verbosity = int(options.get('verbosity', 1))
        connection = connections[db]

        drop_queries = u'\n'.join(sql_delete(app, self.style,
                                             connection)).encode('utf-8')
        cursor = connection.cursor()
        for query in drop_queries.split(';'):
            if query != '':
                if verbosity:
                    self.stdout.write('\n\nExecuting query\n%s' %
                                      query.strip())
                cursor.execute(query.strip())
        cursor.close()

        create_queries = u'\n'.join(sql_all(app, self.style,
                                            connection)).encode('utf-8')
        cursor = connection.cursor()
        for query in create_queries.split(';'):
            if query != '':
                if verbosity:
                    self.stdout.write('\n\nExecuting query\n%s' %
                                      query.strip())
                cursor.execute(query.strip())
        cursor.close()

        call_command('loaddata',
                     'initial_data',
                     verbosity=verbosity,
                     database=db)
Example #6
0
 def test_sql_delete(self):
     app = app_cache.get_app_config("commands_sql").models_module
     output = sql_delete(app, no_style(), connections[DEFAULT_DB_ALIAS])
     drop_tables = [o for o in output if o.startswith("DROP TABLE")]
     self.assertEqual(len(drop_tables), 3)
     # Lower so that Oracle's upper case tbl names wont break
     sql = drop_tables[-1].lower()
     six.assertRegex(self, sql, r"^drop table .commands_sql_comment.*")
Example #7
0
 def test_sql_delete(self):
     app_config = apps.get_app_config('commands_sql')
     output = sql_delete(app_config, no_style(), connections[DEFAULT_DB_ALIAS], close_connection=False)
     drop_tables = [o for o in output if o.startswith('DROP TABLE')]
     self.assertEqual(len(drop_tables), 3)
     # Lower so that Oracle's upper case tbl names wont break
     sql = drop_tables[-1].lower()
     six.assertRegex(self, sql, r'^drop table .commands_sql_comment.*')
Example #8
0
 def test_sql_delete(self):
     app = models.get_app('commands_sql')
     output = sql_delete(app, no_style(), connections[DEFAULT_DB_ALIAS])
     drop_tables = [o for o in output if o.startswith('DROP TABLE')]
     self.assertEqual(len(drop_tables), 3)
     # Lower so that Oracle's upper case tbl names wont break
     sql = drop_tables[-1].lower()
     six.assertRegex(self, sql, r'^drop table .commands_sql_comment.*')
Example #9
0
 def test_sql_delete(self):
     app = models.get_app('commands_sql')
     output = sql_delete(app, no_style(), connections[DEFAULT_DB_ALIAS])
     # Oracle produces DROP SEQUENCE and DROP TABLE for this command.
     if connections[DEFAULT_DB_ALIAS].vendor == 'oracle':
         sql = output[1].lower()
     else:
         sql = output[0].lower()
     six.assertRegex(self, sql, r'^drop table .commands_sql_book.*')
Example #10
0
 def test_sql_delete(self):
     app = models.get_app("commands_sql")
     output = sql_delete(app, no_style(), connections[DEFAULT_DB_ALIAS])
     # Oracle produces DROP SEQUENCE and DROP TABLE for this command.
     if connections[DEFAULT_DB_ALIAS].vendor == "oracle":
         sql = output[1].lower()
     else:
         sql = output[0].lower()
     six.assertRegex(self, sql, r"^drop table .commands_sql_book.*")
Example #11
0
    def handle_app(self, app, **options):
        db = options.get('database')
        connection = connections[db]

        commands = sql_delete(app, no_style(), connection)

        for command in commands:
            cursor = connection.cursor()
            cursor.execute(command)
Example #12
0
 def init(cls, application_names):
     applications = [models.get_app(application_name)
         for application_name in application_names]
     upgrade = u''.join([
         u'\n'.join(sql_all(application, no_style()) + ['']).encode('utf-8')
             for application in applications])
     downgrade = u''.join([u'\n'.join(sql_delete(application, no_style()) + ['']).encode('utf-8')
             for application in applications])
     cls.add(upgrade, downgrade)
Example #13
0
    def handle_app(self, app, **options):
        db = options.get('database')
        connection = connections[db]

        commands = sql_delete(app, no_style(), connection)

        for command in commands:
            cursor = connection.cursor()
            cursor.execute(command)
Example #14
0
def dropdb():
    from django.db import models, connections, transaction, DEFAULT_DB_ALIAS
    from django.core.management import sql, color
    app = models.get_app('polls')
    connection = connections[DEFAULT_DB_ALIAS]
    sql_list = sql.sql_delete(app, color.no_style(), connection)
    cursor = connection.cursor()
    for sql in sql_list:
        cursor.execute(sql)
    transaction.commit_unless_managed(using=DEFAULT_DB_ALIAS)
Example #15
0
def destroy_model_tables():
    try:
        statements = sql_delete(d51.django.apps.tagging.tests.support.models, no_style())
        execute_sql(statements)
    except Exception, e:
        # Postgres seems to like to pretend that the table isn't there, even
        # when the database says it is.  We're catching this error here to keep
        # everyone happy.  "Pick your battles" I believe is the phrase.
        if re.search('table "[a-z_]+" does not exist', str(e)):
                return
        raise e
Example #16
0
def stats_restart(request):
    """
    Delete statistics, we do drop table, not the recommended way but damn
    effective.
    """
    from django.core import management
    from django.db import connection, models
    from django.core.management.color import no_style
    from django.core.management import sql

    cursor = connection.cursor()
    logger.info("stats restart")

    # this tables are not going to be deleted
    tables = [
        "openproximity_bluetoothdongle",
        "openproximity_campaignfile",
        "openproximity_marketingcampaign",
        "openproximity_remotescannerbluetoothdongle",
        "openproximity_scannerbluetoothdongle",
        "openproximity_uploaderbluetoothdongle",
        "openproximity_generalsetting",
        "openproximity_userprofile",
    ]
    model = models.get_app("openproximity")
    drop = ""

    drop_table = sql.sql_delete(model, no_style(), connection)

    for line in drop_table:
        table_name = line.split()[2].replace('"', "").replace(";", "")

        if line.startswith("DROP TABLE"):
            # we don't want to loose settings
            if table_name not in tables:
                drop += "DROP TABLE %s;\n" % table_name

            elif line.find("CREATE INDEX") > -1:
                drop += "DROP INDEX %s;\n" % table_name
    try:
        server = rpyc.connect("localhost", 8010)
        server.root.Lock()
        logger.info("database locked")
    except:
        pass

    logger.info("about to drop")
    for line in drop.splitlines():
        try:
            logger.debug(line)
            connection.cursor().execute(line)
        except Exception, err:
            logger.error("%s failed" % line)
            logger.exception(err)
Example #17
0
def execute_test_sql(start, end, sql, debug=False):
    """
    Execute a test SQL sequence. This method also creates and destroys the 
    database tables required by the models registered against the test application.
    
    start and end are the start- and end-point states of the application cache.
    
    sql is the list of sql statements to execute.
    
    cleanup is a list of extra sql statements required to clean up. This is
    primarily for any extra m2m tables that were added during a test that won't 
    be cleaned up by Django's sql_delete() implementation.
    
    debug is a helper flag. It displays the ALL the SQL that would be executed,
    (including setup and teardown SQL), and executes the Django-derived setup/teardown
    SQL.
    """    
    # Set up the initial state of the app cache
    cache.app_models['tests'] = copy.deepcopy(start)
        
    # Install the initial tables and indicies
    style = no_style()    
    execute_transaction(sql_create(evo_test, style), output=debug)
    execute_transaction(sql_indexes(evo_test, style), output=debug)
    create_test_data(models.get_models(evo_test))
            
    # Set the app cache to the end state
    cache.app_models['tests'] = copy.deepcopy(end)
    
    try:
        # Execute the test sql
        if debug:
            write_sql(sql)
        else:
            execute_transaction(sql, output=True)
    finally:
        # Cleanup the apps.
        if debug:
            print sql_delete(evo_test, style)
        else:
            execute_transaction(sql_delete(evo_test, style), output=debug)
Example #18
0
def execute_test_sql(start, end, sql, debug=False):
    """
    Execute a test SQL sequence. This method also creates and destroys the 
    database tables required by the models registered against the test application.
    
    start and end are the start- and end-point states of the application cache.
    
    sql is the list of sql statements to execute.
    
    cleanup is a list of extra sql statements required to clean up. This is
    primarily for any extra m2m tables that were added during a test that won't 
    be cleaned up by Django's sql_delete() implementation.
    
    debug is a helper flag. It displays the ALL the SQL that would be executed,
    (including setup and teardown SQL), and executes the Django-derived setup/teardown
    SQL.
    """
    # Set up the initial state of the app cache
    cache.app_models['tests'] = copy.deepcopy(start)

    # Install the initial tables and indicies
    style = no_style()
    execute_transaction(sql_create(evo_test, style), output=debug)
    execute_transaction(sql_indexes(evo_test, style), output=debug)
    create_test_data(models.get_models(evo_test))

    # Set the app cache to the end state
    cache.app_models['tests'] = copy.deepcopy(end)

    try:
        # Execute the test sql
        if debug:
            write_sql(sql)
        else:
            execute_transaction(sql, output=True)
    finally:
        # Cleanup the apps.
        if debug:
            print sql_delete(evo_test, style)
        else:
            execute_transaction(sql_delete(evo_test, style), output=debug)
Example #19
0
def reset_db():
    using = DEFAULT_DB_ALIAS
    connection = connections[using]
    sql_list = sql_delete(elephantblog.models, no_style(), connection)
    sql_list += sql_all(elephantblog.models, no_style(), connection)
    try:
        cursor = connection.cursor()
        for sql in sql_list:
            cursor.execute(sql)
    except Exception, e:
        transaction.rollback_unless_managed()
        raise CommandError("Error: database couldn't be reset: %s" % e)
Example #20
0
def reset_db():
    using = DEFAULT_DB_ALIAS
    connection = connections[using]
    sql_list = sql_delete(elephantblog.models, no_style(), connection)
    sql_list += sql_all(elephantblog.models, no_style(), connection)
    try:
        cursor = connection.cursor()
        for sql in sql_list:
            cursor.execute(sql)
    except Exception, e:
        transaction.rollback_unless_managed()
        raise CommandError("Error: database couldn't be reset: %s" % e)
Example #21
0
def stats_restart(request):
    '''
    Delete statistics, we do drop table, not the recommended way but damn
    effective.
    '''
    from django.core import management
    from django.db import connection, models
    from django.core.management.color import no_style
    from django.core.management import sql

    cursor = connection.cursor()
    logger.info("stats restart")

    # this tables are not going to be deleted
    tables = [
        'openproximity_bluetoothdongle', 'openproximity_campaignfile',
        'openproximity_marketingcampaign',
        'openproximity_remotescannerbluetoothdongle',
        'openproximity_scannerbluetoothdongle',
        'openproximity_uploaderbluetoothdongle',
        'openproximity_generalsetting', 'openproximity_userprofile'
    ]
    model = models.get_app('openproximity')
    drop = ""

    drop_table = sql.sql_delete(model, no_style(), connection)

    for line in drop_table:
        table_name = line.split()[2].replace('"', '').replace(';', '')

        if line.startswith('DROP TABLE'):
            # we don't want to loose settings
            if table_name not in tables:
                drop += "DROP TABLE %s;\n" % table_name

            elif line.find('CREATE INDEX') > -1:
                drop += "DROP INDEX %s;\n" % table_name
    try:
        server = rpyc.connect('localhost', 8010)
        server.root.Lock()
        logger.info("database locked")
    except:
        pass

    logger.info("about to drop")
    for line in drop.splitlines():
        try:
            logger.debug(line)
            connection.cursor().execute(line)
        except Exception, err:
            logger.error("%s failed" % line)
            logger.exception(err)
Example #22
0
    def resetapp(self,appname):
        """
        Reset application to initial values
        """
        self.reload_all_data()
        
        from django.core.management.sql import sql_delete,sql_all
        from django.db import connections, DEFAULT_DB_ALIAS
        # Remove tables
        for i in sql_delete(self.get_app(appname), self.style, connections[DEFAULT_DB_ALIAS]):
            connections[DEFAULT_DB_ALIAS].cursor().execute(i)
        # Resynchronize database
#        for i in sql_all(self.get_app(appname), self.style, connections[DEFAULT_DB_ALIAS]):
#            connections[DEFAULT_DB_ALIAS].cursor().execute(i)
        self.syncdb()
Example #23
0
 def get_sql(self,appname):
     """
     Return all SQL statements for given application
     """
     from django.core.management.sql import sql_delete,sql_create,sql_custom,sql_indexes
     from django.db import connections, DEFAULT_DB_ALIAS
     app=self.get_app(appname)
     db=connections[DEFAULT_DB_ALIAS]
     # Tables creation statements
     create='\n'.join(sql_create(app, self.style, db))
     # Custom SQL statements
     custom='\n'.join(sql_custom(app, self.style, db))
     # Index creation statements
     indexes='\n'.join(sql_indexes(app, self.style, db))
     # Delete statements
     delete='\n'.join(sql_delete(app, self.style, db))
     return (create,custom,indexes,delete)
Example #24
0
def sql_delete(app, db_name=None):
    """Return SQL statements for deleting all models in an app.

    This provides compatibility with all supported versions of Django.

    Args:
        app (module):
            The application module containing the models to delete.

        db_name (str, optional):
            The database connection name. Defaults to the default database
            connection.

    Returns:
        list:
        The list of SQL statements for deleting the models and constraints.
    """
    connection = connections[db_name or DEFAULT_DB_ALIAS]

    if BaseDatabaseSchemaEditor:
        # Django >= 1.7
        introspection = connection.introspection

        all_table_names = set(introspection.table_names())
        deleted_models = set()

        introspection = connection.introspection

        with connection.schema_editor(collect_sql=True) as schema_editor:
            for model in get_models(app):
                table_name = convert_table_name(connection,
                                                model._meta.db_table)

                if (table_name in all_table_names
                        and model not in deleted_models):
                    schema_editor.delete_model(model)
                    deleted_models.add(model)

        return schema_editor.collected_sql
    else:
        # Django < 1.7
        style = color.no_style()

        return sql_utils.sql_delete(app, style, connection)
Example #25
0
def sql_delete(app, db_name=None):
    """Return SQL statements for deleting all models in an app.

    This provides compatibility with all supported versions of Django.

    Args:
        app (module):
            The application module containing the models to delete.

        db_name (str, optional):
            The database connection name. Defaults to the default database
            connection.

    Returns:
        list:
        The list of SQL statements for deleting the models and constraints.
    """
    connection = connections[db_name or DEFAULT_DB_ALIAS]

    if BaseDatabaseSchemaEditor:
        # Django >= 1.7
        all_table_names = set(connection.introspection.table_names())
        constraints_sql = []
        models_sql = []
        deleted_models = set()
        deleted_refs = set()

        with connection.schema_editor(collect_sql=True) as schema_editor:
            for model in get_models(app, include_auto_created=True):
                temp_constraints_sql, temp_models_sql = \
                    _sql_delete_model(connection, schema_editor, model,
                                      deleted_models, deleted_refs,
                                      all_table_names)
                constraints_sql += temp_constraints_sql
                models_sql += temp_models_sql

        return constraints_sql + models_sql
    else:
        # Django < 1.7
        style = color.no_style()

        return sql.sql_delete(app, style, connection)
Example #26
0
def sql_delete(app, db_name=None):
    """Return SQL statements for deleting all models in an app.

    This provides compatibility with all supported versions of Django.

    Args:
        app (module):
            The application module containing the models to delete.

        db_name (str, optional):
            The database connection name. Defaults to the default database
            connection.

    Returns:
        list:
        The list of SQL statements for deleting the models and constraints.
    """
    connection = connections[db_name or DEFAULT_DB_ALIAS]

    if BaseDatabaseSchemaEditor:
        # Django >= 1.7
        all_table_names = set(connection.introspection.table_names())
        constraints_sql = []
        models_sql = []
        deleted_models = set()
        deleted_refs = set()

        with connection.schema_editor(collect_sql=True) as schema_editor:
            for model in get_models(app, include_auto_created=True):
                temp_constraints_sql, temp_models_sql = \
                    _sql_delete_model(connection, schema_editor, model,
                                      deleted_models, deleted_refs,
                                      all_table_names)
                constraints_sql += temp_constraints_sql
                models_sql += temp_models_sql

        return constraints_sql + models_sql
    else:
        # Django < 1.7
        style = color.no_style()

        return sql.sql_delete(app, style, connection)
Example #27
0
def sql_delete(app, db_name=None):
    """Return SQL statements for deleting all models in an app.

    This provides compatibility with all supported versions of Django.

    Args:
        app (module):
            The application module containing the models to delete.

        db_name (str, optional):
            The database connection name. Defaults to the default database
            connection.

    Returns:
        list:
        The list of SQL statements for deleting the models and constraints.
    """
    connection = connections[db_name or DEFAULT_DB_ALIAS]

    if BaseDatabaseSchemaEditor:
        # Django >= 1.7
        all_table_names = set(connection.introspection.table_names())
        deleted_models = set()

        introspection = connection.introspection

        with connection.schema_editor(collect_sql=True) as schema_editor:
            for model in get_models(app):
                table_name = introspection.table_name_converter(
                    model._meta.db_table)

                if (table_name in all_table_names and
                    model not in deleted_models):
                    schema_editor.delete_model(model)
                    deleted_models.add(model)

        return schema_editor.collected_sql
    else:
        # Django < 1.7
        style = color.no_style()

        return sql.sql_delete(app, style, connection)
Example #28
0
    def get_delete_sql(self, plugin_models):
        """
        Returns a list of sql statements for deleting the plugin model tabels.
        For this, we used the fake django app "PyLucidPlugins" and attach
        all models to this add temporarly.
        """
        from django.core.management import sql
        from django.core.management.color import no_style
        style = no_style()

        models.loading.register_models("PyLucidPlugins", *plugin_models)

        app = models.get_app("PyLucidPlugins")

        statements = sql.sql_delete(app, style)

        # cleanup
        app_models = models.loading.cache.app_models
        del(app_models["PyLucidPlugins"])

        return statements
Example #29
0
 def handle_gt_12(self, *args, **options):
     connection, router = self.getConnection(*args, **options)
     d_only = options['django_only']
     drop_commands = []
     apps = get_apps()
     for app in apps:
         drop_commands.extend(
             sql.sql_delete(app,self.style, connection)
         )
     connection.enter_transaction_management(True) 
     for cmd in drop_commands:
         cursor = connection.cursor()
         # we're dropping everything so it's alright to cascade
         c =  '%s CASCADE;' % cmd[:-1] 
         if re.match('DROP', c):
             try:
                 cursor.execute(c)
                 connection.commit()
             except DatabaseError as e:
                 connection.rollback()
                 continue
     connection.leave_transaction_management()
Example #30
0
	def handle_app(self, app, **options):
		db = options.get('database', DEFAULT_DB_ALIAS)
		verbosity = int(options.get('verbosity', 1))
		connection = connections[db]

		drop_queries = u'\n'.join(sql_delete(app, self.style, connection)).encode('utf-8')
		cursor = connection.cursor()
		for query in drop_queries.split(';'):
			if query != '':
				if verbosity:
					self.stdout.write('\n\nExecuting query\n%s' % query.strip())
				cursor.execute(query.strip())
		cursor.close()

		create_queries = u'\n'.join(sql_all(app, self.style, connection)).encode('utf-8')
		cursor = connection.cursor()
		for query in create_queries.split(';'):
			if query != '':
				if verbosity:
					self.stdout.write('\n\nExecuting query\n%s' % query.strip())
				cursor.execute(query.strip())
		cursor.close()

		call_command('loaddata', 'initial_data', verbosity = verbosity, database = db)
        '-d', '--article-dir',
        help='The directory holding raw article files for the TUA types')
    parser.add_argument(
        '-r', '--reset-db',
        action='store_true',
        default=False,
        help='Reset the database before loading data?')
    return parser.parse_args()

if __name__ == '__main__':
    args = load_args()
    if args.reset_db:
        conn = connections[DEFAULT_DB_ALIAS]

        # generate sql to delete all app tables
        reset_sql = '\n'.join(
            sql_delete(models.get_app('thresher'), no_style(), conn))
        print "Resetting DB..."
        print reset_sql

        # execute the sql in a transaction
        # TODO: TRANSACTION
        conn.cursor().execute(reset_sql)

        # run syncdb to recreate tables/indexes
        call_command('syncdb')
    if args.schema_dir:
        load_schema_dir(args.schema_dir)
    if args.article_dir:
        load_article_dir(args.article_dir)
Example #32
0
def sql_reset(app, style, connection):
    "Returns a list of the DROP TABLE SQL, then the CREATE TABLE SQL, for the given module."
    return sql_delete(app, style, connection) + sql_all(app, style, connection)
Example #33
0
    def handle(self, *args, **options):

        using = options.get('database', DEFAULT_DB_ALIAS)
        dbname = settings.DATABASES[using]['NAME']
        engine = settings.DATABASES[using]['ENGINE']
        if options.get('interactive'):
            if not confirm("""We are going to flush your database (%s).
Are you sure (y/n) ?""" % dbname):
                raise CommandError("User abort.")

        fixtures = options.pop('fixtures', args)

        # print(20160817, fixtures, options)

        options.update(interactive=False)

        # the following log message was useful on Travis 20150104
        if options.get('verbosity', 1) > 0:
            dd.logger.info("`initdb %s` started on database %s.",
                           ' '.join(fixtures), dbname)

        if engine == 'django.db.backends.sqlite3':
            if dbname != ':memory:' and os.path.isfile(dbname):
                os.remove(dbname)
                del connections[using]
        elif engine == 'django.db.backends.mysql':
            conn = connections[using]
            cursor = conn.cursor()
            cursor.execute("DROP DATABASE %s;" % dbname)
            cursor.execute("CREATE DATABASE %s charset 'utf8';" % dbname)
            # We must now force Django to reconnect, otherwise we get
            # "no database selected" since Django would try to
            # continue on the dropped database:
            del connections[using]

            # now reconnect and set foreign_key_checks to 0
            conn = connections[using]
            cursor = conn.cursor()
            cursor.execute("set foreign_key_checks=0;")
        elif engine == 'django.db.backends.postgresql':
            foralltables(using, "DROP TABLE IF EXISTS {} CASCADE;")
            # cmd = """select 'DROP TABLE "' || tablename || '" IF EXISTS CASCADE;' from pg_tables where schemaname = 'public';"""
            # cursor.execute(cmd)
            # cursor.close()
            del connections[using]
        else:
            raise Exception("Not tested for %r" % engine)
            sql_list = []
            conn = connections[using]

            # adds a "DELETE FROM tablename;" for each table
            # sql = sql_flush(no_style(), conn, only_django=False)
            # sql_list.extend(sql)

            if AFTER17:
                # django.core.management.base.CommandError: App
                # 'sessions' has migrations. Only the sqlmigrate and
                # sqlflush commands can be used when an app has
                # migrations.
                # from django.apps import apps
                # app_list = apps.get_app_configs()
                # for app in app_list:
                #     sql_list.extend(sql_delete(app, no_style(), conn))
                pass

            elif USE_SQLDELETE:
                from django.core.management.sql import sql_delete
                # sql_delete was removed in Django 1.9
                # ~ sql_list = u'\n'.join(sql_reset(app, no_style(), conn)).encode('utf-8')

                app_list = [
                    models.get_app(p.app_label)
                    for p in settings.SITE.installed_plugins
                ]
                for app in app_list:
                    # app_label = app.__name__.split('.')[-2]
                    sql_list.extend(sql_delete(app, no_style(), conn))
                    # print app_label, ':', sql_list

            # ~ print sql_list

            if len(sql_list):
                with conn.constraint_checks_disabled():
                    # for sql in sql_list:
                    #     cursor.execute(sql)

                    pending = self.try_sql(conn, sql_list)
                    while len(pending):
                        pending = self.try_sql(conn, pending)

            transaction.commit_unless_managed()

        settings.SITE._site_config = None  # clear cached instance

        if engine == 'django.db.backends.postgresql':
            #     # a first time to create tables of contenttypes. At
            #     # least on PostgreSQL this is required because for
            #     # some reason the syncdb fails when contenttypes is
            #     # not initialized.
            call_command('migrate', **options)
        call_command('makemigrations', interactive=False, verbosity=0)
        call_command('migrate', '--run-syncdb', **options)

        if len(fixtures):
            # if engine == 'django.db.backends.postgresql':
            #     foralltables(using, "ALTER TABLE {} DISABLE TRIGGER ALL;")

            options.pop('interactive')
            call_command('loaddata', *fixtures, **options)
Example #34
0
    def handle(self, *args, **options):

        #~ from lino.core.kernel import analyze_models
        #~ analyze_models()

        #~ from lino.utils import dblogger

        #~ if not dblogger.logger.isEnabledFor(logging.INFO):
            #~ raise CommandError("System logger must be enabled for INFO")
        #~ dblogger.info(settings.SITE.welcome_text())
        #~ dblogger.info("FIXTURE_DIRS is %s",settings.FIXTURE_DIRS)
        using = options.get('database', DEFAULT_DB_ALIAS)
        dbname = settings.DATABASES[using]['NAME']
        if options.get('interactive'):
            if not confirm("""We are going to flush your database (%s).
Are you sure (y/n) ?""" % dbname):
                raise CommandError("User abort.")

        options.update(interactive=False)
        #~ dblogger.info("Lino initdb %s started on database %s.", args, dbname)

        if USE_DROP_CREATE:

            from django.db import connection
            cursor = connection.cursor()
            #~ cursor.execute("DROP DATABASE %s;", [connection.settings_dict['NAME']])
            #~ cursor.execute("CREATE DATABASE %s;", [connection.settings_dict['NAME']])
            cursor.execute("DROP DATABASE %s;" % dbname)
            cursor.execute("CREATE DATABASE %s;" % dbname)

        else:

            sql_list = []
            conn = connections[using]

            if AFTER17:
                # from django.apps import apps
                # print 20140913, apps
                # app_list = apps.get_app_configs()
                sql = sql_flush(no_style(), conn, only_django=False)
                sql_list.extend(sql)

            elif USE_SQLDELETE:
                #~ sql_list = u'\n'.join(sql_reset(app, no_style(), conn)).encode('utf-8')
                app_list = [models.get_app(app_label)
                            for app_label in app_labels()]
                for app in app_list:
                    # app_label = app.__name__.split('.')[-2]
                    sql_list.extend(sql_delete(app, no_style(), conn))
                    # print app_label, ':', sql_list
            else:
                #~ call_command('flush',verbosity=0,interactive=False)
                #~ call_command('flush',**options)
                sql_list.extend(sql_flush(no_style(), conn, only_django=False))

            #~ print sql_list

            try:
                cursor = conn.cursor()
                for sql in sql_list:
                    # print sql
                    cursor.execute(sql)
            except Exception:
                transaction.rollback_unless_managed(using=using)
                raise

            transaction.commit_unless_managed()

        #~ call_command('reset',*apps,**options)
        #~ call_command('syncdb',load_initial_data=False,**options)
        #~ if USE_SQLDELETE:

        #~ tried to call `syncdb` with `verbosity=0` to avoid the
        #~ irritating message "No fixtures found" (which comes because there
        #~ are no `initial_data` fixtures):
        #~ syncdb_options = dict(**options)
        #~ syncdb_options.update(verbosity=0)
        #~ call_command('syncdb',**syncdb_options)
        #~ not good because all other messages "Creating table..." also disappear.

        #~ """
        #~ When loading a full dump back into the database,
        #~ initdb must disable the post_syncdb signal emitted by syncdb
        #~ which would cause automatisms like
        #~ `django.contrib.auth.management.create_permissions`
        #~ `django.contrib.auth.management.create_superuser`
        #~ `django.contrib.sites.management.create_default_site`
        #~ """
        #~ if options.get('dumped'):
            #~ class NullSignal:
                #~ def connect(*args,**kw):
                    #~ pass
                #~ def send(*args,**kw):
                    #~ pass
            #~ models.signals.post_syncdb = NullSignal()

        settings.SITE._site_config = None  # clear cached instance

        call_command('syncdb', load_initial_data=False, **options)

        if len(args):
            call_command('loaddata', *args, **options)
Example #35
0
 def test_sql_delete(self):
     app = models.get_app('commands_sql')
     output = sql_delete(app, no_style(), connections[DEFAULT_DB_ALIAS])
     six.assertRegex(self, output[0], r'^DROP TABLE .commands_sql_book.*')
Example #36
0
 def handle_app(self, app, **options):
     return u'\n'.join(sql_delete(app, self.style, connections[options.get('database')])).encode('utf-8')
Example #37
0
 def handle_app_config(self, app_config, **options):
     if app_config.models_module is None:
         return
     connection = connections[options['database']]
     statements = sql_delete(app_config, self.style, connection)
     return '\n'.join(statements)
Example #38
0
 def handle_app_config(self, app_config, **options):
     if app_config.models_module is None:
         return
     connection = connections[options['database']]
     statements = sql_delete(app_config, self.style, connection)
     return '\n'.join(statements)
Example #39
0
        help='The directory holding raw article files for the TUA types')
    parser.add_argument('-r',
                        '--reset-db',
                        action='store_true',
                        default=False,
                        help='Reset the database before loading data?')
    return parser.parse_args()


if __name__ == '__main__':
    args = load_args()
    if args.reset_db:
        conn = connections[DEFAULT_DB_ALIAS]

        # generate sql to delete all app tables
        reset_sql = '\n'.join(
            sql_delete(models.get_app('thresher'), no_style(), conn))
        print "Resetting DB..."
        print reset_sql

        # execute the sql in a transaction
        # TODO: TRANSACTION
        conn.cursor().execute(reset_sql)

        # run syncdb to recreate tables/indexes
        call_command('syncdb')
    if args.schema_dir:
        load_schema_dir(args.schema_dir)
    if args.article_dir:
        load_article_dir(args.article_dir)
Example #40
0
 def handle_app(self, app, **options):
     from django.core.management.sql import sql_delete
     return u'\n'.join(sql_delete(app, self.style)).encode('utf-8')
Example #41
0
 def test_sql_delete(self):
     app_config = apps.get_app_config('commands_sql_migrations')
     with self.assertRaises(CommandError):
         sql_delete(app_config, no_style(), connections[DEFAULT_DB_ALIAS])
Example #42
0
    def handle(self, *args, **options):

        #~ from lino.core.kernel import analyze_models
        #~ analyze_models()

        #~ from lino.utils import dblogger

        #~ if not dblogger.logger.isEnabledFor(logging.INFO):
            #~ raise CommandError("System logger must be enabled for INFO")
        #~ dblogger.info(settings.SITE.welcome_text())
        #~ dblogger.info("FIXTURE_DIRS is %s",settings.FIXTURE_DIRS)
        if settings.SITE.readonly:
            dd.logger.info(
                "Skipped `initdb` on readonly site '%s'.",
                settings.SETTINGS_MODULE)
            return

        using = options.get('database', DEFAULT_DB_ALIAS)
        dbname = settings.DATABASES[using]['NAME']
        engine = settings.DATABASES[using]['ENGINE']
        if options.get('interactive'):
            if not confirm("""We are going to flush your database (%s).
Are you sure (y/n) ?""" % dbname):
                raise CommandError("User abort.")

        options.update(interactive=False)
        # the following log message was useful on Travis 20150104
        dd.logger.info(
            "`initdb %s` started on database %s.", ' '.join(args), dbname)

        if engine == 'django.db.backends.sqlite3':
            if dbname != ':memory:' and os.path.isfile(dbname):
                os.remove(dbname)
                del connections[using]
        elif engine == 'django.db.backends.mysql':
            conn = connections[using]
            cursor = conn.cursor()
            cursor.execute("DROP DATABASE %s;" % dbname)
            cursor.execute("CREATE DATABASE %s charset 'utf8';" % dbname)
            # We must now force Django to reconnect, otherwise we get
            # "no database selected" since Django would try to
            # continue on the dropped database:
            del connections[using]
        else:
            raise Exception("Not tested for %r" % engine)
            sql_list = []
            conn = connections[using]

            # adds a "DELETE FROM tablename;" for each table
            # sql = sql_flush(no_style(), conn, only_django=False)
            # sql_list.extend(sql)

            if AFTER17:
                # django.core.management.base.CommandError: App
                # 'sessions' has migrations. Only the sqlmigrate and
                # sqlflush commands can be used when an app has
                # migrations.
                # from django.apps import apps
                # app_list = apps.get_app_configs()
                # for app in app_list:
                #     sql_list.extend(sql_delete(app, no_style(), conn))
                pass

            elif USE_SQLDELETE:
                #~ sql_list = u'\n'.join(sql_reset(app, no_style(), conn)).encode('utf-8')
                
                app_list = [models.get_app(p.app_label)
                            for p in settings.SITE.installed_plugins]
                for app in app_list:
                    # app_label = app.__name__.split('.')[-2]
                    sql_list.extend(sql_delete(app, no_style(), conn))
                    # print app_label, ':', sql_list

            #~ print sql_list

            if len(sql_list):
                with conn.constraint_checks_disabled():
                    # for sql in sql_list:
                    #     cursor.execute(sql)

                    pending = self.try_sql(conn, sql_list)
                    while len(pending):
                        pending = self.try_sql(conn, pending)

                # conn.disable_constraint_checking()
                # try:
                #     cursor = conn.cursor()
                #     pending = self.try_sql(cursor, sql_list)
                #     while len(pending):
                #         pending = self.try_sql(cursor, pending)

                # except Exception:
                #     transaction.rollback_unless_managed(using=using)
                #     raise
                # conn.enable_constraint_checking()

            transaction.commit_unless_managed()

        settings.SITE._site_config = None  # clear cached instance

        if AFTER17:
            call_command('migrate', **options)
        else:
            call_command('syncdb', load_initial_data=False, **options)

        if len(args):
            call_command('loaddata', *args, **options)
Example #43
0
 def handle_app(self, app, **options):
     return u'\n'.join(
         sql_delete(app, self.style,
                    connections[options.get('database')])).encode('utf-8')
Example #44
0
 def handle_app_config(self, app_config, **options):
     if app_config.models_module is None:
         return
     connection = connections[options.get("database")]
     statements = sql_delete(app_config.models_module, self.style, connection)
     return "\n".join(statements)
Example #45
0
 def test_sql_delete(self):
     app = models.get_app('commands_sql')
     output = sql_delete(app, no_style(), connections[DEFAULT_DB_ALIAS])
     six.assertRegex(self, output[0], r'^DROP TABLE .commands_sql_book.*')
 def handle_app(self, app, **options):
     return "\n".join(sql_delete(app, self.style, connections[options.get("database")]))