Esempio n. 1
0
def run_inspectdb(table_name=None):
    """
    Run inspectdb against our secondary in-memory database. Basically,
    we're running the equivalent of this manage.py command:

        ./manage.py inspectdb --database schemabuilding

    Returns the generated models.py
    """
    cmd = inspectdb.Command()
    options = {
        'verbosity': 1,
        'settings': None,
        'pythonpath': None,
        'traceback': False,
        'no_color': False,
        'force_color': False,
        'table': [table_name] or [],
        'database': get_setting("CSV_MODELS_TEMP_DB"),
        'include_partitions': False,
        'include_views': False
    }
    # This command returns a generator of models.py text lines
    gen = cmd.handle_inspection(options)
    return "\n".join(list(gen))
Esempio n. 2
0
    def propertymap(data):
        output={}
        used=[]
        c=inspectdb.Command()
        for k in data:
            att_name, params, notes=inspectdb.Command.normalize_col_name(c, k, used, False)
#             logger.debug('Field %s, %s', att_name, notes)
            used.append(att_name)    
            output[k]=att_name
#         logger.debug('Mappings are %s', output)
        return output
Esempio n. 3
0
def create_static_model():
    current_dir = os.path.dirname(os.path.abspath(__file__))
    new_command = inspectdb.Command()
    retr = new_command.handle_inspection(
        {
            'table': None,
            'database': 'eve_static_data'
        }
    )
    static_model_path = os.path.join(os.path.dirname(current_dir), 'static_data_export', 'static_model.py')
    with open(static_model_path, 'w+') as static_model_file:
        static_model_file.write("\n".join(list(retr)))
Esempio n. 4
0
    def models(self):
        app_name = self.app_name
        table_name = self.table_name
        def strip_prefix(s):
            return s[1:] if s.startswith("u'") else s

        def table2model(table_name):
            return re.sub(r'[^a-zA-Z0-9]', '', table_name.title())
        cursor = connection.cursor()
        try:
            relations = connection.introspection.get_relations(cursor, table_name)
        except NotImplementedError:
            relations = {}
        try:
            constraints = connection.introspection.get_constraints(
                cursor, table_name)
        except NotImplementedError:
            constraints = {}
        primary_key_column = connection.introspection.get_primary_key_column(
            cursor, table_name)
        unique_columns = [
            c['columns'][0] for c in constraints.values()
            if c['unique'] and len(c['columns']) == 1
        ]
        table_description = connection.introspection.get_table_description(
            cursor, table_name)


        used_column_names = []
        column_to_field_name = {}
        class_name = table2model(table_name)
        field_list = []
        for row in table_description:
            # Holds Field notes, to be displayed in a Python comment.
            comment_notes = []
            # Holds Field parameters such as 'db_column'.
            extra_params = OrderedDict()
            column_name = row[0]
            is_relation = column_name in relations
            att_name, params, notes = inspectdb.Command().normalize_col_name(
                column_name, used_column_names, is_relation)
            extra_params.update(params)
            comment_notes.extend(notes)
            used_column_names.append(att_name)
            column_to_field_name[column_name] = att_name
            if column_name == primary_key_column:
                extra_params['primary_key'] = True
            elif column_name in unique_columns:
                extra_params['unique'] = True
            field_type, field_params, field_notes = inspectdb.Command(
            ).get_field_type(connection, table_name, row)
            extra_params.update(field_params)
            comment_notes.extend(field_notes)

            field_type += '('
            if att_name == 'id' and extra_params == {'primary_key': True}:
                if field_type == 'AutoField(':
                    continue
                elif field_type == 'IntegerField(' and not connection.features.can_introspect_autofield:
                    comment_notes.append('AutoField?')
            if row[6]:  # If it's NULL...
                if field_type == 'BooleanField(':
                    field_type = 'NullBooleanField('
                else:
                    extra_params['blank'] = True
                    extra_params['null'] = True

            field_desc = '%s = %s%s' % (
                att_name,
                # Custom fields will have a dotted path
                '' if '.' in field_type else 'models.',
                field_type,
            )
            if field_type.startswith('ForeignKey('):
                field_desc += ', models.DO_NOTHING'

            if extra_params:
                if not field_desc.endswith('('):
                    field_desc += ', '
                field_desc += ', '.join(
                    '%s=%s' % (k, strip_prefix(repr(v)))
                    for k, v in extra_params.items())
            field_desc += ')'
            if comment_notes:
                field_desc += '  # ' + ' '.join(comment_notes)
            result = '    %s' % field_desc
            field_list.append(result + '\n')
        class_header = 'class ' + class_name + '(models.Model):\n'
        class_meta = "    class Meta:\n        db_table='{}'".format(table_name)
        executable_statement = class_header + "".join(field_list) + class_meta
        exec(executable_statement)
        return eval(class_name)