docs-invenio-rdm icon indicating copy to clipboard operation
docs-invenio-rdm copied to clipboard

Update database alembic migration recipe generation

Open slint opened this issue 8 months ago • 1 comments

There's a simpler and probably more correct way to generate Alembic migration compared to the existing docs from https://inveniordm.docs.cern.ch/develop/howtos/alembic/#failing-alembic-commands

The gist of it is:

# In the module you're creating the recipe
python -m venv .venv
source .venv/bin/activate

# Install the package in editable mode with all dependencies (including tests)
pip install -e .[tests,opensearch2]

# Create the alembic directory in the module
mkdir -p invenio_foobar/alembic
cd invenio_foobar/alembic

# Create the initial recipe
invenio alembic revision --path `pwd` --empty --branch invenio_foobar "Create foobar branch"

# Start a test container for the DB
eval "$(docker-services-cli up --db ${DB:-postgresql} --env)"

# Export the DB URI so that `invenio` commands are aware
export INVENIO_SQLALCHEMY_DATABASE_URI=${SQLALCHEMY_DATABASE_URI}

# Create the database
invenio db create

# Manually delete your module's tables
psql ${SQLALCHEMY_DATABASE_URI} -c "DROP TABLE foobar_table CASCADE;"

# Creat the alembic table on latest known heads
invenio alembic stamp

# Create the migration
invenio alembic revision -p "<parent recipe ID>" -d "<dependent recipe ID>" "Create base tables."

slint avatar Mar 21 '25 09:03 slint

Should also mention to register the new entrypoint:

invenio_db.alembic =
    invenio_foobar = invenio_foobar:alembic

sakshamarora1 avatar Mar 27 '25 12:03 sakshamarora1