Class: Sequel::MysqlDbCommands
- Inherits:
-
Object
- Object
- Sequel::MysqlDbCommands
- Defined in:
- lib/sequel_rake_tasks.rb
Instance Method Summary collapse
- #create_database ⇒ Object
- #drop_database ⇒ Object
- #dump_schema(schema_file) ⇒ Object
-
#initialize(connection_config) ⇒ MysqlDbCommands
constructor
A new instance of MysqlDbCommands.
- #load_schema(schema_file) ⇒ Object
- #load_sql_file(filename) ⇒ Object
- #migrate(migrator_klass, migrations_dir, migrations_opts) ⇒ Object
Constructor Details
#initialize(connection_config) ⇒ MysqlDbCommands
Returns a new instance of MysqlDbCommands.
101 102 103 |
# File 'lib/sequel_rake_tasks.rb', line 101 def initialize(connection_config) @connection_config = connection_config end |
Instance Method Details
#create_database ⇒ Object
111 112 113 114 115 |
# File 'lib/sequel_rake_tasks.rb', line 111 def create_database with_master_connection do |connection| connection.execute("CREATE DATABASE IF NOT EXISTS `#{database_name}` DEFAULT CHARACTER SET #{charset} DEFAULT COLLATE #{collation}") end end |
#drop_database ⇒ Object
105 106 107 108 109 |
# File 'lib/sequel_rake_tasks.rb', line 105 def drop_database with_master_connection do |connection| connection.execute("DROP DATABASE IF EXISTS `#{database_name}`") end end |
#dump_schema(schema_file) ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/sequel_rake_tasks.rb', line 129 def dump_schema(schema_file) with_database_connection do |connection| connection.extension :schema_dumper text = connection.dump_schema_migration(same_db: false) text = text.gsub(/ +$/, '') # remove trailing whitespace File.open(schema_file, 'w') { |f| f.write(text) } end end |
#load_schema(schema_file) ⇒ Object
123 124 125 126 127 |
# File 'lib/sequel_rake_tasks.rb', line 123 def load_schema(schema_file) with_database_connection do |connection| eval(File.read schema_file).apply(connection, :up) end end |
#load_sql_file(filename) ⇒ Object
117 118 119 120 121 |
# File 'lib/sequel_rake_tasks.rb', line 117 def load_sql_file(filename) with_database_connection do |connection| connection.execute("SET FOREIGN_KEY_CHECKS = 0; SOURCE #{filename}; SET FOREIGN_KEY_CHECKS = 1") end end |
#migrate(migrator_klass, migrations_dir, migrations_opts) ⇒ Object
138 139 140 141 142 143 144 145 |
# File 'lib/sequel_rake_tasks.rb', line 138 def migrate(migrator_klass, migrations_dir, migrations_opts) with_database_connection do |connection| args = [connection, migrations_dir] args << migrations_opts if migrations_opts migrator = migrator_klass.new(*args) migrator.run end end |