Remove Tables from Schema - ruby-on-rails

This is probably a fairly basic question, but I'm at a loss on how to clean up after installing a gem I decided not to use. During the install process of Attachinary, the install instructions said to run rake attachinary:install:migrations - creating a new table and index in my schema, noted here:
create_table "attachinary_files", force: :cascade do |t|
t.integer "attachinariable_id"
t.string "attachinariable_type"
t.string "scope"
t.string "public_id"
t.string "version"
t.integer "width"
t.integer "height"
t.string "format"
t.string "resource_type"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "attachinary_files", ["attachinariable_type", "attachinariable_id", "scope"], name: "by_scoped_parent", using: :btree
I later decided to go with a simplier attachement gem and I'm attempting to clean up and remove all the "stuff" created during the attachinary install.
Any advice on how to clean up the db? I'm running Postgresql if that makes any difference.

Create a migration:
bundle exec rails g migration remove_attachinary
Then tell Rails what to do:
def up
drop_table :attachinary_files
end
Remove the change method present by default
If ever you'd like this migration to be reversible, copy your previous code in a down method

Related

rails migration generating random tables. Where is this coming from?

When I run rails db:migrate without a new migration it seems to have added two new tables: questions and questions_1.
In my schema file I see:
create_table "questions", id: false, force: :cascade do |t|
t.integer "id"
t.text "text"
t.boolean "active"
t.integer "organization_id"
t.datetime "created_at"
t.datetime "updated_at"
t.bigint "account_id"
t.bigint "team_id"
end
create_table "questions_1", id: false, force: :cascade do |t|
t.integer "id"
t.text "text"
t.boolean "active"
t.integer "organization_id"
t.datetime "created_at"
t.datetime "updated_at"
t.bigint "account_id"
t.bigint "team_id"
end
I don't have any migrations making these tables. I'm guessing this is some sort of convention. Where do I look to fix this? All recent changes are in the app/ directory and a migration I made has been rolled back and then deleted. Yet when I run rails db:migrate I always get these new tables.
Any ideas?
Rails won't create tables with any name other than the ones specified in a migration. If the table already exists then it will throw an error.
I believe someone has run a migration creating the second table on your database, and then deleted the migration. When rails creates your schema.rb file, it uses your database rather than any migrations, meaning this file reflects your actual database state rather than the one your migrations suggest would be the case.
If you're sure you don't want the second table and it has no data in it, then you can write out a new migration to delete this table, run it, and then delete it. This will return your database state to the one your migrations suggest would be the case. You can then run rake db:schema:dump to update your schema.

Error trying to migrate PG database hosted on Heroku -Rails

Im getting this error when running heroku run rake db:migrate -> ERROR: column "frequency" cannot be cast automatically to type integer I'm not sure what I'm supposed to do in order to fix this error. Here is my schema local that works fine when I migrated:
schema:
create_table "assignments", force: :cascade do |t|
t.string "name"
t.string "description"
t.integer "account_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "attachment"
t.integer "finished_count"
t.datetime "start_at"
t.datetime "end_at"
t.integer "frequency"
t.integer "status", default: 0
t.index ["status"], name: "index_assignments_on_status"
end
Cannot I not use integer as a type with PG? if not, what should I do instead?
Without seeing your migration, which you should post. A likely cause is that the frequency column already has data in it. Therefore PG cannot blindly convert that data into a integer if it is not something that is like a number.

Data migrations for older version of friendly id

I'm migrating a very old app from friendly_id 3.2 to 5.1.
I have a user model which currently has a cached_slug field. I created a new field called just slug. Initially, I thought I could just copy the data over from cached_slug to slug, but I noticed that there's also a whole other table called slugs which looks like this:
create_table "slugs", force: :cascade do |t|
t.string "name", limit: 255
t.integer "sluggable_id"
t.integer "sequence", default: 1, null: false
t.string "sluggable_type", limit: 40
t.string "scope", limit: 255
t.datetime "created_at"
end
Following the directions in the FriendlyID README for the Rails Quickstart, I ran rails generate friendly_id which created this table:
create_table "friendly_id_slugs", force: :cascade do |t|
t.string "slug", null: false
t.integer "sluggable_id", null: false
t.string "sluggable_type", limit: 50
t.string "scope"
t.datetime "created_at"
end
So now I'm all confused about what I need to do to complete migration. I tried creating a new user with the console and the friendly_id_slugs table is still empty, so I'm not sure when or what it's used for.
I have two questions:
1) What is this other table used for
2) What do I need to do to migrate my old slugs to the new table/field so it will continue to work moving forward?
Thanks!
If you don't mind losing the FriendlyId's History records (i.e: the old slugs),
drop the old slug table
in the rails console, run <ModelName>.find_each(&:save) for all friendly_id models.
Step 2 should regenerate new slugs.
Warning: Test before doing this on production servers!
Update: You can also perform step 2 in a migration file
class RegenerateSlugs < ActiveRecord::Migration
def change
# <ModelName>.find_each(&:save)
User.find_each(&:save)
Article.find_each(&:save)
end
end

unable to create an entry with chinese characters in rails console

I am creating a platform for chinese students in ruby on rails. This is my schema
ActiveRecord::Schema.define(version: 20130827203308) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "posts", force: true do |t|
t.text "message"
t.integer "student_id"
end
add_index "posts", ["student_id"], name: "index_posts_on_student_id", using: :btree
create_table "students", force: true do |t|
t.string "first_name"
t.string "last_name"
t.string "email"
t.string "number"
t.string "college"
t.string "password"
t.float "budget"
t.string "picture"
t.datetime "created_at"
t.datetime "updated_at"
end
when I head to the rails console and do this
Post.create(:message => "I am looking at a 3 bedroom apartment")
I can Begin and Commit this into my db.
But when I do
Post.create(:message => "我在Suffolk University附近想租一个3房一厅的apt有没有同学有兴趣“)
my rails console freeze.
I am using the type "text" for my messages in the posts table. Should I be using "string" ? or should I be using some sort of a language gem to do this?
Try installing readline package with rvm and recompile your ruby:
$ rvm pkg install readline
$ rvm install <your_ruby_version>

How to rename fields and make them not nulls

My current schema looks like:
create_table "users", :force => true do |t|
t.string "username"
t.string "firstname"
t.string "lastname"
t.datetime "loggedin_at"
t.datetime "created_at"
t.datetime "updated_at"
t.integer `user_status`
end
I want to rename username to user_name, and make all the fields not null (they are currently nullable).
How would I do this? Do I create a migration file using rails generate, and then have to tweak it manually? How so?
It can help if you specify the Rails version number.
Before 3.0, it is:
Rails: How can I rename a database column in a Ruby on Rails migration?
If it is 3.0 or later, then you can look at the Migration documentation, such as rails generate instead of script/generate.

Resources