Skip to content

Instantly share code, notes, and snippets.

@rponte
Created October 31, 2011 02:35
Show Gist options
  • Select an option

  • Save rponte/1326786 to your computer and use it in GitHub Desktop.

Select an option

Save rponte/1326786 to your computer and use it in GitHub Desktop.

Revisions

  1. rponte revised this gist Oct 31, 2011. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions db_examples_on_activerecord.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    ## ORACLE

    def disable_referential_integrity(&block) #:nodoc:
    sql_constraints = <<-SQL
    SELECT constraint_name, owner, table_name
    @@ -41,7 +40,6 @@ def disable_referential_integrity #:nodoc:
    end

    ## POSTGRES

    def disable_referential_integrity #:nodoc:
    if supports_disable_referential_integrity? then
    execute(tables.collect { |name| "ALTER TABLE #{quote_table_name(name)} DISABLE TRIGGER ALL" }.join(";"))
  2. rponte revised this gist Oct 31, 2011. 1 changed file with 23 additions and 1 deletion.
    24 changes: 23 additions & 1 deletion db_examples_on_activerecord.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,25 @@
    ## ORACLE

    def disable_referential_integrity(&block) #:nodoc:
    sql_constraints = <<-SQL
    SELECT constraint_name, owner, table_name
    FROM user_constraints
    WHERE constraint_type = 'R'
    AND status = 'ENABLED'
    SQL
    old_constraints = select_all(sql_constraints)
    begin
    old_constraints.each do |constraint|
    execute "ALTER TABLE #{constraint["table_name"]} DISABLE CONSTRAINT #{constraint["constraint_name"]}"
    end
    yield
    ensure
    old_constraints.each do |constraint|
    execute "ALTER TABLE #{constraint["table_name"]} ENABLE CONSTRAINT #{constraint["constraint_name"]}"
    end
    end
    end

    ## SQLSERVER
    def disable_referential_integrity
    do_execute "EXEC sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'"
    @@ -31,4 +53,4 @@ def disable_referential_integrity #:nodoc:
    end
    end

    ## ORACLE

  3. rponte created this gist Oct 31, 2011.
    34 changes: 34 additions & 0 deletions db_examples_on_activerecord.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    ## SQLSERVER
    def disable_referential_integrity
    do_execute "EXEC sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'"
    yield
    ensure
    do_execute "EXEC sp_MSforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL'"
    end

    ## MYSQL
    def disable_referential_integrity #:nodoc:
    old = select_value("SELECT @@FOREIGN_KEY_CHECKS")

    begin
    update("SET FOREIGN_KEY_CHECKS = 0")
    yield
    ensure
    update("SET FOREIGN_KEY_CHECKS = #{old}")
    end
    end

    ## POSTGRES

    def disable_referential_integrity #:nodoc:
    if supports_disable_referential_integrity? then
    execute(tables.collect { |name| "ALTER TABLE #{quote_table_name(name)} DISABLE TRIGGER ALL" }.join(";"))
    end
    yield
    ensure
    if supports_disable_referential_integrity? then
    execute(tables.collect { |name| "ALTER TABLE #{quote_table_name(name)} ENABLE TRIGGER ALL" }.join(";"))
    end
    end

    ## ORACLE