SchemaPlus / schema_plus_views

Provides support for creating and dropping views in ActiveRecord migrations. Also support for querying views.
MIT License
28 stars 16 forks source link

Create Views in database with each column on a separate line shows up with /n in schema #3

Closed RichardGrossman closed 9 years ago

RichardGrossman commented 9 years ago

In the database schema, the view looks like this:

CREATE OR REPLACE VIEW usersx AS SELECT users.id, users.client_id, users.username, users.firstname

With the schema_plus_views gem, the schema.rb looks like this:

CREATE OR REPLACE VIEW usersx AS SELECT users.id,\n users.client_id,\n users.usernam\n, users.firstname\n ...

postgresql rake 10.4.2 rails 4.2.0 ruby 2.2.1p85 osx yosemite

ronen commented 9 years ago

@RichardGrossman the schema.rb should look like:

create_view "usersx", "SELECT users.id,\n users.client_id\n users.firstname FROM users [...]"

i.e. the \n is in a quoted string so should turn back into a newline when ruby parses the string.

So is there a problem...?

smostovoy commented 9 years ago

hey guys, I have the opposite problem - my schema.rb gets no \n when all my colleagues have them. We seem to have same PG version, ruby, gem and OS.
We have reset our DBs, I have reinstalled ruby - stays the same.

RichardGrossman commented 9 years ago

I'm getting this in schema.rb - the create_view's are all on one really long line, whereas tables are indented etc.:

create_view "vzcf_occurrences", " SELECT 'occurrences'::character(20) AS source,\n ('/request/'::text || btrim((r.request_number)::text)) AS vzcf_url,\n o.id,\n 0 AS series_id,\n 0 AS sequence_id,\n s.id AS space_id,\n s.name AS space_name,\n s.slug AS space_slug,\n s.property_id,\n p.name AS property_name,\n p.slug AS property_slug,\n r.user_id,\n (((u.firstname)::text || ' '::text) || (u.lastname)::text) AS user_name,\n r.title,\n r.description,\n o.start_datetime,\n o.end_datetime,\n (o.end_datetime - o.start_datetime) AS duration,\n date_part('hours'::text, (o.end_datetime - o.start_datetime)) AS hours,\n CASE\n WHEN (date_part('hours'::text, (o.end_datetime - o.start_datetime)) >= (23)::double precision) THEN true\n ELSE false\n END AS all_day,\n o.max_attendance,\n o.updated_at\n FROM ((((occurrences o\n JOIN requests r ON ((o.request_id = r.id)))\n JOIN spaces s ON ((o.space_id = s.id)))\n JOIN users u ON ((r.user_id = u.id)))\n JOIN properties p ON ((s.property_id = p.id)))", :force => true

create_table "workflow_templates", force: :cascade do |t| t.integer "space_id" t.integer "workflow_order" t.string "workflow_description" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "workflow_label" end

ronen commented 9 years ago

I'm getting this in schema.rb - the create_view's are all on one really long line, whereas tables are indented etc.

@RichardGrossman Yes, that's to be expected. create_view takes as its argument a character string of literal SQL, whereas create_table is a ruby method that takes a block of ruby code.

That said, I agree a long string with embedded newlines is hard to read. So I've changed the dumper to generate the string in the form of a multiline 'here document'. If you update to release 0.2.2, it should now look something like:

create_view "vzcf_occurrences", <<-END_VIEW_VZCF_OCCURRENCES, :force => true
 SELECT 'occurrences'::character(20) AS source,
 ('/request/'::text || btrim((r.request_number)::text)) AS vzcf_url,
 o.id,
 0 AS series_id,
 0 AS sequence_id,
 s.id AS space_id,
 ...etc...
END_VIEW_VZCF_OCCURRENCES

@smostovoy Can't say I know offhand what's going in your case... schema_plus_views just prints the string it gets back from the database connection, unmodified except for escaping special characters (and now splitting on newlines to form the heredoc). Maybe there's some sort of localization set up in your environment that's different from your colleagues?

RichardGrossman commented 9 years ago

I was just about to specifically request using %w{ or eos<< !

Thanks!

ronen commented 9 years ago

OK, going to close out this issue.

@smostovoy feel free to open a new issue if you find something that schema_plus_views ought to be doing differently