mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-12-11 16:21:24 -05:00
FEATURE: start tracking information about migrations that run
This commit adds a new tracking table that lets us know - When a migration ran - What version Discourse was at - How long it took - What version Rails was at The built in tracking in Rails is very limited, does not track this info
This commit is contained in:
parent
3829c78526
commit
bbbb09a6fb
3 changed files with 116 additions and 0 deletions
30
db/migrate/20000225050318_add_schema_migration_details.rb
Normal file
30
db/migrate/20000225050318_add_schema_migration_details.rb
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
class AddSchemaMigrationDetails < ActiveRecord::Migration
|
||||||
|
def up
|
||||||
|
# schema_migrations table is way too thin, does not give info about
|
||||||
|
# duration of migration or the date it happened, this migration together with the
|
||||||
|
# monkey patch adds a lot of information to the migration table
|
||||||
|
|
||||||
|
create_table :schema_migration_details do |t|
|
||||||
|
t.string :version, null: false
|
||||||
|
t.string :name
|
||||||
|
t.string :hostname
|
||||||
|
t.string :git_version
|
||||||
|
t.string :rails_version
|
||||||
|
t.integer :duration
|
||||||
|
t.string :direction # this really should be a pg enum type but annoying to wire up for little gain
|
||||||
|
t.datetime :created_at, null: false
|
||||||
|
end
|
||||||
|
|
||||||
|
add_index :schema_migration_details, [:version]
|
||||||
|
|
||||||
|
execute("INSERT INTO schema_migration_details(version, created_at)
|
||||||
|
SELECT version, current_timestamp
|
||||||
|
FROM schema_migrations
|
||||||
|
ORDER BY version
|
||||||
|
")
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
drop_table :schema_migration_details
|
||||||
|
end
|
||||||
|
end
|
54
lib/freedom_patches/schema_migration_details.rb
Normal file
54
lib/freedom_patches/schema_migration_details.rb
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
module FreedomPatches
|
||||||
|
module SchemaMigrationDetails
|
||||||
|
def exec_migration(conn, direction)
|
||||||
|
rval = nil
|
||||||
|
|
||||||
|
time = Benchmark.measure do
|
||||||
|
rval=super
|
||||||
|
end
|
||||||
|
|
||||||
|
sql = <<SQL
|
||||||
|
INSERT INTO schema_migration_details(
|
||||||
|
version,
|
||||||
|
hostname,
|
||||||
|
name,
|
||||||
|
git_version,
|
||||||
|
duration,
|
||||||
|
direction,
|
||||||
|
rails_version,
|
||||||
|
created_at
|
||||||
|
) values (
|
||||||
|
:version,
|
||||||
|
:hostname,
|
||||||
|
:name,
|
||||||
|
:git_version,
|
||||||
|
:duration,
|
||||||
|
:direction,
|
||||||
|
:rails_version,
|
||||||
|
:created_at
|
||||||
|
)
|
||||||
|
SQL
|
||||||
|
|
||||||
|
hostname = `hostname` rescue ""
|
||||||
|
sql = ActiveRecord::Base.send(:sanitize_sql_array, [sql, {
|
||||||
|
version: version || "",
|
||||||
|
duration: (time.real * 1000).to_i,
|
||||||
|
hostname: hostname,
|
||||||
|
name: name,
|
||||||
|
git_version: Discourse.git_version,
|
||||||
|
created_at: Time.zone.now,
|
||||||
|
direction: direction.to_s,
|
||||||
|
rails_version: Rails.version
|
||||||
|
}])
|
||||||
|
|
||||||
|
conn.execute(sql)
|
||||||
|
|
||||||
|
rval
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class ActiveRecord::Migration
|
||||||
|
prepend FreedomPatches::SchemaMigrationDetails
|
||||||
|
end
|
|
@ -0,0 +1,32 @@
|
||||||
|
require 'rails_helper'
|
||||||
|
require_dependency "freedom_patches/schema_migration_details"
|
||||||
|
|
||||||
|
describe FreedomPatches::SchemaMigrationDetails do
|
||||||
|
|
||||||
|
# we usually don't really need this model so lets not clutter up with it
|
||||||
|
class SchemaMigrationDetail < ActiveRecord::Base
|
||||||
|
end
|
||||||
|
|
||||||
|
class TestMigration < ActiveRecord::Migration
|
||||||
|
def up
|
||||||
|
sleep 0.001
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "logs information on migration" do
|
||||||
|
migration = TestMigration.new("awesome_migration","20160225050318")
|
||||||
|
|
||||||
|
ActiveRecord::Base.connection_pool.with_connection do |conn|
|
||||||
|
migration.exec_migration(conn, :up)
|
||||||
|
end
|
||||||
|
|
||||||
|
info = SchemaMigrationDetail.find_by(version: "20160225050318")
|
||||||
|
|
||||||
|
expect(info.duration).to be > 0
|
||||||
|
expect(info.git_version).to eq Discourse.git_version
|
||||||
|
expect(info.direction).to eq "up"
|
||||||
|
expect(info.rails_version).to eq Rails.version
|
||||||
|
expect(info.filename).to eq migration.filename
|
||||||
|
expect(info.name).to eq "awesome_migration"
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue