[LNT] r306362 - migrations/*.py: Rework session handling
Matthias Braun via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 26 21:11:33 PDT 2017
Author: matze
Date: Mon Jun 26 21:11:32 2017
New Revision: 306362
URL: http://llvm.org/viewvc/llvm-project?rev=306362&view=rev
Log:
migrations/*.py: Rework session handling
Use shorter lived sessions and make sure we `close()` them after each
upgrade step.
Modified:
lnt/trunk/lnt/server/db/migrations/upgrade_0_to_1.py
lnt/trunk/lnt/server/db/migrations/upgrade_10_to_11.py
lnt/trunk/lnt/server/db/migrations/upgrade_1_to_2.py
lnt/trunk/lnt/server/db/migrations/upgrade_2_to_3.py
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
Modified: lnt/trunk/lnt/server/db/migrations/upgrade_0_to_1.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/db/migrations/upgrade_0_to_1.py?rev=306362&r1=306361&r2=306362&view=diff
==============================================================================
--- lnt/trunk/lnt/server/db/migrations/upgrade_0_to_1.py (original)
+++ lnt/trunk/lnt/server/db/migrations/upgrade_0_to_1.py Mon Jun 26 21:11:32 2017
@@ -319,3 +319,4 @@ def upgrade(engine):
# Materialize the test suite tables.
initialize_testsuite(engine, session, "nts")
initialize_testsuite(engine, session, "compile")
+ session.close()
Modified: lnt/trunk/lnt/server/db/migrations/upgrade_10_to_11.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/db/migrations/upgrade_10_to_11.py?rev=306362&r1=306361&r2=306362&view=diff
==============================================================================
--- lnt/trunk/lnt/server/db/migrations/upgrade_10_to_11.py (original)
+++ lnt/trunk/lnt/server/db/migrations/upgrade_10_to_11.py Mon Jun 26 21:11:32 2017
@@ -36,33 +36,23 @@ def add_baselines(test_suite):
return base
-def upgrade_testsuite(engine, session, name):
+def upgrade_testsuite(engine, name):
# Grab Test Suite.
+ session = sqlalchemy.orm.sessionmaker(engine)()
test_suite = session.query(upgrade_0_to_1.TestSuite). \
filter_by(name=name).first()
assert (test_suite is not None)
# Add FieldChange to the test suite.
base = add_baselines(test_suite)
-
- # Create tables. We commit now since databases like Postgres run
- # into deadlocking issues due to previous queries that we have run
- # during the upgrade process. The commit closes all of the
- # relevant transactions allowing us to then perform our upgrade.
- session.commit()
base.metadata.create_all(engine)
# Commit changes (also closing all relevant transactions with
# respect to Postgres like databases).
session.commit()
-
-
-
+ session.close()
def upgrade(engine):
- # Create a session.
- session = sqlalchemy.orm.sessionmaker(engine)()
-
# Create our FieldChangeField table and commit.
- upgrade_testsuite(engine, session, 'nts')
- upgrade_testsuite(engine, session, 'compile')
+ upgrade_testsuite(engine, 'nts')
+ upgrade_testsuite(engine, 'compile')
Modified: lnt/trunk/lnt/server/db/migrations/upgrade_1_to_2.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/db/migrations/upgrade_1_to_2.py?rev=306362&r1=306361&r2=306362&view=diff
==============================================================================
--- lnt/trunk/lnt/server/db/migrations/upgrade_1_to_2.py (original)
+++ lnt/trunk/lnt/server/db/migrations/upgrade_1_to_2.py Mon Jun 26 21:11:32 2017
@@ -145,3 +145,4 @@ def upgrade(engine):
# Commit the results.
session.commit()
+ session.close()
Modified: lnt/trunk/lnt/server/db/migrations/upgrade_2_to_3.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/db/migrations/upgrade_2_to_3.py?rev=306362&r1=306361&r2=306362&view=diff
==============================================================================
--- lnt/trunk/lnt/server/db/migrations/upgrade_2_to_3.py (original)
+++ lnt/trunk/lnt/server/db/migrations/upgrade_2_to_3.py Mon Jun 26 21:11:32 2017
@@ -70,3 +70,4 @@ def upgrade(engine):
# Create our FieldChangeField table and commit.
upgrade_testsuite(engine, session, 'nts')
upgrade_testsuite(engine, session, 'compile')
+ session.close()
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=306362&r1=306361&r2=306362&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 Mon Jun 26 21:11:32 2017
@@ -9,14 +9,10 @@ import sqlalchemy
# Upgrade TestSuite
def upgrade(engine):
- # Create a session.
- session = sqlalchemy.orm.sessionmaker(engine)()
-
# Add our new column. SQLAlchemy doesn't really support adding a new column to an
# existing table, so instead of requiring SQLAlchemy-Migrate, just execute the raw SQL.
- session.connection().execute("""
+ with engine.begin() as trans:
+ trans.execute("""
ALTER TABLE "TestSuiteSampleFields"
ADD COLUMN "bigger_is_better" INTEGER DEFAULT 0
""")
-
- session.commit()
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=306362&r1=306361&r2=306362&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 Mon Jun 26 21:11:32 2017
@@ -27,21 +27,19 @@ def upgrade(engine):
session.add(ts)
session.commit()
+ session.close()
+
# upgrade_3_to_4.py added this column, so it is not in the ORM.
- session.connection().execute("""
+ with engine.begin() as trans:
+ trans.execute("""
UPDATE "TestSuiteSampleFields"
SET bigger_is_better=1
WHERE "Name"='score'
- """)
- session.commit()
-
- # FIXME: This is obviously not the right way to do this, but I gave up
- # trying to find out how to do it properly in SQLAlchemy without
- # SQLAlchemy-migrate installed.
- session.connection().execute("""
+""")
+ # FIXME: This is obviously not the right way to do this, but I gave up
+ # trying to find out how to do it properly in SQLAlchemy without
+ # SQLAlchemy-migrate installed.
+ trans.execute("""
ALTER TABLE "NT_Sample"
ADD COLUMN "score" FLOAT
""")
- session.commit()
-
-
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=306362&r1=306361&r2=306362&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 Mon Jun 26 21:11:32 2017
@@ -26,23 +26,21 @@ def upgrade(engine):
info_key=".mem",)
ts.sample_fields.append(mem_bytes)
session.add(ts)
-
session.commit()
+ session.close()
+
# upgrade_3_to_4.py added this column, so it is not in the ORM.
- session.connection().execute("""
+ with engine.begin() as trans:
+ trans.execute("""
UPDATE "TestSuiteSampleFields"
SET bigger_is_better=0
WHERE "Name"='mem_bytes'
- """)
- session.commit()
+""")
- # FIXME: This is obviously not the right way to do this, but I gave up
- # trying to find out how to do it properly in SQLAlchemy without
- # SQLAlchemy-migrate installed.
- session.connection().execute("""
+ # FIXME: This is obviously not the right way to do this, but I gave up
+ # trying to find out how to do it properly in SQLAlchemy without
+ # SQLAlchemy-migrate installed.
+ trans.execute("""
ALTER TABLE "NT_Sample"
ADD COLUMN "mem_bytes" FLOAT
""")
- session.commit()
-
-
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=306362&r1=306361&r2=306362&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 Mon Jun 26 21:11:32 2017
@@ -39,16 +39,17 @@ def upgrade(engine):
ts.sample_fields.append(hash_field)
session.add(ts)
session.commit()
+ session.close()
- session.connection().execute("""
+ with engine.begin() as trans:
+ trans.execute("""
ALTER TABLE "NT_Sample"
ADD COLUMN "hash_status" INTEGER
""")
- # For MD5 hashes, 32 characters is enough to store the full has.
- # Assume that for hashing schemes producing longer hashes, storing
- # just the first 32 characters is good enough for our use case.
- session.connection().execute("""
-ALTER TABLE "NT_Sample"
-ADD COLUMN "hash" VARCHAR(32)
-""")
- session.commit()
+ # For MD5 hashes, 32 characters is enough to store the full has.
+ # Assume that for hashing schemes producing longer hashes, storing
+ # just the first 32 characters is good enough for our use case.
+ trans.execute("""
+ ALTER TABLE "NT_Sample"
+ ADD COLUMN "hash" VARCHAR(32)
+ """)
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=306362&r1=306361&r2=306362&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 Mon Jun 26 21:11:32 2017
@@ -39,8 +39,9 @@ def add_profiles(test_suite):
return Base
-def upgrade_testsuite(engine, session, name):
+def upgrade_testsuite(engine, name):
# Grab Test Suite.
+ session = sqlalchemy.orm.sessionmaker(engine)()
test_suite = session.query(upgrade_0_to_1.TestSuite).\
filter_by(name=name).first()
assert(test_suite is not None)
@@ -48,26 +49,19 @@ def upgrade_testsuite(engine, session, n
# Add FieldChange to the test suite.
Base = add_profiles(test_suite)
-
- session.connection().execute("""
-ALTER TABLE "%s_Sample"
-ADD COLUMN "ProfileID" INTEGER
- """ % (db_key_name,))
-
- # Create tables. We commit now since databases like Postgres run
- # into deadlocking issues due to previous queries that we have run
- # during the upgrade process. The commit closes all of the
- # relevant transactions allowing us to then perform our upgrade.
- session.commit()
Base.metadata.create_all(engine)
# Commit changes (also closing all relevant transactions with
# respect to Postgres like databases).
session.commit()
+ session.close()
+
+ with engine.begin() as trans:
+ trans.execute("""
+ALTER TABLE "%s_Sample"
+ADD COLUMN "ProfileID" INTEGER
+""" % (db_key_name,))
def upgrade(engine):
- # Create a session.
- session = sqlalchemy.orm.sessionmaker(engine)()
-
# Create our FieldChangeField table and commit.
- upgrade_testsuite(engine, session, 'nts')
- upgrade_testsuite(engine, session, 'compile')
+ upgrade_testsuite(engine, 'nts')
+ upgrade_testsuite(engine, 'compile')
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=306362&r1=306361&r2=306362&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 Mon Jun 26 21:11:32 2017
@@ -22,18 +22,17 @@ def upgrade(engine):
info_key=".code_size",)
ts.sample_fields.append(code_size)
session.add(ts)
-
session.commit()
+ session.close()
+
# upgrade_3_to_4.py added this column, so it is not in the ORM.
- session.connection().execute("""
+ with engine.begin() as trans:
+ trans.execute("""
UPDATE "TestSuiteSampleFields"
SET bigger_is_better=0
WHERE "Name"='code_size'
- """)
- session.commit()
-
- session.connection().execute("""
+""")
+ trans.execute("""
ALTER TABLE "NT_Sample"
ADD COLUMN "code_size" FLOAT
""")
- session.commit()
More information about the llvm-commits
mailing list