summaryrefslogtreecommitdiff
path: root/yoyo/backends.py
blob: ea062b06db9761d383ae98892a16e5c6beaa308d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# Copyright 2015 Oliver Cope
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from datetime import datetime
from contextlib import contextmanager
from importlib import import_module
from itertools import count
from logging import getLogger
import os
import time

from . import exceptions, utils
from .migrations import topological_sort

logger = getLogger('yoyo.migrations')


class TransactionManager(object):
    """
    Returned by the :meth:`~yoyo.backends.DatabaseBackend.transaction`
    context manager.

    If rollback is called, the transaction is flagged to be rolled back
    when the context manager block closes
    """

    def __init__(self, backend):
        self.backend = backend
        self._rollback = False

    def __enter__(self):
        self._do_begin()
        return self

    def __exit__(self, exc_type, value, traceback):
        if exc_type:
            self._do_rollback()
            return None

        if self._rollback:
            self._do_rollback()
        else:
            self._do_commit()

    def rollback(self):
        """
        Flag that the transaction will be rolled back when the with statement
        exits
        """
        self._rollback = True

    def _do_begin(self):
        """
        Instruct the backend to begin a transaction
        """
        self.backend.begin()

    def _do_commit(self):
        """
        Instruct the backend to commit the transaction
        """
        self.backend.commit()

    def _do_rollback(self):
        """
        Instruct the backend to roll back the transaction
        """
        self.backend.rollback()


class SavepointTransactionManager(TransactionManager):

    id = None
    id_generator = count(1)

    def _do_begin(self):
        assert self.id is None
        self.id = 'sp_{}'.format(next(self.id_generator))
        self.backend.savepoint(self.id)

    def _do_commit(self):
        """
        This does nothing.

        Trying to the release savepoint here could cause an database error in
        databases where DDL queries cause the transaction to be committed
        and all savepoints released.
        """

    def _do_rollback(self):
        self.backend.savepoint_rollback(self.id)


class DatabaseBackend(object):

    driver_module = None
    connection = None
    lock_table = '_yoyo_lock'
    create_migration_table_sql = """
        CREATE TABLE {table_name} (
            id VARCHAR(255) NOT NULL PRIMARY KEY,
            ctime TIMESTAMP
        )"""
    create_lock_table_sql = """
        CREATE TABLE {table_name} (
            locked INT DEFAULT 1,
            ctime TIMESTAMP,
            pid INT NOT NULL,
            PRIMARY KEY (locked)
        )"""
    list_tables_sql = "SELECT table_name FROM information_schema.tables"
    is_applied_sql = "SELECT COUNT(1) FROM {0.migration_table} WHERE id=?"
    insert_migration_sql = ("INSERT INTO {0.migration_table} (id, ctime) "
                            "VALUES (?, ?)")
    delete_migration_sql = "DELETE FROM {0.migration_table} WHERE id=?"
    applied_ids_sql = "SELECT id FROM {0.migration_table} ORDER by ctime"
    create_test_table_sql = "CREATE TABLE {table_name} (id INT PRIMARY KEY)"

    _driver = None
    _in_transaction = False

    def __init__(self, dburi, migration_table):
        self.uri = dburi
        self.DatabaseError = self.driver.DatabaseError
        self._connection = self.connect(dburi)
        self.migration_table = migration_table
        self.create_tables()
        self.has_transactional_ddl = self._check_transactional_ddl()

    def _load_driver_module(self):
        """
        Load the dbapi driver module and register the base exception class
        """
        driver = import_module(self.driver_module)
        exceptions.register(driver.DatabaseError)
        return driver

    @property
    def driver(self):
        if self._driver:
            return self._driver
        self._driver = self._load_driver_module()
        return self._driver

    @property
    def connection(self):
        return self._connection

    def _check_transactional_ddl(self):
        """
        Return True if the database supports committing/rolling back
        DDL statements within a transaction
        """
        table_name = '_yoyo_tmp_{}'.format(utils.get_random_string(10))
        sql = self.create_test_table_sql.format(table_name=table_name)
        with self.transaction() as t:
            self.execute(sql)
            t.rollback()
        try:
            with self.transaction():
                self.execute("DROP TABLE {}".format(table_name))
        except self.DatabaseError:
            return True
        return False

    def list_tables(self):
        """
        Return a list of tables present in the backend.
        This is used by the test suite to clean up tables
        generated during testing
        """
        cursor = self.execute(self.list_tables_sql)
        return [row[0] for row in cursor.fetchall()]

    def transaction(self):
        if not self._in_transaction:
            return TransactionManager(self)

        else:
            return SavepointTransactionManager(self)

    def cursor(self):
        return self.connection.cursor()

    def commit(self):
        self.connection.commit()
        self._in_transaction = False

    def rollback(self):
        self.connection.rollback()
        self._in_transaction = False

    def begin(self):
        """
        Begin a new transaction
        """
        self._in_transaction = True
        self.execute("BEGIN")

    def savepoint(self, id):
        """
        Create a new savepoint with the given id
        """
        self.execute("SAVEPOINT {}".format(id))

    def savepoint_release(self, id):
        """
        Release (commit) the savepoint with the given id
        """
        self.execute("RELEASE SAVEPOINT {}".format(id))

    def savepoint_rollback(self, id):
        """
        Rollback the savepoint with the given id
        """
        self.execute("ROLLBACK TO SAVEPOINT {}".format(id))

    @contextmanager
    def disable_transactions(self):
        """
        Disable the connection's transaction support, for example by
        setting the isolation mode to 'autocommit'
        """
        self.rollback()
        yield

    @contextmanager
    def lock(self, timeout=10):
        """
        Create a lock to prevent concurrent migrations.

        :param timeout: duration in seconds before raising a LockTimeout error.
        """

        pid = os.getpid()
        self._insert_lock_row(pid, timeout)
        try:
            yield
        finally:
            self._delete_lock_row(pid)

    def _insert_lock_row(self, pid, timeout, poll_interval=0.5):
        started = time.time()
        while True:
            try:
                with self.transaction():
                    self.execute("INSERT INTO {} (locked, ctime, pid) "
                                 "VALUES (1, ?, ?)".format(self.lock_table),
                                 (datetime.utcnow(), pid))
            except self.DatabaseError:
                if timeout and time.time() > started + timeout:
                    cursor = self.execute("SELECT pid FROM {}"
                                        .format(self.lock_table))
                    row = cursor.fetchone()
                    if row:
                        raise exceptions.LockTimeout(
                            "Process {} has locked this database "
                            "(run yoyo break-lock to remove this lock)"
                            .format(row[0]))
                    else:
                        raise exceptions.LockTimeout(
                            "Database locked "
                            "(run yoyo break-lock to remove this lock)")
                time.sleep(poll_interval)
            else:
                return

    def _delete_lock_row(self, pid):
        with self.transaction():
            self.execute("DELETE FROM {} WHERE pid=?"
                         .format(self.lock_table),
                         (pid,))

    def break_lock(self):
        with self.transaction():
            self.execute("DELETE FROM {}" .format(self.lock_table))

    def execute(self, stmt, args=tuple()):
        """
        Create a new cursor, execute a single statement and return the cursor
        object
        """
        cursor = self.cursor()
        cursor.execute(self._with_placeholders(stmt), args)
        return cursor

    def create_tables(self):
        """
        Create the migrations and lock tables if they do not already exist.
        """
        statements = [
            self.create_migration_table_sql.format(table_name=self.migration_table),
            self.create_lock_table_sql.format(table_name=self.lock_table)
        ]

        for stmt in statements:
            try:
                with self.transaction():
                    self.execute(stmt)
            except self.DatabaseError:
                pass

    def _with_placeholders(self, sql):
        placeholder_gen = {'qmark': '?',
                           'format': '%s',
                           'pyformat': '%s'}.get(self.driver.paramstyle)
        if placeholder_gen is None:
            raise ValueError("Unsupported paramstyle: %r" %
                             (self.driver.paramstyle,))
        return sql.replace('?', placeholder_gen)

    def is_applied(self, migration):
        sql = self._with_placeholders(self.is_applied_sql.format(self))
        return self.execute(sql, (migration.id,)).fetchone()[0] > 0

    def get_applied_migration_ids(self):
        """
        Return the list of migration ids in the order in which they
        were applied
        """
        sql = self._with_placeholders(self.applied_ids_sql.format(self))
        return [row[0] for row in self.execute(sql).fetchall()]

    def to_apply(self, migrations):
        """
        Return the subset of migrations not already applied.
        """
        ms = (m for m in migrations if not self.is_applied(m))
        return migrations.__class__(topological_sort(ms),
                                    migrations.post_apply)

    def to_rollback(self, migrations):
        """
        Return the subset of migrations already applied and which may be
        rolled back.

        The order of migrations will be reversed.
        """
        ms = (m for m in migrations if self.is_applied(m))
        return migrations.__class__(reversed(topological_sort(ms)),
                                    migrations.post_apply)

    def apply_migrations(self, migrations, force=False):
        if migrations:
            self.apply_migrations_only(migrations, force=force)
            self.run_post_apply(migrations, force=force)

    def apply_migrations_only(self, migrations, force=False):
        """
        Apply the list of migrations, but do not run any post-apply hooks
        present.
        """
        if not migrations:
            return
        for m in migrations:
            try:
                self.apply_one(m, force=force)
            except exceptions.BadMigration:
                continue

    def run_post_apply(self, migrations, force=False):
        """
        Run any post-apply migrations present in ``migrations``
        """
        for m in migrations.post_apply:
            self.apply_one(m, mark=False, force=force)

    def rollback_migrations(self, migrations, force=False):
        if not migrations:
            return
        for m in migrations:
            try:
                self.rollback_one(m, force)
            except exceptions.BadMigration:
                continue

    def mark_migrations(self, migrations):
        with self.transaction():
            for m in migrations:
                try:
                    self.mark_one(m)
                except exceptions.BadMigration:
                    continue

    def unmark_migrations(self, migrations):
        with self.transaction():
            for m in migrations:
                try:
                    self.unmark_one(m)
                except exceptions.BadMigration:
                    continue

    def apply_one(self, migration, force=False, mark=True):
        """
        Apply a single migration
        """
        logger.info("Applying %s", migration.id)
        migration.process_steps(self, 'apply', force=force)
        if mark:
            with self.transaction():
                self.mark_one(migration)

    def rollback_one(self, migration, force=False):
        """
        Rollback a single migration
        """
        logger.info("Rolling back %s", migration.id)
        migration.process_steps(self, 'rollback', force=force)
        with self.transaction():
            self.unmark_one(migration)

    def unmark_one(self, migration):
        sql = self._with_placeholders(self.delete_migration_sql.format(self))
        self.execute(sql, (migration.id,))

    def mark_one(self, migration):
        logger.info("Marking %s applied", migration.id)
        sql = self._with_placeholders(self.insert_migration_sql).format(self)
        self.execute(sql, (migration.id, datetime.utcnow()))


class ODBCBackend(DatabaseBackend):
    driver_module = 'pyodbc'

    def connect(self, dburi):
        args = [('UID', dburi.username),
                ('PWD', dburi.password),
                ('ServerName', dburi.hostname),
                ('Port', dburi.port),
                ('Database', dburi.database)]
        args.extend(dburi.args.items())
        s = ';'.join('{}={}'.format(k, v) for k, v in args if v is not None)
        return self.driver.connect(s)


class MySQLBackend(DatabaseBackend):

    driver_module = 'pymysql'

    def connect(self, dburi):
        kwargs = dburi.args
        if dburi.username is not None:
            kwargs['user'] = dburi.username
        if dburi.password is not None:
            kwargs['passwd'] = dburi.password
        if dburi.hostname is not None:
            kwargs['host'] = dburi.hostname
        if dburi.port is not None:
            kwargs['port'] = dburi.port
        if 'unix_socket' in dburi.args:
            kwargs['unix_socket'] = dburi.args['unix_socket']
        kwargs['db'] = dburi.database

        return self.driver.connect(**kwargs)


class MySQLdbBackend(DatabaseBackend):

    driver_module = 'MySQLdb'

    def connect(self, dburi):
        kwargs = dburi.args
        if dburi.username is not None:
            kwargs['user'] = dburi.username
        if dburi.password is not None:
            kwargs['passwd'] = dburi.password
        if dburi.hostname is not None:
            kwargs['host'] = dburi.hostname
        if dburi.port is not None:
            kwargs['port'] = dburi.port
        kwargs['db'] = dburi.database

        return self.driver.connect(**kwargs)


class SQLiteBackend(DatabaseBackend):

    driver_module = 'sqlite3'
    list_tables_sql = "SELECT name FROM sqlite_master WHERE type = 'table'"

    def connect(self, dburi):
        conn = self.driver.connect(dburi.database)
        conn.isolation_level = None
        return conn


class PostgresqlBackend(DatabaseBackend):

    driver_module = 'psycopg2'

    def connect(self, dburi):
        connect_args = {'dbname': dburi.database}
        if dburi.username is not None:
            connect_args['user'] = dburi.username
        if dburi.password is not None:
            connect_args['password'] = dburi.password
        if dburi.port is not None:
            connect_args['port'] = dburi.port
        if dburi.hostname is not None:
            connect_args['host'] = dburi.hostname
        return self.driver.connect(**connect_args)

    @contextmanager
    def disable_transactions(self):
        with super(PostgresqlBackend, self).disable_transactions():
            saved = self.connection.autocommit
            self.connection.autocommit = True
            yield
            self.connection.autocommit = saved