# 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 buildbot.test.util import migration
from twisted.trial import unittest


class Migration(migration.MigrateTestMixin, unittest.TestCase):

    def setUp(self):
        return self.setUpMigrateTest()

    def tearDown(self):
        return self.tearDownMigrateTest()

    def create_tables_thd(self, conn):
        metadata = sa.MetaData()
        metadata.bind = conn

        changes = sa.Table('changes', metadata,
                           sa.Column('changeid', sa.Integer, primary_key=True),
                           # the rest is unimportant
                           )
        changes.create()

        # Links (URLs) for changes
        change_links = sa.Table('change_links', metadata,
                                sa.Column('changeid', sa.Integer,
                                          sa.ForeignKey('changes.changeid'), nullable=False),
                                sa.Column('link', sa.String(1024), nullable=False),
                                )
        change_links.create()

        sa.Index('change_links_changeid', change_links.c.changeid).create()

    # tests

    def test_update(self):
        def setup_thd(conn):
            self.create_tables_thd(conn)

        def verify_thd(conn):
            metadata = sa.MetaData()
            metadata.bind = conn

            try:
                conn.execute("select * from change_links")
            except:
                pass
            else:
                self.fail("change_links still exists")

        return self.do_test_migration(19, 20, setup_thd, verify_thd)
