Revision f6c0005f tools/lib/migrate.py
b/tools/lib/migrate.py | ||
---|---|---|
34 | 34 |
# or implied, of GRNET S.A. |
35 | 35 |
|
36 | 36 |
from sqlalchemy import create_engine |
37 |
from sqlalchemy import Table, MetaData |
|
37 |
from sqlalchemy import Table, Column, String, MetaData |
|
38 |
from sqlalchemy.sql import select |
|
38 | 39 |
|
39 | 40 |
from django.conf import settings |
40 | 41 |
|
... | ... | |
51 | 52 |
self.backend = ModularBackend(*options) |
52 | 53 |
|
53 | 54 |
def execute(self): |
54 |
pass |
|
55 |
pass |
|
56 |
|
|
57 |
class Cache(): |
|
58 |
def __init__(self, db): |
|
59 |
self.engine = create_engine(db) |
|
60 |
metadata = MetaData(self.engine) |
|
61 |
|
|
62 |
columns=[] |
|
63 |
columns.append(Column('path', String(2048), primary_key=True)) |
|
64 |
columns.append(Column('hash', String(255))) |
|
65 |
self.files = Table('files', metadata, *columns) |
|
66 |
self.conn = self.engine.connect() |
|
67 |
self.engine.echo = True |
|
68 |
metadata.create_all(self.engine) |
|
69 |
|
|
70 |
def put(self, path, hash): |
|
71 |
# Insert or replace. |
|
72 |
s = self.files.delete().where(self.files.c.path==path) |
|
73 |
r = self.conn.execute(s) |
|
74 |
r.close() |
|
75 |
s = self.files.insert() |
|
76 |
r = self.conn.execute(s, {'path': path, 'hash': hash}) |
|
77 |
r.close() |
|
78 |
|
|
79 |
def get(self, path): |
|
80 |
s = select([self.files.c.hash], self.files.c.path == path) |
|
81 |
r = self.conn.execute(s) |
|
82 |
l = r.fetchone() |
|
83 |
r.close() |
|
84 |
if not l: |
|
85 |
return l |
|
86 |
return l[0] |
Also available in: Unified diff