mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
fix(ci): use the bound database for map defaults and retry preloads
Write both defaults in one guarded statement so a SQLite source imported by the Postgres suite needs no Postgres lock. Release a failed speculative chunk promise so opening the map can retry it.
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
import { resolveEngine } from '../engine';
|
||||
import { writeTransaction } from '../engine/write-transaction';
|
||||
import type { DB } from '../kysely/db';
|
||||
import type { EntityManager } from '@mikro-orm/core';
|
||||
import { Seeder } from '@mikro-orm/seeder';
|
||||
@@ -12,27 +10,33 @@ const STYLE = 'default_user_setting_maplibre_style';
|
||||
/** Give a new installation a starting basemap before its first user is created. */
|
||||
export class MapDefaultsSeeder extends Seeder {
|
||||
async run(em: EntityManager): Promise<void> {
|
||||
await writeTransaction(em, resolveEngine(), async (tx) => {
|
||||
const db = (tx as SqlEntityManager).getKysely<Pick<DB, 'app_settings' | 'schema_version' | 'users'>>();
|
||||
// SchemaVersionSeeder writes this marker after the first bootstrap even on
|
||||
// SSO-only installations that are still waiting for their first account.
|
||||
const initialized = await db.selectFrom('schema_version').select('id').limit(1).executeTakeFirst();
|
||||
if (initialized) return;
|
||||
const user = await db.selectFrom('users').select('id').limit(1).executeTakeFirst();
|
||||
if (user) return;
|
||||
const db = (em as SqlEntityManager).getKysely<Pick<DB, 'app_settings' | 'schema_version' | 'users'>>();
|
||||
const defaults = db
|
||||
.selectNoFrom((eb) => [eb.val<string>(PROVIDER).as('key'), eb.val(JSON.stringify('maplibre-gl')).as('value')])
|
||||
.unionAll(
|
||||
db.selectNoFrom((eb) => [
|
||||
eb.val<string>(STYLE).as('key'),
|
||||
eb.val(JSON.stringify(OPENFREEMAP_DEFAULT_STYLE)).as('value'),
|
||||
]),
|
||||
)
|
||||
.as('map_defaults');
|
||||
|
||||
const provider = await db
|
||||
.insertInto('app_settings')
|
||||
.values({ key: PROVIDER, value: JSON.stringify('maplibre-gl') })
|
||||
.onConflict((conflict) => conflict.column('key').doNothing())
|
||||
.returning('key')
|
||||
.executeTakeFirst();
|
||||
if (!provider) return;
|
||||
await db
|
||||
.insertInto('app_settings')
|
||||
.values({ key: STYLE, value: JSON.stringify(OPENFREEMAP_DEFAULT_STYLE) })
|
||||
.onConflict((conflict) => conflict.column('key').doNothing())
|
||||
.execute();
|
||||
});
|
||||
// SchemaVersionSeeder marks completed first boots, including SSO-only installs
|
||||
// still waiting for a user. Check and insert together on the bound database.
|
||||
await db
|
||||
.insertInto('app_settings')
|
||||
.columns(['key', 'value'])
|
||||
.expression(
|
||||
db
|
||||
.selectFrom(defaults)
|
||||
.select(['key', 'value'])
|
||||
.where(({ not, exists, selectFrom }) => not(exists(selectFrom('schema_version').select('id'))))
|
||||
.where(({ not, exists, selectFrom }) => not(exists(selectFrom('users').select('id'))))
|
||||
.where(({ not, exists, selectFrom }) =>
|
||||
not(exists(selectFrom('app_settings').select('key').where('key', '=', PROVIDER))),
|
||||
),
|
||||
)
|
||||
.onConflict((conflict) => conflict.column('key').doNothing())
|
||||
.execute();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user