Upgrade to the 0.7 series of SQLAlchemy.

This commit is contained in:
Logan Hanks
2011-12-15 11:52:13 -08:00
parent 626bf7d2e4
commit 59421ba17f
7 changed files with 26 additions and 25 deletions

View File

@@ -25,7 +25,7 @@ from copy import deepcopy
import random
import sqlalchemy as sa
from sqlalchemy.databases import postgres
from sqlalchemy.dialects import postgres
from r2.lib.utils import storage, storify, iters, Results, tup, TransSet
import operators
@@ -39,8 +39,6 @@ max_val_len = 1000
transactions = TransSet()
BigInteger = postgres.PGBigInteger
MAX_THING_ID = 9223372036854775807 # http://www.postgresql.org/docs/8.3/static/datatype-numeric.html
def make_metadata(engine):
@@ -124,7 +122,7 @@ def get_rel_type_table(metadata):
def get_thing_table(metadata, name):
table = sa.Table(g.db_app_name + '_thing_' + name, metadata,
sa.Column('thing_id', BigInteger, primary_key = True),
sa.Column('thing_id', sa.BigInteger, primary_key = True),
sa.Column('ups', sa.Integer, default = 0, nullable = False),
sa.Column('downs',
sa.Integer,
@@ -146,7 +144,7 @@ def get_thing_table(metadata, name):
def get_data_table(metadata, name):
data_table = sa.Table(g.db_app_name + '_data_' + name, metadata,
sa.Column('thing_id', BigInteger, nullable = False,
sa.Column('thing_id', sa.BigInteger, nullable = False,
primary_key = True),
sa.Column('key', sa.String, nullable = False,
primary_key = True),
@@ -156,9 +154,9 @@ def get_data_table(metadata, name):
def get_rel_table(metadata, name):
rel_table = sa.Table(g.db_app_name + '_rel_' + name, metadata,
sa.Column('rel_id', BigInteger, primary_key = True),
sa.Column('thing1_id', BigInteger, nullable = False),
sa.Column('thing2_id', BigInteger, nullable = False),
sa.Column('rel_id', sa.BigInteger, primary_key = True),
sa.Column('thing1_id', sa.BigInteger, nullable = False),
sa.Column('thing2_id', sa.BigInteger, nullable = False),
sa.Column('name', sa.String, nullable = False),
sa.Column('date', sa.DateTime(timezone = True),
default = sa.func.now(), nullable = False),
@@ -371,7 +369,7 @@ def make_thing(type_id, ups, downs, date, deleted, spam, id=None):
id = do_insert(table)
params['thing_id'] = id
return id
except sa.exceptions.SQLError, e:
except sa.exc.SQLError, e:
if not 'IntegrityError' in e.message:
raise
# wrap the error to prevent db layer bleeding out
@@ -419,7 +417,7 @@ def make_relation(rel_type_id, thing1_id, thing2_id, name, date=None):
name = name,
date = date)
return r.last_inserted_ids()[0]
except sa.exceptions.SQLError, e:
except sa.exc.SQLError, e:
if not 'IntegrityError' in e.message:
raise
# wrap the error to prevent db layer bleeding out
@@ -482,7 +480,7 @@ def set_data(table, type_id, thing_id, **vals):
i = table.insert(values = dict(thing_id = thing_id))
u = table.update(sa.and_(table.c.thing_id == thing_id,
table.c.key == sa.bindparam('key')))
table.c.key == sa.bindparam('_key')))
inserts = []
for key, val in vals.iteritems():
@@ -490,7 +488,7 @@ def set_data(table, type_id, thing_id, **vals):
#TODO one update?
if key in keys:
u.execute(key = key, value = val, kind = kind)
u.execute(_key = key, value = val, kind = kind)
else:
inserts.append({'key':key, 'value':val, 'kind': kind})
@@ -684,7 +682,7 @@ def add_sort(sort, t_table, select):
if k and orig_col.startswith(k):
table = t_table[k]
col = orig_col[len(k):]
if not table:
if table is None:
table = t_table[None]
else:
table = t_table

View File

@@ -182,7 +182,7 @@ class HardCacheBackend(object):
self.profile_stop(prof)
return value
except sa.exceptions.IntegrityError, e:
except sa.exc.IntegrityError, e:
self.profile_stop(prof)
return self.get(category, ids, force_write_table=True)

View File

@@ -40,7 +40,11 @@ def get_engine(name, db_host='', db_user='', db_pass='', db_port='5432',
return sa.create_engine('postgres://%s/%s' % (host, name),
strategy='threadlocal',
pool_size = int(pool_size),
max_overflow = int(max_overflow))
max_overflow = int(max_overflow),
# our code isn't ready for unicode to appear
# in place of strings yet
use_native_unicode=False,
)
class db_manager:
def __init__(self):

View File

@@ -20,13 +20,12 @@
# CondeNet, Inc. All Rights Reserved.
################################################################################
from sqlalchemy import Column, String, DateTime, Date, Float, Integer, Boolean,\
func as safunc, and_, or_
from sqlalchemy.exceptions import IntegrityError
BigInteger, func as safunc, and_, or_
from sqlalchemy.exc import IntegrityError
from sqlalchemy.schema import PrimaryKeyConstraint
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.databases.postgres import PGBigInteger as BigInteger, \
PGInet as Inet
from sqlalchemy.dialects.postgresql.base import PGInet as Inet
from sqlalchemy.ext.declarative import declarative_base
from pylons import g
from r2.lib.utils import Enum

View File

@@ -25,7 +25,7 @@ from r2.lib.db.tdb_sql import make_metadata, index_str, create_table
from pylons import g, c
from datetime import datetime
import sqlalchemy as sa
from sqlalchemy.exceptions import IntegrityError
from sqlalchemy.exc import IntegrityError
from xml.dom.minidom import Document
from r2.lib.utils import tup, randstr

View File

@@ -23,7 +23,7 @@ import sha, datetime
from email.MIMEText import MIMEText
import sqlalchemy as sa
from sqlalchemy.databases.postgres import PGInet, PGBigInteger
from sqlalchemy.dialects.postgresql.base import PGInet
from r2.lib.db.tdb_sql import make_metadata, index_str, create_table
from r2.lib.utils import Storage, timeago, Enum, tup
@@ -42,7 +42,7 @@ def mail_queue(metadata):
sa.Column("msg_hash", sa.String),
# the id of the account who started it
sa.Column('account_id', PGBigInteger),
sa.Column('account_id', sa.BigInteger),
# the name (not email) for the from
sa.Column('from_name', sa.String),
@@ -81,7 +81,7 @@ def sent_mail_table(metadata, name = 'sent_mail'):
sa.Column('msg_hash', sa.String, primary_key=True),
# the account who started it
sa.Column('account_id', PGBigInteger),
sa.Column('account_id', sa.BigInteger),
# the "To" address of the email
sa.Column('to_addr', sa.String),
@@ -175,7 +175,7 @@ class EmailHandler(object):
has_opted_out(email, _update = True)
opt_count(_update = True)
return (email, True)
except sa.exceptions.SQLError:
except sa.exc.SQLError:
return (email, False)
return (None, False)

View File

@@ -69,7 +69,7 @@ setup(
"pycrypto",
"Babel>=0.9.1",
"cython>=0.14",
"SQLAlchemy==0.5.3",
"SQLAlchemy==0.7.4",
"BeautifulSoup",
"cssutils==0.9.5.1",
"chardet",