Rename the tables in the databases, by prefixing the app name. This is
in preparation for merging the two databases and the two Django
projects into one.

Note that this renames *all* standard Autotest DB tables in both the
autotest_web and tko databases.  If you have scripts written directly
against these databases, *they will break*.  If your scripts access
the RPC interfaces, they should continue to work.

Another patch will be along within the next few weeks to actually move
the TKO tables into the autotest_web database.

From: James Ren <jamesren@google.com>
Signed-off-by: Steve Howard <showard@google.com>

Rename the tables in the databases, by prefixing the app name. This is
in preparation for merging the two databases and the two Django projects
into one.

Note that this renames *all* standard Autotest DB tables in both the autotest_web and tko databases.  If you have scripts written directly against these databases, *they will break*.  If your scripts access the RPC interfaces, they should continue to work.

From: James Ren <jamesren@google.com>
Signed-off-by: Steve Howard <showard@google.com>


git-svn-id: http://test.kernel.org/svn/autotest/trunk@4040 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/frontend/afe/models.py b/frontend/afe/models.py
index 6486300..9af822d 100644
--- a/frontend/afe/models.py
+++ b/frontend/afe/models.py
@@ -64,7 +64,7 @@
 
 
     class Meta:
-        db_table = 'atomic_groups'
+        db_table = 'afe_atomic_groups'
 
 
     def __unicode__(self):
@@ -111,7 +111,7 @@
 
 
     class Meta:
-        db_table = 'labels'
+        db_table = 'afe_labels'
 
     def __unicode__(self):
         return unicode(self.name)
@@ -162,7 +162,7 @@
 
 
     class Meta:
-        db_table = 'users'
+        db_table = 'afe_users'
 
     def __unicode__(self):
         return unicode(self.login)
@@ -191,7 +191,8 @@
                        string_values=True)
 
     hostname = dbmodels.CharField(max_length=255, unique=True)
-    labels = dbmodels.ManyToManyField(Label, blank=True)
+    labels = dbmodels.ManyToManyField(Label, blank=True,
+                                      db_table='afe_hosts_labels')
     locked = dbmodels.BooleanField(default=False)
     synch_id = dbmodels.IntegerField(blank=True, null=True,
                                      editable=settings.FULL_ADMIN)
@@ -342,7 +343,7 @@
 
 
     class Meta:
-        db_table = 'hosts'
+        db_table = 'afe_hosts'
 
     def __unicode__(self):
         return unicode(self.hostname)
@@ -357,7 +358,7 @@
     objects = model_logic.ExtendedManager()
 
     class Meta:
-        db_table = 'host_attributes'
+        db_table = 'afe_host_attributes'
 
 
 class Test(dbmodels.Model, model_logic.ModelExtensions):
@@ -400,14 +401,16 @@
     test_type = dbmodels.SmallIntegerField(choices=Types.choices())
     sync_count = dbmodels.IntegerField(default=1)
     path = dbmodels.CharField(max_length=255, unique=True)
-    dependency_labels = dbmodels.ManyToManyField(Label, blank=True)
 
+    dependency_labels = (
+        dbmodels.ManyToManyField(Label, blank=True,
+                                 db_table='afe_autotests_dependency_labels'))
     name_field = 'name'
     objects = model_logic.ExtendedManager()
 
 
     class Meta:
-        db_table = 'autotests'
+        db_table = 'afe_autotests'
 
     def __unicode__(self):
         return unicode(self.name)
@@ -430,7 +433,7 @@
 
 
     class Meta:
-        db_table = 'profilers'
+        db_table = 'afe_profilers'
 
     def __unicode__(self):
         return unicode(self.name)
@@ -446,8 +449,10 @@
     """
     name = dbmodels.CharField(max_length=255, unique=True)
     description = dbmodels.CharField(max_length=255, blank=True)
-    users = dbmodels.ManyToManyField(User, blank=False)
-    hosts = dbmodels.ManyToManyField(Host, blank=True)
+    users = dbmodels.ManyToManyField(User, blank=False,
+                                     db_table='afe_acl_groups_users')
+    hosts = dbmodels.ManyToManyField(Host, blank=True,
+                                     db_table='afe_acl_groups_hosts')
 
     name_field = 'name'
     objects = model_logic.ExtendedManager()
@@ -566,7 +571,7 @@
 
 
     class Meta:
-        db_table = 'acl_groups'
+        db_table = 'afe_acl_groups'
 
     def __unicode__(self):
         return unicode(self.name)
@@ -585,7 +590,7 @@
         cursor = connection.cursor()
         cursor.execute("""
             SELECT job_id, status, aborted, complete, COUNT(*)
-            FROM host_queue_entries
+            FROM afe_host_queue_entries
             WHERE job_id IN %s
             GROUP BY job_id, status, aborted, complete
             """ % id_list)
@@ -646,7 +651,9 @@
     timeout = dbmodels.IntegerField(default=DEFAULT_TIMEOUT)
     run_verify = dbmodels.BooleanField(default=True)
     email_list = dbmodels.CharField(max_length=250, blank=True)
-    dependency_labels = dbmodels.ManyToManyField(Label, blank=True)
+    dependency_labels = (
+            dbmodels.ManyToManyField(Label, blank=True,
+                                     db_table='afe_jobs_dependency_labels'))
     reboot_before = dbmodels.SmallIntegerField(choices=RebootBefore.choices(),
                                                blank=True,
                                                default=DEFAULT_REBOOT_BEFORE)
@@ -737,7 +744,7 @@
 
 
     class Meta:
-        db_table = 'jobs'
+        db_table = 'afe_jobs'
 
     def __unicode__(self):
         return u'%s (%s-%s)' % (self.name, self.id, self.owner)
@@ -750,13 +757,13 @@
     objects = model_logic.ExtendedManager()
 
     class Meta:
-        db_table = 'ineligible_host_queues'
+        db_table = 'afe_ineligible_host_queues'
 
 
 class HostQueueEntry(dbmodels.Model, model_logic.ModelExtensions):
     Status = host_queue_entry_states.Status
     ACTIVE_STATUSES = host_queue_entry_states.ACTIVE_STATUSES
-    COMPLETE_STATUSES = host_queue_entry_states.COMPLETE_STATUSES 
+    COMPLETE_STATUSES = host_queue_entry_states.COMPLETE_STATUSES
 
     job = dbmodels.ForeignKey(Job)
     host = dbmodels.ForeignKey(Host, blank=True, null=True)
@@ -871,7 +878,7 @@
 
 
     class Meta:
-        db_table = 'host_queue_entries'
+        db_table = 'afe_host_queue_entries'
 
 
 
@@ -895,7 +902,7 @@
         super(AbortedHostQueueEntry, self).save(*args, **kwargs)
 
     class Meta:
-        db_table = 'aborted_host_queue_entries'
+        db_table = 'afe_aborted_host_queue_entries'
 
 
 class RecurringRun(dbmodels.Model, model_logic.ModelExtensions):
@@ -917,7 +924,7 @@
     objects = model_logic.ExtendedManager()
 
     class Meta:
-        db_table = 'recurring_run'
+        db_table = 'afe_recurring_run'
 
     def __unicode__(self):
         return u'RecurringRun(job %s, start %s, period %s, count %s)' % (
@@ -1027,7 +1034,7 @@
 
 
     class Meta:
-        db_table = 'special_tasks'
+        db_table = 'afe_special_tasks'
 
 
     def __unicode__(self):
diff --git a/frontend/afe/rpc_utils.py b/frontend/afe/rpc_utils.py
index 26555d9..8b993a8 100644
--- a/frontend/afe/rpc_utils.py
+++ b/frontend/afe/rpc_utils.py
@@ -100,9 +100,11 @@
                 (running and finished)), ('Cannot specify more than one '
                                           'filter to this function')
 
-    not_queued = ('(SELECT job_id FROM host_queue_entries WHERE status != "%s")'
+    not_queued = ('(SELECT job_id FROM afe_host_queue_entries '
+                  'WHERE status != "%s")'
                   % models.HostQueueEntry.Status.QUEUED)
-    not_finished = '(SELECT job_id FROM host_queue_entries WHERE not complete)'
+    not_finished = ('(SELECT job_id FROM afe_host_queue_entries '
+                    'WHERE not complete)')
 
     if not_yet_run:
         where = ['id NOT IN ' + not_queued]
@@ -121,7 +123,7 @@
     labels.
     """
     extra_args = {}
-    where_str = ('hosts.id in (select host_id from hosts_labels '
+    where_str = ('afe_hosts.id in (select host_id from afe_hosts_labels '
                  'where label_id=%s)')
     extra_args['where'] = [where_str] * len(multiple_labels)
     extra_args['params'] = [models.Label.smart_get(label).id
@@ -144,8 +146,8 @@
                     str(label['id'])
                     for label in only_if_needed_labels.values('id'))
             query = models.Host.objects.add_join(
-                query, 'hosts_labels', join_key='host_id',
-                join_condition=('hosts_labels_exclude_OIN.label_id IN (%s)'
+                query, 'afe_hosts_labels', join_key='host_id',
+                join_condition=('afe_hosts_labels_exclude_OIN.label_id IN (%s)'
                                 % only_if_needed_ids),
                 suffix='_exclude_OIN', exclude=True)
 
@@ -157,9 +159,10 @@
                     str(atomic_group['id'])
                     for atomic_group in atomic_group_labels.values('id'))
             query = models.Host.objects.add_join(
-                    query, 'hosts_labels', join_key='host_id',
-                    join_condition=('hosts_labels_exclude_AG.label_id IN (%s)'
-                                    % atomic_group_label_ids),
+                    query, 'afe_hosts_labels', join_key='host_id',
+                    join_condition=(
+                            'afe_hosts_labels_exclude_AG.label_id IN (%s)'
+                            % atomic_group_label_ids),
                     suffix='_exclude_AG', exclude=True)
 
     assert 'extra_args' not in filter_data
diff --git a/frontend/client/src/autotest/public/EmbeddedTkoClientTest.html b/frontend/client/src/autotest/public/EmbeddedTkoClientTest.html
index 097bd7a..0acea9b 100644
--- a/frontend/client/src/autotest/public/EmbeddedTkoClientTest.html
+++ b/frontend/client/src/autotest/public/EmbeddedTkoClientTest.html
@@ -12,9 +12,9 @@
 
       var plot1 = Autotest.createMetricsPlot(document.getElementById("plot1_canvas"));
       queries = {}
-      queries["__main__"] = "SELECT test_name, AVG(IF(kernel LIKE '2.6.11%', iteration_value, NULL)) '2.6.11', STDDEV(IF(kernel LIKE '2.6.11%', iteration_value, NULL)) 'errors-2.6.11', AVG(IF(kernel LIKE '2.6.18%', iteration_value, NULL)) '2.6.18', STDDEV(IF(kernel LIKE '2.6.18%', iteration_value, NULL)) 'errors-2.6.18' FROM perf_view_2 WHERE test_idx < 1000 AND test_name IN ('dbench', 'tbench') AND iteration_key = 'throughput' AND (kernel LIKE '2.6.11%' OR kernel LIKE '2.6.18%') GROUP BY test_name";
-      queries["__2.6.11__"] = "SELECT test_idx, iteration_value FROM perf_view_2 WHERE test_idx < 1000 AND test_name IN ('dbench', 'tbench') AND iteration_key = 'throughput' AND kernel LIKE '2.6.11%%' AND test_name = %s ORDER BY iteration_value";
-      queries["__2.6.18__"] = "SELECT test_idx, iteration_value FROM perf_view_2 WHERE test_idx < 1000 AND test_name IN ('dbench', 'tbench') AND iteration_key = 'throughput' AND kernel LIKE '2.6.18%%' AND test_name = %s ORDER BY iteration_value";
+      queries["__main__"] = "SELECT test_name, AVG(IF(kernel LIKE '2.6.11%', iteration_value, NULL)) '2.6.11', STDDEV(IF(kernel LIKE '2.6.11%', iteration_value, NULL)) 'errors-2.6.11', AVG(IF(kernel LIKE '2.6.18%', iteration_value, NULL)) '2.6.18', STDDEV(IF(kernel LIKE '2.6.18%', iteration_value, NULL)) 'errors-2.6.18' FROM tko_perf_view_2 WHERE test_idx < 1000 AND test_name IN ('dbench', 'tbench') AND iteration_key = 'throughput' AND (kernel LIKE '2.6.11%' OR kernel LIKE '2.6.18%') GROUP BY test_name";
+      queries["__2.6.11__"] = "SELECT test_idx, iteration_value FROM tko_perf_view_2 WHERE test_idx < 1000 AND test_name IN ('dbench', 'tbench') AND iteration_key = 'throughput' AND kernel LIKE '2.6.11%%' AND test_name = %s ORDER BY iteration_value";
+      queries["__2.6.18__"] = "SELECT test_idx, iteration_value FROM tko_perf_view_2 WHERE test_idx < 1000 AND test_name IN ('dbench', 'tbench') AND iteration_key = 'throughput' AND kernel LIKE '2.6.18%%' AND test_name = %s ORDER BY iteration_value";
       plot1.refresh({
           plot : "Bar",
           invert : [],
@@ -23,8 +23,8 @@
 
       var plot2 = Autotest.createMetricsPlot(document.getElementById("plot2_canvas"));
       queries = {}
-      queries["__main__"] = "SELECT kernel, AVG(iteration_value) 'throughput', STDDEV(iteration_value) 'errors-throughput' FROM perf_view_2 WHERE test_idx < 1000 AND test_name = 'dbench' AND iteration_key ='throughput' GROUP BY kernel";
-      queries["__throughput__"] = "SELECT test_idx, iteration_value FROM perf_view_2 WHERE test_idx < 1000 AND test_name = 'dbench' AND iteration_key ='throughput' AND kernel = %s ORDER BY iteration_value";
+      queries["__main__"] = "SELECT kernel, AVG(iteration_value) 'throughput', STDDEV(iteration_value) 'errors-throughput' FROM tko_perf_view_2 WHERE test_idx < 1000 AND test_name = 'dbench' AND iteration_key ='throughput' GROUP BY kernel";
+      queries["__throughput__"] = "SELECT test_idx, iteration_value FROM tko_perf_view_2 WHERE test_idx < 1000 AND test_name = 'dbench' AND iteration_key ='throughput' AND kernel = %s ORDER BY iteration_value";
       plot2.refresh({
           plot : "Line",
           invert : [],
diff --git a/frontend/client/src/autotest/tko/DBColumnSelector.java b/frontend/client/src/autotest/tko/DBColumnSelector.java
index 58add13..bcfcfc5 100644
--- a/frontend/client/src/autotest/tko/DBColumnSelector.java
+++ b/frontend/client/src/autotest/tko/DBColumnSelector.java
@@ -5,8 +5,8 @@
 
 
 public class DBColumnSelector extends ExtendedListBox {
-    public static final String PERF_VIEW = "perf_view";
-    public static final String TEST_VIEW = "test_view";
+    public static final String PERF_VIEW = "tko_perf_view";
+    public static final String TEST_VIEW = "tko_test_view";
 
     public DBColumnSelector(String view) {
         this(view, false);
diff --git a/frontend/client/src/autotest/tko/MachineQualHistogramFrontend.java b/frontend/client/src/autotest/tko/MachineQualHistogramFrontend.java
index 9ce7d64..5d226cd 100644
--- a/frontend/client/src/autotest/tko/MachineQualHistogramFrontend.java
+++ b/frontend/client/src/autotest/tko/MachineQualHistogramFrontend.java
@@ -60,7 +60,7 @@
             sql.append(" AND ");
         }
         
-        sql.append("status = 'GOOD', test_idx, NULL)) 'good' FROM test_view_outer_joins");
+        sql.append("status = 'GOOD', test_idx, NULL)) 'good' FROM tko_test_view_outer_joins");
         if (hasGFilter) {
             sql.append(" WHERE ");
             sql.append(gFilterString);
diff --git a/frontend/client/src/autotest/tko/MetricsPlotFrontend.java b/frontend/client/src/autotest/tko/MetricsPlotFrontend.java
index 75cad3c..cd4cd51 100644
--- a/frontend/client/src/autotest/tko/MetricsPlotFrontend.java
+++ b/frontend/client/src/autotest/tko/MetricsPlotFrontend.java
@@ -231,7 +231,7 @@
             addSeriesSelects(series, sql);
         }
         
-        sql.append(" FROM perf_view_2");
+        sql.append(" FROM tko_perf_view_2");
             
         String xFilterString = globalFilter.getFilterString();
         if (xFilterString.equals("")) {
@@ -261,7 +261,7 @@
         
         sql.append("SELECT test_idx, ");
         sql.append(valueSelector.getSelectedValue());
-        sql.append(" FROM perf_view_2 WHERE ");
+        sql.append(" FROM tko_perf_view_2 WHERE ");
         
         String seriesFilter = series.getFilterString();
         if (!xFilterString.equals("") || !seriesFilter.equals("")) {
@@ -296,13 +296,13 @@
         
         sql.append(", ");
         sql.append(series.getAggregation());
-        sql.append(ifClause);
+        sql.append(ifClause.toString());
         sql.append(") '");
         sql.append(series.getName());
         sql.append("'");
         if (series.wantErrorBars()) {
             sql.append(", STDDEV(");
-            sql.append(ifClause);
+            sql.append(ifClause.toString());
             sql.append(") 'errors-");
             sql.append(series.getName());
             sql.append("'");
diff --git a/frontend/make_superuser.py b/frontend/make_superuser.py
index d04c7cb..1d20578 100755
--- a/frontend/make_superuser.py
+++ b/frontend/make_superuser.py
@@ -24,20 +24,20 @@
 for username in sys.argv[1:]:
     cur.execute("""
         SELECT access_level
-        FROM users
+        FROM afe_users
         WHERE login = %s""", username)
     row = cur.fetchone()
 
     if row is None:
         print "User %s does not exist. Creating..." % username
         cur.execute("""
-            INSERT INTO users (login, access_level)
+            INSERT INTO afe_users (login, access_level)
             VALUES (%s, 100)""", username)
         print "    Done"
     else:
         print "Updating user %s..." % username
         cur.execute("""
-            UPDATE users
+            UPDATE afe_users
             SET access_level = 100
             WHERE login = %s""", username)
         if (cur.rowcount == 1):
diff --git a/frontend/migrations/044_rename_afe_tables.py b/frontend/migrations/044_rename_afe_tables.py
new file mode 100644
index 0000000..3c01ba4
--- /dev/null
+++ b/frontend/migrations/044_rename_afe_tables.py
@@ -0,0 +1,37 @@
+import common
+from autotest_lib.database import db_utils
+
+
+ORIG_NAMES = (
+        'aborted_host_queue_entries',
+        'acl_groups',
+        'acl_groups_hosts',
+        'acl_groups_users',
+        'atomic_groups',
+        'autotests',
+        'autotests_dependency_labels',
+        'host_attributes',
+        'host_queue_entries',
+        'hosts',
+        'hosts_labels',
+        'ineligible_host_queues',
+        'jobs',
+        'jobs_dependency_labels',
+        'labels',
+        'profilers',
+        'recurring_run',
+        'special_tasks',
+        'users',
+        )
+
+RENAMES_UP = dict((name, 'afe_' + name) for name in ORIG_NAMES)
+
+RENAMES_DOWN = dict((value, key) for key, value in RENAMES_UP.iteritems())
+
+
+def migrate_up(manager):
+    db_utils.rename(manager, RENAMES_UP)
+
+
+def migrate_down(manager):
+    db_utils.rename(manager, RENAMES_DOWN)
diff --git a/frontend/migrations/common.py b/frontend/migrations/common.py
new file mode 100644
index 0000000..4c8760b
--- /dev/null
+++ b/frontend/migrations/common.py
@@ -0,0 +1,8 @@
+import os, sys
+dirname = os.path.dirname(sys.modules[__name__].__file__)
+autotest_dir = os.path.abspath(os.path.join(dirname, "..", '..'))
+client_dir = os.path.join(autotest_dir, "client")
+sys.path.insert(0, client_dir)
+import setup_modules
+sys.path.pop(0)
+setup_modules.setup(base_path=autotest_dir, root_module_name="autotest_lib")