forked from fryfrog/sonarrAnnounced
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Use database relations instead of duplicating data. * Move database queries to db module Since the query for snatches returns tuples it is a bit more inconvienient when using the data. But for the greater good is should be fine. Converting to named tuple or similar seems like a waste. Convert script included to read the old design from one file and write the new design to another one.
- Loading branch information
1 parent
ef884ea
commit ef931ee
Showing
6 changed files
with
123 additions
and
17 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#!/usr/bin/python3 | ||
import datetime | ||
import logging | ||
from pony.orm import * | ||
import os | ||
import sys | ||
|
||
# This script converts a database file from the old database design to the new one. | ||
# It takes two arguments. The old brain.db and new database to be created. | ||
# E.g. ./convert_db.py ~/.arrnounced/brain.db new_brain.db | ||
# Copy the old file for safe keeping and replace it with the new file. | ||
# N.B. Since this script converts from old to new design in one single database | ||
# session it consumes a fair bit of memory. | ||
|
||
def def_new(db): | ||
class Announced(db.Entity): | ||
date = Required(datetime.datetime) | ||
title = Required(str) | ||
indexer = Required(str) | ||
torrent = Required(str) | ||
backend = Required(str) | ||
snatched = Set('Snatched') | ||
|
||
class Snatched(db.Entity): | ||
date = Required(datetime.datetime) | ||
announced = Required(Announced) | ||
backend = Required(str) | ||
|
||
def def_old(db): | ||
class Announced(db.Entity): | ||
date = Required(datetime.datetime) | ||
title = Required(str) | ||
indexer = Required(str) | ||
torrent = Required(str) | ||
backend = Required(str) | ||
|
||
class Snatched(db.Entity): | ||
date = Required(datetime.datetime) | ||
title = Required(str) | ||
indexer = Required(str) | ||
torrent = Required(str) | ||
backend = Required(str) | ||
|
||
def open_database(filename, definition, create_db): | ||
db = Database() | ||
definition(db) | ||
db.bind('sqlite', os.path.realpath(filename), create_db=create_db) | ||
db.generate_mapping(create_tables=create_db) | ||
return db | ||
|
||
if len(sys.argv) != 3: | ||
print("Usage: " + sys.argv[0] + " <old database> <new database>") | ||
sys.exit(1) | ||
|
||
if os.path.isfile(sys.argv[2]): | ||
print("Error: New database cannot be existing file") | ||
sys.exit(1) | ||
|
||
old_db = open_database(sys.argv[1], def_old, False) | ||
new_db = open_database(sys.argv[2], def_new, True) | ||
|
||
with db_session: | ||
old_anns = old_db.Announced.select().order_by(old_db.Announced.id) | ||
for old_ann in old_anns: | ||
new_ann = new_db.Announced(date=old_ann.date, | ||
title=old_ann.title, | ||
indexer=old_ann.indexer, | ||
torrent=old_ann.torrent, | ||
backend=old_ann.backend) | ||
|
||
old_sns = list(select(s for s in old_db.Snatched if s.title == old_ann.title)) | ||
#old_sns.show() | ||
if len(old_sns) == 0: | ||
#print(old_ann.title + " was not snatched") | ||
continue | ||
elif len(old_sns) > 1: | ||
print("Warning: Found more than once snatch which matched an announcment. Choosing first one") | ||
|
||
old_sn = old_sns[0] | ||
new_db.Snatched(date=old_sn.date, | ||
announced=new_ann, | ||
backend=old_sn.backend) | ||
|
||
#new_db.Announced.select().order_by(new_db.Announced.id).show() | ||
#new_db.Snatched.select().order_by(new_db.Snatched.id).show() |
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
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
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
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
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