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)
def add_user(self, user):
from user.models import list_items
if not user in self.users:
self.users.append(user)
l = user.library
if not self in l.items:
l.items.append(self)
state.db.session.add(l)
q = list_items.insert({'item_id': self.id, 'list_id': l.id})
state.db.session.execute(q)
def json(self, keys=None):
j = {}
@ -507,10 +508,11 @@ class Item(db.Model):
def queue_download(self):
u = state.user()
if not u in self.users:
state.downloads.transfers[self.id] = {
'added': datetime.utcnow(),
'progress': 0
}
if not self.id in state.downloads.transfers:
state.downloads.transfers[self.id] = {
'added': datetime.utcnow(),
'progress': 0
}
logger.debug('queue %s for download', self.id)
self.add_user(u)
@ -781,37 +783,6 @@ class File(db.Model):
state.db.session.add(self)
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():
used = list(set(
get_sort_name(a)

View file

@ -336,7 +336,9 @@ def cleanup_lists():
import user.models
with db.session():
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()
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)
DB_VERSION = 11
DB_VERSION = 12

View file

@ -64,13 +64,6 @@ CREATE TABLE person (
numberofnames INTEGER,
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 (
id INTEGER NOT NULL,
item_id VARCHAR(32),
@ -434,7 +427,6 @@ def update_database():
item.models.Find,
item.models.Item,
item.models.Sort,
item.models.Transfer,
item.models.user_items,
item.person.Person,
user.models.List,

View file

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