-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set postgres sequence values to allow inserts
This fixes #2119, where the explicit inserts done as Buildbot imports changes from pickles skip the call to nextval('changes_changeid_seq'), and thus leave that sequence value out of sync with the table contents.
- Loading branch information
Showing
4 changed files
with
143 additions
and
3 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
master/buildbot/db/migrate/versions/021_fix_postgres_sequences.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# This file is part of Buildbot. Buildbot is free software: you can | ||
# redistribute it and/or modify it under the terms of the GNU General Public | ||
# License as published by the Free Software Foundation, version 2. | ||
# | ||
# This program is distributed in the hope that it will be useful, but WITHOUT | ||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
# details. | ||
# | ||
# You should have received a copy of the GNU General Public License along with | ||
# this program; if not, write to the Free Software Foundation, Inc., 51 | ||
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
# | ||
# Copyright Buildbot Team Members | ||
|
||
import sqlalchemy as sa | ||
|
||
def upgrade(migrate_engine): | ||
# see bug #2119 | ||
|
||
# this only applies to postgres | ||
if migrate_engine.dialect.name != 'postgresql': | ||
return | ||
|
||
metadata = sa.MetaData() | ||
metadata.bind = migrate_engine | ||
|
||
to_fix = [ | ||
'buildrequests.id', | ||
'builds.id', | ||
'buildsets.id', | ||
'changes.changeid', | ||
'patches.id', | ||
'sourcestampsets.id', | ||
'sourcestamps.id', | ||
'objects.id', | ||
'users.uid', | ||
] | ||
|
||
for col in to_fix: | ||
tbl_name, col_name = col.split('.') | ||
tbl = sa.Table(tbl_name, metadata, autoload=True) | ||
col = tbl.c[col_name] | ||
|
||
res = migrate_engine.execute(sa.select([ sa.func.max(col) ])) | ||
max = res.fetchall()[0][0] | ||
|
||
if max: | ||
seq_name = "%s_%s_seq" % (tbl_name, col_name) | ||
r = migrate_engine.execute("SELECT setval('%s', %d)" | ||
% (seq_name, max)) | ||
r.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
master/buildbot/test/unit/test_db_migrate_versions_021_fix_postgres_sequences.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# This file is part of Buildbot. Buildbot is free software: you can | ||
# redistribute it and/or modify it under the terms of the GNU General Public | ||
# License as published by the Free Software Foundation, version 2. | ||
# | ||
# This program is distributed in the hope that it will be useful, but WITHOUT | ||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
# details. | ||
# | ||
# You should have received a copy of the GNU General Public License along with | ||
# this program; if not, write to the Free Software Foundation, Inc., 51 | ||
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
# | ||
# Copyright Buildbot Team Members | ||
|
||
import sqlalchemy as sa | ||
from twisted.trial import unittest | ||
from buildbot.test.util import migration | ||
|
||
class Migration(migration.MigrateTestMixin, unittest.TestCase): | ||
|
||
def setUp(self): | ||
return self.setUpMigrateTest() | ||
|
||
def tearDown(self): | ||
return self.tearDownMigrateTest() | ||
|
||
cols = [ | ||
'buildrequests.id', | ||
'builds.id', | ||
'buildsets.id', | ||
'changes.changeid', | ||
'patches.id', | ||
'sourcestampsets.id', | ||
'sourcestamps.id', | ||
'objects.id', | ||
'users.uid', | ||
] | ||
|
||
# tests | ||
|
||
def test_update(self): | ||
def setup_thd(conn): | ||
metadata = sa.MetaData() | ||
metadata.bind = conn | ||
|
||
# insert a row into each table, giving an explicit id column so | ||
# that the sequence is not advanced correctly, but leave no rows in | ||
# one table to test that corner case | ||
for i, col in enumerate(self.cols): | ||
tbl_name, col_name = col.split('.') | ||
tbl = sa.Table(tbl_name, metadata, | ||
sa.Column(col_name, sa.Integer, primary_key=True)) | ||
tbl.create() | ||
if i > 1: | ||
conn.execute(tbl.insert(), { col_name : i }) | ||
|
||
def verify_thd(conn): | ||
metadata = sa.MetaData() | ||
metadata.bind = conn | ||
|
||
# try inserting *without* an ID, and verify that the resulting ID | ||
# is as expected | ||
for i, col in enumerate(self.cols): | ||
print i, col | ||
tbl_name, col_name = col.split('.') | ||
tbl = sa.Table(tbl_name, metadata, | ||
sa.Column(col_name, sa.Integer, primary_key=True)) | ||
r = conn.execute(tbl.insert(), {}) | ||
if i > 1: | ||
exp = i+1 | ||
else: | ||
exp = 1 | ||
self.assertEqual(r.inserted_primary_key[0], exp) | ||
|
||
return self.do_test_migration(20, 21, setup_thd, verify_thd) |