Last active
December 14, 2015 11:29
-
-
Save yamkazu/5079894 to your computer and use it in GitHub Desktop.
PostgreSQLですべてのデータを削除する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.sql.Sql | |
class DatabaseCleaner { | |
static excludes = ["databasechangelog", "databasechangeloglock"] | |
static clean(sql) { | |
// テーブルデータの削除 | |
sql.eachRow("SELECT tablename FROM pg_tables WHERE schemaname = 'public';") { row -> | |
if (row.tablename in excludes) return | |
sql.executeUpdate("TRUNCATE TABLE ${row.tablename} CASCADE;".toString()) | |
} | |
// シーケンスの初期化 | |
sql.eachRow("SELECT relname AS sequnence_name FROM pg_class WHERE relkind = 'S';") { row -> | |
sql.execute("SELECT setval('${row.sequnence_name}', (SELECT start_value FROM ${row.sequnence_name}), false);".toString()) | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment