Skip to content

Commit

Permalink
Allow mark to accept multiple timestamps
Browse files Browse the repository at this point in the history
If there is a list of migrations that needs to be marked as completed
(or not completed or deferred), we currently have to do:

```
dbmigrator mark -t 20160228212456
dbmigrator mark -t 20160228202637
```

Now it will accept a list and we can do this instead:

```
dbmigrator mark -t 20160228212456 20160228202637
```
  • Loading branch information
karenc committed Nov 22, 2017
1 parent 3024d5e commit d180d37
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
34 changes: 18 additions & 16 deletions dbmigrator/commands/mark.py
Expand Up @@ -14,32 +14,34 @@


@utils.with_cursor
def cli_command(cursor, migrations_directory='', migration_timestamp='',
def cli_command(cursor, migrations_directory='', migration_timestamps=None,
completed=None, **kwargs):
if completed is None:
raise Exception('-t, -f or -d must be supplied.')

migrations = {version: migration
for version, _, migration in utils.get_migrations(
migrations_directory, import_modules=True)}
migration = migrations.get(migration_timestamp)
if migration is None:
raise SystemExit(
'Migration {} not found'.format(migration_timestamp))

utils.mark_migration(cursor, migration_timestamp, completed)
if not completed:
message = 'not been run and not deferred'
elif completed == 'deferred':
message = 'deferred'
else:
message = 'completed'
logger.info('Migration {} marked as {}'.format(migration_timestamp,
message))

for migration_timestamp in migration_timestamps:
migration = migrations.get(migration_timestamp)
if migration is None:
raise SystemExit(
'Migration {} not found'.format(migration_timestamp))

utils.mark_migration(cursor, migration_timestamp, completed)
if not completed:
message = 'not been run and not deferred'
elif completed == 'deferred':
message = 'deferred'
else:
message = 'completed'
logger.info('Migration {} marked as {}'.format(migration_timestamp,
message))


def cli_loader(parser):
parser.add_argument('migration_timestamp')
parser.add_argument('migration_timestamps', nargs='+')
parser.add_argument('-t', action='store_const',
const=True,
dest='completed',
Expand Down
24 changes: 24 additions & 0 deletions dbmigrator/tests/test_cli.py
Expand Up @@ -192,6 +192,12 @@ def test_multiple_contexts(self):


class MarkTestCase(BaseTestCase):
def test_missing_version(self):
cmd = ['--db-connection-string', testing.db_connection_string]

with self.assertRaises(SystemExit) as cm:
self.target(cmd + ['mark', '-t'])

def test_missing_t_f_d_option(self):
cmd = ['--db-connection-string', testing.db_connection_string]

Expand Down Expand Up @@ -221,6 +227,24 @@ def test_mark_as_true(self):
self.assertIn('20160228202637 add_table False', stdout)
self.assertIn('20160228212456 cool_stuff True', stdout)

def test_mark_more_than_one(self):
testing.install_test_packages()
cmd = ['--db-connection-string', testing.db_connection_string]

self.target(cmd + ['--context', 'package-a', 'init', '--version', '0'])
self.target(cmd + ['--context', 'package-a',
'mark', '-t', '20160228212456', '20160228202637'])

logger.info.assert_called_with(
'Migration 20160228202637 marked as completed')

with testing.captured_output() as (out, err):
self.target(cmd + ['--context', 'package-a', 'list'])

stdout = out.getvalue()
self.assertIn('20160228202637 add_table True', stdout)
self.assertIn('20160228212456 cool_stuff True', stdout)

def test_mark_as_true_already_true(self):
testing.install_test_packages()
cmd = ['--db-connection-string', testing.db_connection_string]
Expand Down

0 comments on commit d180d37

Please sign in to comment.