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

Cannot clear database inside a transaction #8527

Closed
varanauskas opened this issue Jan 14, 2022 · 1 comment
Closed

Cannot clear database inside a transaction #8527

varanauskas opened this issue Jan 14, 2022 · 1 comment

Comments

@varanauskas
Copy link
Contributor

Issue Description

queryRunner.clearDatabase() fails if trying to run inside a transaction.

Expected Behavior

It clears the database inside the transaction, then operations can be performed inside the transaction on a temporarily empty database, and that transaction can later be reverted

Actual Behavior

It throws TransactionAlreadyStartedError when trying to clearDatabase inside a transaction

// TransactionAlreadyStartedError: Transaction already started for the given connection, commit current transaction before starting a new one.

Steps to Reproduce

  1. Connect to a databsae
  2. Start Transaction
  3. clearDatabase
  4. It throws TransactionAlreadyStartedError
const connection = await createConnection();
const queryRunner = connection.createQueryRunner();
await queryRunner.startTransaction();
await queryRunner.clearDatabase();
// ^ This fails with `TransactionAlreadyStartedError`

My Environment

Dependency Version
Operating System macOS
Node.js version 16.13.1
Typescript version 4.5.4
TypeORM version 0.2.41

Additional Context

I want to clearDatabase inside a transaction, so I could get a temporary blank slate database, and then run migrations inside the same transaction, so I can determine what tables are missing after running all migrations regardless of, and without modifying the initial state of the database

Relevant Database Driver(s)

DB Type Reproducible
aurora-data-api no
aurora-data-api-pg no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql no
nativescript no
oracle no
postgres yes
react-native no
sap no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

Are you willing to resolve this issue by submitting a Pull Request?

  • ✅ Yes, I have the time, and I know how to start.
  • ✖️ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.
@varanauskas
Copy link
Contributor Author

I suggest either:

  • Checking if a transaction is in progress inside clearDatabase, if so, reverting a transaction that is being cleared
  • Adding an options = { startTransaction: true } argument to clearDatabase where a user could override the transaction starting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant