Ejemplo n.º 1
0
    def test_sql_table_exists(self):
        cursor = MagicMock()
        cursor.fetchone.return_value = [1]
        got = sql.sql_table_exists(cursor, 'table')

        self.assertEqual(len(cursor.execute.call_args[0]), 1)
        self.assertIn('table', cursor.execute.call_args[0][0])
        self.assertEqual(got, 1)
Ejemplo n.º 2
0
    def test_sql_table_exists(self):
        cursor = MagicMock()
        cursor.fetchone.return_value = [1]
        got = sql.sql_table_exists(cursor, 'table')

        self.assertEqual(len(cursor.execute.call_args[0]), 1)
        self.assertIn('table', cursor.execute.call_args[0][0])
        self.assertEqual(got, 1)
Ejemplo n.º 3
0
    def _init_schema(self):
        self.prepare_mappings()

        for database in self.mappings:
            for collection in self.mappings[database]:
                collection_mapping = self.mappings[database][collection]

                self.insert_accumulator[collection] = 0

                with self.pgsql.cursor() as cursor:
                    pk_found = False
                    pk_name = collection_mapping['pk']
                    columns = ['_creationdate TIMESTAMP']
                    indices = [u"INDEX idx_{0}__creation_date ON {0} (_creationdate DESC)".format(collection)] + \
                              collection_mapping.get('indices', [])

                    for column in collection_mapping:
                        column_mapping = collection_mapping[column]

                        if 'dest' in column_mapping:
                            name = column_mapping['dest']
                            column_type = column_mapping['type']

                            constraints = ''
                            if name == pk_name:
                                constraints = "CONSTRAINT {0}_PK PRIMARY KEY".format(
                                    collection.upper())
                                pk_found = True

                            if column_type != ARRAY_TYPE and column_type != ARRAY_OF_SCALARS_TYPE:
                                columns.append(
                                    name + ' ' +
                                    pg_type_for_mapping_type(column_type) +
                                    ' ' + constraints)

                            if 'index' in column_mapping:
                                indices.append(
                                    u"INDEX idx_{2}_{0} ON {1} ({0})".format(
                                        name, collection,
                                        collection.replace('.', '_')))

                    if not pk_found:
                        columns.append(pk_name + ' SERIAL CONSTRAINT ' +
                                       collection.upper() + '_PK PRIMARY KEY')

                    if sql_table_exists(cursor, collection):
                        sql_drop_table(cursor, collection)

                    sql_create_table(cursor, collection, columns)

                    for index in indices:
                        cursor.execute("CREATE " + index)

                    self.commit()
    def _init_schema(self):
        self.prepare_mappings()

        for database in self.mappings:
            for collection in self.mappings[database]:
                collection_mapping = self.mappings[database][collection]

                self.insert_accumulator[collection] = 0

                with self.pgsql.cursor() as cursor:
                    pk_found = False
                    pk_name = collection_mapping['pk']
                    columns = ['_creationdate TIMESTAMP']
                    indices = [u"INDEX idx_{0}__creation_date ON {0} (_creationdate DESC)".format(collection)] + \
                              collection_mapping.get('indices', [])

                    for column in collection_mapping:
                        column_mapping = collection_mapping[column]

                        if 'dest' in column_mapping:
                            name = column_mapping['dest']
                            column_type = column_mapping['type']

                            constraints = ''
                            if name == pk_name:
                                constraints = "CONSTRAINT {0}_PK PRIMARY KEY".format(collection.upper())
                                pk_found = True

                            if column_type != ARRAY_TYPE and column_type != ARRAY_OF_SCALARS_TYPE:
                                columns.append(name + ' ' + pg_type_for_mapping_type(column_type) + ' ' + constraints)

                            if 'index' in column_mapping:
                                indices.append(u"INDEX idx_{2}_{0} ON {1} ({0})".format(name, collection, collection.replace('.', '_')))

                    if not pk_found:
                        columns.append(pk_name + ' SERIAL CONSTRAINT ' + collection.upper() + '_PK PRIMARY KEY')

                    if sql_table_exists(cursor, collection):
                        sql_drop_table(cursor, collection)

                    sql_create_table(cursor, collection, columns)

                    for index in indices:
                        cursor.execute("CREATE " + index)

                    self.commit()
    def _init_schema(self):
        self.prepare_mappings()

        try:
            for database in self.mappings:
                foreign_keys = []

                with self.pgsql.cursor() as cursor:
                    for collection in self.mappings[database]:
                        self.insert_accumulator[collection] = 0

                        pk_found = False
                        pk_name = self.mappings[database][collection]['pk']
                        columns = ['_creationdate TIMESTAMP']
                        indices = [u"INDEX idx_{0}__creation_date ON {0} (_creationdate DESC)".format(collection)] + \
                                  self.mappings[database][collection].get('indices', [])

                        for column in self.mappings[database][collection]:
                            column_mapping = self.mappings[database][
                                collection][column]

                            if 'dest' in column_mapping:
                                name = column_mapping['dest']
                                column_type = column_mapping['type']
                                nullable = column_mapping.get('nullable', True)

                                constraints = ''
                                if name == pk_name:
                                    constraints = "CONSTRAINT {0}_PK PRIMARY KEY".format(
                                        collection.upper())
                                    pk_found = True

                                if not nullable:
                                    constraints = '{} NOT NULL'.format(
                                        constraints)

                                if column_type != ARRAY_TYPE and column_type != ARRAY_OF_SCALARS_TYPE:
                                    columns.append(name + ' ' + column_type +
                                                   ' ' + constraints)

                                if 'index' in column_mapping:
                                    indices.append(
                                        u"INDEX idx_{2}_{0} ON {1} ({0})".
                                        format(name, collection,
                                               collection.replace('.', '_')))

                            if 'fk' in column_mapping:
                                foreign_keys.append({
                                    'table':
                                    column_mapping['dest'],
                                    'ref':
                                    collection,
                                    'fk':
                                    column_mapping['fk'],
                                    'pk':
                                    pk_name
                                })

                        if not pk_found:
                            columns.append(pk_name + ' SERIAL CONSTRAINT ' +
                                           collection.upper() +
                                           '_PK PRIMARY KEY')

                        if sql_table_exists(cursor, collection):
                            sql_drop_table(cursor, collection)

                        sql_create_table(cursor, collection, columns)

                        for index in indices:
                            cursor.execute("CREATE " + index)

                    sql_add_foreign_keys(cursor, foreign_keys)
                    self.commit()

        except psycopg2.Error:
            LOG.error(u"A fatal error occured during tables creation")

            if not self.quiet:
                LOG.error(u"Traceback:\n%s", traceback.format_exc())