\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{software\_writer} \pdfinfo{ /Title (active-record-migrations.pdf) /Creator (Cheatography) /Author (software\_writer) /Subject (Active Record Migrations Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{C40000} \definecolor{LightBackground}{HTML}{FBEFEF} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Active Record Migrations Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{software\_writer} via \textcolor{DarkBackground}{\uline{cheatography.com/182173/cs/40093/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}software\_writer \\ \uline{cheatography.com/software-writer} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 31st August, 2023.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Update Table}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{change\_table :products do |t| \newline t.remove :description, :name \newline t.string :service\_number \newline t.index :service\_number \newline t.rename :creator, :maker \newline end} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Use the `change\_table` method to update existing tables. Like create\_table, the change\_table method accepts the name of the table and a block to add columns or indexes to the table:} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Rename Table}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\seqsplit{rename\_table('octopuses'}, 'octopi')} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Use the `rename\_table(old, new)` to rename a table.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Drop Table}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{def change \newline drop\_table :accounts \newline \newline drop\_table :posts do |t| \newline t.string :title \newline t.text :content \newline end \newline end} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{To drop an existing table, you can use the drop\_table method. It ignores the provided block and options when running the migration, but uses them when reverting the migration to generate the corresponding table.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Create Columns}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\# add\_column(table\_name, column\_name, type, **options) \newline \newline add\_column(:users, :picture, :binary)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{You can specify new columns while creating a new table. But if you want to add new columns to an existing table, use the add\_column method.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Modify Columns}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\# \seqsplit{change\_column(table\_name}, column\_name, type, **options) \newline \newline change\_column :users, :name, :string, limit: 80} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{The `change\_column` method allows you to modify the type and attributes of existing columns.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Rename Column}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\# \seqsplit{rename\_column(table\_name}, column\_name, new\_column\_name) \newline \newline rename\_column(:suppliers, :description, :name)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{To rename a column, use the `rename\_column` method.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Remove Column}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{remove\_column :users, :password} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{To drop a column, you may use the remove\_column method.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Referencing Tables}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{def change \newline add\_reference :posts, :user \# or \newline \newline \# add\_belongs\_to :posts, :user \newline end \newline \newline change\_table :posts do |t| \newline t.references :user \# or \newline \# t.belongs\_to :user \newline end} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{To add foreign key, use the `add\_reference :table\_name, :reference` method. For example, if the posts table references a user, then posts will have a `user\_id` column in it.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.8 cm} x{5.2 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Data Types}} \tn % Row 0 \SetRowColor{LightBackground} :integer & whole number without any fractions \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} :float & floating point number (do not use for calculations that need accuracy) \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} :decimal & specific precisions, used for financial calculations that need accuracy \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} :bigint & very large integer \tn % Row Count 9 (+ 1) % Row 4 \SetRowColor{LightBackground} :boolean & true / false \tn % Row Count 10 (+ 1) % Row 5 \SetRowColor{white} :string & limited to 255 characters. \tn % Row Count 11 (+ 1) % Row 6 \SetRowColor{LightBackground} :text & unlimited (depending on the database) \tn % Row Count 13 (+ 2) % Row 7 \SetRowColor{white} :date & year, month, and day without time \tn % Row Count 15 (+ 2) % Row 8 \SetRowColor{LightBackground} :time & hour, minutes, and seconds wihout date \tn % Row Count 17 (+ 2) % Row 9 \SetRowColor{white} :datetime / :timestamp & date and time \tn % Row Count 19 (+ 2) % Row 10 \SetRowColor{LightBackground} :json & JSON data \tn % Row Count 20 (+ 1) % Row 11 \SetRowColor{white} :binary & raw binary data \tn % Row Count 21 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.224 cm} x{1.584 cm} x{1.584 cm} x{2.808 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{8.4cm}}{\bf\textcolor{white}{Column Types}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Type}} & {\bf{MySQL}} & {\bf{SQLite}} & {\bf{PostgreSQL}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{:integer} & int(11) & integer & integer \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{:bigint} & ? & ? & ? \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{:float} & float & float & float \tn % Row Count 7 (+ 1) % Row 4 \SetRowColor{LightBackground} \seqsplit{:decimal} & decimal & decimal & decimal \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} \seqsplit{:boolean} & boolean & boolean & boolean \tn % Row Count 11 (+ 2) % Row 6 \SetRowColor{LightBackground} \seqsplit{string} & varchar (255) & varchar (255) & character varying (255) \tn % Row Count 13 (+ 2) % Row 7 \SetRowColor{white} :text & text & text & text \tn % Row Count 14 (+ 1) % Row 8 \SetRowColor{LightBackground} :date & date & date & date \tn % Row Count 15 (+ 1) % Row 9 \SetRowColor{white} :time & time & time & time \tn % Row Count 16 (+ 1) % Row 10 \SetRowColor{LightBackground} \seqsplit{:datetime} & \seqsplit{:datetime} & \seqsplit{datetime} & timestamp \tn % Row Count 18 (+ 2) % Row 11 \SetRowColor{white} \seqsplit{:timestamp} & \seqsplit{datetime} & \seqsplit{datetime} & timestamp \tn % Row Count 20 (+ 2) % Row 12 \SetRowColor{LightBackground} :json & ? & ? & ? \tn % Row Count 21 (+ 1) % Row 13 \SetRowColor{white} \seqsplit{:binary} & blob & blob & bytea \tn % Row Count 23 (+ 2) % Row 14 \SetRowColor{LightBackground} \seqsplit{:virtual} & ? & ? & ? \tn % Row Count 25 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Create Index}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\# while creating a table \newline create\_table :users do |t| \newline t.string :name, index: true \newline t.string :email, index: \{ unique: true \} \newline t.index {[}:title, :address{]}, unique: true \newline end \newline \newline \newline \# adds a new index on the `posts` table for the `title` column. \newline add\_index :posts, :title \newline \newline \# adds a new index for the `company\_id` and `manager\_id` columns; \newline add\_index :suppliers, {[}:company\_id, :manager\_id{]}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{ActiveRecord supports several types of indexes. Use the add\_index or t.index method to add new index. Additionally, you can mark the index as unique or give it a different name.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Rename Index}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{rename\_index :people, \seqsplit{'index\_people\_on\_last\_name'}, \seqsplit{'index\_users\_on\_last\_name'} \newline \newline \# OR \newline \newline change\_table :users do |t| \newline \seqsplit{t.rename\_index(:user\_id}, :account\_id) \newline end} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{To rename an index, use the `rename\_index` method. This method accepts the table name as its first argument and the current index name and the desired name as its second and third arguments, respectively. \newline \newline Alternatively, use the `rename\_index` method while changing a table. This is useful when you want to change multiple index names in a single migration.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Drop Index}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{remove\_index :accounts, :branch\_id \newline \newline remove\_index :accounts, column: {[}:branch\_id, :party\_id{]} \newline \newline remove\_index :accounts, name: :by\_branch\_party \newline \newline \# or \newline \newline change\_table :users do |t| \newline \seqsplit{t.remove\_index(:branch\_id)} \newline t.remove\_index(column: {[}:branch\_id, :party\_id{]}) \newline end} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{To drop an index, you may use the `remove\_index` method, passing the name of the table and either the column name or the name of the index. Alternatively, use the `remove\_index` method while changing the table.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}