Class: ActualDbSchema::Store::DbAdapter
- Inherits:
-
Object
- Object
- ActualDbSchema::Store::DbAdapter
- Defined in:
- lib/actual_db_schema/store.rb
Overview
Stores migrated files in the database.
Constant Summary collapse
- TABLE_NAME =
"actual_db_schema_migrations"- RECORD_COLUMNS =
%w[version filename content branch migrated_at].freeze
Instance Method Summary collapse
- #delete(filename) ⇒ Object
- #materialize_all ⇒ Object
- #migration_files ⇒ Object
- #read ⇒ Object
- #stored_migration?(filename) ⇒ Boolean
- #write(filename) ⇒ Object
Instance Method Details
#delete(filename) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/actual_db_schema/store.rb', line 172 def delete(filename) version = extract_version(filename) return unless version if table_exists? connection.execute(<<~SQL.squish) DELETE FROM #{quoted_table} WHERE #{quoted_column("version")} = #{connection.quote(version)} SQL end File.delete(filename) if File.exist?(filename) end |
#materialize_all ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/actual_db_schema/store.rb', line 189 def materialize_all return unless table_exists? FileUtils.mkdir_p(folder) rows = connection.exec_query(<<~SQL.squish) SELECT filename, content FROM #{quoted_table} SQL rows.each do |row| write_cache_file(row["filename"], row["content"]) end end |
#migration_files ⇒ Object
167 168 169 170 |
# File 'lib/actual_db_schema/store.rb', line 167 def migration_files materialize_all Dir["#{folder}/**/[0-9]*_*.rb"] end |
#read ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/actual_db_schema/store.rb', line 154 def read return {} unless table_exists? rows = connection.exec_query(<<~SQL.squish) SELECT version, migrated_at, branch FROM #{quoted_table} SQL rows.map do |row| Item.new(row["version"].to_s, row["migrated_at"], row["branch"]) end.index_by(&:version) end |
#stored_migration?(filename) ⇒ Boolean
185 186 187 |
# File 'lib/actual_db_schema/store.rb', line 185 def stored_migration?(filename) filename.to_s.start_with?(folder.to_s) end |
#write(filename) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/actual_db_schema/store.rb', line 142 def write(filename) ensure_table! version = extract_version(filename) return unless version basename = File.basename(filename) content = File.read(filename) upsert_record(version, basename, content, Git.current_branch, Time.current) write_cache_file(basename, content) end |