def create_models_from_app(cls, app_name): """ Manually create Models (used only for testing) from the specified string app name. Models are loaded from the module '<app_name>.models' """ from django.db import connection, DatabaseError from django.test.simple import DjangoTestSuiteRunner # NB: `load_app` is deprecated as of Django 1.7. if re.match(r'''^1\.7.*$''', django.get_version()) is not None: from django.apps import apps # NB: Django 1.7 doesn't work with the fully-qualified app name. app = apps.get_app_config(app_name.split('.')[1]) else: from django.db.models.loading import load_app app = load_app(app_name) from django.core.management import sql from django.core.management.color import no_style sql = sql.sql_create(app, no_style(), connection) cursor = connection.cursor() for statement in sql: try: cursor.execute(statement) except DatabaseError, excn: logger.debug(excn.message)
def sql_create(app, db_name=None): """Return SQL statements for creating all models for an app. This provides compatibility with all supported versions of Django. Args: app (module): The application module. db_name (str, optional): The database connection name. Defaults to the default database connection. Returns: list: The list of SQL statements used to create the models for the app. """ connection = connections[db_name or DEFAULT_DB_ALIAS] if BaseDatabaseSchemaEditor: # Django >= 1.7 with connection.schema_editor(collect_sql=True) as schema_editor: for model in get_models(app): schema_editor.create_model(model) return schema_editor.collected_sql else: # Django < 1.7 style = color.no_style() return (sql.sql_create(app, style, connection) + sql.sql_indexes(app, style, connection))
def test_sql_create(self): app_config = apps.get_app_config('commands_sql') output = sql_create(app_config, no_style(), connections[DEFAULT_DB_ALIAS]) tables = set() create_table_re = re.compile(r'^create table .(?P<table>[\w_]+).*', re.IGNORECASE) reference_re = re.compile(r'.* references .(?P<table>[\w_]+).*', re.IGNORECASE) for statement in output: create_table = create_table_re.match(statement) if create_table: # Lower since Oracle's table names are upper cased. tables.add(create_table.group('table').lower()) continue reference = reference_re.match(statement) if reference: # Lower since Oracle's table names are upper cased. table = reference.group('table').lower() self.assertIn( table, tables, "The table %s is referenced before its creation." % table) self.assertEqual( tables, { 'commands_sql_comment', 'commands_sql_book', 'commands_sql_book_comments' })
def test_sql_create(self): app = app_cache.get_app_config('commands_sql').models_module output = sql_create(app, no_style(), connections[DEFAULT_DB_ALIAS]) create_tables = [o for o in output if o.startswith('CREATE TABLE')] self.assertEqual(len(create_tables), 3) # Lower so that Oracle's upper case tbl names wont break sql = create_tables[-1].lower() six.assertRegex(self, sql, r'^create table .commands_sql_book.*')
def test_sql_create(self): app = models.get_app('commands_sql') output = sql_create(app, no_style(), connections[DEFAULT_DB_ALIAS]) create_tables = [o for o in output if o.startswith('CREATE TABLE')] self.assertEqual(len(create_tables), 3) # Lower so that Oracle's upper case tbl names wont break sql = create_tables[-1].lower() six.assertRegex(self, sql, r'^create table .commands_sql_book.*')
def install(model): from django.core.management import sql, color from django.db import connection style = color.no_style() cursor = connection.cursor() statements = sql.sql_create(model, style, connection) for sql in statements: cursor.execute(sql)
def test_sql_create_check(self): """Regression test for #23416 -- Check that db_params['check'] is respected.""" app_config = apps.get_app_config('commands_sql') output = sql_create(app_config, no_style(), connections[DEFAULT_DB_ALIAS]) success = False for statement in output: if 'CHECK' in statement: success = True if not success: self.fail("'CHECK' not found in output %s" % output)
def create_model_tables(): """ Create the table for the provided model(s) Yes, yes, yes. This *should* be part of Django. Instead, this logic is locked down like p**n star with an STD inside the `django.core.management` command, so we've got the logic here. """ style = no_style() app = d51.django.apps.tagging.tests.support.models statements = sql_create(app, style) + sql_indexes(app, style) execute_sql(statements)
def _create_test_models(cls, app_name='testapp'): """Create dynamic test models. Defaulted to models registered at app_name.models.py """ app = loading.load_app(app_name) create_sql = sql.sql_create(app, color.no_style(), connection) cursor = connection.cursor() for statement in create_sql: try: cursor.execute(statement) except DatabaseError as ex: LOGGER.debug(ex)
def create_models_from_app(cls, app_name): """ Manually create Models (used only for testing) from the specified string app name. Models are loaded from the module "<app_name>.models" """ app = load_app(app_name) asql = sql.sql_create(app, no_style(), connection) cursor = connection.cursor() for statement in asql: try: cursor.execute(statement) except DatabaseError, excn: print excn.message
def teste_banco(request): app = load_app("modulos.noticia") app = import_module("modulos.noticia.admin") raise Exception(app) connection = connections["default"] style = color.no_style() criar = sql.sql_create(app, style, connection) raise Exception(criar) raise Exception(path) cor = color.no_style model = import_module("modulos.noticia.models") cursor = connection.cursor() statements, pending = sql.sql_model_create(model, style)
def add_app(args, output): " <app>: Add tables for a new application" if len(args) != 1: raise CommandError('./manage.py migration app <name-of-app>') app_label = args[0] app = models.get_app(app_label) from django.core.management.sql import sql_create up_sql = sql_create(app, no_style()) down_sql = sql_delete(app, no_style()) app_name = app.__name__.replace('.', '_') migration_output = app_mtemplate % (clean_up_create_sql(up_sql), clean_up_create_sql(down_sql)) migration_output = migration_code(migration_output) save_migration(output, migration_output, app_name)
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)
def create_models_from_app(app_name): """ Manually create Models (used only for testing) from the specified string app name. Models are loaded from the module "<app_name>.models" """ from django.db import connection, DatabaseError from django.db.models.loading import load_app from django.core.management import sql from django.core.management.color import no_style app = load_app(app_name) sql = sql.sql_create(app, no_style(), connection) cursor = connection.cursor() for statement in sql: try: cursor.execute(statement) except DatabaseError: pass
def add_app(args, output): " <app>: Add tables for a new application" if len(args) != 1: raise CommandError('./manage.py migration app <name-of-app>') app_label = args[0] app = models.get_app(app_label) from django.core.management.sql import sql_create up_sql = sql_create(app, no_style(), connections[DEFAULT_DB_ALIAS]) down_sql = sql_delete(app, no_style()) app_name = app.__name__.replace('.', '_') migration_output = app_mtemplate % ( clean_up_create_sql(up_sql), clean_up_create_sql(down_sql) ) migration_output = migration_code(migration_output) save_migration(output, migration_output, app_name)
def get_create_table2(*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 models.loading.register_models("PyLucidPlugins", *plugin_models) # get all delete statements for the given App app = models.get_app("PyLucidPlugins") statements = sql.sql_create(app, style) #cleanup app_models = models.loading.cache.app_models del(app_models["PyLucidPlugins"]) # settings.INSTALLED_APPS = old_inst_apps return statements
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)
def test_sql_create(self): app_config = apps.get_app_config('commands_sql') output = sql_create(app_config, no_style(), connections[DEFAULT_DB_ALIAS]) tables = set() create_table_re = re.compile(r'^create table .(?P<table>[\w_]+).*', re.IGNORECASE) reference_re = re.compile(r'.* references .(?P<table>[\w_]+).*', re.IGNORECASE) for statement in output: create_table = create_table_re.match(statement) if create_table: # Lower since Oracle's table names are upper cased. tables.add(create_table.group('table').lower()) continue reference = reference_re.match(statement) if reference: # Lower since Oracle's table names are upper cased. table = reference.group('table').lower() self.assertIn( table, tables, "The table %s is referenced before its creation." % table ) self.assertEqual(tables, { 'commands_sql_comment', 'commands_sql_book', 'commands_sql_book_comments' })
def handle_app(self, app, **options): return u'\n'.join( sql_create(app, self.style, connections[options.get('database')])).encode('utf-8')
def instalar_plugin(request): conteudo_arquivo = """aplicacoes = ( 'django.contrib.auth', 'third_party.grappelli', 'third_party.djblets', 'django.contrib.admin', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.sitemaps', 'django.contrib.syndication', 'django.contrib.messages', 'modulos.catalogo', 'modulos.cinema', 'modulos.configuracao', 'modulos.disco_virtual', 'modulos.index_enquete', 'modulos.index_evento', 'modulos.index_gallery', 'modulos.newsletter', 'modulos.noticia', 'modulos.novidades', "modulos.onde_encontrar", 'modulos.paginas', 'modulos.popup', 'modulos.produtos', 'modulos.sociable', 'modulos.tags', 'modulos.scrum', 'third_party.debug_toolbar', 'third_party.tagging', )""" caminho_xml = "%s/../plugins_instalados.py" % settings.MEDIA_ROOT arquivo_xml = open(caminho_xml, "w") arquivo_xml.write(unicode(conteudo_arquivo).encode('utf-8')) arquivo_xml.close() return HttpResponse("Instalado") app = load_app("modulos.noticia") admin_teste = import_module("modulos.noticia.admin") connection = connections["default"] cursor = connection.cursor() style = color.no_style() sql_tabelas = sql.sql_create(app, style, connection) # for sql_query in sql_tabelas: # cursor.execute(sql_query) varaveis = import_module(get_urlconf()) reload(import_module(settings.ROOT_URLCONF)) clear_url_caches() #varaveis.urlpatterns = patterns('', #(r'^admin/configuracao/', include('modulos.configuracao.admin_urls')), #(r'^admin/index_enquete/', include('modulos.index_enquete.admin_urls')), #(r'^admin/', include(admin.site.urls)), #(r'^disco_virtual/', include('modulos.disco_virtual.urls')), #(r'^onde-encontrar/', include('modulos.onde_encontrar.urls')), #(r'^enquete/', include('modulos.index_enquete.urls')), #(r'^eventos/', include('modulos.index_evento.urls')), #(r'^galerias/', include('modulos.index_gallery.urls')), #(r'^grappelli/', include('grappelli.urls')), #(r'^newsletter/', include('modulos.newsletter.urls')), #(r'^popup/', include('modulos.popup.urls')), #(r'^noticia/', include('modulos.noticia.urls')), #(r'^utils/', include('modulos.utils.urls')), #(r'^site_media/(.*)', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}), #) return HttpResponse("Instalado")
def test_sql_create(self): app = models.get_app("commands_sql") output = sql_create(app, no_style(), connections[DEFAULT_DB_ALIAS]) # Lower so that Oracle's upper case tbl names wont break sql = output[0].lower() six.assertRegex(self, sql, r"^create table .commands_sql_book.*")
def handle_app(self, app, **options): return u'\n'.join(sql_create(app, self.style, connections[options.get('database')])).encode('utf-8')
def test_sql_create(self): app = models.get_app('commands_sql') output = sql_create(app, no_style(), connections[DEFAULT_DB_ALIAS]) # Lower so that Oracle's upper case tbl names wont break sql = output[0].lower() six.assertRegex(self, sql, r'^create table .commands_sql_book.*')
def modeltest(app_name, use_aka=True): if not app_name: raise Exception("No test name given") if not os.path.exists('settings.py'): raise Exception('Oops... file settings.py does not exist! Please copy your settings there!') from django.conf import settings from django.db.models.loading import get_apps, get_app from deseb.schema_evolution import evolvediff from django.core.management.color import no_style from deseb.actions import get_introspected_evolution_options from django.core.management.sql import sql_create, sql_indexes from django.db.transaction import commit_on_success from django.db import connection from deseb.actions import get_schemas, show_evolution_plan if DEBUG: print "Test %s" % app_name #reset on post state and pre state from deseb import add_aka_support if use_aka: add_aka_support() style = no_style() settings.INSTALLED_APPS = tuple(list(settings.INSTALLED_APPS[:5]) + [app_name]) write_file(app_name+"/models.py", '') # re-init models.py write_file(app_name+"/errdiff.%s.actual" % settings.DATABASE_ENGINE, "") write_file(app_name+"/errors.%s.actual" % settings.DATABASE_ENGINE, "") get_apps() drop_all_tables() reload_models(app_name, 'pre') app = get_app(app_name) create = sql_create(app, style) + sql_indexes(app, style) write_file(app_name+"/init.%s.actual" % settings.DATABASE_ENGINE, create) #FIXME: compare to init.correct later instead of copying write_file(app_name+"/init.%s.planned" % settings.DATABASE_ENGINE, create) reset = sql_create(app, style) #print 'SQL:', '\n'.join(reset) commit_on_success(run_sql)(reset) reset_idx = sql_indexes(app, style) run_sql(reset_idx) reload_models(app_name, 'post') if use_aka: from deseb.storage import update_with_aka, save_renames update_with_aka(app_name) save_renames(app_name) cursor = connection.cursor() db_schema, model_schema = get_schemas(cursor, app, style) diff = show_evolution_plan(cursor, app, style, db_schema, model_schema) write_file(app_name+"/diff.%s.actual" % settings.DATABASE_ENGINE, diff) #FIXME: compare to diff.correct later instead of copying write_file(app_name+"/diff.%s.planned" % settings.DATABASE_ENGINE, diff) actions = get_introspected_evolution_options(app, style, db_schema, model_schema) write_file(app_name+"/actions.%s.actual" % settings.DATABASE_ENGINE, actions) #FIXME: compare to diff.correct later instead of copying write_file(app_name+"/actions.%s.planned" % settings.DATABASE_ENGINE, actions) try: commit_on_success(run_sql)(actions) except: #print 'changes rolled back' from django.db import transaction transaction.rollback() raise #else: #print 'changes committed' cursor = connection.cursor() db_schema, model_schema = get_schemas(cursor, app, style, model_schema=model_schema) # due to sqlite3/pysqlite bug, caused deferred index creation, we reget db schema. # this is f*****g weird, but i've no any explanation, why getting indxes # doesn't work correctly first time db_schema, model_schema = get_schemas(cursor, app, style, model_schema=model_schema) diff = show_evolution_plan(cursor, app, style, db_schema, model_schema) write_file(app_name+"/errdiff.%s.actual" % settings.DATABASE_ENGINE, diff) diff1 = diff.split('\n',1)[1] if diff1: print "Errors:" print diff1 try: actions, db_schema, model_schema = get_introspected_evolution_options(app, style, db_schema, model_schema) except Exception: actions = ['Was unable to generate error diff SQL commands'] write_file(app_name+"/errors.%s.actual" % settings.DATABASE_ENGINE, actions) #FIXME: compare to diff.correct later instead of copying #write_file(app_name+"/errors.%s.planned" % settings.DATABASE_ENGINE, actions) return diff1
def handle_app(self, app, **options): from django.core.management.sql import sql_create return '\n'.join(sql_create(app, self.style))
def test_sql_create(self): app_config = apps.get_app_config('commands_sql_migrations') with self.assertRaises(CommandError): sql_create(app_config, no_style(), connections[DEFAULT_DB_ALIAS])
def handle_app_config(self, app_config, **options): if app_config.models_module is None: return connection = connections[options.get('database')] statements = sql_create(app_config, self.style, connection) return '\n'.join(statements)
def handle_app_config(self, app_config, **options): if app_config.models_module is None: return connection = connections[options['database']] statements = sql_create(app_config, self.style, connection) return '\n'.join(statements)
def handle_app(self, app, **options): from django.core.management.sql import sql_create return '\n'.join(sql_create(app, self.style)).encode('utf-8')
def test_sql_create(self): app = models.get_app('commands_sql') output = sql_create(app, no_style(), connections[DEFAULT_DB_ALIAS]) six.assertRegex(self, output[0], r'^CREATE TABLE .commands_sql_book.*')