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

transaction:false not working on PostgreSQL #285

Closed
kashifsoofi opened this issue Mar 24, 2022 · 13 comments
Closed

transaction:false not working on PostgreSQL #285

kashifsoofi opened this issue Mar 24, 2022 · 13 comments

Comments

@kashifsoofi
Copy link

Hi,
We are using dbmate for migrations, we are running into an issue where we want to run following

-- migrate:up transaction:false

CREATE INDEX CONCURRENTLY index_name ON table(new_id);

We are getting following error
Error: pq: CREATE INDEX CONCURRENTLY cannot run inside a transaction block

I didn't had to chance to run dbmate from source, has anyone else run into similar?

Thanks

@rohitpaulk
Copy link

@kashifsoofi could you confirm what version of dbmate you're running?

@rohitpaulk
Copy link

The transaction:false option was added in https://github.com/amacneil/dbmate/releases/tag/v1.5.0, I want to make sure that you're running a version that includes that change.

@kashifsoofi
Copy link
Author

I have tested with dbmate version 1.11.0 and dbmate version 1.14.0, from code it looks like it should not create a transaction

@rohitpaulk
Copy link

Got it, thanks! Will see if I can replicate.

@kashifsoofi
Copy link
Author

If I run a single CREATE INDEX CONCURRENTLY statement then there is no error

@rohitpaulk
Copy link

@kashifsoofi the example in the description has only one statement - could you paste an example file here that triggers the error?

@kashifsoofi
Copy link
Author

kashifsoofi commented Mar 24, 2022

Modifying test file in dbmate code

-- migrate:up
create table users (
  id integer,
  name varchar(255)
);
insert into users (id, name) values (1, 'alice');

CREATE INDEX CONCURRENTLY index_users_on_id ON users(id);

-- migrate:down
drop table users;

Like I said, if i create another migration file, with only CREATE INDEX statement it works, work around would be to create lot of files :(

@kashifsoofi
Copy link
Author

kashifsoofi commented Mar 24, 2022

I think the solution would be to somehow run each statement 1 by 1 incase there are multiple statements in a migration file. Maybe with an option multi-statement:true so that script owner can control it 🤔

@kashifsoofi
Copy link
Author

Something like this

func (m migrationOptions) MultiStatement() bool {
	if val, ok := m["multi-statement"]; ok {
		return val != "true"
	}

	return false
}

And then in db.go

...
			err = doTransaction(sqlDB, execMigration)
		} else if up.Options.MultiStatement() {
			var stmtSeparatorRegExp = regexp.MustCompile(`;`)
			stmts := stmtSeparatorRegExp.Split(up.Contents, -1)

			for i, stmt := range stmts {
				err = execMigrationStmt(sqlDB, stmt, i+1 == len(stmts))
				if err != nil {
					break
				}
			}
		} else {
			// run outside of transaction
...

@rohitpaulk
Copy link

#285 (comment)

@kashifsoofi if you use transaction:false in the script in that comment, does the error still occur? i.e. something like this:

-- migrate:up transaction:false
create table users (
  id integer,
  name varchar(255)
);
insert into users (id, name) values (1, 'alice');

CREATE INDEX CONCURRENTLY index_users_on_id ON users(id);

-- migrate:down
drop table users;

@kashifsoofi
Copy link
Author

@rohitpaulk Yes it does occur, however if I have file with only single step then there is no error

-- migrate:up transaction:false
CREATE INDEX CONCURRENTLY index_users_on_id ON users(id);

-- migrate:down

@x-EricH-x
Copy link

x-EricH-x commented Nov 24, 2022

@rohitpaulk Yes it does occur, however if I have file with only single step then there is no error

-- migrate:up transaction:false
CREATE INDEX CONCURRENTLY index_users_on_id ON users(id);

-- migrate:down

I can confirm the same issue,
-- migrate:up transaction:false works when there is only one statement,
and fails when there are multiple.

@amacneil
Copy link
Owner

Duplicate of #182

@amacneil amacneil marked this as a duplicate of #182 Dec 19, 2022
@amacneil amacneil closed this as not planned Won't fix, can't repro, duplicate, stale Dec 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants