2017-06-19 35 views
0

所以,我試圖實現一種方法來添加數據庫遷移沒有ORM/ODM爲我的SQLite3數據庫和錯誤我得到(syntax error: expecting token 'EOF', not 'end')是這個代碼:獲取語法錯誤:期望令牌'EOF',而不是'結束',並找不出原因

src/project/database/migration/migrations/1.cr

require "../migration" 

module Project 
    ver = 1 
    migration = Migration.new ver 

    migration.register_up |db| do 
    db.exec "create table version (version int)" 
    db.exec "insert into version values (?)", ver 
    end 

    migration.register_down |db| do 
    db.exec "drop table version" 
    end 

    Migrations[ver] = migration 
end 

我看不出與語法的緊迫問題。該文件導入下面的文件,因爲它需要的類和線Migrations = [] of Migration

src/project/database/migration/migration.cr

require "db" 
require "sqlite3" 

module Project 

    Migrations = [] of Migration 

    class Migration 
    def initialize(@version : Int) 
    end 

    # Registers a callback that will be called when the `up`-method is called. 
    # The callback must return either `true` for a successful migration, 
    # or `false` for a failed migration. If an `up` migration has 
    # failed, the `down` migration will be called to restore the database 
    # back to its previous state. 
    # The callback will receive an instance of `DB::Database` 
    # 
    # Example: 
    # 
    # ``` 
    # migration = Migration.new(1) 
    # 
    # migration.register_up |db| do 
    # # Advance migration 
    # end 
    # 
    # migration.register_down |db| do 
    # # Recede migration 
    # end 
    # ``` 
    def register_up(&block : (DB::Database) -> Bool) 
     @up = block 
    end 

    # Registers a callback that will be called when the `down`-method is called. 
    # See the `register_up` method for more information 
    def register_down(&block : (DB::Database) -> Bool) 
     @down = block 
    end 

    # Advances DB to the next version 
    def up(conn : DB::Database) 
     result = @up.call(conn) 
     unless result 
     # Failed migration, rollback 
     @down.call(conn) 
     raise Exception.new(`Failed to migrate database to version: #{@version}. Rolling back.`) 
     end 
    end 

    # Recedes DB to the previous version 
    def down(conn : DB::Database) 
     result = @down.call(conn) 
     unless result 
     # Failed migration, rollback 
     @up.call(conn) 
     raise Exception.new(`Failed to migrate database to version: #{@version - 1}. Rolling back.`) 
     end 
    end 
    end 

end 

任何想法?

回答

2

語法錯誤是在這裏:

migration.register_up |db| do 
    # ... 
end 

應該是:

migration.register_up do |db| 
    # ... 
end 

而且在register_down相同。

請參見Blocks and Procs中的「Yield arguments」部分。

相關問題