howto

Using ActiveRecord outside of rails

just the rake

tags
activerecord
ruby

Install dependancies

1
  brew install postgres

Then

1
2
  bundle init
  bundle add activerecord pg activerecord rake

Rakefile

Rakefile:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  # From https://gist.github.com/Rhoxio/ee9a855088c53d447f2eb888bd9d09a4
  require "active_record"
  require "fileutils"
  begin
    require 'dotenv/load'
  rescue LoadError
  end

  FileUtils.mkdir_p "db/migrate"

  namespace :db do
    include ActiveRecord::Tasks

    def db_config
      connection.db_config
    end

    def connection
      puts "Database url #{ENV['DATABASE_URL']}"

      @connection ||= ActiveRecord::Base.establish_connection
    end

    desc "Create the database"
    task :create do
      ActiveRecord::Tasks::DatabaseTasks.create db_config
    end


    desc "Migrate the database"
    task :migrate => [:create] do
      connection
      ActiveRecord::MigrationContext.new( 'db/migrate' ).migrate
      Rake::Task["db:schema"].invoke
    end

    desc "Drop the database"
    task :drop do
      connection
      ActiveRecord::Tasks::DatabaseTasks.drop db_config
    end

    desc "Reset the database"
    task :reset => [:drop, :create, :migrate]

    desc 'Create a db/schema.rb file that is portable against any DB supported by AR'
    task :schema do
      ActiveRecord::Tasks::DatabaseTasks.db_dir = './db'
      ActiveRecord::Tasks::DatabaseTasks.dump_schema( db_config )
    end
  end

  namespace :g do
    desc "Generate migration"
    task :migration do
      name = ARGV[1] || raise("Specify name: rake g:migration your_migration")
      timestamp = Time.now.strftime("%Y%m%d%H%M%S")
      path = File.expand_path("../db/migrate/#{timestamp}_#{name}.rb", __FILE__)
      migration_class = name.split("_").map(&:capitalize).join

      File.open(path, 'w') do |file|
        file.write <<-EOF
  class #{migration_class} < ActiveRecord::Migration[6.0]
    def self.up
    end

    def self.down
    end
  end
        EOF
      end

      puts "Migration #{path} created"
      abort # needed stop other tasks
    end
  end

Now

1
echo rake -T
rake db:create    # Create the database
rake db:drop      # Drop the database
rake db:migrate   # Migrate the database
rake db:reset     # Reset the database
rake db:schema    # Create a db/schema.rb file that is portable against any DB supported by AR
rake g:migration  # Generate migration

Previously

howto

Pulling avatars from slack

Basic bot integration

tags
slack
nextjs
bot

Next

labnotes

Snowpack for fast builds

react and tailwind too

tags
snowpack
react
tailwind