2023-10-16 22:52:02 +03:00
|
|
|
import os
|
|
|
|
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
|
2023-10-20 17:04:58 +03:00
|
|
|
from db_creation import __version__
|
2023-10-16 22:52:02 +03:00
|
|
|
import mysql.connector as mysql
|
|
|
|
|
2023-10-20 17:04:58 +03:00
|
|
|
target_version = __version__
|
2023-10-16 22:52:02 +03:00
|
|
|
|
|
|
|
load_dotenv()
|
|
|
|
DB_USERNAME = os.getenv('DB_USERNAME')
|
|
|
|
DB_PASSWORD = os.getenv('DB_PASSWORD')
|
|
|
|
|
|
|
|
|
|
|
|
def updater_1_0_0(connection: mysql.connection.MySQLConnection) -> str:
|
2023-10-20 17:26:49 +03:00
|
|
|
with connection.cursor() as crsr:
|
|
|
|
crsr.execute("SELECT COLUMN_NAME "
|
|
|
|
"FROM INFORMATION_SCHEMA.COLUMNS "
|
|
|
|
"WHERE TABLE_SCHEMA = 'hfc_db' "
|
|
|
|
"AND TABLE_NAME = 'channels' "
|
|
|
|
"AND COLUMN_NAME = 'locations';")
|
|
|
|
|
|
|
|
exists = (crsr.fetchone() is not None)
|
|
|
|
|
|
|
|
crsr.nextset()
|
|
|
|
|
|
|
|
if exists:
|
|
|
|
crsr.execute("ALTER TABLE `hfc_db`.`channels` DROP COLUMN `locations`;")
|
|
|
|
|
|
|
|
crsr.execute("ALTER TABLE `hfc_db`.`channels` ADD COLUMN `locations` JSON NOT NULL DEFAULT ('[]');")
|
|
|
|
|
2023-10-16 22:52:02 +03:00
|
|
|
return '1.0.1'
|
|
|
|
|
|
|
|
|
|
|
|
updaters = {
|
|
|
|
'1.0.0': updater_1_0_0
|
|
|
|
}
|
|
|
|
|
2023-10-20 17:14:22 +03:00
|
|
|
current_version = input(f'Please enter current version (latest: {__version__}):\n')
|
2023-10-16 22:52:02 +03:00
|
|
|
|
|
|
|
if current_version not in updaters.keys():
|
|
|
|
print('Invalid version.')
|
|
|
|
exit()
|
|
|
|
with mysql.connect(host='localhost', user=DB_USERNAME, password=DB_PASSWORD) as connection:
|
|
|
|
while current_version != target_version:
|
|
|
|
current_version = updaters[current_version](connection)
|
2023-10-20 17:15:22 +03:00
|
|
|
print(f'Updated DB to version {current_version}')
|
|
|
|
|
|
|
|
print(f'DB is now at target version!')
|