Is it possible to include versioning of the database in commit (GIT)? [duplicate]

2

I would like to know if it is possible to version the database (in my case MySQL) through the GIT, or if there is any other effective way to do it.

    
asked by anonymous 06.05.2015 / 14:37

1 answer

1

It is possible, depending on the technologies involved.

Databases themselves usually do not provide versioning tools. However, some frameworks create a means of applying patches to a database, and to modify it incrementally. These patches , or database migrations , can be versioned.

The best-known example is Ruby on Rails Active Record . Here's an example of how to change the type of a column in a table:

class ChangeProductsPrice < ActiveRecord::Migration
  def change
    reversible do |dir|
      change_table :products do |t|
        dir.up   { t.change :price, :string }
        dir.down { t.change :price, :integer }  # rollback
      end
    end
  end
end

In C #, for example, we can also use Entity database migrations Framework .

public partial class AddPostAbstract : DbMigration 
{ 
    public override void Up() 
    { 
        AddColumn("dbo.Posts", "Abstract", c => c.String()); 
        Sql("UPDATE dbo.Posts SET Abstract = LEFT(Content, 100) WHERE Abstract IS NULL"); 
    } 

    public override void Down() 
    { 
        DropColumn("dbo.Posts", "Abstract"); 
    } 
} 
    
06.05.2015 / 15:16