remove transfers table

This commit is contained in:
j 2016-02-15 17:00:17 +05:30
parent acaeb203a1
commit 045eb91be3
5 changed files with 36 additions and 62 deletions

View file

@ -115,12 +115,13 @@ class Item(db.Model):
cls.remove_many(remove) cls.remove_many(remove)
def add_user(self, user): def add_user(self, user):
from user.models import list_items
if not user in self.users: if not user in self.users:
self.users.append(user) self.users.append(user)
l = user.library l = user.library
if not self in l.items: if not self in l.items:
l.items.append(self) q = list_items.insert({'item_id': self.id, 'list_id': l.id})
state.db.session.add(l) state.db.session.execute(q)
def json(self, keys=None): def json(self, keys=None):
j = {} j = {}
@ -507,10 +508,11 @@ class Item(db.Model):
def queue_download(self): def queue_download(self):
u = state.user() u = state.user()
if not u in self.users: if not u in self.users:
state.downloads.transfers[self.id] = { if not self.id in state.downloads.transfers:
'added': datetime.utcnow(), state.downloads.transfers[self.id] = {
'progress': 0 'added': datetime.utcnow(),
} 'progress': 0
}
logger.debug('queue %s for download', self.id) logger.debug('queue %s for download', self.id)
self.add_user(u) self.add_user(u)
@ -781,37 +783,6 @@ class File(db.Model):
state.db.session.add(self) state.db.session.add(self)
state.db.session.commit() state.db.session.commit()
class Transfer(db.Model):
__tablename__ = 'transfer'
item_id = sa.Column(sa.String(32), sa.ForeignKey('item.id'), primary_key=True)
item = sa.orm.relationship('Item', backref=sa.orm.backref('transfer', lazy='dynamic'))
added = sa.Column(sa.DateTime())
progress = sa.Column(sa.Float())
def __repr__(self):
return '='.join(map(str, [self.item_id, self.progress]))
@classmethod
def get(cls, item_id):
return cls.query.filter_by(item_id=item_id).first()
@classmethod
def get_or_create(cls, item_id):
t = cls.get(item_id)
if not t:
t = cls(item_id=item_id)
t.added = datetime.utcnow()
t.progress = 0
t.save()
return t
def save(self):
state.db.session.add(self)
state.db.session.commit()
def remove_unused_names(): def remove_unused_names():
used = list(set( used = list(set(
get_sort_name(a) get_sort_name(a)

View file

@ -336,7 +336,9 @@ def cleanup_lists():
import user.models import user.models
with db.session(): with db.session():
for l in user.models.List.query.all(): for l in user.models.List.query.all():
if not l.user.peered and not l.user.id == settings.USER_ID: if not l.user:
l.remove()
elif not l.user.peered and not l.user.id == settings.USER_ID:
l.remove() l.remove()
peers = [u.id for u in user.models.User.query.filter_by(peered=True)] + [settings.USER_ID] peers = [u.id for u in user.models.User.query.filter_by(peered=True)] + [settings.USER_ID]

View file

@ -100,4 +100,4 @@ USER_AGENT = 'OpenMediaLibrary/%s' % VERSION
DEBUG_HTTP = server.get('debug_http', False) DEBUG_HTTP = server.get('debug_http', False)
DB_VERSION = 11 DB_VERSION = 12

View file

@ -64,13 +64,6 @@ CREATE TABLE person (
numberofnames INTEGER, numberofnames INTEGER,
PRIMARY KEY (name) PRIMARY KEY (name)
); );
CREATE TABLE transfer (
item_id VARCHAR(32) NOT NULL,
added DATETIME,
progress FLOAT,
PRIMARY KEY (item_id),
FOREIGN KEY(item_id) REFERENCES item (id)
);
CREATE TABLE find ( CREATE TABLE find (
id INTEGER NOT NULL, id INTEGER NOT NULL,
item_id VARCHAR(32), item_id VARCHAR(32),
@ -434,7 +427,6 @@ def update_database():
item.models.Find, item.models.Find,
item.models.Item, item.models.Item,
item.models.Sort, item.models.Sort,
item.models.Transfer,
item.models.user_items, item.models.user_items,
item.person.Person, item.person.Person,
user.models.List, user.models.List,

View file

@ -344,6 +344,8 @@ class Update(Thread):
db_version = migrate_10() db_version = migrate_10()
if db_version < 11: if db_version < 11:
db_version = migrate_11() db_version = migrate_11()
if db_version < 12:
db_version = migrate_12()
settings.server['db_version'] = settings.DB_VERSION settings.server['db_version'] = settings.DB_VERSION
def run(self): def run(self):
@ -519,7 +521,6 @@ def migrate_10():
def migrate_11(): def migrate_11():
with db.session() as session: with db.session() as session:
from user.models import User, Metadata, List from user.models import User, Metadata, List
from item.models import Transfer
from changelog import Changelog from changelog import Changelog
import utils import utils
for u in User.query.filter_by(peered=True): for u in User.query.filter_by(peered=True):
@ -546,18 +547,26 @@ def migrate_11():
Changelog.query.filter_by(user_id=u.id).delete() Changelog.query.filter_by(user_id=u.id).delete()
Metadata.query.filter_by(user_id=u.id).delete() Metadata.query.filter_by(user_id=u.id).delete()
session.commit() session.commit()
import state if db.table_exists('transfer'):
import downloads import state
state.online = False import downloads
state.downloads = downloads.Downloads() state.online = False
for t in Transfer.query: state.downloads = downloads.Downloads()
if t.added: r = state.db.session.execute('SELECT item_id, added, progress FROM transfer')
state.downloads.transfers[t.item_id] = { for t in r:
'added': t.added, item_id, added, progress = t
'progress': t.progress if added:
} state.downloads.transfers[item_id] = {
state.db.session.add(t) 'added': added,
state.db.session.commit() 'progress': progress
state.downloads.transfers.commit() }
state.downloads = None state.db.session.commit()
state.downloads.transfers.commit()
state.downloads = None
return 11 return 11
def migrate_12():
db.run_sql([
'DROP TABLE IF EXISTS transfer'
]),
return 12