[sqlalchemy-20] The Session.begin.subtransactions flag is deprecated

Closes-Bug: #2008276
Change-Id: I5472db98c6ae24b8b1e6add2c0d73aeb1ed016ca
This commit is contained in:
Rodolfo Alonso Hernandez 2023-02-18 21:06:38 +01:00 committed by Rodolfo Alonso
parent a30cb8bb1e
commit d4a85833a7
15 changed files with 145 additions and 160 deletions

View File

@ -56,21 +56,20 @@ def transfer_snat_bindings():
sa.Column('l3_agent_id', sa.String(36)))
session = sa.orm.Session(bind=op.get_bind())
with session.begin(subtransactions=True):
# first delete all bindings for dvr routers from
# routerl3agentbindings as this might be bindings with l3 agents
# on compute nodes
for router_attr in session.query(
router_attr_table).filter(router_attr_table.c.distributed):
session.execute(router_binding.delete(
router_binding.c.router_id == router_attr.router_id))
# first delete all bindings for dvr routers from
# routerl3agentbindings as this might be bindings with l3 agents
# on compute nodes
for router_attr in session.query(
router_attr_table).filter(router_attr_table.c.distributed):
session.execute(router_binding.delete(
router_binding.c.router_id == router_attr.router_id))
# now routerl3agentbindings will only contain bindings for snat
# portion of the router
for csnat_binding in session.query(csnat_binding):
session.execute(
router_binding.insert().values(
router_id=csnat_binding.router_id,
l3_agent_id=csnat_binding.l3_agent_id))
# now routerl3agentbindings will only contain bindings for snat
# portion of the router
for csnat_binding in session.query(csnat_binding):
session.execute(
router_binding.insert().values(
router_id=csnat_binding.router_id,
l3_agent_id=csnat_binding.l3_agent_id))
# this commit is necessary to allow further operations
session.commit()

View File

@ -58,11 +58,10 @@ def migrate_values():
for row in session.query(securitygroups):
values.append({'id': row[0],
'description': row[1]})
with session.begin(subtransactions=True):
for value in values:
session.execute(
standardattr.update().values(
description=value['description']).where(
standardattr.c.id == value['id']))
for value in values:
session.execute(
standardattr.update().values(
description=value['description']).where(
standardattr.c.id == value['id']))
# this commit appears to be necessary to allow further operations
session.commit()

View File

@ -69,18 +69,17 @@ def upgrade():
def generate_records_for_existing():
session = sa.orm.Session(bind=op.get_bind())
values = []
with session.begin(subtransactions=True):
for table, model in TABLE_MODELS:
for row in session.query(model):
# NOTE(kevinbenton): without this disabled, pylint complains
# about a missing 'dml' argument.
# pylint: disable=no-value-for-parameter
res = session.execute(
standardattrs.insert().values(resource_type=table))
session.execute(
model.update().values(
standard_attr_id=res.inserted_primary_key[0]).where(
model.c.id == row[0]))
for table, model in TABLE_MODELS:
for row in session.query(model):
# NOTE(kevinbenton): without this disabled, pylint complains
# about a missing 'dml' argument.
# pylint: disable=no-value-for-parameter
res = session.execute(
standardattrs.insert().values(resource_type=table))
session.execute(
model.update().values(
standard_attr_id=res.inserted_primary_key[0]).where(
model.c.id == row[0]))
# this commit is necessary to allow further operations
session.commit()
return values

View File

@ -59,16 +59,15 @@ def upgrade():
routers_to_bindings = defaultdict(list)
session = sa.orm.Session(bind=op.get_bind())
with session.begin(subtransactions=True):
for result in session.query(bindings_table):
routers_to_bindings[result.router_id].append(result)
for result in session.query(bindings_table):
routers_to_bindings[result.router_id].append(result)
for bindings in routers_to_bindings.values():
for index, result in enumerate(bindings):
session.execute(bindings_table.update().values(
binding_index=index + 1).where(
bindings_table.c.router_id == result.router_id).where(
bindings_table.c.l3_agent_id == result.l3_agent_id))
for bindings in routers_to_bindings.values():
for index, result in enumerate(bindings):
session.execute(bindings_table.update().values(
binding_index=index + 1).where(
bindings_table.c.router_id == result.router_id).where(
bindings_table.c.l3_agent_id == result.l3_agent_id))
session.commit()
op.create_unique_constraint(

View File

@ -56,15 +56,14 @@ def update_device_owner_ha_replicated_interface():
sa.Column('device_id', sa.String(255)))
session = sa.orm.Session(bind=op.get_bind())
with session.begin(subtransactions=True):
for router_attr in session.query(
router_attr_table).filter(router_attr_table.c.ha):
session.execute(routerports.update().values(
port_type=constants.DEVICE_OWNER_HA_REPLICATED_INT).where(
routerports.c.router_id == router_attr.router_id).where(
routerports.c.port_type == constants.DEVICE_OWNER_ROUTER_INTF))
session.execute(ports.update().values(
device_owner=constants.DEVICE_OWNER_HA_REPLICATED_INT).where(
ports.c.device_id == router_attr.router_id).where(
ports.c.device_owner == constants.DEVICE_OWNER_ROUTER_INTF))
for router_attr in session.query(
router_attr_table).filter(router_attr_table.c.ha):
session.execute(routerports.update().values(
port_type=constants.DEVICE_OWNER_HA_REPLICATED_INT).where(
routerports.c.router_id == router_attr.router_id).where(
routerports.c.port_type == constants.DEVICE_OWNER_ROUTER_INTF))
session.execute(ports.update().values(
device_owner=constants.DEVICE_OWNER_HA_REPLICATED_INT).where(
ports.c.device_id == router_attr.router_id).where(
ports.c.device_owner == constants.DEVICE_OWNER_ROUTER_INTF))
session.commit()

View File

@ -47,19 +47,18 @@ standardattrs = sa.Table(
def update_existing_records():
session = sa.orm.Session(bind=op.get_bind())
values = []
with session.begin(subtransactions=True):
for row in session.query(TBL_MODEL):
# NOTE from kevinbenton: without this disabled, pylint complains
# about a missing 'dml' argument.
# pylint: disable=no-value-for-parameter
res = session.execute(
standardattrs.insert().values(resource_type=TBL)
)
session.execute(
TBL_MODEL.update().values(
standard_attr_id=res.inserted_primary_key[0]).where(
TBL_MODEL.c.id == row[0])
)
for row in session.query(TBL_MODEL):
# NOTE from kevinbenton: without this disabled, pylint complains
# about a missing 'dml' argument.
# pylint: disable=no-value-for-parameter
res = session.execute(
standardattrs.insert().values(resource_type=TBL)
)
session.execute(
TBL_MODEL.update().values(
standard_attr_id=res.inserted_primary_key[0]).where(
TBL_MODEL.c.id == row[0])
)
# this commit is necessary to allow further operations
session.commit()
return values

View File

@ -52,16 +52,15 @@ portdnses = sa.Table('portdnses', sa.MetaData(),
def migrate_records_for_existing():
session = sa.orm.Session(bind=op.get_bind())
with session.begin(subtransactions=True):
for row in session.query(ports):
if row[1]:
res = session.execute(portdnses.update().values(
dns_name=row[1]).where(portdnses.c.port_id == row[0]))
if res.rowcount == 0:
session.execute(portdnses.insert().values(
port_id=row[0], current_dns_name='',
current_dns_domain='', previous_dns_name='',
previous_dns_domain='', dns_name=row[1]))
for row in session.query(ports):
if row[1]:
res = session.execute(portdnses.update().values(
dns_name=row[1]).where(portdnses.c.port_id == row[0]))
if res.rowcount == 0:
session.execute(portdnses.insert().values(
port_id=row[0], current_dns_name='',
current_dns_domain='', previous_dns_name='',
previous_dns_domain='', dns_name=row[1]))
session.commit()

View File

@ -49,20 +49,19 @@ def upgrade():
sa.Column('port_id', sa.String(36)),
sa.Column('port_type', sa.String(255)))
session = sa.orm.Session(bind=op.get_bind())
with session.begin(subtransactions=True):
router_port_tuples = set()
for ha_bind in session.query(ha_bindings):
router_port_tuples.add((ha_bind.router_id, ha_bind.port_id))
# we have to remove any from the bulk insert that may already exist
# as a result of Ifd3e007aaf2a2ed8123275aa3a9f540838e3c003 being
# back-ported
for router_port in session.query(router_ports).filter(
router_ports.c.port_type ==
constants.DEVICE_OWNER_ROUTER_HA_INTF):
router_port_tuples.discard((router_port.router_id,
router_port.port_id))
new_records = [dict(router_id=router_id, port_id=port_id,
port_type=constants.DEVICE_OWNER_ROUTER_HA_INTF)
for router_id, port_id in router_port_tuples]
router_port_tuples = set()
for ha_bind in session.query(ha_bindings):
router_port_tuples.add((ha_bind.router_id, ha_bind.port_id))
# we have to remove any from the bulk insert that may already exist
# as a result of Ifd3e007aaf2a2ed8123275aa3a9f540838e3c003 being
# back-ported
for router_port in session.query(router_ports).filter(
router_ports.c.port_type ==
constants.DEVICE_OWNER_ROUTER_HA_INTF):
router_port_tuples.discard((router_port.router_id,
router_port.port_id))
new_records = [dict(router_id=router_id, port_id=port_id,
port_type=constants.DEVICE_OWNER_ROUTER_HA_INTF)
for router_id, port_id in router_port_tuples]
op.bulk_insert(router_ports, new_records)
session.commit()

View File

@ -67,20 +67,19 @@ def upgrade():
def generate_records_for_existing():
session = sa.orm.Session(bind=op.get_bind())
values = []
with session.begin(subtransactions=True):
for row in session.query(TABLE_MODEL):
# NOTE(kevinbenton): without this disabled, pylint complains
# about a missing 'dml' argument.
# pylint: disable=no-value-for-parameter
res = session.execute(
standardattrs.insert().values(resource_type=TABLE,
description=row[1])
)
session.execute(
TABLE_MODEL.update().values(
standard_attr_id=res.inserted_primary_key[0]).where(
TABLE_MODEL.c.id == row[0])
)
for row in session.query(TABLE_MODEL):
# NOTE(kevinbenton): without this disabled, pylint complains
# about a missing 'dml' argument.
# pylint: disable=no-value-for-parameter
res = session.execute(
standardattrs.insert().values(resource_type=TABLE,
description=row[1])
)
session.execute(
TABLE_MODEL.update().values(
standard_attr_id=res.inserted_primary_key[0]).where(
TABLE_MODEL.c.id == row[0])
)
# this commit is necessary to allow further operations
session.commit()
return values

View File

@ -39,12 +39,11 @@ networks = sa.Table(
def upgrade_existing_records():
session = sa.orm.Session(bind=op.get_bind())
with session.begin(subtransactions=True):
for row in session.query(networks):
if row[1] is None:
session.execute(networks.update().values(
mtu=constants.DEFAULT_NETWORK_MTU).where(
networks.c.id == row[0]))
for row in session.query(networks):
if row[1] is None:
session.execute(networks.update().values(
mtu=constants.DEFAULT_NETWORK_MTU).where(
networks.c.id == row[0]))
session.commit()

View File

@ -44,16 +44,15 @@ STDATTRS_TABLE = sa.Table(
def update_existing_records():
session = sa.orm.Session(bind=op.get_bind())
with session.begin(subtransactions=True):
for row in session.query(TABLE_MODEL):
res = session.execute(
STDATTRS_TABLE.insert().values(resource_type=TABLE_NAME)
)
session.execute(
TABLE_MODEL.update().values(
standard_attr_id=res.inserted_primary_key[0]).where(
TABLE_MODEL.c.id == row[0])
)
for row in session.query(TABLE_MODEL):
res = session.execute(
STDATTRS_TABLE.insert().values(resource_type=TABLE_NAME)
)
session.execute(
TABLE_MODEL.update().values(
standard_attr_id=res.inserted_primary_key[0]).where(
TABLE_MODEL.c.id == row[0])
)
session.commit()

View File

@ -51,16 +51,15 @@ def upgrade():
networks_to_bindings = defaultdict(list)
session = sa.orm.Session(bind=op.get_bind())
with session.begin(subtransactions=True):
for result in session.query(bindings_table):
networks_to_bindings[result.network_id].append(result)
for result in session.query(bindings_table):
networks_to_bindings[result.network_id].append(result)
for bindings in networks_to_bindings.values():
for index, result in enumerate(bindings):
session.execute(bindings_table.update().values(
binding_index=index + 1).where(
bindings_table.c.network_id == result.network_id).where(
bindings_table.c.dhcp_agent_id == result.dhcp_agent_id))
for bindings in networks_to_bindings.values():
for index, result in enumerate(bindings):
session.execute(bindings_table.update().values(
binding_index=index + 1).where(
bindings_table.c.network_id == result.network_id).where(
bindings_table.c.dhcp_agent_id == result.dhcp_agent_id))
session.commit()
op.create_unique_constraint(

View File

@ -44,9 +44,8 @@ def update_device_owner_ovn_distributed_ports():
sa.Column('device_id', sa.String(255)))
session = sa.orm.Session(bind=op.get_bind())
with session.begin(subtransactions=True):
session.execute(ports.update().values(
device_owner=constants.DEVICE_OWNER_DISTRIBUTED).where(
ports.c.device_owner == constants.DEVICE_OWNER_DHCP).where(
ports.c.device_id.like('{}%'.format(OVN_METADATA_PREFIX))))
session.execute(ports.update().values(
device_owner=constants.DEVICE_OWNER_DISTRIBUTED).where(
ports.c.device_owner == constants.DEVICE_OWNER_DHCP).where(
ports.c.device_id.like('{}%'.format(OVN_METADATA_PREFIX))))
session.commit()

View File

@ -48,17 +48,16 @@ standardattrs = sa.Table(
def generate_records_for_existing():
session = sa.orm.Session(bind=op.get_bind())
with session.begin(subtransactions=True):
for row in session.query(TABLE_MODEL):
res = session.execute(
standardattrs.insert().values(resource_type=TABLE,
description=row[1])
)
session.execute(
TABLE_MODEL.update().values(
standard_attr_id=res.inserted_primary_key[0]).where(
TABLE_MODEL.c.id == row[0])
)
for row in session.query(TABLE_MODEL):
res = session.execute(
standardattrs.insert().values(resource_type=TABLE,
description=row[1])
)
session.execute(
TABLE_MODEL.update().values(
standard_attr_id=res.inserted_primary_key[0]).where(
TABLE_MODEL.c.id == row[0])
)
# this commit is necessary to allow further operations
session.commit()

View File

@ -110,20 +110,19 @@ def migrate_values():
'socket': row[1],
'external_port': row[2]})
with session.begin(subtransactions=True):
for value in values:
internal_ip_address, internal_port = str(
value['socket']).split(':')
external_port = value['external_port']
internal_port = int(internal_port)
session.execute(
pf_table.update().values(
internal_port_start=internal_port,
internal_port_end=internal_port,
external_port_start=external_port,
external_port_end=external_port,
internal_ip_address=internal_ip_address).where(
pf_table.c.id == value['id']))
for value in values:
internal_ip_address, internal_port = str(
value['socket']).split(':')
external_port = value['external_port']
internal_port = int(internal_port)
session.execute(
pf_table.update().values(
internal_port_start=internal_port,
internal_port_end=internal_port,
external_port_start=external_port,
external_port_end=external_port,
internal_ip_address=internal_ip_address).where(
pf_table.c.id == value['id']))
session.commit()