[LNT] r311678 - Share add_column() function between db and db.migrations; NFC

Matthias Braun via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 24 10:34:04 PDT 2017


Author: matze
Date: Thu Aug 24 10:34:04 2017
New Revision: 311678

URL: http://llvm.org/viewvc/llvm-project?rev=311678&view=rev
Log:
Share add_column() function between db and db.migrations; NFC

Modified:
    lnt/trunk/lnt/server/db/migrations/upgrade_3_to_4.py
    lnt/trunk/lnt/server/db/migrations/upgrade_4_to_5.py
    lnt/trunk/lnt/server/db/migrations/upgrade_5_to_6.py
    lnt/trunk/lnt/server/db/migrations/upgrade_6_to_7.py
    lnt/trunk/lnt/server/db/migrations/upgrade_8_to_9.py
    lnt/trunk/lnt/server/db/migrations/upgrade_9_to_10.py
    lnt/trunk/lnt/server/db/migrations/util.py
    lnt/trunk/lnt/server/db/testsuite.py
    lnt/trunk/lnt/server/db/util.py

Modified: lnt/trunk/lnt/server/db/migrations/upgrade_3_to_4.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/db/migrations/upgrade_3_to_4.py?rev=311678&r1=311677&r2=311678&view=diff
==============================================================================
--- lnt/trunk/lnt/server/db/migrations/upgrade_3_to_4.py (original)
+++ lnt/trunk/lnt/server/db/migrations/upgrade_3_to_4.py Thu Aug 24 10:34:04 2017
@@ -2,11 +2,10 @@
 
 from sqlalchemy import Column, Integer
 
-from lnt.server.db.migrations.util import introspect_table, add_column
+from lnt.server.db.migrations.util import introspect_table
+from lnt.server.db.util import add_column
 
 
 def upgrade(engine):
-    test_suite_sample_fields = introspect_table(engine,
-                                                'TestSuiteSampleFields')
     bigger_is_better = Column('bigger_is_better', Integer, default=0)
-    add_column(engine, test_suite_sample_fields, bigger_is_better)
+    add_column(engine, 'TestSuiteSampleFields', bigger_is_better)

Modified: lnt/trunk/lnt/server/db/migrations/upgrade_4_to_5.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/db/migrations/upgrade_4_to_5.py?rev=311678&r1=311677&r2=311678&view=diff
==============================================================================
--- lnt/trunk/lnt/server/db/migrations/upgrade_4_to_5.py (original)
+++ lnt/trunk/lnt/server/db/migrations/upgrade_4_to_5.py Thu Aug 24 10:34:04 2017
@@ -6,7 +6,8 @@ from sqlalchemy import update, Column, F
 from sqlalchemy.orm import sessionmaker
 
 import lnt.server.db.migrations.upgrade_0_to_1 as upgrade_0_to_1
-from lnt.server.db.migrations.util import add_column, introspect_table
+from lnt.server.db.migrations.util import introspect_table
+from lnt.server.db.util import add_column
 
 
 def upgrade(engine):
@@ -34,9 +35,6 @@ def upgrade(engine):
 
     with engine.begin() as trans:
         trans.execute(set_scores)
-
-    # Give the NT table a score column.
-
-    nt_sample = introspect_table(engine, 'NT_Sample')
-    score = Column('score', Float)
-    add_column(engine, nt_sample, score)
+        # Give the NT table a score column.
+        score = Column('score', Float)
+        add_column(trans, 'NT_Sample', score)

Modified: lnt/trunk/lnt/server/db/migrations/upgrade_5_to_6.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/db/migrations/upgrade_5_to_6.py?rev=311678&r1=311677&r2=311678&view=diff
==============================================================================
--- lnt/trunk/lnt/server/db/migrations/upgrade_5_to_6.py (original)
+++ lnt/trunk/lnt/server/db/migrations/upgrade_5_to_6.py Thu Aug 24 10:34:04 2017
@@ -2,7 +2,8 @@
 
 import sqlalchemy
 from sqlalchemy import *
-from lnt.server.db.migrations.util import add_column, introspect_table
+from lnt.server.db.migrations.util import introspect_table
+from lnt.server.db.util import add_column
 
 ###
 # Upgrade TestSuite
@@ -38,7 +39,5 @@ def upgrade(engine):
     # upgrade_3_to_4.py added this column, so it is not in the ORM.
     with engine.begin() as trans:
         trans.execute(set_mem)
-
-    nt_sample = introspect_table(engine, 'NT_Sample')
-    mem_bytes = Column('mem_bytes', Float)
-    add_column(engine, nt_sample, mem_bytes)
+        mem_bytes = Column('mem_bytes', Float)
+        add_column(trans, 'NT_Sample', mem_bytes)

Modified: lnt/trunk/lnt/server/db/migrations/upgrade_6_to_7.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/db/migrations/upgrade_6_to_7.py?rev=311678&r1=311677&r2=311678&view=diff
==============================================================================
--- lnt/trunk/lnt/server/db/migrations/upgrade_6_to_7.py (original)
+++ lnt/trunk/lnt/server/db/migrations/upgrade_6_to_7.py Thu Aug 24 10:34:04 2017
@@ -11,7 +11,8 @@ import sqlalchemy
 from lnt.server.db.migrations.upgrade_0_to_1 \
   import SampleType, TestSuite, SampleField
 
-from lnt.server.db.migrations.util import add_column, introspect_table
+from lnt.server.db.migrations.util import introspect_table
+from lnt.server.db.util import add_column
 
 
 def upgrade(engine):
@@ -37,11 +38,10 @@ def upgrade(engine):
     ts.sample_fields.append(hash_status_field)
     ts.sample_fields.append(hash_field)
     session.add(ts)
-    session.commit()
-    session.close()
 
-    nt_sample = introspect_table(engine, 'NT_Sample')
     hash_status = sqlalchemy.Column('hash_status', sqlalchemy.Integer)
     hash_string = sqlalchemy.Column('hash', sqlalchemy.String(32))
-    add_column(engine, nt_sample, hash_status)
-    add_column(engine, nt_sample, hash_string)
+    add_column(session, 'NT_Sample', hash_status)
+    add_column(session, 'NT_Sample', hash_string)
+    session.commit()
+    session.close()

Modified: lnt/trunk/lnt/server/db/migrations/upgrade_8_to_9.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/db/migrations/upgrade_8_to_9.py?rev=311678&r1=311677&r2=311678&view=diff
==============================================================================
--- lnt/trunk/lnt/server/db/migrations/upgrade_8_to_9.py (original)
+++ lnt/trunk/lnt/server/db/migrations/upgrade_8_to_9.py Thu Aug 24 10:34:04 2017
@@ -9,7 +9,8 @@ from sqlalchemy.orm import relation
 # change the actual schema, but rather adds functionality vis-a-vis orders.
 import lnt.server.db.migrations.upgrade_0_to_1 as upgrade_0_to_1
 import lnt.server.db.migrations.upgrade_2_to_3 as upgrade_2_to_3
-from lnt.server.db.migrations.util import add_column, introspect_table
+from lnt.server.db.migrations.util import introspect_table
+from lnt.server.db.util import add_column
 
 
 def add_profiles(test_suite):
@@ -46,15 +47,15 @@ def upgrade_testsuite(engine, name):
     # Add FieldChange to the test suite.
     Base = add_profiles(test_suite)
     Base.metadata.create_all(engine)
+    ts_sample_table_name = "{}_Sample".format(db_key_name)
+
+    profile_id = Column('ProfileID', Integer)
+    add_column(session, ts_sample_table_name, profile_id)
     # Commit changes (also closing all relevant transactions with
     # respect to Postgres like databases).
     session.commit()
     session.close()
 
-    ts_sample_table = introspect_table(engine, "{}_Sample".format(db_key_name))
-    profile_id = Column('ProfileID', Integer)
-    add_column(engine, ts_sample_table, profile_id)
-
 
 def upgrade(engine):
     # Create our FieldChangeField table and commit.

Modified: lnt/trunk/lnt/server/db/migrations/upgrade_9_to_10.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/db/migrations/upgrade_9_to_10.py?rev=311678&r1=311677&r2=311678&view=diff
==============================================================================
--- lnt/trunk/lnt/server/db/migrations/upgrade_9_to_10.py (original)
+++ lnt/trunk/lnt/server/db/migrations/upgrade_9_to_10.py Thu Aug 24 10:34:04 2017
@@ -10,7 +10,8 @@ from sqlalchemy import update, Column, F
 
 import lnt.server.db.migrations.upgrade_0_to_1 as upgrade_0_to_1
 
-from lnt.server.db.migrations.util import add_column, introspect_table
+from lnt.server.db.migrations.util import introspect_table
+from lnt.server.db.util import add_column
 
 
 def upgrade(engine):
@@ -38,7 +39,5 @@ def upgrade(engine):
 
     with engine.begin() as trans:
         trans.execute(update_code_size)
-
-    nt_sample = introspect_table(engine, 'NT_Sample')
-    code_size = Column('code_size', Float)
-    add_column(engine, nt_sample, code_size)
+        code_size = Column('code_size', Float)
+        add_column(trans, 'NT_Sample', code_size)

Modified: lnt/trunk/lnt/server/db/migrations/util.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/db/migrations/util.py?rev=311678&r1=311677&r2=311678&view=diff
==============================================================================
--- lnt/trunk/lnt/server/db/migrations/util.py (original)
+++ lnt/trunk/lnt/server/db/migrations/util.py Thu Aug 24 10:34:04 2017
@@ -1,22 +1,8 @@
 import sqlalchemy
-from sqlalchemy import DDL
 from sqlalchemy.ext.compiler import compiles
 from sqlalchemy.schema import DDLElement
 
 
-class _AddColumn(DDLElement):
-    def __init__(self, table, column):
-        self.table = table
-        self.column = column
-
-
- at compiles(_AddColumn)
-def _visit_add_column(element, compiler, **kw):
-    return ("ALTER TABLE %s ADD COLUMN %s" %
-            (compiler.preparer.format_table(element.table),
-             compiler.get_column_specification(element.column)))
-
-
 class _RenameTable(DDLElement):
     def __init__(self, old_name, new_name):
         self.old_name = old_name
@@ -30,22 +16,6 @@ def _visite_rename_table(element, compil
              compiler.preparer.quote(element.new_name)))
 
 
-def add_column(engine, table, column):
-    # type: (sqlalchemy.engine.Engine, sqlalchemy.Table, sqlalchemy.Column)
-    #       -> None
-    """Add this column to the table.
-
-    This is a stopgap to a real migration system.  Inspect the Column pass
-    and try to issue an ALTER command to make the column.
-
-    :param engine: to execute on.
-    :param table: the Table to add the column to.
-    :param column: Column to add
-    """
-    add_column = _AddColumn(table, column)
-    add_column.execute(bind=engine)
-
-
 def introspect_table(engine, name, autoload=True):
     # type: (sqlalchemy.engine.Engine, str) -> sqlalchemy.Table
     """Create a SQLAlchemy Table from the table name in the current DB.

Modified: lnt/trunk/lnt/server/db/testsuite.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/db/testsuite.py?rev=311678&r1=311677&r2=311678&view=diff
==============================================================================
--- lnt/trunk/lnt/server/db/testsuite.py (original)
+++ lnt/trunk/lnt/server/db/testsuite.py Thu Aug 24 10:34:04 2017
@@ -332,8 +332,7 @@ def upgrade_to(engine, tsschema, new_sch
         elif not dry_run:
             # Add missing columns
             column = testsuitedb.make_sample_column(name, type)
-            util.add_sqlalchemy_column(engine, '%s_Sample' % ts_name,
-                                       column)
+            util.add_column(engine, '%s_Sample' % ts_name, column)
 
     if len(old_metrics) != 0:
         raise _MigrationError("Metrics removed: %s" %
@@ -360,7 +359,7 @@ def upgrade_to(engine, tsschema, new_sch
         # Add missing columns
         if old_field is None and not dry_run:
             column = testsuitedb.make_run_column(name)
-            util.add_sqlalchemy_column(engine, '%s_Run' % ts_name, column)
+            util.add_column(engine, '%s_Run' % ts_name, column)
 
     if len(old_run_fields) > 0:
         raise _MigrationError("Run fields removed: %s" %
@@ -380,8 +379,7 @@ def upgrade_to(engine, tsschema, new_sch
         # Add missing columns
         if old_field is None and not dry_run:
             column = testsuitedb.make_machine_column(name)
-            util.add_sqlalchemy_column(engine, '%s_Machine' % ts_name,
-                                       column)
+            util.add_column(engine, '%s_Machine' % ts_name, column)
 
     if len(old_machine_fields) > 0:
         raise _MigrationError("Machine fields removed: %s" %

Modified: lnt/trunk/lnt/server/db/util.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/db/util.py?rev=311678&r1=311677&r2=311678&view=diff
==============================================================================
--- lnt/trunk/lnt/server/db/util.py (original)
+++ lnt/trunk/lnt/server/db/util.py Thu Aug 24 10:34:04 2017
@@ -1,23 +1,38 @@
 import sqlalchemy
 import sqlalchemy.ext.compiler
 import re
+from sqlalchemy.schema import DDLElement
+from sqlalchemy.ext.compiler import compiles
 
 
 def path_has_no_database_type(path):
     return '://' not in path
 
 
-def _alter_table_statement(dialect, table_name, column):
-    """Given an SQLAlchemy Column object, create an `ALTER TABLE` statement
-    that adds the column to the existing table."""
-    # Code inspired by sqlalchemy.schema.CreateColumn documentation.
-    compiler = dialect.ddl_compiler(dialect, None)
-    text = "ALTER TABLE \"%s\" ADD COLUMN " % table_name
-    text += compiler.get_column_specification(column)
-    return text
-
-
-def add_sqlalchemy_column(engine, table_name, column):
-    statement = _alter_table_statement(engine.dialect, table_name, column)
-    with engine.begin() as trans:
-        trans.execute(statement)
+class _AddColumn(DDLElement):
+    def __init__(self, table_name, column):
+        self.table_name = table_name
+        self.column = column
+
+
+ at compiles(_AddColumn)
+def _visit_add_column(element, compiler, **kw):
+    return ("ALTER TABLE %s ADD COLUMN %s" %
+            (compiler.preparer.quote(element.table_name),
+             compiler.get_column_specification(element.column)))
+
+
+def add_column(connectable, table_name, column):
+    # type: (sqlalchemy.Connectable, sqlalchemy.Table, sqlalchemy.Column)
+    # -> None
+    """Add this column to the table named `table_name`.
+
+    This is a stopgap to a real migration system.  Inspect the Column pass
+    and try to issue an ALTER command to make the column.
+
+    :param connectable: to execute on.
+    :param table_name: name of table to add the column to.
+    :param column: Column to add
+    """
+    statement = _AddColumn(table_name, column)
+    statement.execute(bind=connectable)




More information about the llvm-commits mailing list