本站源代码
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

290 lignes
6.5KB

  1. package testfixtures
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "strings"
  6. )
  7. // PostgreSQL is the PG helper for this package
  8. type PostgreSQL struct {
  9. baseHelper
  10. // UseAlterConstraint If true, the contraint disabling will do
  11. // using ALTER CONTRAINT sintax, only allowed in PG >= 9.4.
  12. // If false, the constraint disabling will use DISABLE TRIGGER ALL,
  13. // which requires SUPERUSER privileges.
  14. UseAlterConstraint bool
  15. tables []string
  16. sequences []string
  17. nonDeferrableConstraints []pgConstraint
  18. tablesChecksum map[string]string
  19. }
  20. type pgConstraint struct {
  21. tableName string
  22. constraintName string
  23. }
  24. func (h *PostgreSQL) init(db *sql.DB) error {
  25. var err error
  26. h.tables, err = h.tableNames(db)
  27. if err != nil {
  28. return err
  29. }
  30. h.sequences, err = h.getSequences(db)
  31. if err != nil {
  32. return err
  33. }
  34. h.nonDeferrableConstraints, err = h.getNonDeferrableConstraints(db)
  35. if err != nil {
  36. return err
  37. }
  38. return nil
  39. }
  40. func (*PostgreSQL) paramType() int {
  41. return paramTypeDollar
  42. }
  43. func (*PostgreSQL) databaseName(q queryable) (string, error) {
  44. var dbName string
  45. err := q.QueryRow("SELECT current_database()").Scan(&dbName)
  46. return dbName, err
  47. }
  48. func (h *PostgreSQL) tableNames(q queryable) ([]string, error) {
  49. var tables []string
  50. sql := `
  51. SELECT pg_namespace.nspname || '.' || pg_class.relname
  52. FROM pg_class
  53. INNER JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace
  54. WHERE pg_class.relkind = 'r'
  55. AND pg_namespace.nspname NOT IN ('pg_catalog', 'information_schema')
  56. AND pg_namespace.nspname NOT LIKE 'pg_toast%';
  57. `
  58. rows, err := q.Query(sql)
  59. if err != nil {
  60. return nil, err
  61. }
  62. defer rows.Close()
  63. for rows.Next() {
  64. var table string
  65. if err = rows.Scan(&table); err != nil {
  66. return nil, err
  67. }
  68. tables = append(tables, table)
  69. }
  70. if err = rows.Err(); err != nil {
  71. return nil, err
  72. }
  73. return tables, nil
  74. }
  75. func (h *PostgreSQL) getSequences(q queryable) ([]string, error) {
  76. const sql = `
  77. SELECT pg_namespace.nspname || '.' || pg_class.relname AS sequence_name
  78. FROM pg_class
  79. INNER JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace
  80. WHERE pg_class.relkind = 'S'
  81. `
  82. rows, err := q.Query(sql)
  83. if err != nil {
  84. return nil, err
  85. }
  86. defer rows.Close()
  87. var sequences []string
  88. for rows.Next() {
  89. var sequence string
  90. if err = rows.Scan(&sequence); err != nil {
  91. return nil, err
  92. }
  93. sequences = append(sequences, sequence)
  94. }
  95. if err = rows.Err(); err != nil {
  96. return nil, err
  97. }
  98. return sequences, nil
  99. }
  100. func (*PostgreSQL) getNonDeferrableConstraints(q queryable) ([]pgConstraint, error) {
  101. var constraints []pgConstraint
  102. sql := `
  103. SELECT table_schema || '.' || table_name, constraint_name
  104. FROM information_schema.table_constraints
  105. WHERE constraint_type = 'FOREIGN KEY'
  106. AND is_deferrable = 'NO'
  107. `
  108. rows, err := q.Query(sql)
  109. if err != nil {
  110. return nil, err
  111. }
  112. defer rows.Close()
  113. for rows.Next() {
  114. var constraint pgConstraint
  115. if err = rows.Scan(&constraint.tableName, &constraint.constraintName); err != nil {
  116. return nil, err
  117. }
  118. constraints = append(constraints, constraint)
  119. }
  120. if err = rows.Err(); err != nil {
  121. return nil, err
  122. }
  123. return constraints, nil
  124. }
  125. func (h *PostgreSQL) disableTriggers(db *sql.DB, loadFn loadFunction) (err error) {
  126. defer func() {
  127. // re-enable triggers after load
  128. var sql string
  129. for _, table := range h.tables {
  130. sql += fmt.Sprintf("ALTER TABLE %s ENABLE TRIGGER ALL;", h.quoteKeyword(table))
  131. }
  132. if _, err2 := db.Exec(sql); err2 != nil && err == nil {
  133. err = err2
  134. }
  135. }()
  136. tx, err := db.Begin()
  137. if err != nil {
  138. return err
  139. }
  140. var sql string
  141. for _, table := range h.tables {
  142. sql += fmt.Sprintf("ALTER TABLE %s DISABLE TRIGGER ALL;", h.quoteKeyword(table))
  143. }
  144. if _, err = tx.Exec(sql); err != nil {
  145. return err
  146. }
  147. if err = loadFn(tx); err != nil {
  148. tx.Rollback()
  149. return err
  150. }
  151. return tx.Commit()
  152. }
  153. func (h *PostgreSQL) makeConstraintsDeferrable(db *sql.DB, loadFn loadFunction) (err error) {
  154. defer func() {
  155. // ensure constraint being not deferrable again after load
  156. var sql string
  157. for _, constraint := range h.nonDeferrableConstraints {
  158. sql += fmt.Sprintf("ALTER TABLE %s ALTER CONSTRAINT %s NOT DEFERRABLE;", h.quoteKeyword(constraint.tableName), h.quoteKeyword(constraint.constraintName))
  159. }
  160. if _, err2 := db.Exec(sql); err2 != nil && err == nil {
  161. err = err2
  162. }
  163. }()
  164. var sql string
  165. for _, constraint := range h.nonDeferrableConstraints {
  166. sql += fmt.Sprintf("ALTER TABLE %s ALTER CONSTRAINT %s DEFERRABLE;", h.quoteKeyword(constraint.tableName), h.quoteKeyword(constraint.constraintName))
  167. }
  168. if _, err := db.Exec(sql); err != nil {
  169. return err
  170. }
  171. tx, err := db.Begin()
  172. if err != nil {
  173. return err
  174. }
  175. defer tx.Rollback()
  176. if _, err = tx.Exec("SET CONSTRAINTS ALL DEFERRED"); err != nil {
  177. return err
  178. }
  179. if err = loadFn(tx); err != nil {
  180. return err
  181. }
  182. return tx.Commit()
  183. }
  184. func (h *PostgreSQL) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (err error) {
  185. // ensure sequences being reset after load
  186. defer func() {
  187. if err2 := h.resetSequences(db); err2 != nil && err == nil {
  188. err = err2
  189. }
  190. }()
  191. if h.UseAlterConstraint {
  192. return h.makeConstraintsDeferrable(db, loadFn)
  193. }
  194. return h.disableTriggers(db, loadFn)
  195. }
  196. func (h *PostgreSQL) resetSequences(db *sql.DB) error {
  197. for _, sequence := range h.sequences {
  198. _, err := db.Exec(fmt.Sprintf("SELECT SETVAL('%s', %d)", sequence, resetSequencesTo))
  199. if err != nil {
  200. return err
  201. }
  202. }
  203. return nil
  204. }
  205. func (h *PostgreSQL) isTableModified(q queryable, tableName string) (bool, error) {
  206. checksum, err := h.getChecksum(q, tableName)
  207. if err != nil {
  208. return false, err
  209. }
  210. oldChecksum := h.tablesChecksum[tableName]
  211. return oldChecksum == "" || checksum != oldChecksum, nil
  212. }
  213. func (h *PostgreSQL) afterLoad(q queryable) error {
  214. if h.tablesChecksum != nil {
  215. return nil
  216. }
  217. h.tablesChecksum = make(map[string]string, len(h.tables))
  218. for _, t := range h.tables {
  219. checksum, err := h.getChecksum(q, t)
  220. if err != nil {
  221. return err
  222. }
  223. h.tablesChecksum[t] = checksum
  224. }
  225. return nil
  226. }
  227. func (h *PostgreSQL) getChecksum(q queryable, tableName string) (string, error) {
  228. sqlStr := fmt.Sprintf(`
  229. SELECT md5(CAST((array_agg(t.*)) AS TEXT))
  230. FROM %s AS t
  231. `,
  232. h.quoteKeyword(tableName),
  233. )
  234. var checksum sql.NullString
  235. if err := q.QueryRow(sqlStr).Scan(&checksum); err != nil {
  236. return "", err
  237. }
  238. return checksum.String, nil
  239. }
  240. func (*PostgreSQL) quoteKeyword(s string) string {
  241. parts := strings.Split(s, ".")
  242. for i, p := range parts {
  243. parts[i] = fmt.Sprintf(`"%s"`, p)
  244. }
  245. return strings.Join(parts, ".")
  246. }
上海开阖软件有限公司 沪ICP备12045867号-1