Commit Graph

653 Commits

Author SHA1 Message Date
Zuul 7b40c33cb6 Merge "Add required database API(s) for cache" 2024-02-27 14:59:26 +00:00
Zuul 02be49c998 Merge "Add new tables for cache operations" 2024-02-19 20:35:01 +00:00
Abhishek Kekane 10d663d04d Add required database API(s) for cache
Added required database API methods for cache related
operations.

Related blueprint centralized-cache-db

Change-Id: Iae1f1927566f4bda74c5aeccf464df820cfae9d4
2024-02-15 14:35:05 +00:00
Abhishek Kekane b6bd03d4bc Add new tables for cache operations
Added expand script and model to define `cached_images` and
`node_reference` database tables.

Related blueprint centralized-cache-db

Change-Id: I9991a4e945bd4d231e942096445fd5374ff7ff2a
2024-02-09 08:40:46 +00:00
Takashi Kajinami 9f896ab03d Bump hacking
hacking 3.0.x is too old.

Change-Id: I33875c36dbbdb946841f8c583636ccdf88d3331f
2024-01-27 16:55:11 +00:00
Zuul db05d6c27c Merge "db: Re-use a single connection" 2023-09-25 14:21:11 +00:00
Zuul 8593179896 Merge "db: Use context, session args for metadef DB APIs" 2023-09-23 11:44:38 +00:00
Zuul 78d0c8545d Merge "db: Trivial parameter swap" 2023-09-22 07:20:12 +00:00
Zuul 88bb1568fc Merge "db: Remove explicit autocommit" 2023-09-21 22:35:10 +00:00
Zuul 05d8cafcbd Merge "db: Replace use of update.values parameter" 2023-09-21 02:55:09 +00:00
Zuul 18813914cc Merge "db: Sync migrations, models" 2023-09-19 19:14:38 +00:00
Zuul 96ab3e976f Merge "db: Drop the convert_unicode, unicode_error flags" 2023-09-19 00:40:21 +00:00
Zuul 2de1932b97 Merge "db: Don't inherit cache for DeleteFromSelect" 2023-09-15 20:40:07 +00:00
Zuul 7cb1d5e0ae Merge "db: Drop use of implicit auto-commit" 2023-09-14 13:31:46 +00:00
Zuul 269cc2f105 Merge "db: Enable foreign keys for SQLite backend" 2023-09-13 14:09:48 +00:00
Zuul fc3e21d01b Merge "db: Don't pass strings to Connection.execute()" 2023-09-13 09:34:00 +00:00
Zuul 5a8afa825b Merge "db: Replace use of aliased keyword arguments" 2023-09-12 14:52:51 +00:00
Zuul 4a6bf9ae12 Merge "db: Pass case.whens as positionals, not a list" 2023-09-12 09:38:31 +00:00
Zuul f6cb466793 Merge "db: Replace use of 'Query.values()'" 2023-09-11 08:21:00 +00:00
Zuul d001291007 Merge "db: Don't invoke and_() without arguments" 2023-09-08 13:52:15 +00:00
Zuul b059c89815 Merge "db: Enable batch migrations by default" 2023-09-06 16:02:36 +00:00
Zuul fe3d7a1420 Merge "db: Disable logging by default for alembic" 2023-09-06 14:01:22 +00:00
Zuul 035fbe9fea Merge "db: Remove use of 'bind' arguments" 2023-09-04 08:28:14 +00:00
Zuul 6d62668c32 Merge "db: Replace use of legacy select() calling style" 2023-09-04 08:28:12 +00:00
Zuul 6f5f24623c Merge "tests: Add tests for loading, unloading metadefs" 2023-08-29 15:20:18 +00:00
Zuul 7508e1ab0d Merge "db: Avoid import variable shadowing" 2023-08-28 18:12:48 +00:00
Zuul 2f45c85bc2 Merge "db: Update import location of declarative_base" 2023-08-28 18:10:12 +00:00
Abhishek Kekane fd222f3128 Sort locations based on store weight
Related to blueprint store-weight

Change-Id: I2383a476cb7e79c7efecdf33203cff0b50ef3bbb
2023-08-01 14:16:08 +00:00
Stephen Finucane 9558093b3e db: Use context, session args for metadef DB APIs
This aligns the metadef DB APIs with the rest of the APIs and simplifies
our life when we decide to move off the legacy engine facade in the
future.

Change-Id: Ifc1605e8e89c48b071ea505e40a9488df208fb37
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2023-07-25 10:10:25 +01:00
Stephen Finucane 62893e9373 db: Trivial parameter swap
Align '_image_update' with 'image_update'. This caught me out once or
twice.

Change-Id: I78e005b51485a224e59c075a00798de5fa5a3896
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2023-07-25 10:10:19 +01:00
Stephen Finucane d1368745c5 db: Remove explicit autocommit
We already got rid of implicit autocommit. Now get rid of explicit
autocommit. This is far more involved than the prior patch but the basic
strategy here is that no public method should take a 'session' argument
and instead should call 'get_session'. In addition, all private methods
should take both a 'context' and 'session' argument and they should be
the first and second arguments, respectively. Finally, public methods
should only call private methods. Combined, this ensures we only ever
have one session in play and the transactions are correctly started and
ended as expected.

Change-Id: I5245c402699e179c219039514b626a52fa0028d8
2023-07-25 10:08:52 +01:00
Stephen Finucane 707ac69914 db: Replace use of update.values parameter
Resolve the following RemovedIn20Warning warning:

  The update.values parameter will be removed in SQLAlchemy 2.0. Please
  refer to the Update.values() method.

Curiously this didn't raise an explicit warning and was only spotted
when updating to SQLAlchemy 2.0 itself.

Change-Id: I1ecb144553992966824420af2121f2f3d64836c6
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2023-07-25 09:06:52 +00:00
Stephen Finucane e3571b2936 db: Sync migrations, models
Recent versions of Alembic are now able to detect the difference in
these types. Given only SQLite is affected and SQLite is only
supported for testing purposes, we simply modify the existing
migration rather than create a new migration.

Change-Id: Idea46240fa17f44442a1da0992d3e073a22205e6
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2023-07-25 09:06:45 +00:00
Stephen Finucane e1c9d68030 db: Drop the convert_unicode, unicode_error flags
Resolve the following SQLAlchemy deprecation warning:

  The convert_unicode on Engine and String as well as the unicode_error
  flag on String are deprecated.  All modern DBAPIs now support Python
  Unicode natively under Python 2, and under Python 3 all strings are
  inherently Unicode.  These flags will be removed in a future release.

Change-Id: I0de54294cc311b32f0956942a62deb2e47729a4f
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2023-07-25 09:05:52 +00:00
Stephen Finucane ede0f9c1b4 db: Don't inherit cache for DeleteFromSelect
Resolve the following SAWarning warning:

  Class DeleteFromSelect will not make use of SQL compilation caching as
  it does not set the 'inherit_cache' attribute to ``True``.  This can
  have significant performance implications including some performance
  degradations in comparison to prior SQLAlchemy versions.  Set this
  attribute to True if this object can make use of the cache key
  generated by the superclass.  Alternatively, this attribute may be set
  to False which will disable this warning.

This uses different arguments so the cache presumably does not make
sense.

Change-Id: I958ba79ca7ac6709e8c16eceb212f329c807aea8
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2023-07-25 09:05:44 +00:00
Stephen Finucane 99e3e5e389 db: Re-use a single connection
Rather than creating multiple connections, use the same connection but
multiple transactions. This should be slightly faster.

Change-Id: I02dc85729c3fbb6d775a32e3c2924e79abe3761c
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2023-07-25 10:03:33 +01:00
Stephen Finucane 8c5091cd48 db: Drop use of implicit auto-commit
Resolve the following RemovedIn20Warning warning:

  The current statement is being autocommitted using implicit
  autocommit, which will be removed in SQLAlchemy 2.0. Use the .begin()
  method of Engine or Connection in order to use an explicit transaction
  for DML and DDL statements.

This is the trickiest one hence it's left to last. We really need to get
off of the legacy EngineFacade pattern since it's harder to track
transactions when they're not local (the request context-based binding
of sessions allows us to decorate individual methods) but that's a
larger work item that we're not going to tackle here.

Change-Id: Ie16965a8e3e2f316b807068a868cee6fbb2f33ba
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2023-07-25 10:03:11 +01:00
Stephen Finucane 2e716b53f3 db: Enable foreign keys for SQLite backend
We were doing this adhoc in tests, which doesn't seem to be possible in
SQLAlchemy 2.0. Do it everywhere instead.

This highlights a small issue that we have to workaround in the tests.

Change-Id: I9dd70c9976f83a00a8cb270422e545ff2c77f12f
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2023-07-25 09:56:29 +01:00
Stephen Finucane 25dcacea7c db: Don't pass strings to Connection.execute()
Resolve the following RemovedIn20Warning warning:

  Passing a string to Connection.execute() is deprecated and will be
  removed in version 2.0.  Use the text() construct, or the
  Connection.exec_driver_sql() method to invoke a driver-level SQL
  string.

We also resolve two additional RemovedIn20Warning warnings along the
way, since we're already touching this code:

  The Engine.execute() method is considered legacy as of the 1.x series
  of SQLAlchemy and will be removed in 2.0. All statement execution in
  SQLAlchemy 2.0 is performed by the Connection.execute() method of
  Connection, or in the ORM by the Session.execute() method of Session.

  The Executable.execute() method is considered legacy as of the 1.x
  series of SQLAlchemy and will be removed in 2.0. All statement
  execution in SQLAlchemy 2.0 is performed by the Connection.execute()
  method of Connection, or in the ORM by the Session.execute() method of
  Session.

Change-Id: I547bd2f441085fcdc5dc4a0e58d27d11e8a8b223
2023-07-24 21:34:03 +00:00
Stephen Finucane 44050a2f75 db: Replace use of aliased keyword arguments
Resolve the following RemovedIn20Warning warning:

  The ``aliased`` and ``from_joinpoint`` keyword arguments to
  Query.join() are deprecated and will be removed in SQLAlchemy 2.0.

This one is a little trickier to grok, but the SQLAlchemy docs explain
it rather well [1] (Look for "Legacy Features of Query.join()" and
"Joins to a Target with an ON Clause"). Essentially, because we are
joining e.g. the ImageTag table multiple times, we need to create an
alias and use it for both the join and resulting queries.

[1] https://docs.sqlalchemy.org/en/14/orm/query.html#sqlalchemy.orm.Query.join

Change-Id: Ifb818df8ca8500adcf1354eff0671f5762473110
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2023-07-24 21:33:58 +00:00
Stephen Finucane e5a8918e87 db: Pass case.whens as positionals, not a list
Resolve the following RemovedIn20Warning warnings:

  The "whens" argument to case(), when referring to a sequence of items,
  is now passed as a series of positional elements, rather than as a
  list.

Change-Id: I4df3a52333d71809d6ff305a786ba6ae03a230ca
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2023-07-24 17:08:56 +00:00
Stephen Finucane c70349d823 db: Replace use of 'Query.values()'
Resolve the following RemovedIn20Warning warnings:

  Query.values() is deprecated and will be removed in a future release.
  Please use Query.with_entities() (deprecated since: 1.4)

Change-Id: Id231036ae122c5878205fbb3d591cee53b817613
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2023-07-24 17:08:42 +00:00
Stephen Finucane 36329c0a75 db: Don't invoke and_() without arguments
Resolve the following RemovedIn20Warning warnings:

  Invoking and_() without arguments is deprecated, and will be
  disallowed in a future release. For an empty and_() construct, use
  and_(True, *args).

Change-Id: I7f34d8079975e57df904e59e325bee21ca1417ed
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2023-07-24 17:08:34 +00:00
Stephen Finucane abddf333ad db: Enable batch migrations by default
These are compatible with SQLite.

Change-Id: Id56b5758f9c1caa664c4a983726d85b3b15e9c04
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2023-07-24 17:08:29 +00:00
Stephen Finucane 9b9afcaa29 db: Disable logging by default for alembic
We don't want alembic overriding our logging: we can configure it
ourselves. This removes a whole lot of noise from our test output.

Change-Id: Ic1bc4ab967913a8adaac559482d330d72e47a4b5
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2023-07-24 17:08:23 +00:00
Stephen Finucane 1f5bbf0bd1 db: Remove use of 'bind' arguments
Resolve the following RemovedIn20Warning warnings:

  The MetaData.bind argument is deprecated and will be removed in
  SQLAlchemy 2.0.

  The ``bind`` argument for schema methods that invoke SQL against an
  engine or connection will be required in SQLAlchemy 2.0.

We are also able to resolve the following RemovedIn20Warning warning,
since the migration away from the bind parameter requires updating these
calls.

  The autoload parameter is deprecated and will be removed in version
  2.0.  Please use the autoload_with parameter, passing an engine or
  connection.

Similarly, we can also resolve the following RemovedIn20Warning warning,
since the calls we're updating here are the only ones affected by this
deprecation:

  The insert.values parameter will be removed in SQLAlchemy 2.0.  Please
  refer to the Insert.values() method.

Change-Id: Ic956980a03e1a0e1b6f90c492d8a03a64ea275cc
2023-07-24 17:47:39 +01:00
Stephen Finucane 6c150194c6 db: Replace use of legacy select() calling style
Resolve the following RemovedIn20Warning warning:

  The legacy calling style of select() is deprecated and will be removed
  in SQLAlchemy 2.0.  Please use the new calling style described at
  select().

Change-Id: Iaebe4bf97b99a0f8f3bb8983ee0a3748f7cd482a
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2023-07-24 17:45:39 +01:00
Stephen Finucane 604ffd390c tests: Add tests for loading, unloading metadefs
These methods were not being tested. Add some basic tests to validate
loading and unloading of our stock definitions, preventing regressions
during e.g. the SQLAlchemy 2.0 preparation series.

Implementation of this test highlights an minor issue in the DB API,
whereby the default values for some methods with 'sort_key' and
'sort_dir' parameters are not valid. This means you can't call these
without overriding the parameters. This is corrected.

Change-Id: I6e76dcacc32f05a70757df28ab2f119b25da440b
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2023-07-24 17:33:43 +01:00
Stephen Finucane 1557eab368 db: Avoid import variable shadowing
'session' is a widely used variable name in the glance.db.sqlalchemy.api
module and this is only going to increase over time. This shadowing
prevents easy identification of e.g. missing function parameters. Alias
the import and update the one call to avoid the need to rename all other
instances of the variable name.

Change-Id: I6b2cbf0b97bb3af7354f2c10a8d83909db963d43
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2023-07-24 17:14:22 +01:00
Stephen Finucane 5636008dfe db: Update import location of declarative_base
This moved in 1.4.

Change-Id: Ie3d30669dffd80aa21025a7a407c373b80b2e3cc
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2023-07-24 17:14:22 +01:00