Skip to content

Commit

Permalink
Merge pull request #149 from sergey-dryabzhinsky/fix-lzma-to-xz-migra…
Browse files Browse the repository at this point in the history
…tion

Update migrations, add migration for `xz` compression
  • Loading branch information
Sergey Dryabzhinsky authored Jun 9, 2018
2 parents 23da586 + 0b2bb1c commit 8ca8cc4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dedupsqlfs/db/migrations/m20180606001.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf8 -*-
#
# DB migration 001 by 2017-11-03
# DB migration 001 by 2018-06-06
#
# Table `subvolume` uses md5 hash as BINARY(16) now
#
Expand Down
2 changes: 1 addition & 1 deletion dedupsqlfs/db/migrations/m20180606002.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf8 -*-
#
# DB migration 001 by 2017-11-03
# DB migration 002 by 2018-06-06
#
# Table `name` uses md5 hash as BINARY(16) now
#
Expand Down
2 changes: 1 addition & 1 deletion dedupsqlfs/db/migrations/m20180609001.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf8 -*-
#
# DB migration 001 by 2017-11-03
# DB migration 001 by 2018-06-09
#
# Table `hash` uses binary(64) hash now
#
Expand Down
65 changes: 65 additions & 0 deletions dedupsqlfs/db/migrations/m20180609002.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# -*- coding: utf8 -*-
#
# DB migration 002 by 2018-06-09
#
# - Add new 'xz' compression
# - Remove old 'lzma' compression
# - change all old lzma to new xz compression
#
__author__ = 'sergey'

__NUMBER__ = 20180609002


def run(manager):
"""
:param manager: Database manager
:type manager: dedupsqlfs.db.sqlite.manager.DbManager|dedupsqlfs.db.mysql.manager.DbManager
:return: bool
"""

try:
table_ct = manager.getTable("compression_type")
"""
:type table_ct: dedupsqlfs.db.sqlite.table.compression_type.TableCompressionType |
dedupsqlfs.db.mysql.table.compression_type.TableCompressionType
"""
table_hct = manager.getTable("hash_compression_type")
"""
:type table_ct: dedupsqlfs.db.sqlite.table.compression_type.TableHashCompressionType |
dedupsqlfs.db.mysql.table.compression_type.TableHashCompressionType
"""

newHashTypeId = table_ct.find('xz')
if not newHashTypeId:
cur = table_ct.getCursor()
cur.execute("INSERT INTO compression_type (value) VALUES ('xz');")
newHashTypeId = cur.lastrowid
table_ct.commit()

hashTypeId = table_ct.find('lzma')
if hashTypeId:
cur = table_ct.getCursor()
cur.execute("DELETE FROM compression_type WHERE id=?;", (hashTypeId,))
table_ct.commit()

cur2 = table_hct.getCursor()
cur2.execute("UPDATE hash_compression_type SET type_id=? WHERE type_id=?", (hashTypeId, newHashTypeId,))
table_hct.commit()

except Exception as e:
manager.getLogger().error("Migration #%s error: %s" % (__NUMBER__, e,))
return False

table_opts = manager.getTable("option")

table_opts.getCursor()
mignumber = table_opts.get("migration")
if not mignumber:
table_opts.insert("migration", __NUMBER__)
else:
table_opts.update("migration", __NUMBER__)

table_opts.commit()

return True

0 comments on commit 8ca8cc4

Please sign in to comment.