def forwards(self): # Update content types to last actual state update_contenttypes(noc.sa.models, None) # Convert groups to tags ctype_id = db.execute( "SELECT id FROM django_content_type WHERE model='managedobject'" )[0][0] for category, entry_id in db.execute( "SELECT g.name,o.managedobject_id FROM sa_managedobject_groups o JOIN sa_objectgroup g ON (o.objectgroup_id=g.id)" ): if db.execute("SELECT COUNT(*) FROM tagging_tag WHERE name=%s", [category])[0][0] == 0: db.execute("INSERT INTO tagging_tag(name) VALUES(%s)", [category]) tag_id = db.execute("SELECT id FROM tagging_tag WHERE name=%s", [category])[0][0] db.execute( "INSERT INTO tagging_taggeditem(tag_id,content_type_id,object_id) VALUES(%s,%s,%s)", [tag_id, ctype_id, entry_id]) # Drop groups and fields for t in [ "sa_managedobject_groups", "sa_managedobjectselector_filter_groups", "sa_objectgroup" ]: db.drop_table(t)
def forwards(self, orm): table_names = connection.introspection.table_names() if self.old_table in table_names: if not self.new_table in table_names: db.rename_table(self.old_table, self.new_table) else: db.drop_table(self.old_table) # Adding field 'SubscriptionPlugin.assign_language' db.add_column(u'aldryn_mailchimp_subscriptionplugin', 'assign_language', self.gf('django.db.models.fields.BooleanField')(default=True), keep_default=False)
def forwards(self): mdb = get_db() segments = mdb.noc.networksegments cstate = mdb.noc.inv.networkchartstate msettings = mdb.noc.mapsettings for cid, name, description, selector_id in db.execute( "SELECT id, name, description, selector_id FROM inv_networkchart" ): logger.info("Migrating chart '%s'", name) # Create segment sid = ObjectId() segments.insert({ "_id": sid, "name": name, "description": description, "settings": {}, "selector": selector_id }) # Get state nodes = [] mx = 0.0 my = 0.0 for s in cstate.find({"chart": cid, "type": "mo"}): # object, state: {x, y, w, h} if "x" not in s["state"] or "y" not in s["state"]: continue x = float(s["state"]["x"]) y = float(s["state"]["y"]) mx = max(mx, x) my = max(my, y) nodes += [{ "type": "managedobject", "id": str(s["object"]), "x": x, "y": y }] if nodes: msettings.insert({ "segment": str(sid), "changed": datetime.datetime.now(), "user": "******", "nodes": nodes, "links": [], "width": mx + 70, "height": my + 70 }) # db.drop_table("inv_networkchart") cstate.drop()
def forwards(self, orm): table_names = connection.introspection.table_names() if self.old_table in table_names: if not self.new_table in table_names: db.rename_table(self.old_table, self.new_table) else: db.drop_table(self.old_table) # Adding field 'SubscriptionPlugin.assign_language' db.add_column( u"aldryn_mailchimp_subscriptionplugin", "assign_language", self.gf("django.db.models.fields.BooleanField")(default=True), keep_default=False, )
def test_create(self): """ Test creation and deletion of tables. """ cursor = connection.cursor() # It needs to take at least 2 args self.assertRaises(TypeError, db.create_table) self.assertRaises(TypeError, db.create_table, "test1") # Empty tables (i.e. no columns) are not fine, so make at least 1 db.create_table( "test1", [('email_confirmed', models.BooleanField(default=False))]) db.start_transaction() # And should exist cursor.execute("SELECT * FROM test1") # Make sure we can't do the same query on an empty table try: cursor.execute("SELECT * FROM nottheretest1") self.fail("Non-existent table could be selected!") except: pass # Clear the dirty transaction db.rollback_transaction() db.start_transaction() # Remove the table db.drop_table("test1") # Make sure it went try: cursor.execute("SELECT * FROM test1") self.fail("Just-deleted table could be selected!") except: pass # Clear the dirty transaction db.rollback_transaction() db.start_transaction() # Try deleting a nonexistent one try: db.delete_table("nottheretest1") self.fail("Non-existent table could be deleted!") except: pass db.rollback_transaction()
def test_create(self): """ Test creation and deletion of tables. """ cursor = connection.cursor() # It needs to take at least 2 args self.assertRaises(TypeError, db.create_table) self.assertRaises(TypeError, db.create_table, "test1") # Empty tables (i.e. no columns) are not fine, so make at least 1 db.create_table("test1", [('email_confirmed', models.BooleanField(default=False))]) db.start_transaction() # And should exist cursor.execute("SELECT * FROM test1") # Make sure we can't do the same query on an empty table try: cursor.execute("SELECT * FROM nottheretest1") self.fail("Non-existent table could be selected!") except: pass # Clear the dirty transaction db.rollback_transaction() db.start_transaction() # Remove the table db.drop_table("test1") # Make sure it went try: cursor.execute("SELECT * FROM test1") self.fail("Just-deleted table could be selected!") except: pass # Clear the dirty transaction db.rollback_transaction() db.start_transaction() # Try deleting a nonexistent one try: db.delete_table("nottheretest1") self.fail("Non-existent table could be deleted!") except: pass db.rollback_transaction()
def forwards(self): "Write your forwards migration here" db.execute("DROP INDEX cm_config_managed_object_id") db.execute( "CREATE UNIQUE INDEX cm_config_managed_object_id ON cm_config(managed_object_id)" ) db.delete_column("cm_objectnotify", "category_id") db.delete_column("cm_objectnotify", "location_id") for column in [ "activator_id", "profile_name", "scheme", "address", "port", "user", "password", "super_password", "remote_path", "trap_source_ip", "trap_community" ]: db.delete_column("cm_config", column) for table in ["cm_config", "cm_rpsl", "cm_dns", "cm_prefixlist"]: db.delete_column(table, "location_id") db.drop_table("%s_categories" % table) db.drop_table("cm_object_categories") db.drop_table("cm_objectaccess") db.execute("DELETE FROM cm_objectcategory") db.drop_table("cm_objectcategory") db.drop_table("cm_objectlocation")
def forwards(self): db.delete_column("sa_managedobjectselector", "filter_activator_id") db.drop_table("sa_activator") db.drop_table("sa_collector") db.drop_table("main_shard")
def forwards(self, orm): # Deleting model 'AbstractStockAlert' db.drop_table('partner_stockalert') db.rename_table('partner_abstractstockalert', 'partner_stockalert')
def forwards(self): for t in ["tagging_taggeditem", "tagging_tag"]: if db.execute("SELECT COUNT(*) FROM pg_class WHERE relname='%s'" % t)[0][0] == 1: db.drop_table(t)
def backwards(self, orm): db.drop_table("hub_accountbadge")
def forwards(self): def q(s): if s is None: return None s = s.strip() if not s or s == "None": return None if s and s.startswith("'") and s.endswith("'"): return s[1:-1] else: return s def iteritems(s, sep): last = None for l in s.splitlines(): if sep not in l: if last is not None: last += "\n" + l elif last: k = l.split(sep)[0] if self.rx_field.match(k): yield last.split(sep, 1) last = l else: last += "\n" + l else: last = l if last: yield last.split(sep, 1) delta = datetime.timedelta(days=5 * 365) user_cache = dict(db.execute("SELECT id, username FROM auth_user")) collection = get_db()["noc.audittrail"] left = db.execute("SELECT COUNT(*) FROM main_audittrail")[0][0] logger.info("Migration audit trail") last_id = 0 while True: bulk = collection.initialize_unordered_bulk_op() rc = 0 for a_id, user_id, timestamp, model, db_table, op, subject, body in db.execute( """ SELECT id, user_id, "timestamp", model, db_table, operation, subject, body FROM main_audittrail WHERE id > %s ORDER BY id LIMIT 1000 """, [last_id]): o = { "timestamp": timestamp, "user": user_cache[user_id], "model_id": "%s.%s" % (db_table.split("_")[0], model), "op": op, "expires": timestamp + delta } changes = [] if op == "C": # Parse create operation for k, v in iteritems(body, " = "): if k == "id": continue changes += [{"field": k, "old": None, "new": q(v)}] elif op == "M": # Parse modify operation for k, v in iteritems(body, ": "): if k == "id": o["id"] = q(v) continue X = v.split(" -> ", 1) if len(X) == 1: x, y = X[0], None else: x, y = X changes += [{"field": k, "old": q(x), "new": q(y)}] elif op == "D": # Parse delete operation for k, v in iteritems(body, " = "): if k == "id": o["id"] = q(v) continue changes += [{"field": k, "old": None, "new": v}] else: raise ValueError("Invalid op '%s'" % op) o["changes"] = changes rc += 1 bulk.insert(o) last_id = a_id left -= rc logger.info(" ... %d records left", left) if rc: bulk.execute({"w": 0}) else: break db.drop_table("main_audittrail")
def forwards(self): def q(s): if s is None: return None s = s.strip() if not s or s == "None": return None if s and s.startswith("'") and s.endswith("'"): return s[1:-1] else: return s def iteritems(s, sep): last = None for l in s.splitlines(): if sep not in l: if last is not None: last += "\n" + l elif last: k = l.split(sep)[0] if self.rx_field.match(k): yield last.split(sep, 1) last = l else: last += "\n" + l else: last = l if last: yield last.split(sep, 1) delta = datetime.timedelta(days=5 * 365) user_cache = dict(db.execute("SELECT id, username FROM auth_user")) collection = get_db()["noc.audittrail"] left = db.execute("SELECT COUNT(*) FROM main_audittrail")[0][0] logger.info("Migration audit trail") last_id = 0 while True: bulk = [] for a_id, user_id, timestamp, model, db_table, op, subject, body in db.execute( """ SELECT id, user_id, "timestamp", model, db_table, operation, subject, body FROM main_audittrail WHERE id > %s ORDER BY id LIMIT 1000 """, [last_id]): o = { "timestamp": timestamp, "user": user_cache[user_id], "model_id": "%s.%s" % (db_table.split("_")[0], model), "op": op, "expires": timestamp + delta } changes = [] if op == "C": # Parse create operation for k, v in iteritems(body, " = "): if k == "id": continue changes += [{"field": k, "old": None, "new": q(v)}] elif op == "M": # Parse modify operation for k, v in iteritems(body, ": "): if k == "id": o["id"] = q(v) continue X = v.split(" -> ", 1) if len(X) == 1: x, y = X[0], None else: x, y = X changes += [{"field": k, "old": q(x), "new": q(y)}] elif op == "D": # Parse delete operation for k, v in iteritems(body, " = "): if k == "id": o["id"] = q(v) continue changes += [{"field": k, "old": None, "new": v}] else: raise ValueError("Invalid op '%s'" % op) o["changes"] = changes bulk += [InsertOne(o)] last_id = a_id left -= len(bulk) logger.info(" ... %d records left", left) if bulk: logger.info("Commiting changes to database") try: r = collection.bulk_write(bulk) logger.info("Database has been synced") logger.info("Inserted: %d, Modify: %d, Deleted: %d", r.inserted_count + r.upserted_count, r.modified_count, r.deleted_count) except BulkWriteError as e: logger.error("Bulk write error: '%s'", e.details) logger.error("Stopping check") break else: break db.drop_table("main_audittrail")
def forwards(self): for t in ["sa_managedobject_groups", "sa_managedobjectselector_filter_groups", "sa_objectgroup"]: db.drop_table(t)
def backwards(self, orm): db.drop_table('hub_badge')
def forwards(self): db.delete_column("dns_dnszonerecord", "type_id") db.drop_table("dns_dnszonerecordtype")
def backwards(self, orm): db.drop_table('hub_contribution')
def forwards(self): db.drop_table("cm_config")
def forwards(self, orm): db.drop_table('neuroelectro_user') db.rename_table('auth_user','neuroelectro_user')