Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data migrations v2 #1956

Merged
merged 16 commits into from Jan 9, 2019
Merged

Data migrations v2 #1956

merged 16 commits into from Jan 9, 2019

Conversation

noirbizarre
Copy link
Contributor

@noirbizarre noirbizarre commented Nov 16, 2018

This PR introduce a new migration engine and drops legacy JS migrations.
Migrations are now python based allowing to reuse some udata helpers.

Migration format

A migration file is structured like this:

'''
My migration docstring
'''

def migrate(db):
    db.test.insert_one({'key': 'value'})

Logging

If you want to output some logging, just use the standard python logging facilities

'''
My migration docstring
'''
import logging

log = logging.getLogger(__name__)

def migrate(db):
    log.info('Log some output')

Rollback/revert handling

A migration can optionnaly be reverted. You need to add the optionnal rollback() method:

'''
My migration docstring
'''
import logging

log = logging.getLogger(__name__)

def migrate(db):
    db.test.insert_one({'key': 'value'})
    log.info('Log some output')

def rollback(db):
    db.test.delete_one({'key': 'value'})

Commands

udata db migrate

udata db migrate

udata db status

udata db status

udata db info <migration>

udata db info

udata db unrecord <migration>

udata db unrecord

Miscellaneous

As a positive side-effect, migrations are not depending anymore on raw Mongo db.eval which:

  • makes it more secure
  • makes it compatible with Mongo 4+
  • allows to run on sandboxed Mongo instances (like Heroku)

Fixes #981

@abulte
Copy link
Contributor

abulte commented Nov 16, 2018

We can use udata models in there, right?

@noirbizarre noirbizarre changed the title [WIP] Data migrations v2 Data migrations v2 Jan 7, 2019
Copy link
Contributor

@abulte abulte left a comment

Choose a reason for hiding this comment

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

A few typos.

CHANGELOG.md Outdated

### Breaking changes

- The new migration system ([#1956](https://github.com/opendatateam/udata/pull/1956)) use a new python based format. Pre-2.0 migrations are not compatible so you might need to upgrade to the latest `udata` version `<2.0.0`, execute migrations and then upgrade to `udata` 2+.
Copy link
Contributor

Choose a reason for hiding this comment

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

s/use/uses/

@click.argument('filename', default=None, required=False, metavar='[FILENAME]')
def info(plugin_or_specs, filename):
'''
Display detailled info about a migration
Copy link
Contributor

Choose a reason for hiding this comment

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

s/detailled/detailed/

'''
Status is the status of the last operation.

Will be `None` is the record doesn't exists.
Copy link
Contributor

Choose a reason for hiding this comment

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

s/is/if/

Copy link
Contributor

Choose a reason for hiding this comment

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

s/exists/exist/

self.module.rollback(db)
out = _extract_output(q)
self.add_record('rollback', out, db._state, True)
msg = 'Error while executing migration, rollback has been applyied'
Copy link
Contributor

Choose a reason for hiding this comment

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

s/applyied/applied/

return out

def unrecord(self):
'''Only record a migration without applying it'''
Copy link
Contributor

Choose a reason for hiding this comment

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

Wrong docstring?

return Migration(plugin, filename)


def list_availables():
Copy link
Contributor

Choose a reason for hiding this comment

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

s/list_availables/list_available/

'''
List available migrations for udata and enabled plugins

Each row is tuple with following signature:
Copy link
Contributor

Choose a reason for hiding this comment

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

s/tuple/a tuple/


def _iter(plugin, module):
'''
Iter over migrations for a given plugin module
Copy link
Contributor

Choose a reason for hiding this comment

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

s/Iter/Iterate/



def _module_name(plugin):
'''Get the module namefor a given plugin'''
Copy link
Contributor

Choose a reason for hiding this comment

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

s/namefor/name for/

@noirbizarre noirbizarre merged commit 7e15c97 into opendatateam:py3 Jan 9, 2019
@noirbizarre noirbizarre deleted the data-migrations-v2 branch January 9, 2019 21:07
abulte added a commit that referenced this pull request Mar 11, 2020
### Breaking changes

- Migration to Python 3.7 [#1766](#1766)
- The new migration system ([#1956](#1956)) uses a new python based format. Pre-2.0 migrations are not compatible so you might need to upgrade to the latest `udata` version `<2.0.0`, execute migrations and then upgrade to `udata` 2+.
- The targeted mongo version is now Mongo 3.6. Backward support is not guaranteed

### New features

- New migration system [#1956](#1956):
  - Use python based migrations instead of relying on mongo internal and deprecated `js_exec`
  - Handle rollback (optionnal)
  - Detailled history
- Template hooks generalization: allows to dynamically extend template with widgets and snippets from extensions. See [the dedicated documentation section](https://udata.readthedocs.io/en/stable/extending/#hooks) [#2323](#2323)
- Markdown now supports [Github Flavored Markdown (GFM) specs](https://github.github.com/gfm/) (ie. the already supported [CommonMark specs](https://spec.commonmark.org) plus tables, strikethrough, autolinks support and predefined disallowed raw HTML) [#2341](#2341)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants