Let database_upgrader use the photodb instead of raw sql con.
This commit is contained in:
parent
44d1fb2fff
commit
f3eb6345f7
1 changed files with 26 additions and 28 deletions
|
@ -5,21 +5,21 @@ import sys
|
||||||
|
|
||||||
import etiquette
|
import etiquette
|
||||||
|
|
||||||
def upgrade_1_to_2(sql):
|
def upgrade_1_to_2(photodb):
|
||||||
'''
|
'''
|
||||||
In this version, a column `tagged_at` was added to the Photos table, to keep
|
In this version, a column `tagged_at` was added to the Photos table, to keep
|
||||||
track of the last time the photo's tags were edited (added or removed).
|
track of the last time the photo's tags were edited (added or removed).
|
||||||
'''
|
'''
|
||||||
cur = sql.cursor()
|
cur = photodb.sql.cursor()
|
||||||
cur.execute('ALTER TABLE photos ADD COLUMN tagged_at INT')
|
cur.execute('ALTER TABLE photos ADD COLUMN tagged_at INT')
|
||||||
|
|
||||||
def upgrade_2_to_3(sql):
|
def upgrade_2_to_3(photodb):
|
||||||
'''
|
'''
|
||||||
Preliminary support for user account management was added. This includes a `user` table
|
Preliminary support for user account management was added. This includes a `user` table
|
||||||
with id, username, password hash, and a timestamp.
|
with id, username, password hash, and a timestamp.
|
||||||
Plus some indices.
|
Plus some indices.
|
||||||
'''
|
'''
|
||||||
cur = sql.cursor()
|
cur = photodb.sql.cursor()
|
||||||
cur.execute('''
|
cur.execute('''
|
||||||
CREATE TABLE IF NOT EXISTS users(
|
CREATE TABLE IF NOT EXISTS users(
|
||||||
id TEXT,
|
id TEXT,
|
||||||
|
@ -31,19 +31,19 @@ def upgrade_2_to_3(sql):
|
||||||
cur.execute('CREATE INDEX IF NOT EXISTS index_user_id ON users(id)')
|
cur.execute('CREATE INDEX IF NOT EXISTS index_user_id ON users(id)')
|
||||||
cur.execute('CREATE INDEX IF NOT EXISTS index_user_username ON users(username COLLATE NOCASE)')
|
cur.execute('CREATE INDEX IF NOT EXISTS index_user_username ON users(username COLLATE NOCASE)')
|
||||||
|
|
||||||
def upgrade_3_to_4(sql):
|
def upgrade_3_to_4(photodb):
|
||||||
'''
|
'''
|
||||||
Add an `author_id` column to Photos.
|
Add an `author_id` column to Photos.
|
||||||
'''
|
'''
|
||||||
cur = sql.cursor()
|
cur = photodb.sql.cursor()
|
||||||
cur.execute('ALTER TABLE photos ADD COLUMN author_id TEXT')
|
cur.execute('ALTER TABLE photos ADD COLUMN author_id TEXT')
|
||||||
cur.execute('CREATE INDEX IF NOT EXISTS index_photo_author ON photos(author_id)')
|
cur.execute('CREATE INDEX IF NOT EXISTS index_photo_author ON photos(author_id)')
|
||||||
|
|
||||||
def upgrade_4_to_5(sql):
|
def upgrade_4_to_5(photodb):
|
||||||
'''
|
'''
|
||||||
Add table `bookmarks` and its indices.
|
Add table `bookmarks` and its indices.
|
||||||
'''
|
'''
|
||||||
cur = sql.cursor()
|
cur = photodb.sql.cursor()
|
||||||
cur.execute('''
|
cur.execute('''
|
||||||
CREATE TABLE IF NOT EXISTS bookmarks(
|
CREATE TABLE IF NOT EXISTS bookmarks(
|
||||||
id TEXT,
|
id TEXT,
|
||||||
|
@ -55,7 +55,7 @@ def upgrade_4_to_5(sql):
|
||||||
cur.execute('CREATE INDEX IF NOT EXISTS index_bookmark_id ON bookmarks(id)')
|
cur.execute('CREATE INDEX IF NOT EXISTS index_bookmark_id ON bookmarks(id)')
|
||||||
cur.execute('CREATE INDEX IF NOT EXISTS index_bookmark_author ON bookmarks(author_id)')
|
cur.execute('CREATE INDEX IF NOT EXISTS index_bookmark_author ON bookmarks(author_id)')
|
||||||
|
|
||||||
def upgrade_5_to_6(sql):
|
def upgrade_5_to_6(photodb):
|
||||||
'''
|
'''
|
||||||
When Albums were first introduced, they shared the ID counter and
|
When Albums were first introduced, they shared the ID counter and
|
||||||
relationship table with tags, because they were mostly identical at the time.
|
relationship table with tags, because they were mostly identical at the time.
|
||||||
|
@ -70,7 +70,7 @@ def upgrade_5_to_6(sql):
|
||||||
# 1. Start the id_numbers.albums value at the tags value so that the number
|
# 1. Start the id_numbers.albums value at the tags value so that the number
|
||||||
# can continue to increment safely and separately, instead of starting at
|
# can continue to increment safely and separately, instead of starting at
|
||||||
# zero and bumping into existing albums.
|
# zero and bumping into existing albums.
|
||||||
cur = sql.cursor()
|
cur = photodb.sql.cursor()
|
||||||
cur.execute('SELECT * FROM id_numbers WHERE tab == "tags"')
|
cur.execute('SELECT * FROM id_numbers WHERE tab == "tags"')
|
||||||
last_id = cur.fetchone()[1]
|
last_id = cur.fetchone()[1]
|
||||||
cur.execute('INSERT INTO id_numbers VALUES("albums", ?)', [last_id])
|
cur.execute('INSERT INTO id_numbers VALUES("albums", ?)', [last_id])
|
||||||
|
@ -103,7 +103,7 @@ def upgrade_5_to_6(sql):
|
||||||
[album_id, album_id]
|
[album_id, album_id]
|
||||||
)
|
)
|
||||||
|
|
||||||
def upgrade_6_to_7(sql):
|
def upgrade_6_to_7(photodb):
|
||||||
'''
|
'''
|
||||||
Most of the indices were renamed, so delete them and let them regenerate
|
Most of the indices were renamed, so delete them and let them regenerate
|
||||||
next time.
|
next time.
|
||||||
|
@ -112,7 +112,7 @@ def upgrade_6_to_7(sql):
|
||||||
separate table `album_associated_directories`, so that we can have albums
|
separate table `album_associated_directories`, so that we can have albums
|
||||||
which load from multiple directories.
|
which load from multiple directories.
|
||||||
'''
|
'''
|
||||||
cur = sql.cursor()
|
cur = photodb.sql.cursor()
|
||||||
cur.execute('SELECT name FROM sqlite_master WHERE type == "index"')
|
cur.execute('SELECT name FROM sqlite_master WHERE type == "index"')
|
||||||
indices = [x[0] for x in cur.fetchall()]
|
indices = [x[0] for x in cur.fetchall()]
|
||||||
for index in indices:
|
for index in indices:
|
||||||
|
@ -139,32 +139,30 @@ def upgrade_6_to_7(sql):
|
||||||
''')
|
''')
|
||||||
cur.execute('DROP TABLE deleting_albums')
|
cur.execute('DROP TABLE deleting_albums')
|
||||||
|
|
||||||
def upgrade_7_to_8(sql):
|
def upgrade_7_to_8(photodb):
|
||||||
'''
|
'''
|
||||||
Give the Tags table a description field.
|
Give the Tags table a description field.
|
||||||
'''
|
'''
|
||||||
cur = sql.cursor()
|
cur = photodb.sql.cursor()
|
||||||
cur.execute('ALTER TABLE tags ADD COLUMN description TEXT')
|
cur.execute('ALTER TABLE tags ADD COLUMN description TEXT')
|
||||||
|
|
||||||
def upgrade_8_to_9(sql):
|
def upgrade_8_to_9(photodb):
|
||||||
'''
|
'''
|
||||||
Give the Photos table a searchhidden field.
|
Give the Photos table a searchhidden field.
|
||||||
'''
|
'''
|
||||||
cur = sql.cursor()
|
cur = photodb.sql.cursor()
|
||||||
cur.execute('ALTER TABLE photos ADD COLUMN searchhidden INT')
|
cur.execute('ALTER TABLE photos ADD COLUMN searchhidden INT')
|
||||||
cur.execute('UPDATE photos SET searchhidden = 0')
|
cur.execute('UPDATE photos SET searchhidden = 0')
|
||||||
cur.execute('CREATE INDEX index_photos_searchhidden on photos(searchhidden)')
|
cur.execute('CREATE INDEX index_photos_searchhidden on photos(searchhidden)')
|
||||||
|
|
||||||
def upgrade_all(database_filename):
|
def upgrade_all(data_directory):
|
||||||
'''
|
'''
|
||||||
Given the filename of a phototagger database, apply all of the needed
|
Given the directory containing a phototagger database, apply all of the
|
||||||
upgrade_x_to_y functions in order.
|
needed upgrade_x_to_y functions in order.
|
||||||
'''
|
'''
|
||||||
if not os.path.isfile(database_filename):
|
photodb = etiquette.photodb.PhotoDB(data_directory, create=False, skip_version_check=True)
|
||||||
raise FileNotFoundError(database_filename)
|
|
||||||
|
|
||||||
sql = sqlite3.connect(database_filename)
|
cur = photodb.sql.cursor()
|
||||||
cur = sql.cursor()
|
|
||||||
|
|
||||||
cur.execute('PRAGMA user_version')
|
cur.execute('PRAGMA user_version')
|
||||||
current_version = cur.fetchone()[0]
|
current_version = cur.fetchone()[0]
|
||||||
|
@ -178,20 +176,20 @@ def upgrade_all(database_filename):
|
||||||
print('Upgrading from %d to %d' % (current_version, version_number))
|
print('Upgrading from %d to %d' % (current_version, version_number))
|
||||||
upgrade_function = 'upgrade_%d_to_%d' % (current_version, version_number)
|
upgrade_function = 'upgrade_%d_to_%d' % (current_version, version_number)
|
||||||
upgrade_function = eval(upgrade_function)
|
upgrade_function = eval(upgrade_function)
|
||||||
upgrade_function(sql)
|
upgrade_function(photodb)
|
||||||
sql.cursor().execute('PRAGMA user_version = %d' % version_number)
|
photodb.sql.cursor().execute('PRAGMA user_version = %d' % version_number)
|
||||||
sql.commit()
|
photodb.commit()
|
||||||
current_version = version_number
|
current_version = version_number
|
||||||
print('Upgrades finished.')
|
print('Upgrades finished.')
|
||||||
|
|
||||||
|
|
||||||
def upgrade_all_argparse(args):
|
def upgrade_all_argparse(args):
|
||||||
return upgrade_all(database_filename=args.database_filename)
|
return upgrade_all(data_directory=args.data_directory)
|
||||||
|
|
||||||
def main(argv):
|
def main(argv):
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
|
|
||||||
parser.add_argument('database_filename')
|
parser.add_argument('data_directory')
|
||||||
parser.set_defaults(func=upgrade_all_argparse)
|
parser.set_defaults(func=upgrade_all_argparse)
|
||||||
|
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
Loading…
Reference in a new issue