0

I have built two flask app one for business (APP 1) and another one for admin (APP 2). Both APP 1 and APP 2 share same database and tables except APP 2 is using few subscriptions related tables which are not present in APP 1. Now the problem is whenever i execute SQL Alchemy migrate command in APP 1 it generate drop table syntax for subscriptions tables because those table models are not present in APP 1.

What is the best way to deal with two flask app share same database by using SQL Alchemy migrate?

Divagar
  • 1
  • 1

1 Answers1

0

You can define the tables to be excluded in app1 in you app1's alembic.ini file, example is below:

[alembic:exclude]
tables = table1,table2

Then in you env.py file, you can in include_object function.

include_object is a callable function which is given the chance to return True or False for any object, indicating if the given object should be considered in the autogenerate sweep.

https://alembic.sqlalchemy.org/en/latest/api/runtime.html?highlight=include_object#alembic.runtime.environment.EnvironmentContext.configure.params.include_object

Code in env.py file

exclude_tables = config.get_section('alembic:exclude').get('tables', '').split(',')


def include_object(object, name, type_, reflected, compare_to):
    if type_ == "table" and name in exclude_tables:
        return False
    else:
        return True

exclude_tables will contain the list of all the tables, that you have defined in your alembic.ini file

Then include this include_object function in your context.configure function as below:

context.configure(
    # ...
    include_object = include_object
)