Generate a Migration File from Schema.Rb

Generate a migration file from schema.rb

There's no need to do this. For new installations you should be running rake db:schema:load, not rake db:migrate, this will load the schema into the database, which is faster than running all the migrations.

You should never delete migrations, and certainly not combine them. As for accidentally deleting one, you should be using a version control system, such as Git.

How to generate migration files using schema.rb

Generate a migration, then copy schema.rb code (without the ActiveRecord::Schema.define(:version => x) do and end) into the up or change method.

Generate the schema.rb from Rails Models (without Database)

You can try;

rake db:schema:load

Ruby on Rails. Why schema.rb builded on existing data through db:schema:dump is almost empty?

I've got it! Two days of my life.
File used to import PostgreSQL database has at the beginning:

CREATE SCHEMA employees;
-- and later
CREATE TABLE employees.department;

I thought that since Rails generates database by rake db:structure:load , the file's syntax is correct.

But when I create manually table users in new empty database and then pg_dump that new base I don't have CREATE SCHEMA query there.

And finally rake db:schema:dump fills schema.rb with tables as I want:

create_table "users", id: :serial, force: :cascade do |t|
t.text "name"
end

Because that fresh pg_dumped file has CREATE TABLE public.users query. public.

I think the key is in comments in database.yml file:

# Schema search path. The server defaults to $user,public
#schema_search_path: myapp,sharedapp,public

One picture is more valuable than a thousand words: that's the differences Table users on the right goes to schema.rb after rake db:schema:dump

Thanks guys for the comments. It's made sure me that I do not make a terrible mistake.

Rails Application has schema.rb but no migrate files

doesn't the schema file get created based on the migration files?

Yes, but they can survive independently. I can create my schema and modify it with several migrations, then delete the migrations before sending the project to you.

Rails will still start fine as what he cares about is the content of the schema.rb file.

If you want to construct your database, you should load the schema from schema.rb and not using rake db:migrate. In a very large and long-lived Rails project, you are not guaranteed that all the migrations will be able to run all from the first one to the end.

The way you setup a database is using the command

rake db:schema:load

Then from there you can move forward creating new migrations in the future. You don't really need the old migration files.

Generate Rails migrations from a schema

Sure, connect your application to your database, then run

rake db:schema:dump

This will give you a db/schema.rb ready with all of your definitions. Now that you have that db/schema.rb, simply copy the contents within the declaration into a new migration. I've done this before, and it works just great.



Related Topics



Leave a reply



Submit