Skip to content

Commit

Permalink
Fix "missing keyword: :enum_type" in migrations (#1133)
Browse files Browse the repository at this point in the history
# What it does

I think this makes migrations work!

The `activerecord-postgres_enum` gem was updated across a major version
bump in #928

The 2.0.0 gem release notes include the change:

> BREAKING Renamed enum_name to enum_type because Rails 7 added basic
support for enums

# Why it is important

I believe this is necessary for `db:migrate` to work.

# Implementation notes

The `schema.rb` changes look to be cosmetic reordering _except_ the
dropping of this line:

```
enable_extension "pg_stat_statements"
```

It looks like this is never added via a migration. From some history
spelunking I see it was:

 1. Added in ba23291
 2. Removed explicitly in 6b2e1d2
 3. Added back in 8e35c8f

Do we know if we want this included or not? If so it looks like we can
[enable it in a
migration](https://stackoverflow.com/questions/35180280/rebuild-database-enable-extension-pg-stat-statements-line-got-removed-in-schema)
to get it to stick in the schema.

# Your bandwidth for additional changes to this PR

_Please choose one of the following to help the project maintainers
provide the appropriate level of support:_

- [x] I have the time and interest to make additional changes to this PR
based on feedback.
- [ ] I am interested in feedback but don't need to make the changes
myself.
- [ ] I don't have time or interest in making additional changes to this
work.
- [ ] Other or not sure (please describe):
  • Loading branch information
phinze authored Sep 28, 2023
1 parent f5d0b34 commit 1d441e9
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def change
create_enum :adjustment_source, ["cash", "square"]

change_table :adjustments do |t|
t.enum :payment_source, enum_name: :adjustment_source
t.enum :payment_source, enum_type: :adjustment_source
t.column :square_transaction_id, :string
t.change :adjustable_type, :string, null: true
t.change :adjustable_id, :bigint, null: true
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20190911020605_add_kind_to_adjustments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def change
create_enum :adjustment_kind, ["fine", "membership", "donation", "payment"]

change_table :adjustments do |t|
t.enum :kind, enum_name: :adjustment_kind
t.enum :kind, enum_type: :adjustment_kind
end

reversible do |dir|
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20191102002601_create_notifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def change
t.string :action, null: false
t.references :member, null: false, foreign_key: true
t.uuid :uuid, null: false
t.enum :status, enum_name: :notification_status, default: "pending", null: false
t.enum :status, enum_type: :notification_status, default: "pending", null: false
t.string :subject, null: false

t.timestamps
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20200621211015_add_roles_to_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def change
create_enum :user_role, ["staff", "admin"]

change_table :users do |t|
t.enum :role, enum_name: :user_role, default: "staff", null: false
t.enum :role, enum_type: :user_role, default: "staff", null: false
end
end
end
2 changes: 1 addition & 1 deletion db/migrate/20200710004507_create_hold_requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def change
t.string :email
t.references :member
t.string :notes
t.enum :status, enum_name: :hold_request_status, null: false, default: "new"
t.enum :status, enum_type: :hold_request_status, null: false, default: "new"
t.timestamps
end
end
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20201002020141_add_power_source_to_items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def change
create_enum :power_source, ["solar", "gas", "air", "electric (corded)", "electric (battery)"]

change_table :items do |t|
t.enum :power_source, enum_name: :power_source
t.enum :power_source, enum_type: :power_source
end
end
end
2 changes: 1 addition & 1 deletion db/migrate/20201112012007_create_item_attachments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def change
create_table :item_attachments do |t|
t.belongs_to :item, null: false, foreign_key: true
t.belongs_to :creator, null: false, foreign_key: {to_table: :users}
t.enum :kind, enum_name: :item_attachment_kind
t.enum :kind, enum_type: :item_attachment_kind
t.string :other_kind
t.string :notes

Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20210204025839_create_renewal_requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def change
create_enum :renewal_request_status, ["requested", "approved", "rejected"]

create_table :renewal_requests do |t|
t.enum :status, enum_name: :renewal_request_status, null: false, default: "requested"
t.enum :status, enum_type: :renewal_request_status, null: false, default: "requested"
t.belongs_to :loan, foreign_key: true
t.timestamps
end
Expand Down
174 changes: 87 additions & 87 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,75 +11,75 @@
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2022_10_30_174955) do

# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
enable_extension "plpgsql"

create_enum :adjustment_kind, [
"fine",
"membership",
"donation",
"payment"
"payment",
], force: :cascade

create_enum :adjustment_source, [
"cash",
"square",
"forgiveness"
"forgiveness",
], force: :cascade

create_enum :hold_request_status, [
"new",
"completed",
"denied"
"denied",
], force: :cascade

create_enum :item_attachment_kind, [
"manual",
"parts_list",
"other"
"other",
], force: :cascade

create_enum :item_status, [
"pending",
"active",
"maintenance",
"retired"
"retired",
], force: :cascade

create_enum :notification_status, [
"pending",
"sent",
"bounced",
"error"
"error",
], force: :cascade

create_enum :power_source, [
"solar",
"gas",
"air",
"electric (corded)",
"electric (battery)"
"electric (battery)",
], force: :cascade

create_enum :renewal_request_status, [
"requested",
"approved",
"rejected"
"rejected",
], force: :cascade

create_enum :ticket_status, [
"assess",
"parts",
"repairing",
"resolved"
"resolved",
], force: :cascade

create_enum :user_role, [
"staff",
"admin",
"member",
"super_admin"
"super_admin",
], force: :cascade

create_table "action_text_rich_texts", force: :cascade do |t|
Expand Down Expand Up @@ -254,8 +254,8 @@
t.string "description"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.jsonb "attendees"
t.integer "library_id"
t.jsonb "attendees"
t.index ["calendar_id", "calendar_event_id"], name: "index_events_on_calendar_id_and_calendar_event_id", unique: true
t.index ["library_id"], name: "index_events_on_library_id"
end
Expand All @@ -282,8 +282,8 @@
t.datetime "updated_at", precision: 6, null: false
t.datetime "ended_at"
t.bigint "loan_id"
t.datetime "started_at"
t.integer "library_id"
t.datetime "started_at"
t.datetime "expires_at"
t.index ["creator_id"], name: "index_holds_on_creator_id"
t.index ["item_id"], name: "index_holds_on_item_id"
Expand Down Expand Up @@ -321,10 +321,10 @@
t.string "checkout_notice"
t.integer "holds_count", default: 0, null: false
t.string "other_names"
t.integer "library_id"
t.enum "power_source", enum_type: "power_source"
t.text "location_area"
t.text "location_shelf"
t.integer "library_id"
t.text "plain_text_description"
t.string "url"
t.string "purchase_link"
Expand Down Expand Up @@ -412,8 +412,8 @@
t.string "region"
t.integer "number"
t.text "pronouns", default: [], array: true
t.string "pronunciation"
t.integer "library_id"
t.string "pronunciation"
t.index ["library_id"], name: "index_members_on_library_id"
t.index ["number", "library_id"], name: "index_members_on_number_and_library_id"
t.index ["number"], name: "index_members_on_number", unique: true
Expand Down Expand Up @@ -559,60 +559,6 @@
add_foreign_key "tickets", "users", column: "creator_id"
add_foreign_key "users", "members"

create_view "category_nodes", materialized: true, sql_definition: <<-SQL
WITH RECURSIVE search_tree(id, library_id, name, slug, parent_id, path_names, path_ids) AS (
SELECT categories.id,
categories.library_id,
categories.name,
categories.slug,
categories.parent_id,
ARRAY[categories.name] AS "array",
ARRAY[categories.id] AS "array"
FROM categories
WHERE (categories.parent_id IS NULL)
UNION ALL
SELECT categories.id,
categories.library_id,
categories.name,
categories.slug,
categories.parent_id,
(search_tree.path_names || categories.name),
(search_tree.path_ids || categories.id)
FROM (search_tree
JOIN categories ON ((categories.parent_id = search_tree.id)))
WHERE (NOT (categories.id = ANY (search_tree.path_ids)))
), tree_nodes AS (
SELECT search_tree.id,
search_tree.library_id,
search_tree.name,
search_tree.slug,
search_tree.parent_id,
search_tree.path_names,
search_tree.path_ids,
lower(array_to_string(search_tree.path_names, ' '::text)) AS sort_name,
( SELECT array_agg(st.id) AS array_agg
FROM search_tree st
WHERE (search_tree.id = ANY (st.path_ids))) AS tree_ids
FROM search_tree
ORDER BY (lower(array_to_string(search_tree.path_names, ' '::text)))
)
SELECT tree_nodes.id,
tree_nodes.library_id,
tree_nodes.name,
tree_nodes.slug,
tree_nodes.parent_id,
tree_nodes.path_names,
tree_nodes.path_ids,
tree_nodes.sort_name,
tree_nodes.tree_ids,
( SELECT json_build_object('active', count(DISTINCT categorizations.item_id) FILTER (WHERE (items.status = 'active'::item_status)), 'retired', count(DISTINCT categorizations.item_id) FILTER (WHERE (items.status = 'pending'::item_status)), 'maintenance', count(DISTINCT categorizations.item_id) FILTER (WHERE (items.status = 'maintenance'::item_status)), 'pending', count(DISTINCT categorizations.item_id) FILTER (WHERE (items.status = 'retired'::item_status))) AS json_build_object
FROM (categorizations
LEFT JOIN items ON ((categorizations.item_id = items.id)))
WHERE (categorizations.category_id = ANY (tree_nodes.tree_ids))) AS tree_item_counts
FROM tree_nodes;
SQL
add_index "category_nodes", ["id"], name: "index_category_nodes_on_id", unique: true

create_view "loan_summaries", sql_definition: <<-SQL
SELECT loans.library_id,
loans.item_id,
Expand Down Expand Up @@ -648,24 +594,6 @@
GROUP BY ((date_part('year'::text, adjustments.created_at))::integer), ((date_part('month'::text, adjustments.created_at))::integer)
ORDER BY ((date_part('year'::text, adjustments.created_at))::integer), ((date_part('month'::text, adjustments.created_at))::integer);
SQL
create_view "monthly_appointments", sql_definition: <<-SQL
WITH dates AS (
SELECT min(date_trunc('month'::text, appointments.starts_at)) AS startm,
max(date_trunc('month'::text, appointments.starts_at)) AS endm
FROM appointments
), months AS (
SELECT generate_series(dates.startm, dates.endm, 'P1M'::interval) AS month
FROM dates
)
SELECT (date_part('year'::text, months.month))::integer AS year,
(date_part('month'::text, months.month))::integer AS month,
count(DISTINCT a.id) AS appointments_count,
count(DISTINCT a.id) FILTER (WHERE (a.completed_at IS NOT NULL)) AS completed_appointments_count
FROM (months
LEFT JOIN appointments a ON ((date_trunc('month'::text, a.starts_at) = months.month)))
GROUP BY months.month
ORDER BY months.month;
SQL
create_view "monthly_loans", sql_definition: <<-SQL
WITH dates AS (
SELECT min(date_trunc('month'::text, loans.created_at)) AS startm,
Expand Down Expand Up @@ -702,4 +630,76 @@
GROUP BY months.month
ORDER BY months.month;
SQL
create_view "monthly_appointments", sql_definition: <<-SQL
WITH dates AS (
SELECT min(date_trunc('month'::text, appointments.starts_at)) AS startm,
max(date_trunc('month'::text, appointments.starts_at)) AS endm
FROM appointments
), months AS (
SELECT generate_series(dates.startm, dates.endm, 'P1M'::interval) AS month
FROM dates
)
SELECT (date_part('year'::text, months.month))::integer AS year,
(date_part('month'::text, months.month))::integer AS month,
count(DISTINCT a.id) AS appointments_count,
count(DISTINCT a.id) FILTER (WHERE (a.completed_at IS NOT NULL)) AS completed_appointments_count
FROM (months
LEFT JOIN appointments a ON ((date_trunc('month'::text, a.starts_at) = months.month)))
GROUP BY months.month
ORDER BY months.month;
SQL
create_view "category_nodes", materialized: true, sql_definition: <<-SQL
WITH RECURSIVE search_tree(id, library_id, name, slug, parent_id, path_names, path_ids) AS (
SELECT categories.id,
categories.library_id,
categories.name,
categories.slug,
categories.parent_id,
ARRAY[categories.name] AS "array",
ARRAY[categories.id] AS "array"
FROM categories
WHERE (categories.parent_id IS NULL)
UNION ALL
SELECT categories.id,
categories.library_id,
categories.name,
categories.slug,
categories.parent_id,
(search_tree.path_names || categories.name),
(search_tree.path_ids || categories.id)
FROM (search_tree
JOIN categories ON ((categories.parent_id = search_tree.id)))
WHERE (NOT (categories.id = ANY (search_tree.path_ids)))
), tree_nodes AS (
SELECT search_tree.id,
search_tree.library_id,
search_tree.name,
search_tree.slug,
search_tree.parent_id,
search_tree.path_names,
search_tree.path_ids,
lower(array_to_string(search_tree.path_names, ' '::text)) AS sort_name,
( SELECT array_agg(st.id) AS array_agg
FROM search_tree st
WHERE (search_tree.id = ANY (st.path_ids))) AS tree_ids
FROM search_tree
ORDER BY (lower(array_to_string(search_tree.path_names, ' '::text)))
)
SELECT tree_nodes.id,
tree_nodes.library_id,
tree_nodes.name,
tree_nodes.slug,
tree_nodes.parent_id,
tree_nodes.path_names,
tree_nodes.path_ids,
tree_nodes.sort_name,
tree_nodes.tree_ids,
( SELECT json_build_object('active', count(DISTINCT categorizations.item_id) FILTER (WHERE (items.status = 'active'::item_status)), 'retired', count(DISTINCT categorizations.item_id) FILTER (WHERE (items.status = 'pending'::item_status)), 'maintenance', count(DISTINCT categorizations.item_id) FILTER (WHERE (items.status = 'maintenance'::item_status)), 'pending', count(DISTINCT categorizations.item_id) FILTER (WHERE (items.status = 'retired'::item_status))) AS json_build_object
FROM (categorizations
LEFT JOIN items ON ((categorizations.item_id = items.id)))
WHERE (categorizations.category_id = ANY (tree_nodes.tree_ids))) AS tree_item_counts
FROM tree_nodes;
SQL
add_index "category_nodes", ["id"], name: "index_category_nodes_on_id", unique: true

end

0 comments on commit 1d441e9

Please sign in to comment.