mirror of
https://github.com/cita-777/metapi.git
synced 2026-11-01 12:01:53 +08:00
- Implemented `normalizeMigrationInput` to standardize database migration input for sqlite, mysql, and postgres. - Added `maskConnectionString` to obscure sensitive credentials in connection strings. - Created comprehensive tests for migration input normalization and connection string masking. - Established database schema creation logic for multiple dialects (sqlite, mysql, postgres). - Developed functions for managing database transactions, including begin, commit, and rollback. - Implemented data insertion logic from backup snapshots into the target database.
63 lines
1.9 KiB
TypeScript
63 lines
1.9 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import { maskConnectionString, normalizeMigrationInput } from './databaseMigrationService.js';
|
|
|
|
describe('databaseMigrationService', () => {
|
|
it('accepts postgres migration input with normalized url', () => {
|
|
const normalized = normalizeMigrationInput({
|
|
dialect: 'postgres',
|
|
connectionString: ' postgres://user:pass@db.example.com:5432/metapi ',
|
|
overwrite: true,
|
|
});
|
|
|
|
expect(normalized).toEqual({
|
|
dialect: 'postgres',
|
|
connectionString: 'postgres://user:pass@db.example.com:5432/metapi',
|
|
overwrite: true,
|
|
});
|
|
});
|
|
|
|
it('accepts mysql migration input', () => {
|
|
const normalized = normalizeMigrationInput({
|
|
dialect: 'mysql',
|
|
connectionString: 'mysql://root:pass@db.example.com:3306/metapi',
|
|
});
|
|
|
|
expect(normalized.dialect).toBe('mysql');
|
|
expect(normalized.overwrite).toBe(true);
|
|
});
|
|
|
|
it('accepts sqlite file migration target path', () => {
|
|
const normalized = normalizeMigrationInput({
|
|
dialect: 'sqlite',
|
|
connectionString: './data/target.db',
|
|
overwrite: false,
|
|
});
|
|
|
|
expect(normalized).toEqual({
|
|
dialect: 'sqlite',
|
|
connectionString: './data/target.db',
|
|
overwrite: false,
|
|
});
|
|
});
|
|
|
|
it('rejects unknown dialect', () => {
|
|
expect(() => normalizeMigrationInput({
|
|
dialect: 'oracle',
|
|
connectionString: 'oracle://db',
|
|
} as any)).toThrow(/鏂硅█|sqlite\/mysql\/postgres/i);
|
|
});
|
|
|
|
it('rejects postgres input when scheme mismatches', () => {
|
|
expect(() => normalizeMigrationInput({
|
|
dialect: 'postgres',
|
|
connectionString: 'mysql://root:pass@127.0.0.1:3306/metapi',
|
|
})).toThrow(/postgres/i);
|
|
});
|
|
|
|
it('masks connection string credentials', () => {
|
|
const masked = maskConnectionString('postgres://admin:super-secret@db.example.com:5432/metapi');
|
|
expect(masked).toBe('postgres://admin:***@db.example.com:5432/metapi');
|
|
});
|
|
});
|
|
|