Don't use executescript, add explicit begins.

Today I discovered that the sqlite3 library executescript function
automatically, non-optionally performs a commit before running
the input script. I want to control transactions manually so the
database doesn't get left in a broken state.
master
voussoir 2020-08-08 18:56:11 -07:00
parent 0cf422977b
commit d137f9a889
1 changed files with 33 additions and 18 deletions

View File

@ -10,8 +10,8 @@ def upgrade_1_to_2(ycdldb):
''' '''
In this version, the `duration` column was added to the videos table. In this version, the `duration` column was added to the videos table.
''' '''
ycdldb.sql.executescript(''' ycdldb.sql.execute('ALTER TABLE videos RENAME TO videos_old')
ALTER TABLE videos RENAME TO videos_old; ycdldb.sql.execute('''
CREATE TABLE videos( CREATE TABLE videos(
id TEXT, id TEXT,
published INT, published INT,
@ -21,7 +21,9 @@ def upgrade_1_to_2(ycdldb):
duration INT, duration INT,
thumbnail TEXT, thumbnail TEXT,
download TEXT download TEXT
); )
''')
ycdldb.sql.execute('''
INSERT INTO videos SELECT INSERT INTO videos SELECT
id, id,
published, published,
@ -31,9 +33,9 @@ def upgrade_1_to_2(ycdldb):
NULL, NULL,
thumbnail, thumbnail,
download download
FROM videos_old; FROM videos_old
DROP TABLE videos_old;
''') ''')
ycdldb.sql.execute('DROP TABLE videos_old')
def upgrade_2_to_3(ycdldb): def upgrade_2_to_3(ycdldb):
''' '''
@ -46,8 +48,8 @@ def upgrade_3_to_4(ycdldb):
''' '''
In this version, the `views` column was added to the videos table. In this version, the `views` column was added to the videos table.
''' '''
ycdldb.sql.executescript(''' ycdldb.sql.execute('ALTER TABLE videos RENAME TO videos_old')
ALTER TABLE videos RENAME TO videos_old; ycdldb.sql.execute('''
CREATE TABLE videos( CREATE TABLE videos(
id TEXT, id TEXT,
published INT, published INT,
@ -58,7 +60,9 @@ def upgrade_3_to_4(ycdldb):
views INT, views INT,
thumbnail TEXT, thumbnail TEXT,
download TEXT download TEXT
); )
''')
ycdldb.sql.execute('''
INSERT INTO videos SELECT INSERT INTO videos SELECT
id, id,
published, published,
@ -69,32 +73,35 @@ def upgrade_3_to_4(ycdldb):
NULL, NULL,
thumbnail, thumbnail,
download download
FROM videos_old; FROM videos_old
DROP TABLE videos_old;
''') ''')
ycdldb.sql.execute('DROP TABLE videos_old')
def upgrade_4_to_5(ycdldb): def upgrade_4_to_5(ycdldb):
''' '''
In this version, the `uploads_playlist` column was added to the channels table. In this version, the `uploads_playlist` column was added to the channels table.
''' '''
ycdldb.sql.executescript(''' ycdldb.sql.execute('ALTER TABLE channels RENAME TO channels_old')
ALTER TABLE channels RENAME TO channels_old; ycdldb.sql.execute('''
CREATE TABLE channels( CREATE TABLE channels(
id TEXT, id TEXT,
name TEXT, name TEXT,
uploads_playlist TEXT, uploads_playlist TEXT,
directory TEXT COLLATE NOCASE, directory TEXT COLLATE NOCASE,
automark TEXT automark TEXT
); )
''')
ycdldb.sql.execute('''
INSERT INTO channels SELECT INSERT INTO channels SELECT
id, id,
name, name,
NULL, NULL,
directory, directory,
automark automark
FROM channels_old; FROM channels_old
DROP TABLE channels_old;
''') ''')
ycdldb.sql.execute('DROP TABLE channels_old')
rows = ycdldb.sql.execute('SELECT id FROM channels').fetchall() rows = ycdldb.sql.execute('SELECT id FROM channels').fetchall()
channels = [row[0] for row in rows] channels = [row[0] for row in rows]
for channel in channels: for channel in channels:
@ -131,9 +138,17 @@ def upgrade_all(data_directory):
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)
try:
ycdldb.sql.execute('BEGIN')
upgrade_function(ycdldb) upgrade_function(ycdldb)
except Exception as exc:
ycdldb.rollback()
raise
else:
ycdldb.sql.cursor().execute('PRAGMA user_version = %d' % version_number) ycdldb.sql.cursor().execute('PRAGMA user_version = %d' % version_number)
ycdldb.commit() ycdldb.commit()
current_version = version_number current_version = version_number
print('Upgrades finished.') print('Upgrades finished.')