sqlite3-difftbl-check.py.in 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!@PYTHON@
  2. # Copyright (C) 2011 Internet Systems Consortium.
  3. #
  4. # Permission to use, copy, modify, and distribute this software for any
  5. # purpose with or without fee is hereby granted, provided that the above
  6. # copyright notice and this permission notice appear in all copies.
  7. #
  8. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  9. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  10. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  11. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  13. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  14. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  15. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. import os, sqlite3, sys
  17. from optparse import OptionParser
  18. usage = 'usage: %prog [options] db_file'
  19. parser = OptionParser(usage=usage)
  20. parser.add_option("-u", "--upgrade", action="store_true",
  21. dest="upgrade", default=False,
  22. help="Upgrade the database file [default: %default]")
  23. (options, args) = parser.parse_args()
  24. if len(args) == 0:
  25. parser.error('missing argument')
  26. db_file = args[0]
  27. # If the file doesn't exist, there's nothing to do
  28. if not os.path.exists(db_file):
  29. sys.exit(0)
  30. conn = sqlite3.connect(db_file)
  31. cur = conn.cursor()
  32. try:
  33. # This can be anything that works iff the "diffs" table exists
  34. cur.execute('SELECT name FROM diffs DESC LIMIT 1')
  35. except sqlite3.OperationalError as ex:
  36. # If it fails with 'no such table', create a new one or fail with
  37. # warning depending on the --upgrade command line option.
  38. if str(ex) == 'no such table: diffs':
  39. if options.upgrade:
  40. cur.execute('CREATE TABLE diffs (id INTEGER PRIMARY KEY, ' +
  41. 'zone_id INTEGER NOT NULL, ' +
  42. 'version INTEGER NOT NULL, ' +
  43. 'operation INTEGER NOT NULL, ' +
  44. 'name STRING NOT NULL COLLATE NOCASE, ' +
  45. 'rrtype STRING NOT NULL COLLATE NOCASE, ' +
  46. 'ttl INTEGER NOT NULL, rdata STRING NOT NULL)')
  47. else:
  48. sys.stdout.write('Found an older version of SQLite3 DB file: ' +
  49. db_file + '\n' + "Perform '" + os.getcwd() +
  50. "/sqlite3-difftbl-check.py --upgrade " +
  51. db_file + "'\n" +
  52. 'before continuing install.\n')
  53. sys.exit(1)
  54. conn.close()