Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Małe co nieco Sequela

Małe co nieco Sequela

TRUG#37 presentation about one of Ruby gems - Sequel. Slides are in polish.

Marta Buda

May 20, 2015
Tweet

Other Decks in Programming

Transcript

  1. Sequel? • Narzędzie do korzystania z relacyjnych baz danych. •

    Obsługuje: ADO, Amalgalite, CUBRID, DataObjects, IBM_DB, JDBC, MySQL, Mysql2, ODBC, Oracle, PostgreSQL, SQLAnywhere, SQLite3, Swift oraz TinyTDS. • https://github.com/jeremyevans/sequel • http://sequel.jeremyevans.net
  2. Historia • 1. wersja: 4. marca 2007. • Do tej

    pory 168 wydań. • Obecnie 4.22.0 najnowsza. • Na początku każdego miesiąca uaktualnienia. • TL;DR Rozwija się regularnie od ponad 8 lat.
  3. Od czego zacząć? • Od połączenia z bazą danych. •

    Można nawiązać więcej niż jedno połączenie. • Obiekt Sequel::Database • Więcej na: http://sequel.jeremyevans.net/rdoc/files/ doc/opening_databases_rdoc.html
  4. Migracje • Podobne do ActiveRecord, zmieniają schemat bazy danych. Dodawanie

    tabel, zmiany kolumn, indeksy, itd. itp. • Sequel Migrator odpalany w terminalu: sequel -m path/to/migrations postgres://host/database • Rollback do początku: sequel -m path/to/migrations -M 0 postgres://host/database
  5. Sequel::Dataset • Obiekt reprezentujący zapytania SQL do bazy. • Może

    zawierać całą tabele DB[:table_name] • lub wybrany zbiór rekordów DB.from(“table_name”).where(:column => “value”) .order(:another_column).all
  6. Sequel::Model class Example < Sequel::Model(FIRST_DB[:examples]) end class Other < Sequel::Model(SECOND_DB[:others])

    end • Model powiązany ze swoim Dataset. • Walidacja: def validate super errors.add(:column, ‘cannot be empty’) if !column || column.empty? end
  7. Testy, testy • DatabaseCleaner: DatabaseCleaner[:sequel, {:connection => Sequel.connect(uri)}] • Sequel.mock

    Sequel.mock(:fetch => [ {:column => “value”, :another => “other” }, { :column => “blabla”, :another => “other” } ]) }