def sync_apps(self, connection, app_labels):
cursor = connection.cursor()<EOL>try:<EOL><INDENT>tables = connection.introspection.table_names(cursor)<EOL>created_models = set()<EOL>all_models = [<EOL>(app_config.label,<EOL>router.get_migratable_models(app_config, connection.alias, include_auto_created=False))<EOL>for app_config in apps.get_app_configs()<EOL>if app_config.models_module is not None and app_config.label in app_labels<EOL>]<EOL>def model_installed(model):<EOL><INDENT>opts = model._meta<EOL>converter = connection.introspection.table_name_converter<EOL>return not (<EOL>(converter(opts.db_table) in tables) or<EOL>(opts.auto_created and converter(opts.auto_created._meta.db_table) in tables)<EOL>)<EOL><DEDENT>manifest = OrderedDict(<EOL>(app_name, list(filter(model_installed, model_list)))<EOL>for app_name, model_list in all_models<EOL>)<EOL>if self.verbosity >= <NUM_LIT:1>:<EOL><INDENT>self.stdout.write("<STR_LIT>")<EOL><DEDENT>with transaction.atomic(using=connection.alias, savepoint=connection.features.can_rollback_ddl):<EOL><INDENT>with connection.schema_editor() as editor:<EOL><INDENT>editor.execute("<STR_LIT>".format(settings.POSTGRES_TEMPLATE_SCHEMA))<EOL>statements = editor.connection.ops.prepare_sql_script(CLONE_SCHEMA)<EOL>for statement in statements:<EOL><INDENT>editor.execute(statement, params=None)<EOL><DEDENT><DEDENT>schema_deferred_sql = {}<EOL>with connection.schema_editor() as editor:<EOL><INDENT>schema_model = apps.get_model(settings.POSTGRES_SCHEMA_MODEL)<EOL>editor.create_model(schema_model, verbosity=self.verbosity)<EOL>schema_deferred_sql.update(editor.schema_deferred_sql)<EOL>editor.schema_deferred_sql = {}<EOL>created_models.add(schema_model)<EOL><DEDENT>for app_name, model_list in manifest.items():<EOL><INDENT>for model in model_list:<EOL><INDENT>if not model._meta.can_migrate(connection):<EOL><INDENT>continue<EOL><DEDENT>if model in created_models:<EOL><INDENT>continue  <EOL><DEDENT>if self.verbosity >= <NUM_LIT:3>:<EOL><INDENT>self.stdout.write(<EOL>"<STR_LIT>" % (app_name, model._meta.object_name)<EOL>)<EOL><DEDENT>with connection.schema_editor() as editor:<EOL><INDENT>editor.schema_deferred_sql.update(schema_deferred_sql)<EOL>editor.create_model(model, verbosity=self.verbosity)<EOL>schema_deferred_sql.update(editor.schema_deferred_sql)<EOL>editor.schema_deferred_sql = {}<EOL>created_models.add(model)<EOL><DEDENT><DEDENT><DEDENT>if self.verbosity >= <NUM_LIT:1>:<EOL><INDENT>self.stdout.write("<STR_LIT>")<EOL><DEDENT>with connection.schema_editor() as editor:<EOL><INDENT>editor.schema_deferred_sql = schema_deferred_sql<EOL><DEDENT><DEDENT><DEDENT>finally:<EOL><INDENT>cursor.close()<EOL><DEDENT>return created_models<EOL>
Runs the old syncdb-style operation on a list of app_labels.