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

Add an option to show only unapplied migrations in info #40

Merged
merged 1 commit into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions features/info.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,26 @@ Feature: Info
And successful pgmigrate run with "-t 1 migrate"
When we run pgmigrate with "info"
Then migrate command passed with Single migration

Scenario: Info filters out applied migrations
Given migration dir
And migrations
| file | code |
| V1__Applied_migration.sql | SELECT 1; |
| V2__Unapplied_migration.sql | SELECT 1; |
And database and connection
And successful pgmigrate run with "-t 1 migrate"
When we run pgmigrate with "-o info"
Then migrate command output matches json
"""
{
"2": {
"version": 2,
"type": "auto",
"installed_by": null,
"installed_on": null,
"description": "Unapplied migration",
"transactional": true
}
}
"""
11 changes: 11 additions & 0 deletions features/steps/pgmigrate_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import json

from behave import then


@then('migrate command output matches json') # noqa
def step_impl(context):
ref_data = json.loads(context.text)
out_data = json.loads(context.last_migrate_res['out'])
assert json.dumps(ref_data) == json.dumps(out_data), \
'Actual result: ' + context.last_migrate_res['out']
6 changes: 5 additions & 1 deletion features/steps/run_pgmigrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ def run_pgmigrate(migr_dir, args):
except FunctionTimedOut:
p.terminate()
stdout, stderr = p.communicate()
return p.returncode, str(stdout), str(stderr)
if not isinstance(stdout, str):
stdout = stdout.decode('utf-8')
if not isinstance(stderr, str):
stderr = stderr.decode('utf-8')
return p.returncode, stdout, stderr


@given('successful pgmigrate run with "{args}"')
Expand Down
20 changes: 14 additions & 6 deletions pgmigrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,11 @@ def _is_initialized(schema, cursor):
('beforeAll', 'beforeEach', 'afterEach', 'afterAll'))

Config = namedtuple(
'Config',
('target', 'baseline', 'cursor', 'dryrun', 'callbacks', 'user', 'base_dir',
'conn', 'session', 'conn_instance', 'terminator_instance',
'termination_interval', 'schema', 'disable_schema_check',
'check_serial_versions', 'set_version_info_after_callbacks'))
'Config', ('target', 'baseline', 'cursor', 'dryrun', 'callbacks', 'user',
'base_dir', 'conn', 'session', 'conn_instance',
'terminator_instance', 'termination_interval', 'schema',
'disable_schema_check', 'check_serial_versions',
'set_version_info_after_callbacks', 'show_only_unapplied'))

CONFIG_IGNORE = ['cursor', 'conn_instance', 'terminator_instance']

Expand Down Expand Up @@ -604,6 +604,9 @@ def info(config, stdout=True):
if stdout:
out_state = OrderedDict()
for version in sorted(state, key=int):
if config.show_only_unapplied and state[version].meta[
'installed_on'] is not None:
continue
out_state[version] = state[version].meta
sys.stdout.write(
json.dumps(out_state, indent=4, separators=(',', ': ')) + '\n')
Expand Down Expand Up @@ -821,7 +824,8 @@ def migrate(config):
schema=None,
disable_schema_check=False,
check_serial_versions=False,
set_version_info_after_callbacks=False)
set_version_info_after_callbacks=False,
show_only_unapplied=False)


def get_config(base_dir, args=None):
Expand Down Expand Up @@ -922,6 +926,10 @@ def _main():
action='store_true',
help='Check that there are no gaps '
'in migration versions')
parser.add_argument('-o',
'--show_only_unapplied',
action='store_true',
help='Show only not applied migrations in info')
parser.add_argument('-v',
'--verbose',
default=0,
Expand Down
Loading