使用 testcontainers 和 prisma orm 安装 postgis 时出错

问题描述 投票:0回答:1

我正在使用 testcontainers 创建 e2e 测试,我有一个为 Postgres 安装

Postgis
扩展的迁移,但我收到以下错误:

Command failed: npx prisma migrate dev
    Error: P3006

    Migration `20230524164747_add_postgis` failed to apply cleanly to the shadow database. 
    db error: ERROR: could not open extension control file "/usr/local/share/postgresql/extension/postgis.control": No such file or directory
       0: sql_schema_connector::validate_migrations
               with namespaces=None
                 at schema-engine/connectors/sql-schema-connector/src/lib.rs:301
       1: schema_core::state::DevDiagnostic
                 at schema-engine/core/src/state.rs:266

      24 |
      25 |     // criar as tabelas definidas no prisma no banco de dados
    > 26 |     execSync('npx prisma migrate dev', {
         |             ^
      27 |       env: {
      28 |         ...process.env,
      29 |         DATABASE_URL: urlConnection,

      at Object.<anonymous> (src/test/app.e2e-spec.ts:26:13)

我的迁移:

-- CreateExtension
CREATE EXTENSION IF NOT EXISTS "postgis";

我的测试:

describe('test POST /user', () => {
  let prisma: PrismaClient;
  let app: INestApplication;
  let container: StartedPostgreSqlContainer;

  beforeAll(async () => {
    container = await new PostgreSqlContainer().start();

    const urlConnection = `postgresql://${container.getUsername()}:${container.getPassword()}@${container.getHost()}:${container.getPort()}/${container.getDatabase()}?schema=public`;

    process.env.DATABASE_URL = urlConnection;

    execSync('npx prisma migrate dev', {
      env: {
        ...process.env,
        DATABASE_URL: urlConnection,
      },
    });

    const moduleRef = await Test.createTestingModule({
      imports: [UserModule],
    }).compile();

    app = moduleRef.createNestApplication();

    prisma = new PrismaClient({
      datasources: {
        db: {
          url: urlConnection,
        },
      },
    });

    await app.init();
  });
});

迁移已成功应用,安装此扩展的迁移除外。 容器创建成功! 为什么我无法安装

Postgis

node.js postgresql postgis prisma testcontainers
1个回答
0
投票

我找到了一个解决方案,使用

GenericContainer
和 postgis 图像:

dotenv.config({ path: '.env.test' });
const prismaBinary = './node_modules/.bin/prisma';

describe('[e2e] create user', () => {
  let prisma: PrismaClient;
  let app: INestApplication;
  let container: StartedTestContainer;
  const schema = `test_${randomUUID()}`;

  beforeAll(async () => {
    container = await new GenericContainer('postgis/postgis:16-3.4-alpine')
      .withEnvironment({
        POSTGRES_HOST: dbHost,
        POSTGRES_PASSWORD: dbPass,
        POSTGRES_USER: dbUser,
        POSTGRES_DB: dbName,
      })
      .withExposedPorts({ container: dbPort, host: dbPort })
      .start();

    const urlConnection = `postgresql://${dbUser}:${dbPass}@${dbHost}:${dbPort}/${dbName}?schema=${schema}`;
    process.env.DATABASE_URL = urlConnection;

    execSync(`${prismaBinary} migrate deploy`, {
      env: {
        ...process.env,
        DATABASE_URL: urlConnection,
      },
    });

    const moduleRef = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();
    app = moduleRef.createNestApplication();
    app.useGlobalPipes(new ValidationPipe());

    prisma = new PrismaClient({
      datasources: {
        db: {
          url: urlConnection,
        },
      },
    });

    await app.init();
  });

  afterAll(async () => {
    await container.stop();
  });
});

你可能需要增加jest timeOut,安装postgis需要一段时间

jest.setTimeout(30000);
© www.soinside.com 2019 - 2024. All rights reserved.