Skip to content

Instantly share code, notes, and snippets.

@utek
Last active October 13, 2023 16:07
Show Gist options
  • Star 58 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save utek/6163250 to your computer and use it in GitHub Desktop.
Save utek/6163250 to your computer and use it in GitHub Desktop.
Define ignored tables in alembic.ini

Add this in your ini file:

[alembic:exclude]
tables = spatial_ref_sys

In env.py:

    import re 
    
    def exclude_tables_from_config(config_):
        tables_ = config_.get("tables", None)
        if tables_ is not None:
            tables = tables_.split(",")
        return tables
        
    # Changes from: https://gist.github.com/utek/6163250#gistcomment-3851168
    exclude_tables = re.sub(r"\s+", '',  # replace whitespace
                        config.get_main_option('exclude', '')).split(',')


    def include_object(object, name, type_, *args, **kwargs):
        return not (type_ == 'table' and name in exclude_tables)
     
    def run_migrations_offline():
        """Run migrations in 'offline' mode.
    
        This configures the context with just a URL
        and not an Engine, though an Engine is acceptable
        here as well.  By skipping the Engine creation
        we don't even need a DBAPI to be available.
    
        Calls to context.execute() here emit the given string to the
        script output.
    
        """
        # url = config.get_main_option("sqlalchemy.url")
        context.configure(url=engine.url, include_object=include_object)
    
        with context.begin_transaction():
            context.run_migrations()
    
    
    def run_migrations_online():
        """Run migrations in 'online' mode.
    
        In this scenario we need to create an Engine
        and associate a connection with the context.
    
        """
        if isinstance(engine, Engine):
            connection = engine.connect()
        else:
            raise Exception('Expected engine instance got %s instead' % type(engine))
    
        context.configure(
            connection=connection,
            target_metadata=target_metadata,
            include_object=include_object
        )
    
        try:
            with context.begin_transaction():
                context.run_migrations()
        finally:
            connection.close()
@hergerr
Copy link

hergerr commented Nov 18, 2022

@heathhenley Works like a charm, thanks!

@A-Asror
Copy link

A-Asror commented May 25, 2023

thanks, everything worked 👍👍

@danielshtel
Copy link

In addition to @heathhenley comment, you need to configure context in do_run_migrations, which will be executed in run_migrations online.

def do_run_migrations(connection: Connection) -> None:
    context.configure(
        connection=connection,
        target_metadata=target_metadata,
        include_name=include_name,
        include_schemas=False
    )

    with context.begin_transaction():
        context.run_migrations()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment