Thanks to visit codestin.com
Credit goes to github.com

Skip to content

sqlalchemy-migrate -> Alembic#4450

Merged
wardi merged 8 commits into
ckan:masterfrom
smotornyuk:alembic
May 11, 2019
Merged

sqlalchemy-migrate -> Alembic#4450
wardi merged 8 commits into
ckan:masterfrom
smotornyuk:alembic

Conversation

@smotornyuk
Copy link
Copy Markdown
Member

@smotornyuk smotornyuk commented Sep 10, 2018

Use alembic instead of sqlalchemy-migrate. Main reason - I just like it. Second reason - it's more flexible and supports branching in an easy way. Third reason - sqlalchemy-migrate has some problems with python>=3.6

I rewrote all migrations in alembic so that we can safely drop sqlalchemy-migrate before switching to Py3. Theoretically, there will be no problem for new installations, where alembic is used to create the database from scratch. For existing portals, I'm going to use Alembic for all migrations that were added in CKAN>=2.5(starting from 82). That's mean, that portals that use CKAN<2.5 must use a three-step upgrade workflow:

  1. upgrade codebase to some version >=2.5 and <2.9
  2. Upgrade database
  3. Then upgrade both codebase and database to the desired version as per docs

As for CLI, I'm going to leave all commands as is and update commands internally, so there is no need in updating a lot of documentation or rewriting any of the existing installation scripts.

Are there any objections or suggestions?

PS Just for fun I'm also writing downgrade scripts, even thou I'm not sure whether they have any value

@smotornyuk smotornyuk added the WIP label Sep 10, 2018
@smotornyuk smotornyuk changed the title write migrations sqlalchemy-migrate -> Alembic Sep 10, 2018
@smotornyuk smotornyuk removed the WIP label Sep 20, 2018
@wardi wardi self-assigned this Sep 20, 2018
Copy link
Copy Markdown
Member

@amercader amercader left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@smotornyuk I had a very quick first look.

How will this work for existing installations that set up their database with sqlalchemy-migrate? Will it upgrade to the new version automatically with alembic or is there a step needed?

Comment thread ckan/migration/alembic.ini Outdated
# are written from script.py.mako
# output_encoding = utf-8

sqlalchemy.url = postgresql://ckan_default:root@localhost/ckan_alembic
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this DB actually used? is it a placeholder?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, this is default configuration file benerated durinig alembic initialization, that can be used for development purposes and debuging(for example, you can run all alembic commands directly from folder, containing alembic.ini), but CKAN commands(paster, etc.) are initialized with database url from main configuration file. Probably, i should set user:password and dbname to some base values, like ones from initial configuration file. Or this line may be removed at all...

# encoding: utf-8


def skip_based_on_legacy_engine_version(op, filename):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain what this function does or what is needed for?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a docsting would be nice e.g. "graceful upgrade from old migration numbers to alembic ids"

@wardi
Copy link
Copy Markdown
Contributor

wardi commented Sep 20, 2018

This looks good to me. The handling and deletion of the sqlalchemy migrate version is nice.



def downgrade():
pass
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a comment about creating migrate_version not being necessary once we've switched to alembic ids

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, good idea

@smotornyuk
Copy link
Copy Markdown
Member Author

smotornyuk commented Sep 20, 2018

@amercader this solution should be compatible with existing instances starting from ckan-2.5. I'm checking in every migration existing of sqlalcemy-migrate's version table via skip_based_on_legacy_engine_version - i'll add docstring to it, but it basically compares prefix of migration file(001, 032, etc) with current version pulled from migrate_version table(if latter exists). If there is no such table, all alembic migrations will be applied, otherwise - as long as alembic migration is before the current version of sqlalchemy-migration, it applied without actual execution of migration script(this done via check in first two lines of upgrade functions). Theoretically it may work for any version of existing portals, but i tested it only for range between 2.5 and master.

In short words, one have no need in any additional step(except for installing requirements.txt) for CKAN>=2.5. Any portal with CKAN<2.5 need to migrate first to some version in range 2.5 <= CKAN <=2.8

@kmbn kmbn added the Awaiting tech team feedback This PR or issue needs feedback from the tech team. label Jan 24, 2019
@davidread
Copy link
Copy Markdown

Another reason for moving to Alembic is because sqlalchemy-migrate is 'not maintained': https://groups.google.com/forum/#!topic/sqlalchemy/eF1DE20FMCQ

@wardi
Copy link
Copy Markdown
Contributor

wardi commented May 2, 2019

I think this just needs conflicts resolved

@wardi wardi removed the Awaiting tech team feedback This PR or issue needs feedback from the tech team. label May 2, 2019
@smotornyuk
Copy link
Copy Markdown
Member Author

@wardi , i've updated PR. There are two new migrations that i've converted to alembic migrations and small changes in CLI commands for database management

Comment thread ckan/cli/db.py Outdated
Co-Authored-By: David Read <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants