Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

python - How to upgrade an existing database with sqlalchemy using dynamically loadable modules

I have the Python written code that creates database from table classes loaded dynamically.

This is a piece of code that should give you an idea of what I'm talking about.

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import importlib.util

ModelBase = declarative_base()

def table_list(module=None):
    """
    Scans modules for table classes.
    Returns list of table classes. (ModelBase inherited classes)
    """
    mbname = ".".join([ModelBase.__module__, ModelBase.__qualname__])
    tables = []

    if module is not None:
        for class_name, class_object in inspect.getmembers(module, inspect.isclass):
            for base in class_object.__bases__:
                base_name = ".".join([base.__module__, base.__qualname__])
                if base_name == mbname:
                    log.debug("Found ModelBase inherited class %s.%s.", class_object.__module__, class_object.__qualname__)
                    tables.append(class_object)
    else:
        for module_name in common.modules.keys():
            tables.extend(table_list(common.modules[module_name]))

    return tables

# Load the module.
spec = importlib.util.spec_from_file_location(module_name, filename)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)

# Create database (SQLAlchemy)
engine = create_engine(DATABASE_CONNECTION_URI, encoding='utf-8')
ModelBase.metadata.create_all(engine, tables=[t.__table__ for t in table_list()])
session = sessionmaker(bind=engine)()
session.commit()

In other words, I load modules on the fly, scan them for classes describing the database tables. And from them I build a database. As you can see, here I am simply passing a list of classes to the function 'create_all()'.

It is works perfect when database is created new.

But I have not yet figured out how to update such a database without losing data when its structure changes.

Tell me what I need to study to understand how this can be implemented. Maybe there are already tools that can help me?

question from:https://stackoverflow.com/questions/65885683/how-to-upgrade-an-existing-database-with-sqlalchemy-using-dynamically-loadable-m

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...