Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# Copyright (C) 2019 Bloomberg LP 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# <http://www.apache.org/licenses/LICENSE-2.0> 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14 

15from logging.config import fileConfig 

16 

17from alembic import context 

18from sqlalchemy import engine_from_config 

19from sqlalchemy import pool 

20 

21from buildgrid.server.persistence.sql.models import Base 

22 

23 

24# this is the Alembic Config object, which provides 

25# access to the values within the .ini file in use. 

26config = context.config 

27 

28# Interpret the config file for Python logging. 

29# This line sets up loggers basically. 

30try: 

31 fileConfig(config.config_file_name) 

32except TypeError: 

33 pass 

34 

35target_metadata = Base.metadata 

36 

37# other values from the config, defined by the needs of env.py, 

38# can be acquired: 

39# my_important_option = config.get_main_option("my_important_option") 

40# ... etc. 

41 

42 

43def run_migrations_offline(): # pragma: no cover 

44 """Run migrations in 'offline' mode. 

45 

46 This configures the context with just a URL 

47 and not an Engine, though an Engine is acceptable 

48 here as well. By skipping the Engine creation 

49 we don't even need a DBAPI to be available. 

50 

51 Calls to context.execute() here emit the given string to the 

52 script output. 

53 

54 """ 

55 url = config.get_main_option("sqlalchemy.url") 

56 context.configure( 

57 url=url, target_metadata=target_metadata, literal_binds=True 

58 ) 

59 

60 with context.begin_transaction(): 

61 context.run_migrations() 

62 

63 

64def _run_migrations_online(connection): # pragma: no cover 

65 context.configure( 

66 connection=connection, target_metadata=target_metadata 

67 ) 

68 

69 with context.begin_transaction(): 

70 context.run_migrations() 

71 

72 

73def run_migrations_online(): # pragma: no cover 

74 """Run migrations in 'online' mode. 

75 

76 In this scenario we need to create an Engine 

77 and associate a connection with the context. 

78 

79 """ 

80 try: 

81 connection = config.attributes['connection'] 

82 _run_migrations_online(connection) 

83 

84 except KeyError: 

85 connectable = engine_from_config( 

86 config.get_section(config.config_ini_section), 

87 prefix="sqlalchemy.", 

88 poolclass=pool.NullPool 

89 ) 

90 with connectable.connect() as connection: 

91 _run_migrations_online(connection) 

92 

93 

94if context.is_offline_mode(): # pragma: no cover 

95 run_migrations_offline() 

96else: # pragma: no cover 

97 run_migrations_online()