1
1
mirror of https://github.com/yandex/pgmigrate.git synced 2024-11-09 16:36:05 +03:00

Support 'latest' target version

This commit is contained in:
secwall 2017-04-01 13:05:14 +03:00
parent 4c555fab1b
commit 453398c988
3 changed files with 29 additions and 4 deletions

View File

@ -308,9 +308,9 @@ postgres=# CREATE DATABASE foodb;
CREATE DATABASE
```
Now migrate strait to version 3
Now migrate to latest available version
```
admin@localhost foodb $ pgmigrate -t 3 migrate
admin@localhost foodb $ pgmigrate -t latest migrate
```
Operations log will look like this:

View File

@ -42,6 +42,24 @@ Feature: Getting migrations from dir
And database contains schema_version
And migration info contains single migration
Scenario: 'latest' target migrates to latest version
Given migration dir
And migrations
| file | code |
| V1__Single_migration.sql | INSERT INTO mycooltable (op) values ('Migration 1'); |
| V2__Another_migration.sql | INSERT INTO mycooltable (op) values ('Migration 2'); |
And callbacks
| type | file | code |
| beforeAll | before_all.sql | CREATE TABLE mycooltable (seq SERIAL PRIMARY KEY, op TEXT); |
And database and connection
When we run pgmigrate with our callbacks and "-t latest migrate"
Then pgmigrate command "succeeded"
And database contains schema_version
And query "SELECT * from mycooltable order by seq;" equals
| seq | op |
| 1 | Migration 1 |
| 2 | Migration 2 |
Scenario: Callbacks are executed in correct order
Given migration dir
And migrations

View File

@ -534,7 +534,8 @@ def migrate(config):
Migrate cmdline wrapper
'''
if config.target is None:
LOG.error('Unknown target')
LOG.error('Unknown target (you could use "latest" to '
'migrate to latest available version)')
raise MigrateError('Unknown target')
state = _get_state(config.base_dir, config.baseline,
config.target, config.cursor)
@ -616,6 +617,12 @@ def get_config(base_dir, args=None):
if i in args.__dict__ and args.__dict__[i] is not None:
conf = conf._replace(**{i: args.__dict__[i]})
if conf.target is not None:
if conf.target == 'latest':
conf = conf._replace(target=float('inf'))
else:
conf = conf._replace(target=int(conf.target))
conf = conf._replace(conn_instance=_create_connection(conf.conn))
conf = conf._replace(cursor=conf.conn_instance.cursor())
conf = conf._replace(callbacks=_get_callbacks(conf.callbacks,
@ -635,7 +642,7 @@ def _main():
type=str,
help='Operation')
parser.add_argument('-t', '--target',
type=int,
type=str,
help='Target version')
parser.add_argument('-c', '--conn',
type=str,