Learn rails5.2-activerecord:migration, spring use (incompatible solution)

Source: Internet
Author: User

By chance:

Run rails generate to stop, find answers online, the spring version changes after bundle update, and is incompatible with the running spring instance.

The same reason that spring causes :

After rails g migration, the window shows the build migration file, but the file tree structure does not show this file.

Rails console stops and does not open the console.

Workaround:

Close Spring, spring Stop

??, if Google find the problem, the Chinese description of the problem can not find a good result, instead of describing the problem in English, and then search.

Https://github.com/rails/spring

Spring Application Preloader

A preloader that accelerates development by keeping your program running at the back end. So when you want to run a test, when migrating, you don't have to boot it every time.

This is the function that rails4.1 brings.

Bin/spring Status View.

Bin/spring stop is used and is not compatible when it is encountered.

Features

    • Fully automated, no need to explicitly start, stop back-end processes
    • Reload your app every time it runs
    • When Configs/initializers/gem dependencies changes, restarts your app will start again

Compatibility compatible

Document ruby2.5 is not compatible, rails5.2 is not displayed.

rails5.2 will install the Spring gem by default after Rails new

The recommended commands are executed under the bin/directory.

Usage

New key one rails, run rails G scaffold post name:string and then run a test:

$ Time Bin/rake Test TEST/CONTROLLERS/POSTS_CONTROLLER_TEST.RB

Running via Spring preloader in process 75673Run options:--seed 64649# Running:.......finished in 1.798020s, 3.8932 runs/s, 5.0055 assertions/s.7 runs, 9 assertions, 0 failures, 0 errors, 0 skipsbin/rake Test test/controllers/posts_controller_test.rb

0.13s user 0.02s system 4% CPU 3.091 Total

The speed of the re-measurement will be accelerated:

0.13s User 0.02s system 26% CPU 0.566 Total

This is because when the program file or test file is modified, the change is pick up, and the backend process does not need to start again the next time it runs.

This works like code reloading, allowing you to refresh your browser to see changes as soon as you develop.

But when Configs/initializers/gem dependencies changes, it completely restarts your app.

Spring closes automatically when the terminal is closed.

If you encounter compatibility issues, you can manually turn off bin/spring Stop

You can detect in code that spring is turned on:

>if defined? (Spring)> puts "dd"> EndOutput DD

Removal

    • Unspring your bin/executables: bin/spring binstub--remove--all
    • or replace the gem spring

Deployment

In a product environment, spring cannot be installed.

To prevent installation, use the --without development Test parameter to the bundle install command

$ bundle install --without development test

?? Personal understanding: Do not install the gem under the development, test environment.


Problem location Troubleshooting:

Run in a separate window: Spring server generates logs and outputs.

Migrations

If you delete the database manually, then running Rails db:migrate will be re-migrated.

$ rm db/development.sqlite3$ Rails Db:migrate

Rails Db:rollback will be rolled back once

Rails Db:version will look at the current version, with a unique version number for each operation. The version number is timestamp.

$ls db/migrate/ to list the versions.

You can use the specified migration, the number 0 represents the start, and all the tables with internal data are deleted.

$ rails db:migrate version=0

config/database.yml you can see the database configuration for different environments. 

Development environment using Sqlite3
The production environment needs to use MySQL or PostgreSQL

Enter the database:

$ sqlite3 Db/development.sqlite3
SQLite version 3.8.5 2014-08-15 22:37:57
Enter ". Help" forusage hints.
sqlite>. Tables
Products Schema_migrations
sqlite>. Schema Products
CREATE TABLE "Products" ("id" INTEGER PRIMARY KEY AutoIncrement not NULL, "name" varchar, "Price" decimal (7,2), "Weig HT "Integer," In_stock "Boolean," Expiration_date "date," Created_at "datetime NOT NULL," Updated_at "datetime is NOT NULL," Height "integer");

Analytical:

table schema_migrations is used for the migration of version management. This table was executed by rails during the first migration .

Https://en.wikipedia.org/wiki/Database_index
Database index is a data structure that accelerates the retrieval of the required data from the database.
Add_index:p roducts,: Name

Fields that are automatically added
 
   
  
  • Id:integer: Add unique to the record??, in SQL, equivalent to not NULL auto_increment
  • Created_at and Updated_at:datetime.


Callbacks

Http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
A ActiveRecord object's life cycle uses hooks.
Before_validation/after_validation
Before_save/before_create, etc.

After_initialize calls the method after an object instantiation.






Learn rails5.2-activerecord:migration, spring use (incompatible solution)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.