Пример #1
0
    def _setup_objects(self, pse: PandasSpecialEngine):
        """
        Handles optional pre-upsert operations:
        1. creating the PostgreSQL schema
        2. creating the table
        3. adding missing columns
        4. altering columns data types (if needed) when these columns are
           empty in the db but not in the df
        """
        # create schema if not exists
        # IMPORTANT! `pse.schema` and not `schema`
        # -> With postgres None will be set to `public`
        if self.create_schema and pse.schema is not None:
            pse.create_schema_if_not_exists()

        # create table if not exists
        if self.create_table:
            pse.create_table_if_not_exists()

        # change dtype of empty columns in db
        if self.adapt_dtype_of_empty_db_columns and pse.table_exists():
            pse.adapt_dtype_of_empty_db_columns()

        # add new columns from frame
        if self.add_new_columns and pse.table_exists():
            pse.add_new_columns()
def test_schema_creation(engine, schema):
    # overwrite schema
    schema = schema_for_testing_creation
    table_name = TableNames.NO_TABLE

    # schema may already exist before testing
    if 'postgres' in engine.dialect.dialect_description:
        drop_schema(engine=engine, schema=schema)

    # then try to create a schema from a PandasSpecialEngine instance
    dummy_df = pd.DataFrame(index=pd.Index(data=[0], name='id'))
    with engine.connect() as connection:
        try:
            pse = PandasSpecialEngine(connection=connection,
                                      schema=schema,
                                      table_name=TableNames.NO_TABLE,
                                      df=dummy_df)
            # this should raise HasNoSchemaSystemException
            # if we are not on a postgres engine
            assert not pse.schema_exists()
            pse.create_schema_if_not_exists()
            commit(connection)
            assert pse.schema_exists()
        except Exception as e:
            if pse._db_type == 'postgres' or not isinstance(
                    e, HasNoSchemaSystemException):
                raise e
        finally:
            if pse._db_type == 'postgres':
                drop_schema(engine=engine, schema=schema)