fix(app-backend): Allow it to skip migrations
Signed-off-by: Daniel Dias Branco Arthaud <arthaud@gmail.com>
This commit is contained in:
@@ -14,12 +14,25 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { Knex as KnexType } from 'knex';
|
||||
import { getVoidLogger } from '@backstage/backend-common';
|
||||
import { TestDatabases } from '@backstage/backend-test-utils';
|
||||
import { StaticAssetsStore } from './StaticAssetsStore';
|
||||
|
||||
const logger = getVoidLogger();
|
||||
|
||||
function createDatabaseManager(
|
||||
client: KnexType,
|
||||
skipMigrations: boolean = false,
|
||||
) {
|
||||
return {
|
||||
getClient: async () => client,
|
||||
migrations: {
|
||||
skip: skipMigrations,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe('StaticAssetsStore', () => {
|
||||
const databases = TestDatabases.create({
|
||||
ids: ['POSTGRES_13', 'POSTGRES_9', 'SQLITE_3'],
|
||||
@@ -28,9 +41,11 @@ describe('StaticAssetsStore', () => {
|
||||
it.each(databases.eachSupportedId())(
|
||||
'should store and retrieve assets, %p',
|
||||
async databaseId => {
|
||||
const client = await databases.init(databaseId);
|
||||
const database = createDatabaseManager(client);
|
||||
const store = await StaticAssetsStore.create({
|
||||
logger,
|
||||
database: await databases.init(databaseId),
|
||||
database,
|
||||
});
|
||||
|
||||
await store.storeAssets([
|
||||
@@ -69,9 +84,11 @@ describe('StaticAssetsStore', () => {
|
||||
it.each(databases.eachSupportedId())(
|
||||
'should update assets timestamps, but not contents, %p',
|
||||
async databaseId => {
|
||||
const client = await databases.init(databaseId);
|
||||
const database = createDatabaseManager(client);
|
||||
const store = await StaticAssetsStore.create({
|
||||
logger,
|
||||
database: await databases.init(databaseId),
|
||||
database,
|
||||
});
|
||||
|
||||
await store.storeAssets([
|
||||
@@ -119,7 +136,8 @@ describe('StaticAssetsStore', () => {
|
||||
it.each(databases.eachSupportedId())(
|
||||
'should trim old assets, %p',
|
||||
async databaseId => {
|
||||
const database = await databases.init(databaseId);
|
||||
const knex = await databases.init(databaseId);
|
||||
const database = createDatabaseManager(knex);
|
||||
const store = await StaticAssetsStore.create({
|
||||
logger,
|
||||
database,
|
||||
@@ -137,12 +155,12 @@ describe('StaticAssetsStore', () => {
|
||||
]);
|
||||
|
||||
// Rewrite modified time of "old" to be 1h in the past
|
||||
const updated = await database('static_assets_cache')
|
||||
const updated = await knex('static_assets_cache')
|
||||
.where({ path: 'old' })
|
||||
.update({
|
||||
last_modified_at: database.client.config.client.includes('sqlite3')
|
||||
? database.raw(`datetime('now', '-3600 seconds')`)
|
||||
: database.raw(`now() + interval '-3600 seconds'`),
|
||||
last_modified_at: knex.client.config.client.includes('sqlite3')
|
||||
? knex.raw(`datetime('now', '-3600 seconds')`)
|
||||
: knex.raw(`now() + interval '-3600 seconds'`),
|
||||
});
|
||||
expect(updated).toBe(1);
|
||||
|
||||
|
||||
@@ -14,7 +14,10 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { resolvePackagePath } from '@backstage/backend-common';
|
||||
import {
|
||||
PluginDatabaseManager,
|
||||
resolvePackagePath,
|
||||
} from '@backstage/backend-common';
|
||||
import { Knex } from 'knex';
|
||||
import { Logger } from 'winston';
|
||||
import { DateTime } from 'luxon';
|
||||
@@ -34,7 +37,7 @@ interface StaticAssetRow {
|
||||
|
||||
/** @internal */
|
||||
export interface StaticAssetsStoreOptions {
|
||||
database: Knex;
|
||||
database: PluginDatabaseManager;
|
||||
logger: Logger;
|
||||
}
|
||||
|
||||
@@ -48,15 +51,21 @@ export class StaticAssetsStore implements StaticAssetProvider {
|
||||
#logger: Logger;
|
||||
|
||||
static async create(options: StaticAssetsStoreOptions) {
|
||||
await options.database.migrate.latest({
|
||||
directory: migrationsDir,
|
||||
});
|
||||
return new StaticAssetsStore(options);
|
||||
const { database } = options;
|
||||
const client = await database.getClient();
|
||||
|
||||
if (!database.migrations?.skip) {
|
||||
await client.migrate.latest({
|
||||
directory: migrationsDir,
|
||||
});
|
||||
}
|
||||
|
||||
return new StaticAssetsStore(client, options.logger);
|
||||
}
|
||||
|
||||
private constructor(options: StaticAssetsStoreOptions) {
|
||||
this.#db = options.database;
|
||||
this.#logger = options.logger;
|
||||
private constructor(client: Knex, logger: Logger) {
|
||||
this.#db = client;
|
||||
this.#logger = logger;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -130,7 +130,7 @@ export async function createRouter(
|
||||
if (options.database) {
|
||||
const store = await StaticAssetsStore.create({
|
||||
logger,
|
||||
database: await options.database.getClient(),
|
||||
database: options.database,
|
||||
});
|
||||
|
||||
const assets = await findStaticAssets(staticDir);
|
||||
|
||||
Reference in New Issue
Block a user