Last active
March 27, 2016 13:04
-
-
Save keriszafir/52c9e89ed9c16f39b356 to your computer and use it in GitHub Desktop.
Database fix for rpi2caster 0.3 - store wedge definitions in a single column
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
from rpi2caster import database | |
from rpi2caster import matrix_data | |
DB = database.Database() | |
def correct_diecases(): | |
"""Changes wedge_series, set_width -> wedge_name""" | |
with DB.db_connection: | |
cursor = DB.db_connection.cursor() | |
cursor.execute('ALTER TABLE matrix_cases ' | |
'RENAME TO old_matrix_cases') | |
cursor.execute('CREATE TABLE matrix_cases (' | |
'diecase_id TEXT UNIQUE PRIMARY KEY, ' | |
'typeface TEXT, ' | |
'wedge TEXT, ' | |
'layout TEXT NOT NULL)') | |
cursor.execute('SELECT * FROM old_matrix_cases') | |
diecases = cursor.fetchall() | |
for diecase_definition in diecases: | |
[diecase_id, type_series, type_size, wedge_series, | |
set_width, typeface_name, layout] = diecase_definition | |
if not set_width % 1: | |
set_width = int(set_width) | |
wedge_name = 'S%s-%sE' % (wedge_series, set_width) | |
typeface = '%s-%s %s' % (type_series, type_size, typeface_name) | |
data = [diecase_id, typeface, wedge_name, layout] | |
cursor.execute('INSERT OR REPLACE INTO matrix_cases (' | |
'diecase_id, typeface, wedge, layout' | |
') VALUES (?, ?, ?, ?)''', data) | |
cursor.execute('DROP TABLE wedges') | |
cursor.execute('DROP TABLE old_matrix_cases') | |
DB.db_connection.commit() | |
with DB.db_connection: | |
cursor = DB.db_connection.cursor() | |
cursor.execute('PRAGMA table_info(matrix_cases)') | |
column_items = [col_item[1] for col_item in cursor.fetchall()] | |
if column_items == ['diecase_id', 'type_series', 'type_size', | |
'wedge_series', 'set_width', 'typeface_name', | |
'layout']: | |
print('You have an old style diecases table. This will be changed ' | |
'with rpi2caster 0.3') | |
old_style = True | |
else: | |
print('Your database is already corrected. ' | |
'No need to do anything.') | |
old_style = False | |
try: | |
os.system('cp %s %s' % (DB.path, DB.path + '.bak')) | |
if old_style: | |
print('This will correct the database, so that there is' | |
' a single column "wedges" and "typeface".') | |
correct_diecases() | |
print('Now check if there are the same diecases ' | |
'as you had before...') | |
matrix_data.list_diecases() | |
except AttributeError: | |
print('You are using older version of the program. ' | |
'No need to change the database.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment