`rake db:create` creates a schema when `schema_search_path` is specified #10140

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
6 participants
@senny
Member

senny commented Apr 8, 2013

Closes #3624

After this patch rake db:create will create a schema if schema_search_path is specified. Currently it only works if schema_search_path is a single schema. I've seen a couple of tickets related to schema_search_path so I first wanted to ask what format we actually support. If something like first, second is a valid schema_search_path of course I'd have to adjust the patch to create two schemas.

@senny

This comment has been minimized.

Show comment
Hide comment
@gregmolnar

This comment has been minimized.

Show comment
Hide comment
@gregmolnar

gregmolnar May 2, 2014

Contributor

This one looks good to me.

Contributor

gregmolnar commented May 2, 2014

This one looks good to me.

@@ -15,11 +15,16 @@ def initialize(configuration)
def create(master_established = false)
establish_master_connection unless master_established
connection.create_database configuration['database'],
- configuration.merge('encoding' => encoding)
+ configuration.merge('encoding' => encoding)

This comment has been minimized.

@akshay-vishnoi

akshay-vishnoi May 3, 2014

Contributor

It was correct. Need indentation as this line defines the 2nd argument of connection.create_database.

@akshay-vishnoi

akshay-vishnoi May 3, 2014

Contributor

It was correct. Need indentation as this line defines the 2nd argument of connection.create_database.

@rafaelfranca

This comment has been minimized.

Show comment
Hide comment
@rafaelfranca

rafaelfranca May 4, 2014

Member

@senny please apply this patch.

Member

rafaelfranca commented May 4, 2014

@senny please apply this patch.

@matthewd

This comment has been minimized.

Show comment
Hide comment
@matthewd

matthewd May 5, 2014

Member

I don't think we should do this, any more than we'd mkdir everything listed in ENV['PATH'] -- creating necessary schemata seems the responsibility of schema.rb / structure.sql. My recollection is that there's a version of PG that [inadvertently] produces the error mentioned in #3624, and other versions treat it as the warning it's intended to be. (And if we're going to encounter said error, won't it be in establish_connection, before we get here?)

That said, if we do want this change, we probably don't want to stop iterating the list as soon as we encounter a schema that exists.

There's also the possibility of quoted schema names (potentially including commas). And then there's the $user complication.

Member

matthewd commented May 5, 2014

I don't think we should do this, any more than we'd mkdir everything listed in ENV['PATH'] -- creating necessary schemata seems the responsibility of schema.rb / structure.sql. My recollection is that there's a version of PG that [inadvertently] produces the error mentioned in #3624, and other versions treat it as the warning it's intended to be. (And if we're going to encounter said error, won't it be in establish_connection, before we get here?)

That said, if we do want this change, we probably don't want to stop iterating the list as soon as we encounter a schema that exists.

There's also the possibility of quoted schema names (potentially including commas). And then there's the $user complication.

@rafaelfranca

This comment has been minimized.

Show comment
Hide comment
@rafaelfranca

rafaelfranca May 5, 2014

Member

How would schema.rb create the database schema if we can't even create the database because the schema configure in the databse.yml doesn't exist?

Member

rafaelfranca commented May 5, 2014

How would schema.rb create the database schema if we can't even create the database because the schema configure in the databse.yml doesn't exist?

@matthewd

This comment has been minimized.

Show comment
Hide comment
@matthewd

matthewd May 5, 2014

Member

It's not (supposed to be) an error to set a search_path that contains a schema that doesn't exist.

% psql postgres
psql (9.3.1)
Type "help" for help.

[local]/postgres=# set search_path to lskdjflsdjf;
SET
[local]/postgres=# create table foo ();
ERROR:  no schema has been selected to create in
[local]/postgres=#
Member

matthewd commented May 5, 2014

It's not (supposed to be) an error to set a search_path that contains a schema that doesn't exist.

% psql postgres
psql (9.3.1)
Type "help" for help.

[local]/postgres=# set search_path to lskdjflsdjf;
SET
[local]/postgres=# create table foo ();
ERROR:  no schema has been selected to create in
[local]/postgres=#
@rafaelfranca

This comment has been minimized.

Show comment
Hide comment
@rafaelfranca

rafaelfranca May 5, 2014

Member

If there is no error we can close both issues.

Member

rafaelfranca commented May 5, 2014

If there is no error we can close both issues.

@matthewd

This comment has been minimized.

Show comment
Hide comment
@matthewd

matthewd May 5, 2014

Member

Apparently it was 9.1 that accidentally treated that situation as an error: postgres/postgres@880bfc3

And that was fixed in 9.1.4: postgres/postgres@35400e1

Member

matthewd commented May 5, 2014

Apparently it was 9.1 that accidentally treated that situation as an error: postgres/postgres@880bfc3

And that was fixed in 9.1.4: postgres/postgres@35400e1

@rafaelfranca

This comment has been minimized.

Show comment
Hide comment
@rafaelfranca

rafaelfranca May 5, 2014

Member

Thank you for looking on it

Member

rafaelfranca commented May 5, 2014

Thank you for looking on it

@senny senny deleted the senny:3624_create_missing_schemas branch May 6, 2014

@untidy-hair

This comment has been minimized.

Show comment
Hide comment
@untidy-hair

untidy-hair Jun 7, 2016

Contributor

Hi, I'd still like this sequence of commands to work even if there's a customized schema.

rake db:create
rake db:migrate

But I see this error

rake aborted!
ActiveRecord::StatementInvalid: PG::InvalidSchemaName: ERROR:  no schema has been selected to create in
: CREATE TABLE "schema_migrations" ("version" character varying NOT NULL)

So instead, I have to do

rake db:create
psql -c 'create schema schema_name;' -U user_name db_name
rake db:migrate

Rails 4.2.6
PostgreSQL 9.4.1

Like pointed out above, set search path itself does not fail but when you actually make actions on the non-existent schema, you'll get an error.

Contributor

untidy-hair commented Jun 7, 2016

Hi, I'd still like this sequence of commands to work even if there's a customized schema.

rake db:create
rake db:migrate

But I see this error

rake aborted!
ActiveRecord::StatementInvalid: PG::InvalidSchemaName: ERROR:  no schema has been selected to create in
: CREATE TABLE "schema_migrations" ("version" character varying NOT NULL)

So instead, I have to do

rake db:create
psql -c 'create schema schema_name;' -U user_name db_name
rake db:migrate

Rails 4.2.6
PostgreSQL 9.4.1

Like pointed out above, set search path itself does not fail but when you actually make actions on the non-existent schema, you'll get an error.

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