mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 00:43:15 +00:00
feat(maps): start new installations with MapLibre Liberty
This commit is contained in:
@@ -2,6 +2,7 @@ import { AddonSeeder } from './AddonSeeder';
|
||||
import { AdminSeeder } from './AdminSeeder';
|
||||
import { CategorySeeder } from './CategorySeeder';
|
||||
import { DocumentProviderSeeder } from './DocumentProviderSeeder';
|
||||
import { MapDefaultsSeeder } from './MapDefaultsSeeder';
|
||||
import { PhotoProviderSeeder } from './PhotoProviderSeeder';
|
||||
import { SchemaVersionSeeder } from './SchemaVersionSeeder';
|
||||
import type { EntityManager } from '@mikro-orm/core';
|
||||
@@ -10,13 +11,13 @@ import { Seeder } from '@mikro-orm/seeder';
|
||||
/**
|
||||
* The default seeder, replacing `db/seeds.ts::runSeeds()`.
|
||||
*
|
||||
* Order matters in one place only: `AdminSeeder` runs first because it is the
|
||||
* one that keys off an empty `users` table, and the provider seeders run after
|
||||
* `AddonSeeder` so the shelf exists before anything hangs off it.
|
||||
* Map defaults are set before the first admin is created. The provider seeders
|
||||
* run after `AddonSeeder` so their addon already exists.
|
||||
*/
|
||||
export class DatabaseSeeder extends Seeder {
|
||||
async run(em: EntityManager): Promise<void> {
|
||||
await this.call(em, [
|
||||
MapDefaultsSeeder,
|
||||
AdminSeeder,
|
||||
CategorySeeder,
|
||||
AddonSeeder,
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import type { DB } from '../kysely/db';
|
||||
import type { EntityManager } from '@mikro-orm/core';
|
||||
import { Seeder } from '@mikro-orm/seeder';
|
||||
import type { SqlEntityManager } from '@mikro-orm/sql';
|
||||
import { OPENFREEMAP_DEFAULT_STYLE } from '@trek/shared';
|
||||
|
||||
const PROVIDER = 'default_user_setting_map_provider';
|
||||
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 em.transactional(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 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();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -18,8 +18,10 @@ async function main(): Promise<void> {
|
||||
fs.rmSync(schemaSnapshotPath(), { force: true });
|
||||
|
||||
const { getRawConnection } = await import('../src/db/database');
|
||||
const { AppSettings } = await import('../src/db/entities/AppSettings.entity');
|
||||
const { runSchemaBootstrap } = await import('../src/db/orm');
|
||||
const { createStandaloneOrm } = await import('../src/db/standalone-orm');
|
||||
const { deleteRows } = await import('./helpers/factories/rows');
|
||||
|
||||
// The shared driver binds to the connection database.ts opens on the ORM's
|
||||
// first connect, so this migrates that in-memory database and it can be
|
||||
@@ -27,6 +29,10 @@ async function main(): Promise<void> {
|
||||
const orm = await createStandaloneOrm();
|
||||
try {
|
||||
await runSchemaBootstrap(orm, { snapshot: true });
|
||||
// Suites using the historical snapshot opt into admin defaults themselves.
|
||||
await deleteRows(orm, AppSettings, {
|
||||
key: { $in: ['default_user_setting_map_provider', 'default_user_setting_maplibre_style'] },
|
||||
});
|
||||
writeSchemaSnapshot(getRawConnection().serialize());
|
||||
console.log(`[test-db] schema snapshot written to ${schemaSnapshotPath()}`);
|
||||
} finally {
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import { buildApp } from '../../src/bootstrap';
|
||||
import { AppSettings } from '../../src/db/entities/AppSettings.entity';
|
||||
import { SchemaVersion } from '../../src/db/entities/SchemaVersion.entity';
|
||||
import { Users } from '../../src/db/entities/Users.entity';
|
||||
import { MapDefaultsSeeder } from '../../src/db/seeders/MapDefaultsSeeder';
|
||||
import { deleteRows, insertRow } from '../helpers/factories/rows';
|
||||
import { readAppSetting, setAppSetting } from '../helpers/factories/settings';
|
||||
import { makeUser } from '../helpers/factories/users';
|
||||
import { MikroORM } from '@mikro-orm/core';
|
||||
import type { INestApplication } from '@nestjs/common';
|
||||
import { OPENFREEMAP_DEFAULT_STYLE } from '@trek/shared';
|
||||
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
|
||||
|
||||
const PROVIDER = 'default_user_setting_map_provider';
|
||||
const STYLE = 'default_user_setting_maplibre_style';
|
||||
|
||||
let app: INestApplication;
|
||||
let orm: MikroORM;
|
||||
|
||||
beforeAll(async () => {
|
||||
app = await buildApp();
|
||||
orm = app.get(MikroORM);
|
||||
}, 60_000);
|
||||
|
||||
beforeEach(async () => {
|
||||
await deleteRows(orm, AppSettings, { key: { $in: [PROVIDER, STYLE] } });
|
||||
await deleteRows(orm, SchemaVersion, {});
|
||||
await deleteRows(orm, Users, {});
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app?.close();
|
||||
});
|
||||
|
||||
const seed = () => new MapDefaultsSeeder().run(orm.em.fork());
|
||||
|
||||
describe('fresh-install map defaults', () => {
|
||||
it('sets MapLibre Liberty once and keeps a style explicitly chosen later', async () => {
|
||||
await seed();
|
||||
expect(await readAppSetting(orm, PROVIDER)).toBe(JSON.stringify('maplibre-gl'));
|
||||
expect(await readAppSetting(orm, STYLE)).toBe(JSON.stringify(OPENFREEMAP_DEFAULT_STYLE));
|
||||
await setAppSetting(orm, STYLE, JSON.stringify('trek:ofm-auto'));
|
||||
await seed();
|
||||
expect(await readAppSetting(orm, STYLE)).toBe(JSON.stringify('trek:ofm-auto'));
|
||||
});
|
||||
|
||||
it('keeps the provider and style of an install with an explicit default', async () => {
|
||||
await setAppSetting(orm, PROVIDER, JSON.stringify('leaflet'));
|
||||
await seed();
|
||||
expect(await readAppSetting(orm, PROVIDER)).toBe(JSON.stringify('leaflet'));
|
||||
expect(await readAppSetting(orm, STYLE)).toBeNull();
|
||||
});
|
||||
|
||||
it('leaves an existing installation waiting for its first SSO user alone', async () => {
|
||||
await insertRow(orm, SchemaVersion, { version: 258 });
|
||||
await seed();
|
||||
expect(await readAppSetting(orm, PROVIDER)).toBeNull();
|
||||
expect(await readAppSetting(orm, STYLE)).toBeNull();
|
||||
});
|
||||
|
||||
it('leaves an install with users alone even if the legacy marker is missing', async () => {
|
||||
await makeUser(orm);
|
||||
await seed();
|
||||
expect(await readAppSetting(orm, PROVIDER)).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user