Browse Source

Allow running old migratinos

master
Dylan Baker 4 months ago
parent
commit
a4601594ea
1 changed files with 52 additions and 35 deletions
  1. 52
    35
      db/migrate.rb

+ 52
- 35
db/migrate.rb View File

@@ -1,9 +1,10 @@
1
+require 'pg'
1 2
 require 'sequel'
2 3
 
3 4
 require_relative 'connect'
4 5
 
5 6
 def migrate
6
-  db = connect
7
+  db = DB
7 8
   db.create_table? :threads do
8 9
     primary_key :id
9 10
     String :title
@@ -24,38 +25,54 @@ def migrate
24 25
     column :tsv, 'tsvector'
25 26
   end
26 27
 
27
-  db.run('CREATE INDEX tsv_idx ON posts USING gin(tsv);')
28
-
29
-  db.create_function?(:update_last_post, <<-SQL, language: :plpgsql, returns: :trigger)
30
-    BEGIN
31
-      UPDATE threads
32
-      SET
33
-        last_post_created_at = NEW.created_at,
34
-        last_post_creator = NEW.creator
35
-      WHERE
36
-        threads.id = NEW.thread_id;
37
-      RETURN NEW;
38
-    END
39
-  SQL
40
-
41
-  db.create_function?(:set_posts_tsv, <<-SQL, language: :plpgsql, returns: :trigger)
42
-    BEGIN
43
-      NEW.tsv = to_tsvector(new.body);
44
-      RETURN NEW;
45
-    END
46
-  SQL
47
-
48
-  db.run(<<-SQL)
49
-    CREATE TRIGGER update_last_post
50
-    AFTER INSERT ON posts
51
-    FOR EACH ROW
52
-    EXECUTE PROCEDURE update_last_post();
53
-  SQL
54
-
55
-  db.run(<<-SQL)
56
-    CREATE TRIGGER set_posts_tsv
57
-    BEFORE INSERT OR UPDATE ON posts
58
-    FOR EACH ROW
59
-    EXECUTE PROCEDURE set_posts_tsv();
60
-  SQL
28
+  db.run('CREATE INDEX IF NOT EXISTS tsv_idx ON posts USING gin(tsv);')
29
+
30
+  begin
31
+    db.create_function(:update_last_post, <<-SQL, language: :plpgsql, returns: :trigger)
32
+      BEGIN
33
+        UPDATE threads
34
+        SET
35
+          last_post_created_at = NEW.created_at,
36
+          last_post_creator = NEW.creator
37
+        WHERE
38
+          threads.id = NEW.thread_id;
39
+        RETURN NEW;
40
+      END
41
+    SQL
42
+  rescue Sequel::DatabaseError => e
43
+    raise e unless e.wrapped_exception.is_a?(PG::DuplicateFunction)
44
+  end
45
+
46
+  begin
47
+    db.create_function(:set_posts_tsv, <<-SQL, language: :plpgsql, returns: :trigger)
48
+      BEGIN
49
+        NEW.tsv = to_tsvector(new.body);
50
+        RETURN NEW;
51
+      END
52
+    SQL
53
+  rescue Sequel::DatabaseError => e
54
+    raise e unless e.wrapped_exception.is_a?(PG::DuplicateFunction)
55
+  end
56
+
57
+  begin
58
+    db.run(<<-SQL)
59
+      CREATE TRIGGER update_last_post
60
+      AFTER INSERT ON posts
61
+      FOR EACH ROW
62
+      EXECUTE PROCEDURE update_last_post();
63
+    SQL
64
+  rescue Sequel::DatabaseError => e
65
+    raise e unless e.wrapped_exception.is_a?(PG::DuplicateObject)
66
+  end
67
+
68
+  begin
69
+    db.run(<<-SQL)
70
+      CREATE TRIGGER set_posts_tsv
71
+      BEFORE INSERT OR UPDATE ON posts
72
+      FOR EACH ROW
73
+      EXECUTE PROCEDURE set_posts_tsv();
74
+    SQL
75
+  rescue Sequel::DatabaseError => e
76
+    raise e unless e.wrapped_exception.is_a?(PG::DuplicateObject)
77
+  end
61 78
 end

Loading…
Cancel
Save