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

Added always_confirm_no option #39

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 12 additions & 3 deletions peeweedbevolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,9 @@ def calc_changes(db, ignore_tables=None):
deleted_cols_by_table[ntn] = deletes

for ntn, model in table_names_to_models.items():
# if ignoring this table, skip index manipulation
if model._meta.table_name in ignore_tables:
continue
etn = table_renamed_from.get(ntn, ntn)
deletes = deleted_cols_by_table.get(ntn,set())
existing_indexes_for_table = [i for i in existing_indexes.get(etn, []) if not any([(c in deletes) for c in i.columns])]
Expand Down Expand Up @@ -715,18 +718,21 @@ def calc_index_changes(db, migrator, existing_indexes, model, renamed_cols):
to_run += create_index(model, [fields_by_column_name[col] for col in index[1]], index[2])
return to_run

def evolve(db, interactive=True, ignore_tables=None):
def evolve(db, interactive=True, ignore_tables=None, always_confirm_no=False, testing=False):
if interactive:
print((colorama.Style.BRIGHT + colorama.Fore.RED + 'Making updates to database: {}'.format(db.database) + colorama.Style.RESET_ALL))
to_run = calc_changes(db, ignore_tables=ignore_tables)
# in testing mode, return the sql to run so that it can be checked for correctness
if testing:
return to_run
if not to_run:
if interactive:
print('Nothing to do... Your database is up to date!')
return

commit = True
if interactive:
commit = _confirm(db, to_run)
commit = _confirm(db, to_run, always_confirm_no)
_execute(db, to_run, interactive=interactive, commit=commit)


Expand Down Expand Up @@ -780,7 +786,7 @@ def print_sql(sql):
print(sql)


def _confirm(db, to_run):
def _confirm(db, to_run, always_confirm_no):
print()
print("Your database needs the following %s:" % ('changes' if len(to_run)>1 else 'change'))
print()
Expand All @@ -789,6 +795,9 @@ def _confirm(db, to_run):
print_sql(' %s; %s' % (sql, params or ''))
if is_postgres(db): print_sql('\n COMMIT;')
print()
if always_confirm_no:
print("Confirming 'no' to these changes as always_confirm_no is True")
sys.exit(1)
while True:
print('Do you want to run %s? (%s)' % (('these commands' if len(to_run)>1 else 'this command'), ('type yes, no or test' if is_postgres(db) else 'yes or no')), end=' ')
response = raw_input().strip().lower()
Expand Down