column_timestamp plugin

介绍了一个简单的Rails插件column_timestamp,用于自动记录指定列的更新时间。通过在迁移文件中加入特定参数,可以在不修改业务代码的情况下实现对指定字段更新时间的追踪。

  有些时候,我们可能需要记录某些列的更新时间,类似于rails的timestamps。手工一一处理稍显繁琐,于是将其抽取出来,写成了一个简单的plugin--column_timestamp,自动记录一些简单列的更新时间。

  •   使用方法

  1.将column_timestamp放到plugins目录。

  2.在表中针对要记录更新时间的列建立一个新列:[column_name]_updated_at。

  3.或者,在migration中使用:column_timestamp => true参数。例如:

 def self.up
    create_table :my_table do |t|
    t.string :name
      t.string :status,:column_timestamp => true
      t.timestamps
    end
  end
  

  这样,rails在执行migration的时候就会自动生成"status_updated_at"列。

  4.OK。开始工作了。每当status的值改变,status_updated_at就会自动记录更新的时间。

  5.如果要停止记录XXX_update_at,只需要在相应的model调用suppress_column_timestamps,例如:

class MyModel < ActiveRecord::Base
  suppress_column_timestamps
  ...
end
  • 版本问题 

  在2.0.2中通过。如果要在2.1+跑,可以将column_timestamp/vendor删除。并且注释column_timestamp/init.rb中的

require File.join(File.dirname(__FILE__),"/vendor/dirty_attr/init")

 

2008.10.15  23:59 星期三

[root@localhost ~]# head -100 /var/log/mysqld.log 250922 00:47:45 mysqld_safe Logging to '/var/log/mysqld.log'. 250922 00:47:45 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql 2025-09-22 00:47:45 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2025-09-22 00:47:45 0 [Note] /usr/sbin/mysqld (mysqld 5.6.51) starting as process 41577 ... 2025-09-22 00:47:45 41577 [Warning] Buffered warning: Changed limits: max_open_files: 1024 (requested 5000) 2025-09-22 00:47:45 41577 [Warning] Buffered warning: Changed limits: table_open_cache: 431 (requested 2000) 2025-09-22 00:47:45 41577 [Note] Plugin 'FEDERATED' is disabled. 2025-09-22 00:47:45 41577 [Note] InnoDB: Using atomics to ref count buffer pool pages 2025-09-22 00:47:45 41577 [Note] InnoDB: The InnoDB memory heap is disabled 2025-09-22 00:47:45 41577 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 2025-09-22 00:47:45 41577 [Note] InnoDB: Memory barrier is not used 2025-09-22 00:47:45 41577 [Note] InnoDB: Compressed tables use zlib 1.2.11 2025-09-22 00:47:45 41577 [Note] InnoDB: Using Linux native AIO 2025-09-22 00:47:45 41577 [Note] InnoDB: Using CPU crc32 instructions 2025-09-22 00:47:45 41577 [Note] InnoDB: Initializing buffer pool, size = 128.0M 2025-09-22 00:47:45 41577 [Note] InnoDB: Completed initialization of buffer pool 2025-09-22 00:47:45 41577 [Note] InnoDB: Highest supported file format is Barracuda. 2025-09-22 00:47:45 41577 [Note] InnoDB: 128 rollback segment(s) are active. 2025-09-22 00:47:45 41577 [Note] InnoDB: Waiting for purge to start 2025-09-22 00:47:45 41577 [Note] InnoDB: 5.6.51 started; log sequence number 1625987 2025-09-22 00:47:45 41577 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 482452ca-976f-11f0-a3ab-000c2932bbd7. 2025-09-22 00:47:45 41577 [Note] RSA private key file not found: /var/lib/mysql//private_key.pem. Some authentication plugins will not work. 2025-09-22 00:47:45 41577 [Note] RSA public key file not found: /var/lib/mysql//public_key.pem. Some authentication plugins will not work. 2025-09-22 00:47:45 41577 [Note] Server hostname (bind-address): '*'; port: 3306 2025-09-22 00:47:45 41577 [Note] IPv6 is available. 2025-09-22 00:47:45 41577 [Note] - '::' resolves to '::'; 2025-09-22 00:47:45 41577 [Note] Server socket created on IP: '::'. 2025-09-22 00:47:45 41577 [Note] Event Scheduler: Loaded 0 events 2025-09-22 00:47:45 41577 [Note] /usr/sbin/mysqld: ready for connections. Version: '5.6.51' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Server (GPL) 2025-09-22 02:09:26 41577 [Note] /usr/sbin/mysqld: Normal shutdown 2025-09-22 02:09:26 41577 [Note] Giving 0 client threads a chance to die gracefully 2025-09-22 02:09:26 41577 [Note] Event Scheduler: Purging the queue. 0 events 2025-09-22 02:09:26 41577 [Note] Shutting down slave threads 2025-09-22 02:09:26 41577 [Note] Forcefully disconnecting 0 remaining clients 2025-09-22 02:09:26 41577 [Note] Binlog end 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'partition' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_SYS_FIELDS' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_SYS_INDEXES' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_SYS_TABLES' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_FT_CONFIG' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_FT_DELETED' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_METRICS' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_CMPMEM' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_CMP_RESET' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_CMP' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_LOCK_WAITS' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_LOCKS' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'INNODB_TRX' 2025-09-22 02:09:26 41577 [Note] Shutting down plugin 'InnoDB' 2025-09-22 02:09:26 41577 [Note] InnoDB: FTS optimize thread exiting. 2025-09-22 02:09:26 41577 [Note] InnoDB: Starting shutdown... 2025-09-22 02:09:27 41577 [Note] InnoDB: Shutdown completed; log sequence number 1625997 2025-09-22 02:09:27 41577 [Note] Shutting down plugin 'BLACKHOLE' 2025-09-22 02:09:27 41577 [Note] Shutting down plugin 'ARCHIVE' 2025-09-22 02:09:27 41577 [Note] Shutting down plugin 'MRG_MYISAM' 2025-09-22 02:09:27 41577 [Note] Shutting down plugin 'MyISAM' 2025-09-22 02:09:27 41577 [Note] Shutting down plugin 'MEMORY' 2025-09-22 02:09:27 41577 [Note] Shutting down plugin 'CSV' 2025-09-22 02:09:27 41577 [Note] Shutting down plugin 'sha256_password' 2025-09-22 02:09:27 41577 [Note] Shutting down plugin 'mysql_old_password' 2025-09-22 02:09:27 41577 [Note] Shutting down plugin 'mysql_native_password' 2025-09-22 02:09:27 41577 [Note] Shutting down plugin 'binlog' 2025-09-22 02:09:27 41577 [Note] /usr/sbin/mysqld: Shutdown complete 250922 02:09:27 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended 250922 02:09:28 mysqld_safe Logging to '/var/log/mysqld.log'. 250922 02:09:28 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql 2025-09-22 02:09:28 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2025-09-22 02:09:28 0 [Note] /usr/sbin/mysqld (mysqld 5.6.51) starting as process 41965 ... 2025-09-22 02:09:28 41965 [Warning] Buffered warning: Changed limits: max_open_files: 1024 (requested 5000) 2025-09-22 02:09:28 41965 [Warning] Buffered warning: Changed limits: table_open_cache: 431 (requested 2000) 2025-09-22 02:09:28 41965 [Note] Plugin 'FEDERATED' is disabled. 2025-09-22 02:09:28 41965 [Note] InnoDB: Using atomics to ref count buffer pool pages 2025-09-22 02:09:28 41965 [Note] InnoDB: The InnoDB memory heap is disabled 2025-09-22 02:09:28 41965 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 2025-09-22 02:09:28 41965 [Note] InnoDB: Memory barrier is not used 2025-09-22 02:09:28 41965 [Note] InnoDB: Compressed tables use zlib 1.2.11
09-23
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值