Skip to content

Allow migrations directory and context to be used #42

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions dbmigrator/tests/data/md/20170810093842_create_a_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- coding: utf-8 -*-


def up(cursor):
cursor.execute('CREATE TABLE a_table (name TEXT)')


def down(cursor):
cursor.execute('DROP TABLE a_table')
23 changes: 23 additions & 0 deletions dbmigrator/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,29 @@ def test_wide(self):
\n""", stdout)
self.assertEqual('', stderr)

def test_migrations_directory_and_context(self):
testing.install_test_packages()

cmd = ['--db-connection-string', testing.db_connection_string]
md = os.path.join(testing.test_data_path, 'md')
self.target(cmd + ['init'])
with testing.captured_output() as (out, err):
self.target(cmd + [
'--context', 'package-a', '--context', 'package-b',
'--migrations-directory', md, 'list'])

stdout = out.getvalue()

# Assert package-a migrations are in
self.assertIn('20160228202637 add_table', stdout)
self.assertIn('20160228212456 cool_stuff', stdout)

# Assert package-b migrations are in
self.assertIn('20160228210326 initial_data', stdout)

# Assert migrations directory migrations are in
self.assertIn('20170810093842 create_a_table', stdout)


class InitTestCase(BaseTestCase):
def test_multiple_contexts(self):
Expand Down
9 changes: 7 additions & 2 deletions dbmigrator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get_settings_from_entry_points(settings, contexts):
context, __package__).values()
for entry_point in entry_points:
setting_name = entry_point.name
if settings.get(setting_name):
if not isinstance(settings.get(setting_name, []), list):
# don't overwrite settings given from the CLI
continue

Expand All @@ -76,7 +76,12 @@ def get_settings_from_entry_points(settings, contexts):
context_settings[setting_name] = value

for name, value in context_settings.items():
settings[name] = value
if isinstance(settings.get(name), list):
if not isinstance(value, list):
value = [value]
settings[name] += value
else:
settings[name] = value


def get_settings_from_config(filename, config_names, settings):
Expand Down