Class: SqlMigrate::Migrator
- Inherits:
-
Object
- Object
- SqlMigrate::Migrator
- Extended by:
- Forwardable
- Defined in:
- lib/sql_migrate/migrator.rb
Constant Summary collapse
- VERSION_TABLE_NAME =
"migrate_versions".freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
Instance Method Summary collapse
- #applied_versions ⇒ Object
- #create_migrate_versions_if_not_exist ⇒ Object
-
#initialize(config = nil) ⇒ Migrator
constructor
A new instance of Migrator.
- #migrate ⇒ Object
- #migration_files ⇒ Object
- #table_names ⇒ Object
Constructor Details
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
5 6 7 |
# File 'lib/sql_migrate/migrator.rb', line 5 def config @config end |
Instance Method Details
#applied_versions ⇒ Object
46 47 48 |
# File 'lib/sql_migrate/migrator.rb', line 46 def applied_versions connection.query("select * from #{VERSION_TABLE_NAME}", as: :array).to_a.flatten end |
#create_migrate_versions_if_not_exist ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/sql_migrate/migrator.rb', line 29 def create_migrate_versions_if_not_exist unless table_names.include?(VERSION_TABLE_NAME) sql = <<-EOS create table `#{VERSION_TABLE_NAME}` ( `version` varchar(128), PRIMARY KEY (`version`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 EOS logger.info("create #{VERSION_TABLE_NAME}") connection.query(sql) end end |
#migrate ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/sql_migrate/migrator.rb', line 14 def migrate create_migrate_versions_if_not_exist versions = applied_versions migration_files.each do |migration| version_name = File.basename(migration) next if versions.include?(version_name) unless config.applied logger.info("apply migration #{version_name}") queries_from_migration_file(migration).each { |sql| execute(sql) } end sql = "insert into #{VERSION_TABLE_NAME} (`version`) values (\"#{version_name}\")" execute(sql) end end |
#migration_files ⇒ Object
50 51 52 53 54 |
# File 'lib/sql_migrate/migrator.rb', line 50 def migration_files config.migration_paths.map { |path| Dir.glob(File.join(File.(path), "*")) }.flatten.sort end |
#table_names ⇒ Object
42 43 44 |
# File 'lib/sql_migrate/migrator.rb', line 42 def table_names connection.query("show tables", as: :array).to_a.flatten end |