Coverage for manila/db/migrations/alembic/migration.py: 100%

24 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2026-02-18 22:19 +0000

1# Copyright 2014 Mirantis Inc. 

2# 

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

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

5# 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, WITHOUT 

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

12# License for the specific language governing permissions and limitations 

13# under the License. 

14 

15import os 

16 

17import alembic 

18from alembic import config as alembic_config 

19import alembic.migration as alembic_migration # pylint: disable=import-error 

20from oslo_config import cfg 

21 

22from manila.db.sqlalchemy import api as db_api 

23 

24CONF = cfg.CONF 

25 

26 

27def _alembic_config(): 

28 path = os.path.join(os.path.dirname(__file__), os.pardir, 'alembic.ini') 

29 config = alembic_config.Config(path) 

30 return config 

31 

32 

33def version(): 

34 """Current database version. 

35 

36 :returns: Database version 

37 :rtype: string 

38 """ 

39 engine = db_api.get_engine() 

40 with engine.connect() as conn: 

41 context = alembic_migration.MigrationContext.configure(conn) 

42 return context.get_current_revision() 

43 

44 

45def upgrade(revision): 

46 """Upgrade database. 

47 

48 :param version: Desired database version 

49 :type version: string 

50 """ 

51 return alembic.command.upgrade(_alembic_config(), revision or 'head') 

52 

53 

54def downgrade(revision): 

55 """Downgrade database. 

56 

57 :param version: Desired database version 

58 :type version: string 

59 """ 

60 return alembic.command.downgrade(_alembic_config(), revision or 'base') 

61 

62 

63def stamp(revision): 

64 """Stamp database with provided revision. 

65 

66 Don't run any migrations. 

67 

68 :param revision: Should match one from repository or head - to stamp 

69 database with most recent revision 

70 :type revision: string 

71 """ 

72 return alembic.command.stamp(_alembic_config(), revision or 'head') 

73 

74 

75def revision(message=None, autogenerate=False): 

76 """Create template for migration. 

77 

78 :param message: Text that will be used for migration title 

79 :type message: string 

80 :param autogenerate: If True - generates diff based on current database 

81 state 

82 :type autogenerate: bool 

83 """ 

84 return alembic.command.revision(_alembic_config(), message, autogenerate)