首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> SQL Server 資料庫 >> 關於SqlServer

SQrbL:以 Ruby DSL 簡化 SQL 遷移腳本管理

     編輯:關於SqlServer

背景:SQL 遷移腳本的維護惡夢

隨專案演進,純 SQL 遷移腳本愈來愈冗長、難以組織。Rails 透過 ActiveRecord Migration 解決此問題。Sam Livingston-Gray 開發獨立 Ruby 工具 SQrbL (SQL + Ruby),結合 Ruby 表達力與 SQL,生成分層級的遷移腳本目錄結構。

SQrbL 腳本撰寫範例


include Sqrbl
Sqrbl.migration do
  @output_directory = '/path/to/generated/sql'

  group "Widgets" do
    step "Create widgets" do
      up do
        helpers do
          def widget_import_note
            'Imported from old_widgets'
          end
        end

        action "Migrate old_widgets" do
          <<-SQL
            #{ insert_into("new_widgets", {
                :name     => 'widget_name',
                :part_num => 'CONCAT("X_", part_number)',
                :note     => widget_import_note
              }) }
            FROM old_widgets
          SQL
        end
      end

      down do
        action "Drop imported organizational contacts" do
          'DELETE FROM new_widgets WHERE note LIKE "Imported from old_widgets"'
        end
      end
    end
  end

  group 'Second Group' do
    step 'Step one' do
      up { write '-- Step one up' }
      down { write '-- Step one down' }
    end
    step 'Step two' do
      up { write '-- Step two up' }
      down { write '-- Step two down' }
    end
  end
end

生成的檔案結構


/path/to/generated/sql/
├── up/
│   ├── 1_widgets/
│   │   └── 1_create_widgets.sql
│   └── 2_second_group/
│       ├── 1_step_one.sql
│       └── 2_step_two.sql
├── down/
│   ├── 1_widgets/
│   │   └── 1_create_widgets.sql
│   └── 2_second_group/
│       ├── 1_step_one.sql
│       └── 2_step_two.sql
├── all_up.sql
└── all_down.sql

all_up.sql 實際內容


-- Migrate old_widgets
INSERT INTO new_widgets (name, part_num, note)
SELECT widget_name AS name,
       CONCAT("X_", part_number) AS part_num,
       "Imported from old_widgets" AS note
FROM old_widgets;
-- Step one up
-- Step two up

特點與限制

  • 優點:Ruby DSL 可重構共用邏輯(helpers)、動態生成 SQL、層次化目錄自動管理 up/down 配對、獨立於 Rails 專案可用。
  • 限制:目前僅簡化 INSERT(insert_into helper),缺乏完整 SQL DSL(CREATE TABLE、ALTER 等仍需手寫 SQL 字串)。版本 0.1.3 仍屬早期。
  • 適用對象:未使用 Rails/ActiveRecord Migration、尋找輕量級獨立遷移工具的團隊。

總結

SQrbL 展示「內建領域特定語言」如何降低 SQL 遷移腳本的維護複雜度。雖功能尚待擴充,但其「群組-步驟-上/下」結構、Helper 重用、自動生成執行順序檔案等設計,為遷移工具提供了優秀的參考架構。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved