Coverage for manila/db/migrations/alembic/versions/56cdbe267881_add_share_export_locations_table.py: 100%

27 statements  

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

1# Copyright 2015 Mirantis Inc. 

2# All Rights Reserved. 

3# 

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

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

6# a copy of the License at 

7# 

8# http://www.apache.org/licenses/LICENSE-2.0 

9# 

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

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

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

13# License for the specific language governing permissions and limitations 

14# under the License. 

15 

16"""Add share_export_locations table 

17 

18Revision ID: 56cdbe267881 

19Revises: 17115072e1c3 

20Create Date: 2015-02-27 14:06:30.464315 

21 

22""" 

23 

24# revision identifiers, used by Alembic. 

25revision = '56cdbe267881' 

26down_revision = '30cb96d995fa' 

27 

28from alembic import op 

29import sqlalchemy as sa 

30from sqlalchemy import func 

31from sqlalchemy.sql import table 

32 

33 

34def upgrade(): 

35 export_locations_table = op.create_table( 

36 'share_export_locations', 

37 sa.Column('id', sa.Integer, primary_key=True, nullable=False), 

38 sa.Column('created_at', sa.DateTime), 

39 sa.Column('updated_at', sa.DateTime), 

40 sa.Column('deleted_at', sa.DateTime), 

41 sa.Column('deleted', sa.Integer, default=0), 

42 sa.Column('path', sa.String(2000)), 

43 sa.Column('share_id', sa.String(36), 

44 sa.ForeignKey('shares.id', name="sel_id_fk")), 

45 mysql_engine='InnoDB', 

46 mysql_charset='utf8') 

47 

48 shares_table = table( 

49 'shares', 

50 sa.Column('created_at', sa.DateTime), 

51 sa.Column('deleted_at', sa.DateTime), 

52 sa.Column('deleted', sa.Integer), 

53 sa.Column('export_location', sa.String(length=255)), 

54 sa.Column('id', sa.String(length=36)), 

55 sa.Column('updated_at', sa.DateTime)) 

56 

57 export_locations = [] 

58 

59 with sa.orm.Session(bind=op.get_bind()) as session: 

60 

61 for share in session.query(shares_table).all(): 

62 deleted = share.deleted if isinstance(share.deleted, int) else 0 

63 export_locations.append({ 

64 'created_at': share.created_at, 

65 'updated_at': share.updated_at, 

66 'deleted_at': share.deleted_at, 

67 'deleted': deleted, 

68 'share_id': share.id, 

69 'path': share.export_location, 

70 }) 

71 op.bulk_insert(export_locations_table, export_locations) 

72 

73 op.drop_column('shares', 'export_location') 

74 

75 

76def downgrade(): 

77 """Remove share_export_locations table. 

78 

79 This method can lead to data loss because only first export_location 

80 is saved in shares table. 

81 """ 

82 

83 op.add_column('shares', 

84 sa.Column('export_location', sa.String(255))) 

85 

86 export_locations_table = table( 

87 'share_export_locations', 

88 sa.Column('share_id', sa.String(length=36)), 

89 sa.Column('path', sa.String(length=255)), 

90 sa.Column('updated_at', sa.DateTime), 

91 sa.Column('deleted', sa.Integer)) 

92 

93 connection = op.get_bind() 

94 

95 with sa.orm.Session(bind=connection) as session: 

96 export_locations = session.query( 

97 func.min(export_locations_table.c.updated_at), 

98 export_locations_table.c.share_id, 

99 export_locations_table.c.path).filter( 

100 export_locations_table.c.deleted == 0).group_by( 

101 export_locations_table.c.share_id, 

102 export_locations_table.c.path).all() 

103 

104 shares = sa.Table( 

105 'shares', sa.MetaData(), 

106 autoload_with=connection) 

107 

108 for location in export_locations: 

109 # pylint: disable=no-value-for-parameter 

110 update = ( 

111 shares.update().where(shares.c.id == location.share_id). 

112 values(export_location=location.path)) 

113 connection.execute(update) 

114 

115 op.drop_table('share_export_locations')