How to check if the database exists or not in rails before doing a rake db:setup

后端 未结 7 1273
你的背包
你的背包 2020-12-05 23:58

How to check if the database exists or not in rails before doing a rake db:setup?

I would like to check if a database already exists before a db:create is being done

相关标签:
7条回答
  • 2020-12-06 00:03

    TRY THIS

     IF EXISTS 
           (
             SELECT name FROM master.dbo.sysdatabases 
            WHERE name = N'New_Database'
            )
        BEGIN
            SELECT 'Database Name already Exist' AS Message
        END
        ELSE
        BEGIN
            CREATE DATABASE [New_Database]
            SELECT 'New Database is Created'
        END
    
    0 讨论(0)
  • 2020-12-06 00:05

    Here is what I use to check the state of the DB:

    if db_version=$(bundle exec rake db:version 2>/dev/null)
    then
        if [ "$db_version" = "Current version: 0" ]; then
            echo "DB is empty"
        else
            echo "DB exists"
        fi
    else
        echo "DB does not exist"
    fi
    
    0 讨论(0)
  • 2020-12-06 00:13

    Here are some bash scripts I made for this purpose:

    Postgres

    if echo "\c $PGDATABASE; \dt" | psql | grep schema_migrations 2>&1 >/dev/null
    then
       bundle exec rake db:migrate
    else
       bundle exec rake db:setup
    fi
    

    Mysql

     if echo "use $MYSQLDATABASE; show tables" | mysql | grep schema_migrations 2>&1 > /dev/null
     then
         bundle exec rake db:migrate
     else
         bundle exec rake db:setup
     fi
    

    These check for the presence of the schema_migrations table to determine whether rake db:setup has been run previously.

    0 讨论(0)
  • 2020-12-06 00:15

    Here is a method that checks if the database already exists:

    def database_exists?
      ActiveRecord::Base.connection
    rescue ActiveRecord::NoDatabaseError
      false
    else
      true
    end
    

    References

    • ActiveRecord::Base.connection
    • ActiveRecord::NoDatabaseError
    0 讨论(0)
  • 2020-12-06 00:18

    I made a rake task that expands on some of the previous answers. I use this a lot in a Vagrant+Docker setup, so I can very easily issue a single command and either create a new database or issue a migrate to the current database. I use a branched database paradigm for development. I constantly need to seed a new database or update my existing one.

    In lib/tasks/db_exists.rake:

    namespace :db do
      desc "Checks to see if the database exists"
      task :exists do
        begin
          Rake::Task['environment'].invoke
          ActiveRecord::Base.connection
        rescue
          exit 1
        else
          exit 0
        end
      end
    end
    

    So now I can run a simple bash command:

    rake db:exists && rake db:migrate || rake db:setup
    

    Which I have then further automated into a Makefile (trimmed for brevity):

    .PHONY database
    database:
            rake db:exists && rake db:migrate || rake db:setup
    

    Translates into:

    make database
    

    For all of my local database needs.

    0 讨论(0)
  • 2020-12-06 00:25

    This will return false if the DB doesn't exist or if the connection is not active (at least in Rails 4+).

    ::ActiveRecord::Base.connection_pool.with_connection(&:active?) rescue false
    
    0 讨论(0)
提交回复
热议问题