Skip to content

Instantly share code, notes, and snippets.

@floatingatoll
Created October 26, 2011 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save floatingatoll/1318219 to your computer and use it in GitHub Desktop.
Save floatingatoll/1318219 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is Sync Server
#
# The Initial Developer of the Original Code is the Mozilla Foundation.
# Portions created by the Initial Developer are Copyright (C) 2011
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Toby Elliott (telliott@mozilla.com)
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
from optparse import OptionParser
import MySQLdb
import logging
def main(user, password, host, db, modulo=1, batch=10, dryrun=True):
table = 'wbo'
if modulo > 1:
tables = [table + "%0d" % modulo for modulo in
range(int(modulo))]
else:
tables = [table]
for table in tables:
affected = 0
affected = purge_ttl(host, db, user, password, table, batch, dryrun)
print "did %s (%s)" % (table, affected)
def purge_ttl(host, db, user, password, tablename, batch, dryrun=True):
sql = "delete from %s where ttl < (UNIX_TIMESTAMP() - (86400*28)) limit %s" % (tablename, batch)
try:
total = 0
affected = 1
conn = MySQLdb.connect (host = host, user = user,
passwd = password, db = db)
cursor = conn.cursor()
while affected > 0:
cursor.execute(sql)
affected = cursor.rowcount
total = total + affected
if dryrun:
conn.rollback()
else:
conn.commit()
print "%g removed from %s (total %g)" % (affected, tablename, total)
if dryrun:
affected = 0
conn.close()
return total
except MySQLdb.Error, e:
logging.error("Error %s: %s" % (e.args[0], e.args[1]))
return False
if __name__ == '__main__':
parser = OptionParser(usage="purge_ttl.py [options] db_name")
parser.add_option("--commit", action="store_false", dest="dryrun",
help="run, but don't alter data")
parser.add_option("-u", dest="db_user", help="username for db")
parser.add_option("-p", dest="db_pass", help="password for db")
parser.add_option("-s", dest="db_host", help="hostname for db")
parser.add_option("-m", dest="db_modulo", help="number of wbo tables in db")
parser.add_option("-b", dest="db_batch", help="number of rows to delete at a time")
options, args = parser.parse_args()
if len(args) < 1:
exit(parser.print_help())
if len(args) == 1:
args.append(None)
main(options.db_user,
options.db_pass,
options.db_host,
args[0],
options.db_modulo,
options.db_batch,
options.dryrun,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment