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
#initialize(config = nil) ⇒ Migrator
Returns a new instance of Migrator.
10 11 12 |
# File 'lib/sql_migrate/migrator.rb', line 10 def initialize(config = nil) @config = config || Config.new end |
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
44 45 46 |
# File 'lib/sql_migrate/migrator.rb', line 44 def applied_versions connection.query("select * from #{VERSION_TABLE_NAME}", as: :array).to_a.flatten end |
#create_migrate_versions_if_not_exist ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/sql_migrate/migrator.rb', line 27 def create_migrate_versions_if_not_exist unless table_names.include?(VERSION_TABLE_NAME) sql = " create table `\#{VERSION_TABLE_NAME}` (\n `version` varchar(128),\n PRIMARY KEY (`version`)\n ) ENGINE=InnoDB DEFAULT CHARSET=utf8\n EOS\n logger.info(\"create \#{VERSION_TABLE_NAME}\")\n connection.query(sql)\n end\nend\n" |
#migrate ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# 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) logger.info("apply migration #{version_name}") queries_from_migration_file(migration).each { |sql| execute(sql) } sql = "insert into #{VERSION_TABLE_NAME} (`version`) values (\"#{version_name}\")" execute(sql) end end |
#migration_files ⇒ Object
48 49 50 51 52 |
# File 'lib/sql_migrate/migrator.rb', line 48 def migration_files config.migration_paths.map { |path| Dir.glob(File.join(File.(path), "*")) }.flatten.sort end |
#table_names ⇒ Object
40 41 42 |
# File 'lib/sql_migrate/migrator.rb', line 40 def table_names connection.query("show tables", as: :array).to_a.flatten end |