backend: run migrations only when neccessary

This commit is contained in:
0x1eef 2023-12-20 13:37:47 -03:00
parent ecb1aca5fc
commit fb4bdba6c7
6 changed files with 43 additions and 41 deletions

View file

@ -1,11 +1,17 @@
module Twenty::Migration
require_relative "migration/create_connections"
require_relative "migration/create_issues"
def self.run!
CreateConnections.migrate(:up)
CreateIssues.migrate(:up)
rescue
warn "#{$!.class}: #{$!.message}"
##
# @return [String]
# Returns the path to twenty's migrations.
def self.migrations_path
[File.join(__dir__, "migration")]
end
##
# Runs migrations (if neccessary).
# @return [void]
def self.run!
context = ActiveRecord::MigrationContext.new(migrations_path)
context.migrate
end
ActiveRecord.timestamped_migrations = false
end

View file

@ -0,0 +1,13 @@
class CreateConnections < ActiveRecord::Migration[7.1]
def up
create_table(:connections) do |t|
t.string :name, null: false
t.string :path, null: false
t.timestamps
end
end
def down
drop_table(:connections)
end
end

View file

@ -0,0 +1,15 @@
class CreateIssues < ActiveRecord::Migration[7.1]
def up
create_table(:issues) do |t|
t.string :title, null: false
t.text :content, null: false
t.string :state, null: false
t.belongs_to :connection, null: false
t.timestamps
end
end
def down
drop_table(:issues)
end
end

View file

@ -1,15 +0,0 @@
module Twenty::Migration
class CreateConnections < ActiveRecord::Migration[7.1]
def up
create_table(:connections) do |t|
t.string :name, null: false
t.string :path, null: false
t.timestamps
end
end
def down
drop_table(:connections)
end
end
end

View file

@ -1,17 +0,0 @@
module Twenty::Migration
class CreateIssues < ActiveRecord::Migration[7.1]
def up
create_table(:issues) do |t|
t.string :title, null: false
t.text :content, null: false
t.string :state, null: false
t.belongs_to :connection, null: false
t.timestamps
end
end
def down
drop_table(:issues)
end
end
end

View file

@ -40,7 +40,7 @@ class Twenty::Model < ActiveRecord::Base
prepare
connect
Twenty::Migration.run!
require_relative "model/connection"
require_relative "model/issue"
Twenty::Migration.run!
end