Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: added support for additional cockroachdb tests #13537

Merged
merged 7 commits into from
Jun 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .buildkite/publish/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ services:
- '5435:5432'

cockroachdb:
image: prismagraphql/cockroachdb-custom:22.1.0-beta.5
image: prismagraphql/cockroachdb-custom:22.1.0
restart: always
command: start-single-node --insecure
ports:
Expand Down
4 changes: 3 additions & 1 deletion .buildkite/test/docker-compose.14.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ services:
- TEST_MONGO_URI_MIGRATE=mongodb://root:prisma@mongodb_migrate:27017/tests-migrate?authSource=admin
# CockroachDB
- TEST_COCKROACH_URI=postgresql://prisma@cockroachdb:26257/tests
- TEST_COCKROACH_URI_MIGRATE=postgresql://prisma@cockroachdb:26257/tests-migrate
- TEST_COCKROACH_SHADOWDB_URI_MIGRATE=postgresql://prisma@cockroachdb:26257/tests-migrate-shadowdb
# Other vars
- BUILDKITE
- BUILDKITE_PARALLEL_JOB
Expand Down Expand Up @@ -75,7 +77,7 @@ services:
- '5435:5432'

cockroachdb:
image: prismagraphql/cockroachdb-custom:22.1.0-beta.5
image: prismagraphql/cockroachdb-custom:22.1.0
restart: always
command: start-single-node --insecure
ports:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ jobs:
TEST_MSSQL_SHADOWDB_JDBC_URI_MIGRATE: 'sqlserver://localhost:1433;database=tests-migrate-shadowdb;user=SA;password=Pr1sm4_Pr1sm4;trustServerCertificate=true;'
TEST_MONGO_URI_MIGRATE: 'mongodb://root:prisma@localhost:27017/tests-migrate?authSource=admin'
TEST_COCKROACH_URI: 'postgresql://prisma@localhost:26257/tests'
TEST_COCKROACH_URI_MIGRATE: 'postgresql://prisma@localhost:26257/tests-migrate'
TEST_COCKROACH_SHADOWDB_URI_MIGRATE: 'postgres://prisma:prisma@localhost:26257/tests-migrate-shadowdb'

- uses: codecov/codecov-action@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ services:
- '5435:5432'

cockroachdb:
image: prismagraphql/cockroachdb-custom:22.1.0-beta.5
image: prismagraphql/cockroachdb-custom:22.1.0
restart: always
command: start-single-node --insecure
ports:
Expand Down
204 changes: 204 additions & 0 deletions packages/migrate/src/__tests__/DbExecute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'path'
import stripAnsi from 'strip-ansi'

import { DbExecute } from '../commands/DbExecute'
import { setupCockroach, tearDownCockroach } from '../utils/setupCockroach'
import { setupMSSQL, tearDownMSSQL } from '../utils/setupMSSQL'
import { setupMysql, tearDownMysql } from '../utils/setupMysql'
import type { SetupParams } from '../utils/setupPostgres'
Expand Down Expand Up @@ -453,6 +454,199 @@ COMMIT;`,
})
})

describeIf(!process.env.TEST_SKIP_COCKROACHDB)('cockroachdb', () => {
const connectionString = (
process.env.TEST_COCKROACH_URI_MIGRATE || 'postgresql://prisma@localhost:26257/tests-migrate'
).replace('tests-migrate', 'tests-migrate-db-execute')

// Update env var because it's the one that is used in the schemas tested
process.env.TEST_COCKROACH_URI_MIGRATE = connectionString

const setupParams = {
connectionString,
dirname: '',
}

beforeAll(async () => {
await setupCockroach(setupParams).catch((e) => {
console.error(e)
})
})

afterAll(async () => {
await tearDownCockroach(setupParams).catch((e) => {
console.error(e)
})
})

const sqlScript = `-- Drop & Create & Drop
DROP SCHEMA IF EXISTS "test-dbexecute";
CREATE SCHEMA "test-dbexecute";
DROP SCHEMA "test-dbexecute";`

// eslint-disable-next-line jest/no-identical-title
it('should pass with --file --schema', async () => {
ctx.fixture('schema-only-cockroachdb')

fs.writeFileSync('script.sql', sqlScript)
const result = DbExecute.new().parse(['--schema=./prisma/schema.prisma', '--file=./script.sql'])
await expect(result).resolves.toMatchInlineSnapshot(`Script executed successfully.`)
}, 10000)

// eslint-disable-next-line jest/no-identical-title
it('should use env var from .env file', async () => {
ctx.fixture('schema-only-cockroachdb')

fs.writeFileSync('script.sql', sqlScript)
const result = DbExecute.new().parse(['--schema=./prisma/using-dotenv.prisma', '--file=./script.sql'])
await expect(result).rejects.toMatchInlineSnapshot(`
P1001

Can't reach database server at \`fromdotenvdoesnotexist\`:\`26257\`

Please make sure your database server is running at \`fromdotenvdoesnotexist\`:\`26257\`.

`)
}, 10000)

// eslint-disable-next-line jest/no-identical-title
it('should pass using a transaction with --file --schema', async () => {
ctx.fixture('schema-only-cockroachdb')

fs.writeFileSync(
'script.sql',
`-- start a transaction
BEGIN;

${sqlScript}

-- commit changes
COMMIT;`,
)
const result = DbExecute.new().parse(['--schema=./prisma/schema.prisma', '--file=./script.sql'])
await expect(result).resolves.toMatchInlineSnapshot(`Script executed successfully.`)
}, 10000)

// eslint-disable-next-line jest/no-identical-title
it('should pass with --file --url', async () => {
ctx.fixture('schema-only-cockroachdb')

fs.writeFileSync('script.sql', sqlScript)
const result = DbExecute.new().parse(['--url', connectionString, '--file=./script.sql'])
await expect(result).resolves.toMatchInlineSnapshot(`Script executed successfully.`)
})

// eslint-disable-next-line jest/no-identical-title
it('should pass with empty --file --url', async () => {
ctx.fixture('schema-only-cockroachdb')

fs.writeFileSync('script.sql', '')
const result = DbExecute.new().parse(['--url', connectionString, '--file=./script.sql'])
await expect(result).resolves.toMatchInlineSnapshot(`Script executed successfully.`)
})

// Cockroachdb doesn't have the same limitation as Postgres, as it can drop and create a database
// with a single SQL script.
it('should succeed if DROP DATABASE with --file --schema', async () => {
ctx.fixture('schema-only-cockroachdb')
expect.assertions(0)

fs.writeFileSync(
'script.sql',
`-- Drop & Create & Drop
DROP DATABASE IF EXISTS "test-dbexecute";
CREATE DATABASE "test-dbexecute";
DROP DATABASE "test-dbexecute";`,
)
try {
await DbExecute.new().parse(['--schema=./prisma/schema.prisma', '--file=./script.sql'])
} catch (e) {
expect(e.code).toEqual(undefined)
}
})

// eslint-disable-next-line jest/no-identical-title
it('should fail with P1013 error with invalid url with --file --url', async () => {
ctx.fixture('schema-only-cockroachdb')
expect.assertions(2)

fs.writeFileSync('script.sql', '-- empty')
try {
await DbExecute.new().parse([
'--url=postgresql://johndoe::::////::randompassword@doesnotexist/mydb',
'--file=./script.sql',
])
} catch (e) {
expect(e.code).toEqual('P1013')
expect(e.message).toMatchInlineSnapshot(`
P1013

The provided database string is invalid. invalid port number in database URL. Please refer to the documentation in https://www.prisma.io/docs/reference/database-reference/connection-urls for constructing a correct connection string. In some cases, certain characters must be escaped. Please check the string for any illegal characters.

`)
}
})

// eslint-disable-next-line jest/no-identical-title
it('should fail with P1013 error with invalid url provider with --file --url', async () => {
ctx.fixture('schema-only-cockroachdb')
expect.assertions(2)

fs.writeFileSync('script.sql', '-- empty')
try {
await DbExecute.new().parse(['--url=invalidurl', '--file=./script.sql'])
} catch (e) {
expect(e.code).toEqual('P1013')
expect(e.message).toMatchInlineSnapshot(`
P1013

The provided database string is invalid. \`invalidurl\` is not a known connection URL scheme. Prisma cannot determine the connector. in database URL. Please refer to the documentation in https://www.prisma.io/docs/reference/database-reference/connection-urls for constructing a correct connection string. In some cases, certain characters must be escaped. Please check the string for any illegal characters.

`)
}
})

// eslint-disable-next-line jest/no-identical-title
it('should fail with P1001 error with unreachable url with --file --url', async () => {
ctx.fixture('schema-only-cockroachdb')
expect.assertions(2)

fs.writeFileSync('script.sql', '-- empty')
try {
await DbExecute.new().parse([
'--url=postgresql://johndoe:randompassword@doesnotexist:5432/mydb?schema=public',
'--file=./script.sql',
])
} catch (e) {
expect(e.code).toEqual('P1001')
expect(e.message).toMatchInlineSnapshot(`
P1001

Can't reach database server at \`doesnotexist\`:\`5432\`

Please make sure your database server is running at \`doesnotexist\`:\`5432\`.

`)
}
})

// eslint-disable-next-line jest/no-identical-title
it('should fail with invalid SQL error from database with --file --schema', async () => {
ctx.fixture('schema-only-cockroachdb')

fs.writeFileSync('script.sql', 'ThisisnotSQLitshouldfail')
const result = DbExecute.new().parse(['--schema=./prisma/schema.prisma', '--file=./script.sql'])
await expect(result).rejects.toThrowErrorMatchingInlineSnapshot(`
db error: ERROR: at or near "thisisnotsqlitshouldfail": syntax error
DETAIL: source SQL:
ThisisnotSQLitshouldfail
^


`)
})
})

describe('mysql', () => {
const connectionString = (
process.env.TEST_MYSQL_URI_MIGRATE || 'mysql://root:root@localhost:3306/tests-migrate'
Expand Down Expand Up @@ -646,6 +840,7 @@ DROP DATABASE IF EXISTS "test-dbexecute";
CREATE DATABASE "test-dbexecute";
DROP DATABASE "test-dbexecute";`

// eslint-disable-next-line jest/no-identical-title
it('should pass with --file --schema', async () => {
ctx.fixture('schema-only-sqlserver')

Expand All @@ -662,6 +857,7 @@ DROP DATABASE "test-dbexecute";`
await expect(result).resolves.toMatchInlineSnapshot(`Script executed successfully.`)
})

// eslint-disable-next-line jest/no-identical-title
it('should pass with --file --url', async () => {
ctx.fixture('schema-only-sqlserver')

Expand All @@ -670,6 +866,7 @@ DROP DATABASE "test-dbexecute";`
await expect(result).resolves.toMatchInlineSnapshot(`Script executed successfully.`)
})

// eslint-disable-next-line jest/no-identical-title
it('should pass using a transaction with --file --schema', async () => {
ctx.fixture('schema-only-sqlserver')

Expand All @@ -689,6 +886,7 @@ COMMIT;`,

// Limitation of sqlserver
// DROP DATABASE statement cannot be used inside a user transaction.
// eslint-disable-next-line jest/no-identical-title
it('should fail if DROP DATABASE in a transaction with --file --schema', async () => {
ctx.fixture('schema-only-sqlserver')

Expand All @@ -710,6 +908,7 @@ COMMIT;`,
`)
})

// eslint-disable-next-line jest/no-identical-title
it('should fail with P1013 error with invalid url with --file --url', async () => {
ctx.fixture('schema-only-sqlserver')
expect.assertions(2)
Expand All @@ -730,6 +929,8 @@ COMMIT;`,
`)
}
})

// eslint-disable-next-line jest/no-identical-title
it('should fail with P1013 error with invalid url provider with --file --url', async () => {
ctx.fixture('schema-only-sqlserver')
expect.assertions(2)
Expand All @@ -748,6 +949,7 @@ COMMIT;`,
}
})

// eslint-disable-next-line jest/no-identical-title
it('should fail with P1001 error with unreachable url with --file --url', async () => {
ctx.fixture('schema-only-sqlserver')
expect.assertions(2)
Expand All @@ -771,6 +973,7 @@ COMMIT;`,
}
})

// eslint-disable-next-line jest/no-identical-title
it('should fail with SQL error from database with --file --schema', async () => {
ctx.fixture('schema-only-sqlserver')

Expand All @@ -783,6 +986,7 @@ COMMIT;`,
`)
})

// eslint-disable-next-line jest/no-identical-title
it('should fail with invalid SQL error from database with --file --schema', async () => {
ctx.fixture('schema-only-sqlserver')

Expand Down