def retrieve(): '''\ Fetch a database dump and import, then apply patches. ''' print "Retrieving DB." constant.retrieve_db(database) add_event_to_db(database, "retrieve", time.time())
def post_checkout_branch(branch, database, old_head_ref, new_head_ref): """\ Handle a switch in branches. Fun times ahead. """ if get_last_branch() == branch: logging.warning("Already on branch %s.", branch) return # Still a work in progress #check_branch_rename(branch, database) if not exists_database(database + branch): # The branched DB doesn't exist, create it from our current copy. # XXX: This has problems! Currently we only support branching from the current branch... # I think I can solve this by searching through all the branches, and finding the corresponding name # for old_ref. src_branch = get_last_branch() copy_database(database, database + branch) # Create the "base.sql" file for this branch - any changes to the branch will be relative to its starting # point - and NOT to production. create_base_sql(branch, database) patches = [patch for patch in list_applied_patches(src_branch, database) if patch not in list_merged_patches(src_branch, branch, database) and patch not in list_ignored_patches(src_branch, database)] add_merged_patches(patches, 'base.sql', src_branch, branch, database) # XXX: Use reflog here rather to get the last branch. rename_database(database, database + get_last_branch()) rename_database(database + branch, database) set_last_branch(branch) add_event_to_db(database, "branch-change", time.time())
def create(): '''\ Create a production.sql file from the current database. ''' mysql_schema = subprocess.check_output(["mysqldump", "-u", constant.username, "--password=%s" % constant.password, "--no-data=true", "--add-drop-table=false", database]) makedirs(os.path.join("sql", "applied", database)) with open(os.path.join("sql", "applied", database, "production.sql"), "w") as production: production.write(mysql_schema) add_event_to_db(database, "created", time.time())
def unignore_patches(): branch = git.current_branch() if list_ignored_patches(branch, database): patches = dict([(i, patch) for i, patch in enumerate(list_ignored_patches(branch, database))]) pprint(patches) try: input = raw_input("Please enter a space separated list of patch #s to unignore: ") if input != "": to_unignore = [int(i) for i in input.split(" ")] for unignored in to_unignore: remove_ignored_from_db(git.current_branch(), database, patches[unignored]) except: print "Invalid input." return else: print "No patches to be unignored." add_event_to_db(database, "unignored patches, %s: ", time.time())
def ignore_patches(): # Mark certain patches as to be ignored / unignored. branch = git.current_branch() patches = dict([(i, patch) for i, patch in enumerate(list_file_patches(branch, database)) if patch not in list_applied_patches(branch, database) and patch not in list_ignored_patches(branch, database)]) if not patches: print "No patches to be ignored." return pprint(patches) try: input = raw_input("Please enter a space separated list of patch #s to ignore: ") if input != "": to_ignore = [int(i) for i in input.split(" ")] for ignored in to_ignore: add_ignored_to_db(git.current_branch(), database, patches[ignored]) except: print "Invalid input." return add_event_to_db(database, "ignored patches, %s: ", time.time())
def install(): '''\ Install hooks into .git/hooks directory. ''' user = subprocess.check_output("whoami").strip() name = raw_input("Please enter the shortname you'd like to use (%s): " % user) if not name: name = user for hook in glob.glob(os.path.join("hooks", "*")): shutil.copyfile(hook, os.path.join(".git", "hooks", os.path.basename(hook))) os.chmod(os.path.join(".git", "hooks", os.path.basename(hook)), 0755) with open("constant.py", "a") as open_file: open_file.write('\nshortname = "federico"\n') print "Please ensure that binlogging has been enabled in your my.cnf." print "A line saying 'log-bin=/var/log/mysql/mysql-bin' under the '[mysqld]' section is generally all you need." print "But your distribution may store the files in a different place - Ubuntu uses /var/log/mysql" print "Afterwards, please chown -R mysql:%s /var/log/mysql" % user print "If yours is different, you will need to modify constant.py" add_event_to_db(database, "installed", time.time())
def stash(): branch = git.current_branch() if args.unstash or args.delete: if list_stashed_patches(branch, database): patches = dict([(i, patch) for i, patch in enumerate(list_stashed_patches(branch, database))]) pprint(patches) try: input = raw_input("Please enter a space separated list of patch #s to unstash: " if args.unstash else "Please enter a space separated list of patch #s to delete: ") if input != "": to_unstash = [patches[int(i)] for i in input.split(" ")] for unstashed in to_unstash: if args.unstash: try: apply_patch(database, unstashed, True) remove_stashed_from_db(branch, database, unstashed) except MySqlException: print "Error applying stashed patch %s, not continuing" elif args.delete: # Delete the patch here. os.remove(unstashed) remove_stashed_from_db(branch, database, unstashed) except: print "Invalid input." return else: print "No stashed patches available for unstashing." if args.unstash else "No stashed patches available for deleting." if args.unstash: add_event_to_db(database, "unstashed patches, %s: ", time.time()) else: if fast_check_sqlchanges(database, git.last_commit_time()): print "Stashing all schema changes made on branch %s to database %s" % (branch, database) with amalgamated_sql(branch, database) as amalgamated_sql_file: base_dir = os.path.join(".git", "gitdb", "stashed", branch, database) patch_name = os.path.join(base_dir, free_patch_name()) makedirs(base_dir) sql_changes = calculate_difference(amalgamated_sql_file, database, [filters.filter_auto_increment, lambda input: filters.filter_renames(input, database, git.last_commit_time())]) if not sql_changes: print "No schema changes to stash." return else: print "Your SQL schema changes will be stashed in the file %s. You can use the git db stash command to manipulate them." % patch_name with open(patch_name, "w") as sql_patch: sql_patch.write(sql_changes) # Rollback the database to the last commit. temp_file = NamedTemporaryFile() sql_difference = calculate_difference(database, amalgamated_sql_file, [filters.filter_auto_increment, lambda input: filters.filter_renames(input, database, git.last_commit_time())]) temp_file.write(sql_difference) temp_file.flush() apply_patch(database, temp_file.name, True) temp_file.close() add_stashed_to_db(branch, database, patch_name) add_event_to_db(database, "stash", time.time()) else: print "No schema changes to stash."
def merge(): # Merge in unmerged development patches. from gitdb import handle_pull handle_pull(git.current_branch(), database) add_event_to_db(database, "merge", time.time())