remove v0 -> v1 migration code

This commit is contained in:
Sylver 2024-01-07 11:33:58 +08:00
parent 757e53fba4
commit aec85d03a6
3 changed files with 3 additions and 100 deletions

View File

@ -1,15 +0,0 @@
# migrating
## from micro 0.0.x to micro 1.0.0
I've made a best effort attempt to make migration as painless as possible, mostly for my own sanity. These steps are quite in-depth but in reality the migration should be fairly simple for most users. If you get stuck at any point, please join the [discord server](https://discord.gg/VDMX6VQRZm) and ask for help.
1. Create a backup of the database and the data directory.
2. Update your `.microrc` with the changes seen in [example config](example/.microrc.yaml) (your config may be in json with the example now being yaml, but the keys are 1:1), notable changes are `database` is now `databaseUrl`.
3. Change the docker image from `sylver/micro` or `sylver/micro:master` to `sylver/micro:main`
4. Change the port from `8080` to `3000`. If you are using the example config, do this in `Caddyfile` by changing `micro:8080` to `micro:3000`.
5. Start the container. It should exit on startup with an error message saying that there is data that must be migrated. If it does not, you did not update the image tag correctly or it cannot detect data to be migrated.
6. Read the error message, then stop the container and set the `MIGRATE_OLD_DATABASE` environment variable to `true`
7. Start the container and it will migrate the database automatically.
After that, you should be able to use it as normal. Thumbnails are the only data that is not migrated, as the format changed and it doesn't really matter because they can just be regenerated on demand. If you run into any issues during migration, join the [discord server](https://discord.gg/VDMX6VQRZm) or open an issue on [github](https://github.com/sylv/micro/issues/new).

View File

@ -1,76 +0,0 @@
import type { Connection, IMigrator } from '@mikro-orm/core';
import type { EntityManager } from '@mikro-orm/postgresql';
import { Logger } from '@nestjs/common';
import dedent from 'dedent';
const logger = new Logger('micro');
const migrationErrorWarning = dedent`
An old database for a previous version of micro was found.
To use this database with the new version, it must be migrated to the new format.
This can be done automatically, but first you need to create a database backup and ensure it works.
I am not kidding, do a backup now and make sure it works. If you skip this step and things go wrong, its on you.
Once you have a backup and you are sure that backup works, you can continue on the migration path.
To get started, start micro with the "MIGRATE_OLD_DATABASE" environment variable set to "true".
On startup the database will be migrated to the new format automatically, then startup will continue as normal.
If anything goes wrong during the migration, create a new issue on GitHub https://github.com/sylv/micro/issues/new immediately.
`;
const legacyMigrationWarning = dedent`
You have set "MIGRATE_OLD_DATABASE" to "true", so the old database will be migrated to the new format.
This may take some time, please be patient.
`;
export async function checkForOldDatabase(connection: Connection) {
logger.debug(`Checking for old database`);
const result = await connection.execute(
`SELECT EXISTS(SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = '_prisma_migrations')`
);
return result[0].exists;
}
// https://tenor.com/vGfQ.gif
export async function migrateOldDatabase(em: EntityManager, migrator: IMigrator) {
logger.debug(`Migrating old database`);
if (process.env.MIGRATE_OLD_DATABASE !== 'true') {
logger.error(migrationErrorWarning);
process.exit(1);
}
logger.warn(legacyMigrationWarning);
await em.transactional(async (em) => {
const trx = em.getTransactionContext();
const execute = (sql: string) =>
em
.createQueryBuilder('files')
.raw(sql)
.transacting(trx)
.then((result) => result);
await execute(`CREATE SCHEMA public_old`);
const tables = ['files', 'users', 'links', 'thumbnails', '_prisma_migrations'];
for (const table of tables) {
await execute(`ALTER TABLE "public"."${table}" SET SCHEMA "public_old"`);
}
await migrator.up({ transaction: trx });
await execute(`UPDATE public_old.users SET tags = array[]::text[] WHERE tags IS NULL`);
await execute(dedent`
INSERT INTO public.users (id, username, permissions, password, secret, tags)
SELECT id, username, permissions, password, secret, tags FROM public_old.users
`);
await execute(dedent`
INSERT INTO public.files (id, host, type, size, hash, name, owner_id, created_at)
SELECT id, host, type, size, hash, name, "ownerId", "createdAt" FROM public_old.files
`);
await execute(dedent`
INSERT INTO public.links (id, destination, host, clicks, created_at, owner_id)
SELECT id, destination, host, clicks, "createdAt", "ownerId" FROM public_old.links
`);
});
}

View File

@ -2,26 +2,20 @@ import type { Options } from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/core';
import type { EntityManager } from '@mikro-orm/postgresql';
import { Logger } from '@nestjs/common';
import { checkForOldDatabase, migrateOldDatabase } from './helpers/migrate-old-database.js';
import mikroOrmConfig, { MIGRATIONS_TABLE_NAME, ORM_LOGGER } from './orm.config.js';
const logger = new Logger('migrate');
export const migrate = async (
config: Options = mikroOrmConfig,
skipLock = process.env.SKIP_MIGRATION_LOCK === 'true'
skipLock = process.env.SKIP_MIGRATION_LOCK === 'true',
) => {
logger.debug(`Checking for and running migrations`);
const orm = await MikroORM.init(config);
const em = orm.em.fork({ clear: true }) as EntityManager;
const connection = em.getConnection();
const migrator = orm.getMigrator();
const oldDatabaseExists = await checkForOldDatabase(connection);
if (oldDatabaseExists) {
await migrateOldDatabase(em, migrator);
return;
}
const migrator = orm.getMigrator();
const executedMigrations = await migrator.getExecutedMigrations();
const pendingMigrations = await migrator.getPendingMigrations();
if (!pendingMigrations[0]) {