Rails如何清除数据库日志表


一些无用的日志存的很多,一段时间后清除一下,省点空间。删除前要再三确认属于无用日志。
通过rails c 进入命令行模式

SecurityLog.destroy_all  # 这个会触发回调,如果不需要回调,就不需要执行。可以换成delete_all,不检查model的关联。

ActiveRecord::Base.connection.execute("TRUNCATE security_logs")
如果需要让id从1开始计数,那么就保留下面的语句的 RESTART IDENTITY。
ActiveRecord::Base.connection.execute("TRUNCATE security_logs RESTART IDENTITY")


The accepted answer only works if you need to recreate the whole database.
To drop a single table (with the callbacks) and to get the IDs to start from 1:

Model.destroy_all # Only necessary if you want to trigger callbacks.
ActiveRecord::Base.connection.execute("TRUNCATE #{table_name} RESTART IDENTITY")

If you are using Sqlite, it does not support truncate so do the following:
但是如果你用的是Sqlite,它不支持truncate.那需要执行下面的指令。
Model.destroy_all # Only necessary if you want to trigger callbacks.
ActiveRecord::Base.connection.execute("Delete from #{table_name}")
ActiveRecord::Base.connection.execute("DELETE FROM SQLITE_SEQUENCE WHERE name='#{table_name}'")

阅读量: 278
发布于:
修改于: