def update_005(): import web from infogami.infobase._dbstore.store import Store db = web.database(dbn="postgres", db="openlibrary", user=os.getenv("USER"), pw="") store = Store(db) for row in db.query("SELECT thing.key, thing.created, account.* FROM thing, account WHERE thing.id=account.thing_id"): username = row.key.split("/")[-1] account_key = "account/" + username if store.get(account_key): continue else: account = { "_key": account_key, "type": "account", "email": row.email, "enc_password": row.password, "username": username, "lusername": username.lower(), "bot": row.bot, "status": row.verified and "verified" or "pending", "created_on": row.created.isoformat(), } email_doc = { "_key": "account-email/" + row.email, "type": "account-email", "username": username } store.put_many([account, email_doc])
def test_indexer2(self): s = Store(db) s.indexer = BookIndexer() s.put("book", {"title": "The lord of the rings", "lang": "en"}) assert store.query("", "lang", "en") == [] assert store.query("", "title,lang", "The lord of the rings--en") == [{'key': 'book'}]
def test_indexer(self): s = Store(db) s.indexer = BookIndexer() s.put("book", {"title": "The lord of the rings", "lang": "en"}) assert store.query("", "lang", "en") == [] assert store.query("", "title,lang", "The lord of the rings--en") == [{'key': 'book'}]
def test_indexer(self): s = Store(db) s.put("foo", {"type": "account", "name": "foo", "bot": False, "age": 42, "d": {"x": 1}}) rows = db.query("SELECT name, value from store_index") d = dict((row.name, row.value) for row in rows) assert d == {"_key": "foo", "name": "foo", "bot": "false", "age": "42", "d.x": "1"}
def new_account(self, username, **kw): # backdoor to create new account db.insert("thing", key="/user/" + username) store = Store(db) store.put("account/" + username, dict(kw, type="account", status="active"))
def new_account(self, username, **kw): # backdoor to create new account db.insert("thing", key='/user/' + username) store = Store(db) store.put("account/" + username, dict(kw, type="account", status="active"))
def test_indexer(self): s = Store(db) s.put("foo", {"type": "account", "name": "foo", "bot": False, "age": 42, "d": {"x": 1}}) rows = db.query("SELECT name, value from store_index") d = dict((row.name, row.value) for row in rows) assert d == { "_key": "foo", "name": "foo", "bot": "false", "age": "42", "d.x": "1" }
def migrate_account_table(db): def get_status(row): if row.get("verified"): return "active" else: return "pending" with db.transaction(): store = Store(db) db.query("DECLARE longquery NO SCROLL CURSOR FOR SELECT thing.key, thing.created, account.* FROM thing, account WHERE thing.id=account.thing_id") while True: rows = db.query("FETCH FORWARD 100 FROM longquery").list() if not rows: break docs = [] for row in rows: # Handle bad rows in the thing table. if not row.key.startswith("/people/"): continue username = row.key.split("/")[-1] doc = { "_key": "account/" + username, "type": "account", "status": get_status(row), "created_on": row.created.isoformat(), "username": username, "lusername": username.lower(), "email": row.email, "enc_password": row.password, "bot": row.get("bot", False), } email_doc = { "_key": "account-email/" + row.email, "type": "account-email", "username": username } docs.append(doc) docs.append(email_doc) store.put_many(docs)
def migrate_account_table(db): def get_status(row): if row.get("verified"): return "active" else: return "pending" with db.transaction(): store = Store(db) db.query( "DECLARE longquery NO SCROLL CURSOR FOR SELECT thing.key, thing.created, account.* FROM thing, account WHERE thing.id=account.thing_id" ) while True: rows = db.query("FETCH FORWARD 100 FROM longquery").list() if not rows: break docs = [] for row in rows: # Handle bad rows in the thing table. if not row.key.startswith("/people/"): continue username = row.key.split("/")[-1] doc = { "_key": "account/" + username, "type": "account", "status": get_status(row), "created_on": row.created.isoformat(), "username": username, "lusername": username.lower(), "email": row.email, "enc_password": row.password, "bot": row.get("bot", False), } email_doc = { "_key": "account-email/" + row.email, "type": "account-email", "username": username } docs.append(doc) docs.append(email_doc) store.put_many(docs)
def test_typewise_indexer2(self): global db s = Store(db) s.indexer = TypewiseIndexer() s.indexer.set_indexer("book", BookIndexer()) s.put("book", {"type": "book", "title": "The lord of the rings", "lang": "en"}) s.put("one", {"type": "digit", "name": "one"}) s.put("foo", {"name": "foo"}) assert store.query("", "lang", "en") == [] assert store.query("book", "title,lang", "The lord of the rings--en") == [{"key": "book"}] assert store.query("digit", "name", "one") == [{"key": "one"}] assert store.query("", "name", "foo") == [{"key": "foo"}]
def test_typewise_indexer2(self): s = Store(db) s.indexer = TypewiseIndexer() s.indexer.set_indexer("book", BookIndexer()) s.put("book", {"type": "book", "title": "The lord of the rings", "lang": "en"}) s.put("one", {"type": "digit", "name": "one"}) s.put("foo", {"name": "foo"}) assert store.query("", "lang", "en") == [] assert store.query("book", "title,lang", "The lord of the rings--en") == [{"key": "book"}] assert store.query("digit", "name", "one") == [{"key": "one"}] assert store.query("", "name", "foo") == [{"key": "foo"}]
def update_005(): import web from infogami.infobase._dbstore.store import Store db = web.database(dbn="postgres", db="openlibrary", user=os.getenv("USER"), pw="") store = Store(db) for row in db.query( "SELECT thing.key, thing.created, account.* FROM thing, account WHERE thing.id=account.thing_id" ): username = row.key.split("/")[-1] account_key = "account/" + username if store.get(account_key): continue else: account = { "_key": account_key, "type": "account", "email": row.email, "enc_password": row.password, "username": username, "lusername": username.lower(), "bot": row.bot, "status": row.verified and "verified" or "pending", "created_on": row.created.isoformat(), } email_doc = { "_key": "account-email/" + row.email, "type": "account-email", "username": username } store.put_many([account, email_doc])
def setup_module(mod): utils.setup_db(mod) mod.store = Store(db)